Mercurial > pidgin.yaz
annotate plugins/lagmeter.c @ 1446:38f712e75836
[gaim-migrate @ 1456]
i think this should work more better
committer: Tailor Script <tailor@pidgin.im>
author | Eric Warmenhoven <eric@warmenhoven.org> |
---|---|
date | Tue, 30 Jan 2001 02:27:35 +0000 |
parents | 569835fcabc3 |
children | 94e81315be2a |
rev | line source |
---|---|
193 | 1 #define GAIM_PLUGINS |
2 #include "gaim.h" | |
3 | |
4 #include <time.h> | |
5 #include <sys/types.h> | |
6 #include <sys/time.h> | |
1167
5d1ded9f88b7
[gaim-migrate @ 1177]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1122
diff
changeset
|
7 #include <string.h> |
193 | 8 #include <unistd.h> |
9 #include <math.h> | |
10 | |
11 #define MY_LAG_STRING "ZYXCHECKLAGXYZ" | |
12 | |
1122
889ca2b8697b
[gaim-migrate @ 1132]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1073
diff
changeset
|
13 GModule *handle; |
1377
569835fcabc3
[gaim-migrate @ 1387]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1295
diff
changeset
|
14 GtkWidget *lagbox = NULL; |
193 | 15 GtkWidget *my_lagometer; |
16 struct timeval my_lag_tv; | |
1122
889ca2b8697b
[gaim-migrate @ 1132]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1073
diff
changeset
|
17 guint check_timeout = 0; |
193 | 18 guint delay = 10; |
1377
569835fcabc3
[gaim-migrate @ 1387]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1295
diff
changeset
|
19 static GtkWidget *confdlg = NULL; |
1122
889ca2b8697b
[gaim-migrate @ 1132]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1073
diff
changeset
|
20 struct gaim_connection *my_gc = NULL; |
193 | 21 |
1122
889ca2b8697b
[gaim-migrate @ 1132]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1073
diff
changeset
|
22 static void avail_now(struct gaim_connection *, void *); |
889ca2b8697b
[gaim-migrate @ 1132]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1073
diff
changeset
|
23 |
889ca2b8697b
[gaim-migrate @ 1132]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1073
diff
changeset
|
24 static void update_lag(int us) { |
193 | 25 double pct; |
26 | |
27 if (lagbox == NULL) { | |
28 /* guess we better build it then :P */ | |
29 GtkWidget *label = gtk_label_new("Lag-O-Meter: "); | |
30 GList *tmp = gtk_container_children(GTK_CONTAINER(blist)); | |
31 GtkWidget *vbox2 = (GtkWidget *)tmp->data; | |
32 lagbox = gtk_hbox_new(FALSE, 0); | |
33 my_lagometer = gtk_progress_bar_new(); | |
34 | |
35 gtk_box_pack_start(GTK_BOX(lagbox), label, FALSE, FALSE, 5); | |
36 gtk_box_pack_start(GTK_BOX(lagbox), my_lagometer, TRUE, TRUE, 5); | |
37 gtk_widget_set_usize(my_lagometer, 5, 5); | |
38 | |
39 gtk_widget_show(label); | |
40 gtk_widget_show(my_lagometer); | |
41 | |
42 gtk_box_pack_start(GTK_BOX(vbox2), lagbox, FALSE, TRUE, 0); | |
43 gtk_box_reorder_child(GTK_BOX(vbox2), lagbox, 1); | |
44 gtk_widget_show(lagbox); | |
45 } | |
46 | |
47 pct = us/100000; | |
48 if (pct > 0) | |
49 pct = 25 * log(pct); | |
50 if (pct < 0) | |
51 pct = 0; | |
52 if (pct > 100) | |
53 pct = 100; | |
54 pct /= 100; | |
55 | |
56 gtk_progress_bar_update(GTK_PROGRESS_BAR(my_lagometer), pct); | |
57 } | |
58 | |
1122
889ca2b8697b
[gaim-migrate @ 1132]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1073
diff
changeset
|
59 static void check_lag(struct gaim_connection *gc, char **who, char **message, void *m) { |
889ca2b8697b
[gaim-migrate @ 1132]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1073
diff
changeset
|
60 char *name; |
889ca2b8697b
[gaim-migrate @ 1132]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1073
diff
changeset
|
61 if (gc != my_gc) |
889ca2b8697b
[gaim-migrate @ 1132]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1073
diff
changeset
|
62 return; |
889ca2b8697b
[gaim-migrate @ 1132]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1073
diff
changeset
|
63 |
889ca2b8697b
[gaim-migrate @ 1132]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1073
diff
changeset
|
64 name = g_strdup(normalize(*who)); |
1000
91b7377e7b45
[gaim-migrate @ 1010]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
983
diff
changeset
|
65 if (!strcasecmp(normalize(gc->username), name) && |
983 | 66 (*message != NULL) && |
193 | 67 !strcmp(*message, MY_LAG_STRING)) { |
68 struct timeval tv; | |
69 int ms; | |
70 | |
71 gettimeofday(&tv, NULL); | |
72 | |
73 ms = 1000000 * (tv.tv_sec - my_lag_tv.tv_sec); | |
74 | |
75 ms += tv.tv_usec - my_lag_tv.tv_usec; | |
76 | |
77 update_lag(ms); | |
78 *message = NULL; | |
79 } | |
80 g_free(name); | |
81 } | |
82 | |
1377
569835fcabc3
[gaim-migrate @ 1387]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1295
diff
changeset
|
83 static gint send_lag() { |
193 | 84 gettimeofday(&my_lag_tv, NULL); |
1377
569835fcabc3
[gaim-migrate @ 1387]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1295
diff
changeset
|
85 if (g_slist_find(connections, my_gc)) { |
1295
13e68fb13744
[gaim-migrate @ 1305]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1167
diff
changeset
|
86 char *m = g_strdup(MY_LAG_STRING); |
1377
569835fcabc3
[gaim-migrate @ 1387]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1295
diff
changeset
|
87 serv_send_im(my_gc, my_gc->username, m, 1); |
1295
13e68fb13744
[gaim-migrate @ 1305]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1167
diff
changeset
|
88 g_free(m); |
1122
889ca2b8697b
[gaim-migrate @ 1132]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1073
diff
changeset
|
89 return TRUE; |
889ca2b8697b
[gaim-migrate @ 1132]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1073
diff
changeset
|
90 } else { |
889ca2b8697b
[gaim-migrate @ 1132]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1073
diff
changeset
|
91 debug_printf("LAGMETER: send_lag called for connection that no longer exists\n"); |
889ca2b8697b
[gaim-migrate @ 1132]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1073
diff
changeset
|
92 check_timeout = 0; |
889ca2b8697b
[gaim-migrate @ 1132]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1073
diff
changeset
|
93 return FALSE; |
889ca2b8697b
[gaim-migrate @ 1132]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1073
diff
changeset
|
94 } |
889ca2b8697b
[gaim-migrate @ 1132]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1073
diff
changeset
|
95 } |
889ca2b8697b
[gaim-migrate @ 1132]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1073
diff
changeset
|
96 |
889ca2b8697b
[gaim-migrate @ 1132]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1073
diff
changeset
|
97 static void got_signoff(struct gaim_connection *gc, void *m) { |
889ca2b8697b
[gaim-migrate @ 1132]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1073
diff
changeset
|
98 if (gc != my_gc) |
889ca2b8697b
[gaim-migrate @ 1132]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1073
diff
changeset
|
99 return; |
889ca2b8697b
[gaim-migrate @ 1132]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1073
diff
changeset
|
100 |
889ca2b8697b
[gaim-migrate @ 1132]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1073
diff
changeset
|
101 if (check_timeout > 0) |
889ca2b8697b
[gaim-migrate @ 1132]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1073
diff
changeset
|
102 gtk_timeout_remove(check_timeout); |
889ca2b8697b
[gaim-migrate @ 1132]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1073
diff
changeset
|
103 check_timeout = 0; |
889ca2b8697b
[gaim-migrate @ 1132]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1073
diff
changeset
|
104 |
889ca2b8697b
[gaim-migrate @ 1132]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1073
diff
changeset
|
105 if (confdlg) |
889ca2b8697b
[gaim-migrate @ 1132]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1073
diff
changeset
|
106 gtk_widget_destroy(confdlg); |
889ca2b8697b
[gaim-migrate @ 1132]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1073
diff
changeset
|
107 confdlg = NULL; |
889ca2b8697b
[gaim-migrate @ 1132]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1073
diff
changeset
|
108 |
889ca2b8697b
[gaim-migrate @ 1132]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1073
diff
changeset
|
109 if (lagbox) |
889ca2b8697b
[gaim-migrate @ 1132]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1073
diff
changeset
|
110 gtk_widget_destroy(lagbox); |
889ca2b8697b
[gaim-migrate @ 1132]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1073
diff
changeset
|
111 lagbox = NULL; |
889ca2b8697b
[gaim-migrate @ 1132]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1073
diff
changeset
|
112 |
889ca2b8697b
[gaim-migrate @ 1132]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1073
diff
changeset
|
113 if (g_slist_length(connections) > 1) { |
1377
569835fcabc3
[gaim-migrate @ 1387]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1295
diff
changeset
|
114 if (connections->data == my_gc) { |
569835fcabc3
[gaim-migrate @ 1387]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1295
diff
changeset
|
115 my_gc = NULL; |
1122
889ca2b8697b
[gaim-migrate @ 1132]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1073
diff
changeset
|
116 avail_now(connections->next->data, NULL); |
1377
569835fcabc3
[gaim-migrate @ 1387]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1295
diff
changeset
|
117 } else { |
569835fcabc3
[gaim-migrate @ 1387]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1295
diff
changeset
|
118 my_gc = NULL; |
1122
889ca2b8697b
[gaim-migrate @ 1132]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1073
diff
changeset
|
119 avail_now(connections->data, NULL); |
1377
569835fcabc3
[gaim-migrate @ 1387]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1295
diff
changeset
|
120 } |
1122
889ca2b8697b
[gaim-migrate @ 1132]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1073
diff
changeset
|
121 } else { |
889ca2b8697b
[gaim-migrate @ 1132]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1073
diff
changeset
|
122 my_gc = NULL; |
889ca2b8697b
[gaim-migrate @ 1132]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1073
diff
changeset
|
123 } |
889ca2b8697b
[gaim-migrate @ 1132]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1073
diff
changeset
|
124 } |
889ca2b8697b
[gaim-migrate @ 1132]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1073
diff
changeset
|
125 |
889ca2b8697b
[gaim-migrate @ 1132]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1073
diff
changeset
|
126 static void avail_now(struct gaim_connection *gc, void *m) { |
1377
569835fcabc3
[gaim-migrate @ 1387]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1295
diff
changeset
|
127 if (my_gc) |
569835fcabc3
[gaim-migrate @ 1387]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1295
diff
changeset
|
128 return; |
1122
889ca2b8697b
[gaim-migrate @ 1132]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1073
diff
changeset
|
129 update_lag(0); |
1377
569835fcabc3
[gaim-migrate @ 1387]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1295
diff
changeset
|
130 check_timeout = gtk_timeout_add(1000 * delay, (GtkFunction)send_lag, NULL); |
1122
889ca2b8697b
[gaim-migrate @ 1132]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1073
diff
changeset
|
131 my_gc = gc; |
193 | 132 } |
133 | |
134 void gaim_plugin_remove() { | |
1122
889ca2b8697b
[gaim-migrate @ 1132]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1073
diff
changeset
|
135 if (check_timeout > 0) |
848
5f19ec4a91f7
[gaim-migrate @ 858]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
193
diff
changeset
|
136 gtk_timeout_remove(check_timeout); |
1122
889ca2b8697b
[gaim-migrate @ 1132]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1073
diff
changeset
|
137 check_timeout = 0; |
193 | 138 if (confdlg) |
139 gtk_widget_destroy(confdlg); | |
983 | 140 if (lagbox) |
141 gtk_widget_destroy(lagbox); | |
142 | |
193 | 143 confdlg = NULL; |
983 | 144 lagbox = NULL; |
1122
889ca2b8697b
[gaim-migrate @ 1132]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1073
diff
changeset
|
145 my_gc = NULL; |
193 | 146 } |
147 | |
1047
ece2d1543b20
[gaim-migrate @ 1057]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1000
diff
changeset
|
148 char *gaim_plugin_init(GModule *h) { |
193 | 149 handle = h; |
150 | |
983 | 151 confdlg = NULL; |
152 lagbox = NULL; | |
1377
569835fcabc3
[gaim-migrate @ 1387]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1295
diff
changeset
|
153 my_gc = NULL; |
983 | 154 |
1377
569835fcabc3
[gaim-migrate @ 1387]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1295
diff
changeset
|
155 gaim_signal_connect(handle, event_signon, avail_now, NULL); |
1122
889ca2b8697b
[gaim-migrate @ 1132]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1073
diff
changeset
|
156 gaim_signal_connect(handle, event_im_recv, check_lag, NULL); |
889ca2b8697b
[gaim-migrate @ 1132]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1073
diff
changeset
|
157 gaim_signal_connect(handle, event_signoff, got_signoff, NULL); |
889ca2b8697b
[gaim-migrate @ 1132]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1073
diff
changeset
|
158 |
1377
569835fcabc3
[gaim-migrate @ 1387]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1295
diff
changeset
|
159 if (connections) |
1000
91b7377e7b45
[gaim-migrate @ 1010]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
983
diff
changeset
|
160 avail_now(connections->data, NULL); |
1073
6b013aff4de3
[gaim-migrate @ 1083]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1047
diff
changeset
|
161 |
6b013aff4de3
[gaim-migrate @ 1083]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1047
diff
changeset
|
162 return NULL; |
193 | 163 } |
164 | |
1122
889ca2b8697b
[gaim-migrate @ 1132]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1073
diff
changeset
|
165 static void adjust_timeout(GtkWidget *button, GtkWidget *spinner) { |
193 | 166 delay = CLAMP(gtk_spin_button_get_value_as_int( |
167 GTK_SPIN_BUTTON(spinner)), 0, 3600); | |
1122
889ca2b8697b
[gaim-migrate @ 1132]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1073
diff
changeset
|
168 debug_printf("LAGMETER: new updates: %d\n", delay); |
889ca2b8697b
[gaim-migrate @ 1132]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1073
diff
changeset
|
169 if (check_timeout > 0) |
848
5f19ec4a91f7
[gaim-migrate @ 858]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
193
diff
changeset
|
170 gtk_timeout_remove(check_timeout); |
1122
889ca2b8697b
[gaim-migrate @ 1132]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1073
diff
changeset
|
171 check_timeout = 0; |
889ca2b8697b
[gaim-migrate @ 1132]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1073
diff
changeset
|
172 if (my_gc) |
1377
569835fcabc3
[gaim-migrate @ 1387]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1295
diff
changeset
|
173 check_timeout = gtk_timeout_add(1000 * delay, (GtkFunction)send_lag, NULL); |
193 | 174 gtk_widget_destroy(confdlg); |
175 confdlg = NULL; | |
176 } | |
177 | |
178 void gaim_plugin_config() { | |
179 GtkWidget *label; | |
180 GtkAdjustment *adj; | |
181 GtkWidget *spinner; | |
182 GtkWidget *button; | |
183 GtkWidget *box; | |
184 | |
185 if (confdlg) { | |
983 | 186 gtk_widget_show_all(confdlg); |
193 | 187 return; |
188 } | |
189 | |
190 confdlg = gtk_window_new(GTK_WINDOW_DIALOG); | |
191 gtk_window_set_title(GTK_WINDOW(confdlg), "Gaim Lag Delay"); | |
192 | |
193 box = gtk_hbox_new(FALSE, 0); | |
194 gtk_container_set_border_width(GTK_CONTAINER(box), 5); | |
195 gtk_container_add(GTK_CONTAINER(confdlg), box); | |
196 | |
197 label = gtk_label_new("Delay between updates: "); | |
198 gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5); | |
199 gtk_box_pack_start(GTK_BOX(box), label, FALSE, TRUE, 0); | |
200 | |
201 adj = (GtkAdjustment *)gtk_adjustment_new(delay, 0, 3600, 1, 0, 0); | |
202 spinner = gtk_spin_button_new(GTK_ADJUSTMENT(adj), 0, 0); | |
203 gtk_box_pack_start(GTK_BOX(box), spinner, TRUE, TRUE, 0); | |
204 | |
205 button = gtk_button_new_with_label("OK"); | |
206 gtk_signal_connect(GTK_OBJECT(button), "clicked", | |
207 (GtkSignalFunc)adjust_timeout, spinner); | |
208 gtk_box_pack_start(GTK_BOX(box), button, FALSE, TRUE, 0); | |
209 | |
983 | 210 gtk_widget_show_all(confdlg); |
193 | 211 } |
212 | |
213 char *name() { | |
214 return "Lag-O-Meter, Pluggified"; | |
215 } | |
216 | |
217 char *description() { | |
218 return "Your old familiar Lag-O-Meter, in a brand new form."; | |
219 } |