Mercurial > pidgin
annotate plugins/gestures/stroke-draw.c @ 9843:19fd43d52d18
[gaim-migrate @ 10721]
" I didn't notice this bug had been closed until I went
looking for it again. :) I have been able to crash
Gaim 0.82cvs using the method described in the bug. It
looks like there was a patch to the gestures plugin to
catch a case where it catches the release of a
non-gestures button and the gesture is active. It seems
as though there is a way to confuse GDK (or the
gestures plugin) into missing the button release event
for the gestures button by sending it a bunch of events
at the same time (chord-clicking all 3 buttons of the
mouse at once).
This patch traps when other buttons are clicked after a
gesture is active and cancels the gesture. I don't know
if it's the Right Fix(tm), but it does keep it from
crashing on my system. I also trapped a place or two
where it would actually segfault in Gaim; the button
trap is more of a fix to keep the gesture from
"sticking". If the gesture sticks and we trap the null
data pointers, Gaim still crashes with a badDrawable X
error.
The error was 'BadDrawable (invalid Pixmap or Window
parameter)'.
(Details: serial 5520 error_code 9 request_code 66
minor_code 0) " --Dave (kat) West
committer: Tailor Script <tailor@pidgin.im>
| author | Luke Schierer <lschiere@pidgin.im> |
|---|---|
| date | Mon, 23 Aug 2004 23:56:23 +0000 |
| parents | e0535ba0d667 |
| children | b23e6f9c4d2e |
| rev | line source |
|---|---|
| 4390 | 1 /* |
| 2 GNOME stroke implementation | |
| 3 Copyright (c) 2000, 2001 Dan Nicolaescu | |
| 4 See the file COPYING for distribution information. | |
| 5 */ | |
| 6 | |
| 7 #include "config.h" | |
| 8 | |
| 9 #include <unistd.h> | |
| 10 #include <stdlib.h> | |
| 11 #include <stdio.h> | |
| 12 #include <glib.h> | |
| 13 #include <gtk/gtk.h> | |
| 14 | |
| 15 #include <gdk/gdkx.h> | |
| 16 #include "gstroke.h" | |
| 17 #include "gstroke-internal.h" | |
| 18 | |
| 19 #include <X11/Xlib.h> | |
| 20 #include <X11/Xutil.h> | |
| 21 | |
| 22 | |
| 23 static void gstroke_invisible_window_init (GtkWidget *widget); | |
| 24 /*FIXME: Maybe these should be put in a structure, and not static...*/ | |
|
4529
f630a793b9d4
[gaim-migrate @ 4807]
Christian Hammond <chipx86@chipx86.com>
parents:
4432
diff
changeset
|
25 static Display * gstroke_disp = NULL; |
| 4390 | 26 static Window gstroke_window; |
| 27 static GC gstroke_gc; | |
| 28 static int mouse_button = 2; | |
| 29 static gboolean draw_strokes = FALSE; | |
| 30 | |
| 31 #define GSTROKE_TIMEOUT_DURATION 10 | |
| 32 | |
| 33 #define GSTROKE_SIGNALS "gstroke_signals" | |
| 34 | |
| 35 struct gstroke_func_and_data { | |
| 36 void (*func)(GtkWidget *, void *); | |
| 37 gpointer data; | |
| 38 }; | |
| 39 | |
| 40 | |
| 41 /*FIXME: maybe it's better to just make 2 static variables, not a | |
| 42 structure */ | |
| 43 struct mouse_position { | |
| 44 struct s_point last_point; | |
| 45 gboolean invalid; | |
| 46 }; | |
| 47 | |
| 48 | |
| 49 static struct mouse_position last_mouse_position; | |
| 50 static guint timer_id; | |
| 51 | |
| 52 static void gstroke_execute (GtkWidget *widget, const gchar *name); | |
| 53 | |
| 54 static void | |
| 55 record_stroke_segment (GtkWidget *widget) | |
| 56 { | |
| 57 gint x, y; | |
| 58 struct gstroke_metrics *metrics; | |
| 59 | |
| 9843 | 60 g_return_if_fail( widget != NULL ); |
| 61 | |
| 4390 | 62 gtk_widget_get_pointer (widget, &x, &y); |
| 63 | |
| 64 if (last_mouse_position.invalid) | |
| 65 last_mouse_position.invalid = FALSE; | |
| 66 else if (gstroke_draw_strokes()) | |
| 67 { | |
| 68 #if 1 | |
| 69 XDrawLine (gstroke_disp, gstroke_window, gstroke_gc, | |
| 70 last_mouse_position.last_point.x, | |
| 71 last_mouse_position.last_point.y, | |
| 72 x, y); | |
| 73 /* XFlush (gstroke_disp); */ | |
| 74 #else | |
| 75 /* FIXME: this does not work. It will only work if we create a | |
| 76 corresponding GDK window for stroke_window and draw on | |
| 77 that... */ | |
| 78 gdk_draw_line (widget->window, widget->style->fg_gc[GTK_STATE_NORMAL], | |
| 79 last_mouse_position.last_point.x, | |
| 80 last_mouse_position.last_point.y, | |
| 81 x, | |
| 82 y); | |
| 83 #endif | |
| 84 } | |
| 85 | |
| 86 if (last_mouse_position.last_point.x != x | |
| 87 || last_mouse_position.last_point.y != y) | |
| 88 { | |
| 89 last_mouse_position.last_point.x = x; | |
| 90 last_mouse_position.last_point.y = y; | |
| 91 metrics = (struct gstroke_metrics *)g_object_get_data(G_OBJECT(widget), | |
| 92 GSTROKE_METRICS); | |
| 93 _gstroke_record (x, y, metrics); | |
| 94 } | |
| 95 } | |
| 96 | |
| 97 static gint | |
| 98 gstroke_timeout (gpointer data) | |
| 99 { | |
| 9843 | 100 g_return_val_if_fail(data != NULL, FALSE); |
| 4390 | 101 GtkWidget *widget = GTK_WIDGET (data); |
| 102 record_stroke_segment (widget); | |
| 103 | |
| 104 return TRUE; | |
| 105 } | |
| 106 | |
| 9843 | 107 static void gstroke_cancel(GdkEvent *event) |
| 108 { | |
| 109 last_mouse_position.invalid = TRUE; | |
| 110 | |
| 111 if (timer_id > 0) | |
| 112 g_source_remove (timer_id); | |
| 113 | |
| 114 timer_id = 0; | |
| 115 | |
| 116 if( event != NULL ) | |
| 117 gdk_pointer_ungrab (event->button.time); | |
| 118 | |
| 119 | |
| 120 if (gstroke_draw_strokes() && gstroke_disp != NULL) { | |
| 121 /* get rid of the invisible stroke window */ | |
| 122 XUnmapWindow (gstroke_disp, gstroke_window); | |
| 123 XFlush (gstroke_disp); | |
| 124 } | |
| 125 | |
| 126 } | |
| 127 | |
| 4390 | 128 static gint |
| 129 process_event (GtkWidget *widget, GdkEvent *event, gpointer data G_GNUC_UNUSED) | |
| 130 { | |
| 131 static GtkWidget *original_widget = NULL; | |
|
5861
711db8ff91dd
[gaim-migrate @ 6292]
Christian Hammond <chipx86@chipx86.com>
parents:
5227
diff
changeset
|
132 static GdkCursor *cursor = NULL; |
|
711db8ff91dd
[gaim-migrate @ 6292]
Christian Hammond <chipx86@chipx86.com>
parents:
5227
diff
changeset
|
133 |
| 4390 | 134 switch (event->type) { |
| 135 case GDK_BUTTON_PRESS: | |
| 9843 | 136 if (event->button.button != gstroke_get_mouse_button()) { |
| 137 /* Similar to the bug below catch when any other button is | |
| 138 * clicked after the middle button is clicked (but possibly | |
| 139 * not released) | |
| 140 */ | |
| 141 gstroke_cancel(event); | |
| 142 original_widget = NULL; | |
| 143 break; | |
| 144 } | |
| 4390 | 145 |
| 146 original_widget = widget; /* remeber the widget where | |
| 147 the stroke started */ | |
| 148 | |
| 149 gstroke_invisible_window_init (widget); | |
| 150 | |
| 151 record_stroke_segment (widget); | |
| 152 | |
|
5861
711db8ff91dd
[gaim-migrate @ 6292]
Christian Hammond <chipx86@chipx86.com>
parents:
5227
diff
changeset
|
153 if (cursor == NULL) |
|
711db8ff91dd
[gaim-migrate @ 6292]
Christian Hammond <chipx86@chipx86.com>
parents:
5227
diff
changeset
|
154 cursor = gdk_cursor_new(GDK_PENCIL); |
|
711db8ff91dd
[gaim-migrate @ 6292]
Christian Hammond <chipx86@chipx86.com>
parents:
5227
diff
changeset
|
155 |
| 4390 | 156 gdk_pointer_grab (widget->window, FALSE, |
|
5861
711db8ff91dd
[gaim-migrate @ 6292]
Christian Hammond <chipx86@chipx86.com>
parents:
5227
diff
changeset
|
157 GDK_BUTTON_RELEASE_MASK, NULL, cursor, |
| 4390 | 158 event->button.time); |
| 8555 | 159 timer_id = g_timeout_add (GSTROKE_TIMEOUT_DURATION, |
| 4390 | 160 gstroke_timeout, widget); |
| 161 return TRUE; | |
| 162 | |
| 163 case GDK_BUTTON_RELEASE: | |
| 164 if ((event->button.button != gstroke_get_mouse_button()) | |
|
4399
ce8d35b435de
[gaim-migrate @ 4668]
Christian Hammond <chipx86@chipx86.com>
parents:
4390
diff
changeset
|
165 || (original_widget == NULL)) { |
|
ce8d35b435de
[gaim-migrate @ 4668]
Christian Hammond <chipx86@chipx86.com>
parents:
4390
diff
changeset
|
166 |
| 9843 | 167 /* Nice bug when you hold down one button and press another. */ |
| 168 /* We'll just cancel the gesture instead. */ | |
| 169 gstroke_cancel(event); | |
| 170 original_widget = NULL; | |
| 171 break; | |
|
4399
ce8d35b435de
[gaim-migrate @ 4668]
Christian Hammond <chipx86@chipx86.com>
parents:
4390
diff
changeset
|
172 } |
| 4390 | 173 |
| 174 last_mouse_position.invalid = TRUE; | |
| 175 original_widget = NULL; | |
| 8555 | 176 g_source_remove (timer_id); |
| 4390 | 177 gdk_pointer_ungrab (event->button.time); |
| 178 timer_id = 0; | |
| 179 | |
| 180 { | |
| 181 char result[GSTROKE_MAX_SEQUENCE]; | |
| 182 struct gstroke_metrics *metrics; | |
| 183 | |
| 184 metrics = (struct gstroke_metrics *)g_object_get_data(G_OBJECT (widget), | |
| 185 GSTROKE_METRICS); | |
| 186 if (gstroke_draw_strokes()) { | |
| 187 /* get rid of the invisible stroke window */ | |
| 188 XUnmapWindow (gstroke_disp, gstroke_window); | |
| 189 XFlush (gstroke_disp); | |
| 190 } | |
| 191 | |
| 192 _gstroke_canonical (result, metrics); | |
| 193 gstroke_execute (widget, result); | |
| 194 return FALSE; | |
| 195 } | |
| 196 return TRUE; | |
| 197 default: | |
| 198 break; | |
| 199 } | |
| 200 | |
| 201 return FALSE; | |
| 202 } | |
| 203 | |
| 204 void | |
| 205 gstroke_set_draw_strokes(gboolean draw) | |
| 206 { | |
| 207 draw_strokes = draw; | |
| 208 } | |
| 209 | |
| 210 gboolean | |
| 211 gstroke_draw_strokes(void) | |
| 212 { | |
| 213 return draw_strokes; | |
| 214 } | |
| 215 | |
| 216 void | |
| 217 gstroke_set_mouse_button(gint button) | |
| 218 { | |
| 219 mouse_button = button; | |
| 220 } | |
| 221 | |
| 7631 | 222 guint |
| 4390 | 223 gstroke_get_mouse_button(void) |
| 224 { | |
| 225 return mouse_button; | |
| 226 } | |
| 227 | |
| 228 void | |
| 229 gstroke_enable (GtkWidget *widget) | |
| 230 { | |
| 231 struct gstroke_metrics* | |
| 232 metrics = (struct gstroke_metrics *)g_object_get_data(G_OBJECT(widget), | |
| 233 GSTROKE_METRICS); | |
| 234 if (metrics == NULL) | |
| 235 { | |
| 236 metrics = (struct gstroke_metrics *)g_malloc (sizeof | |
| 237 (struct gstroke_metrics)); | |
| 238 metrics->pointList = NULL; | |
| 239 metrics->min_x = 10000; | |
| 240 metrics->min_y = 10000; | |
| 241 metrics->max_x = 0; | |
| 242 metrics->max_y = 0; | |
| 243 metrics->point_count = 0; | |
| 244 | |
| 245 g_object_set_data(G_OBJECT(widget), GSTROKE_METRICS, metrics); | |
| 246 | |
| 247 g_signal_connect(G_OBJECT(widget), "event", | |
| 248 G_CALLBACK(process_event), NULL); | |
| 249 } | |
| 250 else | |
| 251 _gstroke_init (metrics); | |
| 252 | |
| 253 last_mouse_position.invalid = TRUE; | |
| 254 } | |
| 255 | |
| 256 guint | |
| 257 gstroke_signal_connect (GtkWidget *widget, | |
| 258 const gchar *name, | |
| 259 void (*func)(GtkWidget *widget, void *data), | |
| 260 gpointer data) | |
| 261 { | |
| 262 struct gstroke_func_and_data *func_and_data; | |
| 263 GHashTable *hash_table = | |
| 264 (GHashTable*)g_object_get_data(G_OBJECT(widget), GSTROKE_SIGNALS); | |
| 265 | |
| 266 if (!hash_table) | |
| 267 { | |
| 268 hash_table = g_hash_table_new (g_str_hash, g_str_equal); | |
| 269 g_object_set_data(G_OBJECT(widget), GSTROKE_SIGNALS, | |
| 270 (gpointer)hash_table); | |
| 271 } | |
| 272 func_and_data = g_new (struct gstroke_func_and_data, 1); | |
| 273 func_and_data->func = func; | |
| 274 func_and_data->data = data; | |
| 275 g_hash_table_insert (hash_table, (gpointer)name, (gpointer)func_and_data); | |
| 276 return TRUE; | |
| 277 } | |
| 278 | |
| 279 static void | |
| 280 gstroke_execute (GtkWidget *widget, const gchar *name) | |
| 281 { | |
| 282 | |
| 283 GHashTable *hash_table = | |
| 284 (GHashTable*)g_object_get_data(G_OBJECT(widget), GSTROKE_SIGNALS); | |
| 285 | |
| 286 #if 0 | |
|
5227
6d1707dc8c3d
[gaim-migrate @ 5597]
Christian Hammond <chipx86@chipx86.com>
parents:
4529
diff
changeset
|
287 gaim_debug(GAIM_DEBUG_MISC, "gestures", "gstroke %s\n", name); |
| 4390 | 288 #endif |
| 289 | |
| 290 if (hash_table) | |
| 291 { | |
| 292 struct gstroke_func_and_data *fd = | |
| 293 (struct gstroke_func_and_data*)g_hash_table_lookup (hash_table, name); | |
| 294 if (fd) | |
| 295 (*fd->func)(widget, fd->data); | |
| 296 } | |
| 297 } | |
| 298 | |
| 299 void | |
| 300 gstroke_cleanup (GtkWidget *widget) | |
| 301 { | |
| 302 struct gstroke_metrics *metrics; | |
| 303 GHashTable *hash_table = | |
| 304 (GHashTable*)g_object_get_data(G_OBJECT(widget), GSTROKE_SIGNALS); | |
| 305 if (hash_table) | |
| 306 /* FIXME: does this delete the elements too? */ | |
| 307 g_hash_table_destroy (hash_table); | |
| 308 | |
| 309 g_object_steal_data(G_OBJECT(widget), GSTROKE_SIGNALS); | |
| 310 | |
| 311 metrics = (struct gstroke_metrics*)g_object_get_data(G_OBJECT(widget), | |
| 312 GSTROKE_METRICS); | |
| 313 if (metrics) | |
| 314 g_free (metrics); | |
| 315 g_object_steal_data(G_OBJECT(widget), GSTROKE_METRICS); | |
| 316 } | |
| 317 | |
| 318 | |
| 319 /* This function should be written using Gtk+ primitives*/ | |
| 320 static void | |
| 321 gstroke_invisible_window_init (GtkWidget *widget) | |
| 322 { | |
| 323 XSetWindowAttributes w_attr; | |
| 324 XWindowAttributes orig_w_attr; | |
| 325 unsigned long mask, col_border, col_background; | |
| 326 unsigned int border_width; | |
| 327 XSizeHints hints; | |
| 328 Display *disp = GDK_WINDOW_XDISPLAY(widget->window); | |
| 329 Window wind = GDK_WINDOW_XWINDOW (widget->window); | |
| 330 int screen = DefaultScreen (disp); | |
| 331 | |
| 332 if (!gstroke_draw_strokes()) | |
| 333 return; | |
| 334 | |
| 335 gstroke_disp = disp; | |
| 336 | |
| 337 /* X server should save what's underneath */ | |
| 338 XGetWindowAttributes (gstroke_disp, wind, &orig_w_attr); | |
| 339 hints.x = orig_w_attr.x; | |
| 340 hints.y = orig_w_attr.y; | |
| 341 hints.width = orig_w_attr.width; | |
| 342 hints.height = orig_w_attr.height; | |
| 343 mask = CWSaveUnder; | |
| 344 w_attr.save_under = True; | |
| 345 | |
| 346 /* inhibit all the decorations */ | |
| 347 mask |= CWOverrideRedirect; | |
| 348 w_attr.override_redirect = True; | |
| 349 | |
| 350 /* Don't set a background, transparent window */ | |
| 351 mask |= CWBackPixmap; | |
| 352 w_attr.background_pixmap = None; | |
| 353 | |
| 354 /* Default input window look */ | |
| 355 col_background = WhitePixel (gstroke_disp, screen); | |
| 356 | |
| 357 /* no border for the window */ | |
|
4399
ce8d35b435de
[gaim-migrate @ 4668]
Christian Hammond <chipx86@chipx86.com>
parents:
4390
diff
changeset
|
358 #if 0 |
| 4390 | 359 border_width = 5; |
|
4399
ce8d35b435de
[gaim-migrate @ 4668]
Christian Hammond <chipx86@chipx86.com>
parents:
4390
diff
changeset
|
360 #endif |
| 4390 | 361 border_width = 0; |
|
4399
ce8d35b435de
[gaim-migrate @ 4668]
Christian Hammond <chipx86@chipx86.com>
parents:
4390
diff
changeset
|
362 |
| 4390 | 363 col_border = BlackPixel (gstroke_disp, screen); |
| 364 | |
| 365 gstroke_window = XCreateSimpleWindow (gstroke_disp, wind, | |
| 366 0, 0, | |
| 367 hints.width - 2 * border_width, | |
| 368 hints.height - 2 * border_width, | |
| 369 border_width, | |
| 370 col_border, col_background); | |
| 371 | |
| 372 gstroke_gc = XCreateGC (gstroke_disp, gstroke_window, 0, NULL); | |
| 373 | |
| 374 XSetFunction (gstroke_disp, gstroke_gc, GXinvert); | |
| 375 | |
| 376 XChangeWindowAttributes (gstroke_disp, gstroke_window, mask, &w_attr); | |
| 377 | |
| 378 XSetLineAttributes (gstroke_disp, gstroke_gc, 2, LineSolid, | |
| 379 CapButt, JoinMiter); | |
| 380 XMapRaised (gstroke_disp, gstroke_window); | |
| 381 | |
| 382 #if 0 | |
| 383 /*FIXME: is this call really needed? If yes, does it need the real | |
| 384 argc and argv? */ | |
| 385 hints.flags = PPosition | PSize; | |
| 386 XSetStandardProperties (gstroke_disp, gstroke_window, "gstroke_test", NULL, | |
| 387 (Pixmap)NULL, NULL, 0, &hints); | |
| 388 | |
| 389 | |
| 390 /* Receive the close window client message */ | |
| 391 { | |
| 392 /* FIXME: is this really needed? If yes, something should be done | |
| 393 with wmdelete...*/ | |
| 394 Atom wmdelete = XInternAtom (gstroke_disp, "WM_DELETE_WINDOW", | |
| 395 False); | |
| 396 XSetWMProtocols (gstroke_disp, gstroke_window, &wmdelete, True); | |
| 397 } | |
| 398 #endif | |
| 399 } |
