static gboolean gdk_x11_device_manager_core_translate_event (GdkEventTranslator *translator, GdkDisplay *display, GdkEvent *event, XEvent *xevent) { GdkX11DeviceManagerCore *device_manager; GdkWindow *window; gboolean return_val; GdkToplevelX11 *toplevel = NULL; GdkX11Display *display_x11 = GDK_X11_DISPLAY (display); device_manager = GDK_X11_DEVICE_MANAGER_CORE (translator); return_val = FALSE; window = get_event_window (translator, xevent); if (window) { if (GDK_WINDOW_DESTROYED (window) || !GDK_IS_WINDOW (window)) return FALSE; toplevel = _gdk_x11_window_get_toplevel (window); g_object_ref (window); } event->any.window = window; event->any.send_event = xevent->xany.send_event ? TRUE : FALSE; if (window && GDK_WINDOW_DESTROYED (window)) { if (xevent->type != DestroyNotify) { return_val = FALSE; goto done; } } if (window && (xevent->type == MotionNotify || xevent->type == ButtonRelease)) { if (_gdk_x11_moveresize_handle_event (xevent)) { return_val = FALSE; goto done; } } /* We do a "manual" conversion of the XEvent to a * GdkEvent. The structures are mostly the same so * the conversion is fairly straightforward. We also * optionally print debugging info regarding events * received. */ return_val = TRUE; switch (xevent->type) { case KeyPress: if (window == NULL) { return_val = FALSE; break; } translate_key_event (display, device_manager, event, xevent); set_user_time (window, event); break; case KeyRelease: if (window == NULL) { return_val = FALSE; break; } /* Emulate detectable auto-repeat by checking to see * if the next event is a key press with the same * keycode and timestamp, and if so, ignoring the event. */ if (!display_x11->have_xkb_autorepeat && XPending (xevent->xkey.display)) { XEvent next_event; XPeekEvent (xevent->xkey.display, &next_event); if (next_event.type == KeyPress && next_event.xkey.keycode == xevent->xkey.keycode && next_event.xkey.time == xevent->xkey.time) { return_val = FALSE; break; } } translate_key_event (display, device_manager, event, xevent); break; case ButtonPress: GDK_NOTE (EVENTS, g_message ("button press:\t\twindow: %ld x,y: %d %d button: %d", xevent->xbutton.window, xevent->xbutton.x, xevent->xbutton.y, xevent->xbutton.button)); if (window == NULL) { return_val = FALSE; break; } /* If we get a ButtonPress event where the button is 4 or 5, it's a Scroll event */ switch (xevent->xbutton.button) { case 4: /* up */ case 5: /* down */ case 6: /* left */ case 7: /* right */ event->scroll.type = GDK_SCROLL; if (xevent->xbutton.button == 4) event->scroll.direction = GDK_SCROLL_UP; else if (xevent->xbutton.button == 5) event->scroll.direction = GDK_SCROLL_DOWN; else if (xevent->xbutton.button == 6) event->scroll.direction = GDK_SCROLL_LEFT; else event->scroll.direction = GDK_SCROLL_RIGHT; event->scroll.window = window; event->scroll.time = xevent->xbutton.time; event->scroll.x = (gdouble) xevent->xbutton.x; event->scroll.y = (gdouble) xevent->xbutton.y; event->scroll.x_root = (gdouble) xevent->xbutton.x_root; event->scroll.y_root = (gdouble) xevent->xbutton.y_root; event->scroll.state = (GdkModifierType) xevent->xbutton.state; event->scroll.device = device_manager->core_pointer; if (!set_screen_from_root (display, event, xevent->xbutton.root)) { return_val = FALSE; break; } break; default: event->button.type = GDK_BUTTON_PRESS; event->button.window = window; event->button.time = xevent->xbutton.time; event->button.x = (gdouble) xevent->xbutton.x; event->button.y = (gdouble) xevent->xbutton.y; event->button.x_root = (gdouble) xevent->xbutton.x_root; event->button.y_root = (gdouble) xevent->xbutton.y_root; event->button.axes = NULL; event->button.state = (GdkModifierType) xevent->xbutton.state; event->button.button = xevent->xbutton.button; event->button.device = device_manager->core_pointer; if (!set_screen_from_root (display, event, xevent->xbutton.root)) return_val = FALSE; break; } set_user_time (window, event); break; case ButtonRelease: GDK_NOTE (EVENTS, g_message ("button release:\twindow: %ld x,y: %d %d button: %d", xevent->xbutton.window, xevent->xbutton.x, xevent->xbutton.y, xevent->xbutton.button)); if (window == NULL) { return_val = FALSE; break; } /* We treat button presses as scroll wheel events, so ignore the release */ if (xevent->xbutton.button == 4 || xevent->xbutton.button == 5 || xevent->xbutton.button == 6 || xevent->xbutton.button == 7) { return_val = FALSE; break; } event->button.type = GDK_BUTTON_RELEASE; event->button.window = window; event->button.time = xevent->xbutton.time; event->button.x = (gdouble) xevent->xbutton.x; event->button.y = (gdouble) xevent->xbutton.y; event->button.x_root = (gdouble) xevent->xbutton.x_root; event->button.y_root = (gdouble) xevent->xbutton.y_root; event->button.axes = NULL; event->button.state = (GdkModifierType) xevent->xbutton.state; event->button.button = xevent->xbutton.button; event->button.device = device_manager->core_pointer; if (!set_screen_from_root (display, event, xevent->xbutton.root)) return_val = FALSE; break; case MotionNotify: GDK_NOTE (EVENTS, g_message ("motion notify:\t\twindow: %ld x,y: %d %d hint: %s", xevent->xmotion.window, xevent->xmotion.x, xevent->xmotion.y, (xevent->xmotion.is_hint) ? "true" : "false")); if (window == NULL) { return_val = FALSE; break; } event->motion.type = GDK_MOTION_NOTIFY; event->motion.window = window; event->motion.time = xevent->xmotion.time; event->motion.x = (gdouble) xevent->xmotion.x; event->motion.y = (gdouble) xevent->xmotion.y; event->motion.x_root = (gdouble) xevent->xmotion.x_root; event->motion.y_root = (gdouble) xevent->xmotion.y_root; event->motion.axes = NULL; event->motion.state = (GdkModifierType) xevent->xmotion.state; event->motion.is_hint = xevent->xmotion.is_hint; event->motion.device = device_manager->core_pointer; if (!set_screen_from_root (display, event, xevent->xbutton.root)) { return_val = FALSE; break; } break; case EnterNotify: GDK_NOTE (EVENTS, g_message ("enter notify:\t\twindow: %ld detail: %d subwin: %ld", xevent->xcrossing.window, xevent->xcrossing.detail, xevent->xcrossing.subwindow)); if (window == NULL) { return_val = FALSE; break; } if (!set_screen_from_root (display, event, xevent->xbutton.root)) { return_val = FALSE; break; } event->crossing.type = GDK_ENTER_NOTIFY; event->crossing.window = window; gdk_event_set_device (event, device_manager->core_pointer); /* If the subwindow field of the XEvent is non-NULL, then * lookup the corresponding GdkWindow. */ if (xevent->xcrossing.subwindow != None) event->crossing.subwindow = gdk_x11_window_lookup_for_display (display, xevent->xcrossing.subwindow); else event->crossing.subwindow = NULL; event->crossing.time = xevent->xcrossing.time; event->crossing.x = (gdouble) xevent->xcrossing.x; event->crossing.y = (gdouble) xevent->xcrossing.y; event->crossing.x_root = (gdouble) xevent->xcrossing.x_root; event->crossing.y_root = (gdouble) xevent->xcrossing.y_root; event->crossing.mode = translate_crossing_mode (xevent->xcrossing.mode); event->crossing.detail = translate_notify_type (xevent->xcrossing.detail); event->crossing.focus = xevent->xcrossing.focus; event->crossing.state = xevent->xcrossing.state; break; case LeaveNotify: GDK_NOTE (EVENTS, g_message ("leave notify:\t\twindow: %ld detail: %d subwin: %ld", xevent->xcrossing.window, xevent->xcrossing.detail, xevent->xcrossing.subwindow)); if (window == NULL) { return_val = FALSE; break; } if (!set_screen_from_root (display, event, xevent->xbutton.root)) { return_val = FALSE; break; } event->crossing.type = GDK_LEAVE_NOTIFY; event->crossing.window = window; gdk_event_set_device (event, device_manager->core_pointer); /* If the subwindow field of the XEvent is non-NULL, then * lookup the corresponding GdkWindow. */ if (xevent->xcrossing.subwindow != None) event->crossing.subwindow = gdk_x11_window_lookup_for_display (display, xevent->xcrossing.subwindow); else event->crossing.subwindow = NULL; event->crossing.time = xevent->xcrossing.time; event->crossing.x = (gdouble) xevent->xcrossing.x; event->crossing.y = (gdouble) xevent->xcrossing.y; event->crossing.x_root = (gdouble) xevent->xcrossing.x_root; event->crossing.y_root = (gdouble) xevent->xcrossing.y_root; event->crossing.mode = translate_crossing_mode (xevent->xcrossing.mode); event->crossing.detail = translate_notify_type (xevent->xcrossing.detail); event->crossing.focus = xevent->xcrossing.focus; event->crossing.state = xevent->xcrossing.state; break; /* We only care about focus events that indicate that _this_ * window (not a ancestor or child) got or lost the focus */ case FocusIn: GDK_NOTE (EVENTS, g_message ("focus in:\t\twindow: %ld, detail: %s, mode: %s", xevent->xfocus.window, notify_details[xevent->xfocus.detail], notify_modes[xevent->xfocus.mode])); if (toplevel) { gboolean had_focus = HAS_FOCUS (toplevel); switch (xevent->xfocus.detail) { case NotifyAncestor: case NotifyVirtual: /* When the focus moves from an ancestor of the window to * the window or a descendent of the window, *and* the * pointer is inside the window, then we were previously * receiving keystroke events in the has_pointer_focus * case and are now receiving them in the * has_focus_window case. */ if (toplevel->has_pointer && xevent->xfocus.mode != NotifyGrab && xevent->xfocus.mode != NotifyUngrab) toplevel->has_pointer_focus = FALSE; /* fall through */ case NotifyNonlinear: case NotifyNonlinearVirtual: if (xevent->xfocus.mode != NotifyGrab && xevent->xfocus.mode != NotifyUngrab) toplevel->has_focus_window = TRUE; /* We pretend that the focus moves to the grab * window, so we pay attention to NotifyGrab * NotifyUngrab, and ignore NotifyWhileGrabbed */ if (xevent->xfocus.mode != NotifyWhileGrabbed) toplevel->has_focus = TRUE; break; case NotifyPointer: /* The X server sends NotifyPointer/NotifyGrab, * but the pointer focus is ignored while a * grab is in effect */ if (xevent->xfocus.mode != NotifyGrab && xevent->xfocus.mode != NotifyUngrab) toplevel->has_pointer_focus = TRUE; break; case NotifyInferior: case NotifyPointerRoot: case NotifyDetailNone: break; } if (HAS_FOCUS (toplevel) != had_focus) generate_focus_event (device_manager, window, TRUE); } break; case FocusOut: GDK_NOTE (EVENTS, g_message ("focus out:\t\twindow: %ld, detail: %s, mode: %s", xevent->xfocus.window, notify_details[xevent->xfocus.detail], notify_modes[xevent->xfocus.mode])); if (toplevel) { gboolean had_focus = HAS_FOCUS (toplevel); switch (xevent->xfocus.detail) { case NotifyAncestor: case NotifyVirtual: /* When the focus moves from the window or a descendent * of the window to an ancestor of the window, *and* the * pointer is inside the window, then we were previously * receiving keystroke events in the has_focus_window * case and are now receiving them in the * has_pointer_focus case. */ if (toplevel->has_pointer && xevent->xfocus.mode != NotifyGrab && xevent->xfocus.mode != NotifyUngrab) toplevel->has_pointer_focus = TRUE; /* fall through */ case NotifyNonlinear: case NotifyNonlinearVirtual: if (xevent->xfocus.mode != NotifyGrab && xevent->xfocus.mode != NotifyUngrab) toplevel->has_focus_window = FALSE; if (xevent->xfocus.mode != NotifyWhileGrabbed) toplevel->has_focus = FALSE; break; case NotifyPointer: if (xevent->xfocus.mode != NotifyGrab && xevent->xfocus.mode != NotifyUngrab) toplevel->has_pointer_focus = FALSE; break; case NotifyInferior: case NotifyPointerRoot: case NotifyDetailNone: break; } if (HAS_FOCUS (toplevel) != had_focus) generate_focus_event (device_manager, window, FALSE); } break; default: return_val = FALSE; } done: if (return_val) { if (event->any.window) g_object_ref (event->any.window); if (((event->any.type == GDK_ENTER_NOTIFY) || (event->any.type == GDK_LEAVE_NOTIFY)) && (event->crossing.subwindow != NULL)) g_object_ref (event->crossing.subwindow); } else { /* Mark this event as having no resources to be freed */ event->any.window = NULL; event->any.type = GDK_NOTHING; } if (window) g_object_unref (window); return return_val; }
static gboolean gdk_x11_device_manager_core_translate_event (GdkEventTranslator *translator, GdkDisplay *display, GdkEvent *event, XEvent *xevent) { GdkWindowImplX11 *impl; GdkX11DeviceManagerCore *device_manager; GdkWindow *window; gboolean return_val; int scale; GdkX11Display *display_x11 = GDK_X11_DISPLAY (display); device_manager = GDK_X11_DEVICE_MANAGER_CORE (translator); window = get_event_window (translator, xevent); scale = 1; if (window) { if (GDK_WINDOW_DESTROYED (window) || !GDK_IS_WINDOW (window)) return FALSE; g_object_ref (window); impl = GDK_WINDOW_IMPL_X11 (window->impl); scale = impl->window_scale; } event->any.window = window; event->any.send_event = xevent->xany.send_event ? TRUE : FALSE; if (window && GDK_WINDOW_DESTROYED (window)) { if (xevent->type != DestroyNotify) { return_val = FALSE; goto done; } } if (window && (xevent->type == MotionNotify || xevent->type == ButtonRelease)) { if (_gdk_x11_moveresize_handle_event (xevent)) { return_val = FALSE; goto done; } } /* We do a "manual" conversion of the XEvent to a * GdkEvent. The structures are mostly the same so * the conversion is fairly straightforward. We also * optionally print debugging info regarding events * received. */ return_val = TRUE; switch (xevent->type) { case KeyPress: if (window == NULL) { return_val = FALSE; break; } translate_key_event (display, device_manager, event, xevent); set_user_time (window, event); break; case KeyRelease: if (window == NULL) { return_val = FALSE; break; } /* Emulate detectable auto-repeat by checking to see * if the next event is a key press with the same * keycode and timestamp, and if so, ignoring the event. */ if (!display_x11->have_xkb_autorepeat && XPending (xevent->xkey.display)) { XEvent next_event; XPeekEvent (xevent->xkey.display, &next_event); if (next_event.type == KeyPress && next_event.xkey.keycode == xevent->xkey.keycode && next_event.xkey.time == xevent->xkey.time) { return_val = FALSE; break; } } translate_key_event (display, device_manager, event, xevent); break; case ButtonPress: GDK_NOTE (EVENTS, g_message ("button press:\t\twindow: %ld x,y: %d %d button: %d", xevent->xbutton.window, xevent->xbutton.x, xevent->xbutton.y, xevent->xbutton.button)); if (window == NULL) { return_val = FALSE; break; } /* If we get a ButtonPress event where the button is 4 or 5, it's a Scroll event */ switch (xevent->xbutton.button) { case 4: /* up */ case 5: /* down */ case 6: /* left */ case 7: /* right */ event->scroll.type = GDK_SCROLL; if (xevent->xbutton.button == 4) event->scroll.direction = GDK_SCROLL_UP; else if (xevent->xbutton.button == 5) event->scroll.direction = GDK_SCROLL_DOWN; else if (xevent->xbutton.button == 6) event->scroll.direction = GDK_SCROLL_LEFT; else event->scroll.direction = GDK_SCROLL_RIGHT; event->scroll.window = window; event->scroll.time = xevent->xbutton.time; event->scroll.x = (gdouble) xevent->xbutton.x / scale; event->scroll.y = (gdouble) xevent->xbutton.y / scale; event->scroll.x_root = (gdouble) xevent->xbutton.x_root / scale; event->scroll.y_root = (gdouble) xevent->xbutton.y_root / scale; event->scroll.state = (GdkModifierType) xevent->xbutton.state; event->scroll.device = device_manager->core_pointer; event->scroll.delta_x = 0; event->scroll.delta_y = 0; if (!set_screen_from_root (display, event, xevent->xbutton.root)) { return_val = FALSE; break; } break; default: event->button.type = GDK_BUTTON_PRESS; event->button.window = window; event->button.time = xevent->xbutton.time; event->button.x = (gdouble) xevent->xbutton.x / scale; event->button.y = (gdouble) xevent->xbutton.y / scale; event->button.x_root = (gdouble) xevent->xbutton.x_root / scale; event->button.y_root = (gdouble) xevent->xbutton.y_root / scale; event->button.axes = NULL; event->button.state = (GdkModifierType) xevent->xbutton.state; event->button.button = xevent->xbutton.button; event->button.device = device_manager->core_pointer; if (!set_screen_from_root (display, event, xevent->xbutton.root)) return_val = FALSE; break; } set_user_time (window, event); break; case ButtonRelease: GDK_NOTE (EVENTS, g_message ("button release:\twindow: %ld x,y: %d %d button: %d", xevent->xbutton.window, xevent->xbutton.x, xevent->xbutton.y, xevent->xbutton.button)); if (window == NULL) { return_val = FALSE; break; } /* We treat button presses as scroll wheel events, so ignore the release */ if (xevent->xbutton.button == 4 || xevent->xbutton.button == 5 || xevent->xbutton.button == 6 || xevent->xbutton.button == 7) { return_val = FALSE; break; } event->button.type = GDK_BUTTON_RELEASE; event->button.window = window; event->button.time = xevent->xbutton.time; event->button.x = (gdouble) xevent->xbutton.x / scale; event->button.y = (gdouble) xevent->xbutton.y / scale; event->button.x_root = (gdouble) xevent->xbutton.x_root / scale; event->button.y_root = (gdouble) xevent->xbutton.y_root / scale; event->button.axes = NULL; event->button.state = (GdkModifierType) xevent->xbutton.state; event->button.button = xevent->xbutton.button; event->button.device = device_manager->core_pointer; if (!set_screen_from_root (display, event, xevent->xbutton.root)) return_val = FALSE; break; case MotionNotify: GDK_NOTE (EVENTS, g_message ("motion notify:\t\twindow: %ld x,y: %d %d hint: %s", xevent->xmotion.window, xevent->xmotion.x, xevent->xmotion.y, (xevent->xmotion.is_hint) ? "true" : "false")); if (window == NULL) { return_val = FALSE; break; } event->motion.type = GDK_MOTION_NOTIFY; event->motion.window = window; event->motion.time = xevent->xmotion.time; event->motion.x = (gdouble) xevent->xmotion.x / scale; event->motion.y = (gdouble) xevent->xmotion.y / scale; event->motion.x_root = (gdouble) xevent->xmotion.x_root / scale; event->motion.y_root = (gdouble) xevent->xmotion.y_root / scale; event->motion.axes = NULL; event->motion.state = (GdkModifierType) xevent->xmotion.state; event->motion.is_hint = xevent->xmotion.is_hint; event->motion.device = device_manager->core_pointer; if (!set_screen_from_root (display, event, xevent->xbutton.root)) { return_val = FALSE; break; } break; case EnterNotify: GDK_NOTE (EVENTS, g_message ("enter notify:\t\twindow: %ld detail: %d subwin: %ld", xevent->xcrossing.window, xevent->xcrossing.detail, xevent->xcrossing.subwindow)); if (window == NULL) { return_val = FALSE; break; } if (!set_screen_from_root (display, event, xevent->xbutton.root)) { return_val = FALSE; break; } event->crossing.type = GDK_ENTER_NOTIFY; event->crossing.window = window; gdk_event_set_device (event, device_manager->core_pointer); /* If the subwindow field of the XEvent is non-NULL, then * lookup the corresponding GdkWindow. */ if (xevent->xcrossing.subwindow != None) event->crossing.subwindow = gdk_x11_window_lookup_for_display (display, xevent->xcrossing.subwindow); else event->crossing.subwindow = NULL; event->crossing.time = xevent->xcrossing.time; event->crossing.x = (gdouble) xevent->xcrossing.x / scale; event->crossing.y = (gdouble) xevent->xcrossing.y / scale; event->crossing.x_root = (gdouble) xevent->xcrossing.x_root / scale; event->crossing.y_root = (gdouble) xevent->xcrossing.y_root / scale; event->crossing.mode = translate_crossing_mode (xevent->xcrossing.mode); event->crossing.detail = translate_notify_type (xevent->xcrossing.detail); event->crossing.focus = xevent->xcrossing.focus; event->crossing.state = xevent->xcrossing.state; break; case LeaveNotify: GDK_NOTE (EVENTS, g_message ("leave notify:\t\twindow: %ld detail: %d subwin: %ld", xevent->xcrossing.window, xevent->xcrossing.detail, xevent->xcrossing.subwindow)); if (window == NULL) { return_val = FALSE; break; } if (!set_screen_from_root (display, event, xevent->xbutton.root)) { return_val = FALSE; break; } event->crossing.type = GDK_LEAVE_NOTIFY; event->crossing.window = window; gdk_event_set_device (event, device_manager->core_pointer); /* If the subwindow field of the XEvent is non-NULL, then * lookup the corresponding GdkWindow. */ if (xevent->xcrossing.subwindow != None) event->crossing.subwindow = gdk_x11_window_lookup_for_display (display, xevent->xcrossing.subwindow); else event->crossing.subwindow = NULL; event->crossing.time = xevent->xcrossing.time; event->crossing.x = (gdouble) xevent->xcrossing.x / scale; event->crossing.y = (gdouble) xevent->xcrossing.y / scale; event->crossing.x_root = (gdouble) xevent->xcrossing.x_root / scale; event->crossing.y_root = (gdouble) xevent->xcrossing.y_root / scale; event->crossing.mode = translate_crossing_mode (xevent->xcrossing.mode); event->crossing.detail = translate_notify_type (xevent->xcrossing.detail); event->crossing.focus = xevent->xcrossing.focus; event->crossing.state = xevent->xcrossing.state; break; case FocusIn: case FocusOut: if (window) _gdk_device_manager_core_handle_focus (window, xevent->xfocus.window, device_manager->core_keyboard, NULL, xevent->type == FocusIn, xevent->xfocus.detail, xevent->xfocus.mode); return_val = FALSE; break; default: return_val = FALSE; } done: if (return_val) { if (event->any.window) g_object_ref (event->any.window); if (((event->any.type == GDK_ENTER_NOTIFY) || (event->any.type == GDK_LEAVE_NOTIFY)) && (event->crossing.subwindow != NULL)) g_object_ref (event->crossing.subwindow); } else { /* Mark this event as having no resources to be freed */ event->any.window = NULL; event->any.type = GDK_NOTHING; } if (window) g_object_unref (window); return return_val; }