From: Michael Natterer Date: Tue, 8 Nov 2011 20:41:19 +0000 (+0100) Subject: Bug 663605 - Fix event->state of many event types on quartz X-Git-Url: http://pileus.org/git/?a=commitdiff_plain;h=264241eb1165ace7ffefe70940fa596d372eb132;p=~andy%2Fgtk Bug 663605 - Fix event->state of many event types on quartz Don't try to remember the current keyboard modifier and mouse button states from the last event, because that isn't always right, and don't set event.state = 0 for generated events. Instead, add private functions to get the current states, and implement them with API that retrieves these states independently from an event. --- diff --git a/gdk/quartz/gdkevents-quartz.c b/gdk/quartz/gdkevents-quartz.c index 9337e74fd..f5e100c56 100644 --- a/gdk/quartz/gdkevents-quartz.c +++ b/gdk/quartz/gdkevents-quartz.c @@ -50,9 +50,6 @@ /* This is the window corresponding to the key window */ static GdkWindow *current_keyboard_window; -/* This is the event mask and button state from the last event */ -static GdkModifierType current_keyboard_modifiers; -static GdkModifierType current_button_state; static void append_event (GdkEvent *event, gboolean windowing); @@ -201,6 +198,25 @@ get_mouse_button_from_ns_event (NSEvent *event) } } +static GdkModifierType +get_mouse_button_modifiers_from_ns_buttons (NSUInteger nsbuttons) +{ + GdkModifierType modifiers = 0; + + if (nsbuttons & (1 << 0)) + modifiers |= GDK_BUTTON1_MASK; + if (nsbuttons & (1 << 1)) + modifiers |= GDK_BUTTON3_MASK; + if (nsbuttons & (1 << 2)) + modifiers |= GDK_BUTTON2_MASK; + if (nsbuttons & (1 << 3)) + modifiers |= GDK_BUTTON4_MASK; + if (nsbuttons & (1 << 4)) + modifiers |= GDK_BUTTON5_MASK; + + return modifiers; +} + static GdkModifierType get_mouse_button_modifiers_from_ns_event (NSEvent *event) { @@ -216,13 +232,10 @@ get_mouse_button_modifiers_from_ns_event (NSEvent *event) } static GdkModifierType -get_keyboard_modifiers_from_ns_event (NSEvent *nsevent) +get_keyboard_modifiers_from_ns_flags (NSUInteger nsflags) { GdkModifierType modifiers = 0; - int nsflags; - nsflags = [nsevent modifierFlags]; - if (nsflags & NSAlphaShiftKeyMask) modifiers |= GDK_LOCK_MASK; if (nsflags & NSShiftKeyMask) @@ -237,6 +250,12 @@ get_keyboard_modifiers_from_ns_event (NSEvent *nsevent) return modifiers; } +static GdkModifierType +get_keyboard_modifiers_from_ns_event (NSEvent *nsevent) +{ + return get_keyboard_modifiers_from_ns_flags ([nsevent modifierFlags]); +} + /* Return an event mask from an NSEvent */ static GdkEventMask get_event_mask_from_ns_event (NSEvent *nsevent) @@ -409,7 +428,8 @@ generate_motion_event (GdkWindow *window) event->motion.x_root = x_root; event->motion.y_root = y_root; /* FIXME event->axes */ - event->motion.state = 0; + event->motion.state = _gdk_quartz_events_get_current_keyboard_modifiers () | + _gdk_quartz_events_get_current_mouse_modifiers (); event->motion.is_hint = FALSE; event->motion.device = _gdk_display->core_pointer; @@ -486,7 +506,8 @@ _gdk_quartz_events_send_enter_notify_event (GdkWindow *window) event->crossing.y_root = y_root; event->crossing.mode = GDK_CROSSING_NORMAL; event->crossing.detail = GDK_NOTIFY_ANCESTOR; - event->crossing.state = 0; + event->crossing.state = _gdk_quartz_events_get_current_keyboard_modifiers () | + _gdk_quartz_events_get_current_mouse_modifiers (); gdk_event_set_device (event, _gdk_display->core_pointer); @@ -762,7 +783,8 @@ fill_crossing_event (GdkWindow *toplevel, event->crossing.y_root = y_root; event->crossing.mode = mode; event->crossing.detail = detail; - event->crossing.state = get_keyboard_modifiers_from_ns_event (nsevent); + event->crossing.state = get_keyboard_modifiers_from_ns_event (nsevent) | + _gdk_quartz_events_get_current_mouse_modifiers (); gdk_event_set_device (event, _gdk_display->core_pointer); @@ -953,7 +975,7 @@ fill_key_event (GdkWindow *window, event->key.state |= mask; } - event->key.state |= current_button_state; + event->key.state |= _gdk_quartz_events_get_current_mouse_modifiers (); event->key.string = NULL; @@ -1053,13 +1075,41 @@ synthesize_crossing_event (GdkWindow *window, GdkModifierType _gdk_quartz_events_get_current_keyboard_modifiers (void) { - return current_keyboard_modifiers; + if (gdk_quartz_osx_version () >= GDK_OSX_SNOW_LEOPARD) + { + return get_keyboard_modifiers_from_ns_flags ([NSClassFromString(@"NSEvent") modifierFlags]); + } + else + { + guint carbon_modifiers = GetCurrentKeyModifiers (); + GdkModifierType modifiers = 0; + + if (carbon_modifiers & alphaLock) + modifiers |= GDK_LOCK_MASK; + if (carbon_modifiers & shiftKey) + modifiers |= GDK_SHIFT_MASK; + if (carbon_modifiers & controlKey) + modifiers |= GDK_CONTROL_MASK; + if (carbon_modifiers & optionKey) + modifiers |= GDK_MOD1_MASK; + if (carbon_modifiers & cmdKey) + modifiers |= GDK_MOD2_MASK; + + return modifiers; + } } GdkModifierType _gdk_quartz_events_get_current_mouse_modifiers (void) { - return current_button_state; + if (gdk_quartz_osx_version () >= GDK_OSX_SNOW_LEOPARD) + { + return get_mouse_button_modifiers_from_ns_buttons ([NSClassFromString(@"NSEvent") pressedMouseButtons]); + } + else + { + return get_mouse_button_modifiers_from_ns_buttons (GetCurrentButtonState ()); + } } /* Detect window resizing */ @@ -1144,25 +1194,6 @@ gdk_event_translate (GdkEvent *event, return FALSE; } - /* Keep track of button state, since we don't get that information - * for key events. - */ - switch (event_type) - { - case NSLeftMouseDown: - case NSRightMouseDown: - case NSOtherMouseDown: - current_button_state |= get_mouse_button_modifiers_from_ns_event (nsevent); - break; - case NSLeftMouseUp: - case NSRightMouseUp: - case NSOtherMouseUp: - current_button_state &= ~get_mouse_button_modifiers_from_ns_event (nsevent); - break; - default: - break; - } - if (_gdk_default_filters) { /* Apply global filters */ @@ -1274,8 +1305,6 @@ gdk_event_translate (GdkEvent *event, } } - current_keyboard_modifiers = get_keyboard_modifiers_from_ns_event (nsevent); - return_val = TRUE; switch (event_type)