From 1a32c75ce28e5750fd623a94d300f0dd2716fc02 Mon Sep 17 00:00:00 2001 From: auroux Date: Fri, 25 Sep 2009 22:04:19 +0000 Subject: [PATCH] update event processing code for GTK+ 2.18 --- src/TODO | 12 ++++- src/main.c | 6 +-- src/xo-callbacks.c | 116 ++++++++++++++++++++++++++++++++------------- src/xo-misc.c | 34 +++++++------ src/xo-paint.c | 14 ++---- src/xournal.h | 2 + 6 files changed, 122 insertions(+), 62 deletions(-) diff --git a/src/TODO b/src/TODO index 3c5ec8b..b0b973b 100644 --- a/src/TODO +++ b/src/TODO @@ -48,12 +48,15 @@ DONE: discard Alt-click and Control-click events (go through Patches tracker and take the good stuff... now at 2009-05-09) -** option to have buttons *toggle* the tool rather than act as tool - (ie button 2 causes button 1 to map to tool 2) [Dylan Thurston] +DONE?? option to have buttons *toggle* the tool rather than act as tool + (ie button 2 causes button 1 to map to tool 2) [Dylan Thurston] + +** commit patches from Bob McElrath - paper color chooser (see tracker 2083103) - prerelease: update help file (remove references to pdftoppm/libgnomeprint & update to poppler/gtkprint) +- REMOVE BINARY INSTALLER (poppler breaks ABI all the time) - document config options into manual file? - PDF bg memory usage throttling / delete oldest pdf backgrounds - replace ttsubset by something more modern? (eg. from cairo ?) @@ -71,6 +74,7 @@ DONE: discard Alt-click and Control-click events to load pixmaps from pixmaps/$THEME/ (see Jan 9, 2009 emails) ** autosave patch (Edward Yang) (fix: optional only, w/ menu + cfgfile entries; fix: should clean up autosave.xoj.bg* files too; config interval) +- horizontal mode instead of dual-view / multicolumn ? - patch (ikim@physics.wisc.edu): multicolumn mode + LASSO SELECTION - patch: ortho/snap (revised Apr 13 2009) ** FIXME: get_pressure_multiplier() should access correct members @@ -95,6 +99,10 @@ DONE: discard Alt-click and Control-click events PDF file, default paper - bug in truetype subset generation w/ Adobe 9, see if gtk-print any better? +- drag-and-drop, copy-paste text & images directly into xournal +- proximity detection: eraser proximity switches mapping? + proximity out removes cursor until next motionnotify? + - render page to bitmap: for export, preview, and copy-paste (render using libart, see how gnomecanvas does it?) NO: render using Cairo !!! then can switch to GtkPrint as well. diff --git a/src/main.c b/src/main.c index 2a2dbda..001d09b 100644 --- a/src/main.c +++ b/src/main.c @@ -101,6 +101,7 @@ void init_stuff (int argc, char *argv[]) g_memmove(ui.default_brushes+i, &(ui.brushes[0][i]), sizeof(struct Brush)); ui.cur_mapping = 0; + ui.which_unswitch_button = 0; reset_recognizer(); @@ -223,9 +224,9 @@ void init_stuff (int argc, char *argv[]) gtk_check_menu_item_set_active( GTK_CHECK_MENU_ITEM(GET_COMPONENT("optionsUseXInput")), ui.use_xinput); - /* fix a bug in GTK+ 2.16 and beyond: scrollbars shouldn't get extended + /* fix a bug in GTK+ 2.16 and 2.17: scrollbars shouldn't get extended input events from pointer motion when cursor moves into main window */ -#if GTK_CHECK_VERSION(2,14,0) + if (!gtk_check_version(2, 16, 0)) { g_signal_connect ( GET_COMPONENT("menubar"), @@ -256,7 +257,6 @@ void init_stuff (int argc, char *argv[]) "notify::popup-shown", G_CALLBACK (combobox_popup_disable_xinput), NULL); } -#endif // load the MRU diff --git a/src/xo-callbacks.c b/src/xo-callbacks.c index 71771cd..b1b35bb 100644 --- a/src/xo-callbacks.c +++ b/src/xo-callbacks.c @@ -2356,22 +2356,52 @@ on_canvas_button_press_event (GtkWidget *widget, int mapping; gboolean is_core; struct Item *item; + GdkEvent scroll_event; +#ifdef INPUT_DEBUG + printf("DEBUG: ButtonPress (%s) (x,y)=(%.2f,%.2f), button %d, modifier %x\n", + event->device->name, event->x, event->y, event->button, event->state); +#endif + + if (ui.cur_item_type != ITEM_TEXT) // remove focus from other elements + gtk_widget_grab_focus(GTK_WIDGET(canvas)); + is_core = (event->device == gdk_device_get_core_pointer()); if (!ui.use_xinput && !is_core) return FALSE; if (ui.use_xinput && is_core && ui.discard_corepointer) return FALSE; - if (event->button > 3) return FALSE; // no painting with the mouse wheel! if (event->type != GDK_BUTTON_PRESS) return FALSE; // double-clicks may have broken axes member (free'd) due to a bug in GDK + + if (event->button > 3) { // scroll wheel events! don't paint... + if (ui.use_xinput && !gtk_check_version(2, 17, 0) && event->button <= 7) { + /* with GTK+ 2.17 and later, the entire widget hierarchy is xinput-aware, + so the core button event gets discarded and the scroll event never + gets processed by the main window. This is arguably a GTK+ bug. + We work around it. */ + scroll_event.scroll.type = GDK_SCROLL; + scroll_event.scroll.window = event->window; + scroll_event.scroll.send_event = event->send_event; + scroll_event.scroll.time = event->time; + scroll_event.scroll.x = event->x; + scroll_event.scroll.y = event->y; + scroll_event.scroll.state = event->state; + scroll_event.scroll.device = event->device; + scroll_event.scroll.x_root = event->x_root; + scroll_event.scroll.y_root = event->y_root; + if (event->button == 4) scroll_event.scroll.direction = GDK_SCROLL_UP; + else if (event->button == 5) scroll_event.scroll.direction = GDK_SCROLL_DOWN; + else if (event->button == 6) scroll_event.scroll.direction = GDK_SCROLL_LEFT; + else scroll_event.scroll.direction = GDK_SCROLL_RIGHT; + printf("sending...\n"); + gtk_widget_event(GET_COMPONENT("scrolledwindowMain"), &scroll_event); + } + return FALSE; + } if ((event->state & (GDK_CONTROL_MASK|GDK_MOD1_MASK)) != 0) return FALSE; // no control-clicking or alt-clicking if (!is_core) fix_xinput_coords((GdkEvent *)event); -#ifdef INPUT_DEBUG - printf("DEBUG: ButtonDown (%s) (x,y)=(%.2f,%.2f)\n", - is_core?"core":"xinput", event->x, event->y); -#endif if (!finite(event->x) || !finite(event->y)) return FALSE; // Xorg 7.3 bug if (ui.cur_item_type == ITEM_TEXT) { @@ -2388,17 +2418,21 @@ on_canvas_button_press_event (GtkWidget *widget, // if button_switch_mapping enabled, button 2 or 3 clicks only switch mapping if (ui.button_switch_mapping && event->button > 1) { - if (is_core == ui.use_xinput) return FALSE; // duplicate event - if (ui.cur_mapping == event->button-1) switch_mapping(0); - else switch_mapping(event->button-1); + ui.which_unswitch_button = event->button; + switch_mapping(event->button-1); return FALSE; } ui.is_corestroke = is_core; + ui.stroke_device = event->device; if (ui.use_erasertip && event->device->source == GDK_SOURCE_ERASER) - mapping = NUM_BUTTONS; - else if (ui.button_switch_mapping) mapping = ui.cur_mapping; + mapping = NUM_BUTTONS; + else if (ui.button_switch_mapping) { + mapping = ui.cur_mapping; + if (!mapping && (event->state & GDK_BUTTON2_MASK)) mapping = 1; + if (!mapping && (event->state & GDK_BUTTON3_MASK)) mapping = 2; + } else mapping = event->button-1; // check whether we're in a page @@ -2493,15 +2527,20 @@ on_canvas_button_release_event (GtkWidget *widget, { gboolean is_core; - if (ui.cur_item_type == ITEM_NONE) return FALSE; // not doing anything - - if (event->button != ui.which_mouse_button) return FALSE; // ignore +#ifdef INPUT_DEBUG + printf("DEBUG: ButtonRelease (%s) (x,y)=(%.2f,%.2f), button %d, modifier %x\n", + event->device->name, event->x, event->y, event->button, event->state); +#endif is_core = (event->device == gdk_device_get_core_pointer()); if (!ui.use_xinput && !is_core) return FALSE; if (ui.use_xinput && is_core && !ui.is_corestroke) return FALSE; if (!is_core) fix_xinput_coords((GdkEvent *)event); + if (event->button != ui.which_mouse_button && + event->button != ui.which_unswitch_button) + return FALSE; + if (ui.cur_item_type == ITEM_STROKE) { finalize_stroke(); if (ui.cur_brush->recognizer) recognize_patterns(); @@ -2521,9 +2560,10 @@ on_canvas_button_release_event (GtkWidget *widget, else if (ui.cur_item_type == ITEM_HAND) { ui.cur_item_type = ITEM_NONE; } + + if (!ui.which_unswitch_button || event->button == ui.which_unswitch_button) + switch_mapping(0); // will reset ui.which_unswitch_button - if (!ui.button_switch_mapping || ui.cur_mapping == NUM_BUTTONS) - switch_mapping(0); return FALSE; } @@ -2603,6 +2643,7 @@ on_canvas_motion_notify_event (GtkWidget *widget, { gboolean looks_wrong, is_core; double pt[2]; + GdkModifierType mask; /* we don't care about this event unless some operation is in progress; or if there's a selection (then we might want to change the mouse @@ -2625,11 +2666,15 @@ on_canvas_motion_notify_event (GtkWidget *widget, if (!is_core) ui.is_corestroke = FALSE; #ifdef INPUT_DEBUG - printf("DEBUG: MotionNotify (%s) (x,y)=(%.2f,%.2f)\n", - is_core?"core":"xinput", event->x, event->y); + printf("DEBUG: MotionNotify (%s) (x,y)=(%.2f,%.2f), modifier %x\n", + is_core?"core":"xinput", event->x, event->y, event->state); #endif looks_wrong = !(event->state & (1<<(7+ui.which_mouse_button))); + if (looks_wrong) { + gdk_device_get_state(ui.stroke_device, event->window, NULL, &mask); + looks_wrong = !(mask & (1<<(7+ui.which_mouse_button))); + } if (looks_wrong) { /* mouse button shouldn't be up... give up */ if (ui.cur_item_type == ITEM_STROKE) { @@ -2734,12 +2779,11 @@ on_optionsUseXInput_activate (GtkMenuItem *menuitem, /* HOW THINGS USED TO BE: - We'd like ONLY the canvas window itself to receive - XInput events, while its child window in the GDK hierarchy (also - associated to the canvas widget) receives the core events. - This way on_canvas_... will get both types of events -- otherwise, - the proximity detection code in GDK is broken and we'll lose core - events. + We'd like on_canvas_... to get BOTH core and xinput events. Up to + GTK+ 2.16 this is achieved by making only the canvas's parent + GdkWindow xinput-aware, rather than the entire hierarchy. + Otherwise, the proximity detection code in GDK is broken and + we'll lose core events. Up to GTK+ 2.10, gtk_widget_set_extension_events() only sets extension events for the widget's main window itself; in GTK+ 2.11 @@ -2754,17 +2798,23 @@ on_optionsUseXInput_activate (GtkMenuItem *menuitem, non-responsive). */ - // this causes core events to be discarded... unwanted! -/* - gtk_widget_set_extension_events(GTK_WIDGET (canvas), + if (!gtk_check_version(2, 17, 0)) { + /* GTK+ 2.17 and later: everybody shares a single native window, + so we'll never get any core events, and we might as well set + extension events the way we're supposed to. Doing so helps solve + crasher bugs in 2.17, and prevents us from losing two-button + events in 2.18 */ + gtk_widget_set_extension_events(GTK_WIDGET (canvas), + ui.use_xinput?GDK_EXTENSION_EVENTS_ALL:GDK_EXTENSION_EVENTS_NONE); + } else { + /* GTK+ 2.16 and earlier: we only activate extension events on the + canvas's parent GdkWindow. This allows us to keep receiving core + events. */ + gdk_input_set_extension_events(GTK_WIDGET(canvas)->window, + GDK_POINTER_MOTION_MASK | GDK_BUTTON_MOTION_MASK | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK, ui.use_xinput?GDK_EXTENSION_EVENTS_ALL:GDK_EXTENSION_EVENTS_NONE); -*/ - - // this version only activates extension events on the canvas's parent GdkWindow - gdk_input_set_extension_events(GTK_WIDGET(canvas)->window, - GDK_POINTER_MOTION_MASK | GDK_BUTTON_MOTION_MASK | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK, - ui.use_xinput?GDK_EXTENSION_EVENTS_ALL:GDK_EXTENSION_EVENTS_NONE); - + } + update_mappings_menu(); } diff --git a/src/xo-misc.c b/src/xo-misc.c index b744845..1c29a30 100644 --- a/src/xo-misc.c +++ b/src/xo-misc.c @@ -93,15 +93,15 @@ struct Page *new_page_with_bg(struct Background *bg, double width, double height void realloc_cur_path(int n) { if (n <= ui.cur_path_storage_alloc) return; - ui.cur_path_storage_alloc = n+10; - ui.cur_path.coords = g_realloc(ui.cur_path.coords, 2*(n+10)*sizeof(double)); + ui.cur_path_storage_alloc = n+100; + ui.cur_path.coords = g_realloc(ui.cur_path.coords, 2*(n+100)*sizeof(double)); } void realloc_cur_widths(int n) { if (n <= ui.cur_widths_storage_alloc) return; - ui.cur_widths_storage_alloc = n+10; - ui.cur_widths = g_realloc(ui.cur_widths, (n+10)*sizeof(double)); + ui.cur_widths_storage_alloc = n+100; + ui.cur_widths = g_realloc(ui.cur_widths, (n+100)*sizeof(double)); } // undo utility functions @@ -395,14 +395,18 @@ void fix_xinput_coords(GdkEvent *event) *py = iy + sy; } else { - *px += sx; - *py += sy; + /* with GTK+ 2.16 or earlier, the event comes from the parent gdkwindow + and so needs to be adjusted for scrolling */ + if (gtk_major_version == 2 && gtk_minor_version <= 16) { + *px += sx; + *py += sy; + } /* with GTK+ 2.17, events come improperly translated, and the event's GdkWindow isn't even the same for ButtonDown as for MotionNotify... */ - if (!gtk_check_version(2,17,0)) { // GTK+ 2.17 issues !! + if (gtk_major_version == 2 && gtk_minor_version == 17) { // GTK+ 2.17 issues !! gdk_window_get_position(GTK_WIDGET(canvas)->window, &wx, &wy); - *px -= wx; - *py -= wy; + *px += sx - wx; + *py += sy - wy; } } #endif @@ -1798,7 +1802,8 @@ void resize_journal_items_by(GList *itemlist, double scaling_x, double scaling_y /* NOTE ABOUT BUTTON MAPPINGS: ui.cur_mapping is 0 except while a canvas click event is being processed ... or if ui.button_switch_mapping is - enabled and mappings are switched! */ + enabled and mappings are switched (but even then, canvas should have + a pointer grab from the initial click that switched the mapping) */ void switch_mapping(int m) { @@ -1809,6 +1814,8 @@ void switch_mapping(int m) ui.cur_brush = &(ui.brushes[m][ui.toolno[m]]); if (ui.toolno[m] == TOOL_TEXT) ui.cur_brush = &(ui.brushes[m][TOOL_PEN]); + if (m==0) ui.which_unswitch_button = 0; + update_tool_buttons(); update_color_menu(); update_cursor(); @@ -2114,9 +2121,6 @@ gboolean combobox_popup_disable_xinput (GtkWidget *widget, GdkEvent *event, gboolean is_shown; g_object_get(G_OBJECT(widget), "popup-shown", &is_shown, NULL); - gdk_input_set_extension_events(GTK_WIDGET(canvas)->window, - GDK_POINTER_MOTION_MASK | GDK_BUTTON_MOTION_MASK | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK, - (ui.use_xinput && !is_shown)?GDK_EXTENSION_EVENTS_ALL:GDK_EXTENSION_EVENTS_NONE); + gtk_widget_set_extension_events(GTK_WIDGET (canvas), + (ui.use_xinput && !is_shown)?GDK_EXTENSION_EVENTS_ALL:GDK_EXTENSION_EVENTS_NONE); } - - diff --git a/src/xo-paint.c b/src/xo-paint.c index 85e3536..ba6ffff 100644 --- a/src/xo-paint.c +++ b/src/xo-paint.c @@ -1215,13 +1215,10 @@ void start_text(GdkEvent *event, struct Item *item) get_pointer_coords(event, pt); ui.cur_item_type = ITEM_TEXT; - // HACK TO BYPASS GTK+ 2.17 issues + // HACK TO BYPASS GTK+ 2.17 issue: crash if move text within selection + // also bypass: non-responsiveness of clicks in text box with 2.17 & 2.18 if (!gtk_check_version(2, 17, 0)) { - /* first we make *all* canvas subwindows become XInput aware, - so that it'll be safe to disable XInput later on... (!!!) */ - gtk_widget_set_extension_events(GTK_WIDGET (canvas), - ui.use_xinput?GDK_EXTENSION_EVENTS_ALL:GDK_EXTENSION_EVENTS_NONE); - /* then we ask the canvas's leave-notify handler to disable + /* ask the canvas's leave-notify handler to disable xinput when it's safe to do so... */ ui.need_emergency_disable_xinput = TRUE; } @@ -1292,9 +1289,8 @@ void end_text(void) // HACK TO BYPASS GTK+ 2.17 issues if (!gtk_check_version(2, 17, 0)) { // re-enable XInput if needed (we disabled it during text edition) - gdk_input_set_extension_events(GTK_WIDGET(canvas)->window, - GDK_POINTER_MOTION_MASK | GDK_BUTTON_MOTION_MASK | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK, - ui.use_xinput?GDK_EXTENSION_EVENTS_ALL:GDK_EXTENSION_EVENTS_NONE); + gtk_widget_set_extension_events(GTK_WIDGET (canvas), + ui.use_xinput?GDK_EXTENSION_EVENTS_ALL:GDK_EXTENSION_EVENTS_NONE); } // finalize the text that's been edited... diff --git a/src/xournal.h b/src/xournal.h index 6233fe7..61eb218 100644 --- a/src/xournal.h +++ b/src/xournal.h @@ -223,6 +223,7 @@ typedef struct UIData { gboolean button_switch_mapping; // button clicks switch button 1 mappings gboolean use_erasertip; int which_mouse_button; // the mouse button drawing the current path + int which_unswitch_button; // if button_switch_mapping, the mouse button that switched the mapping struct Page default_page; // the model for the default page int layerbox_length; // the number of entries registered in the layers combo-box struct Item *cur_item; // the item being drawn, or NULL @@ -238,6 +239,7 @@ typedef struct UIData { gboolean pressure_sensitivity; // use pen pressure to control stroke width? double width_minimum_multiplier, width_maximum_multiplier; // calibration for pressure sensitivity gboolean is_corestroke; // this stroke is painted with core pointer + GdkDevice *stroke_device; // who's painting this stroke int screen_width, screen_height; // initial screen size, for XInput events double hand_refpt[2]; int hand_scrollto_cx, hand_scrollto_cy; -- 2.39.2