OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "views/focus/accelerator_handler.h" | 5 #include "views/focus/accelerator_handler.h" |
6 | 6 |
7 #include <gtk/gtk.h> | 7 #include <gtk/gtk.h> |
8 #if defined(HAVE_XINPUT2) | |
9 #include <X11/extensions/XInput2.h> | |
10 #else | |
11 #include <X11/Xlib.h> | 8 #include <X11/Xlib.h> |
12 #endif | |
13 | 9 |
14 #include "views/accelerator.h" | 10 #include "views/accelerator.h" |
15 #include "views/event.h" | 11 #include "views/event.h" |
16 #include "views/focus/focus_manager.h" | 12 #include "views/focus/focus_manager.h" |
17 #include "views/widget/root_view.h" | 13 #include "views/widget/root_view.h" |
18 #include "views/widget/widget_gtk.h" | 14 #include "views/widget/widget_gtk.h" |
19 | 15 |
20 namespace views { | 16 namespace views { |
21 | 17 |
22 namespace { | 18 namespace { |
23 | 19 |
24 RootView* FindRootViewForGdkWindow(GdkWindow* gdk_window) { | 20 RootView* FindRootViewForGdkWindow(GdkWindow* gdk_window) { |
25 gpointer data = NULL; | 21 gpointer data = NULL; |
26 gdk_window_get_user_data(gdk_window, &data); | 22 gdk_window_get_user_data(gdk_window, &data); |
27 GtkWidget* gtk_widget = reinterpret_cast<GtkWidget*>(data); | 23 GtkWidget* gtk_widget = reinterpret_cast<GtkWidget*>(data); |
28 if (!gtk_widget || !GTK_IS_WIDGET(gtk_widget)) { | 24 if (!gtk_widget || !GTK_IS_WIDGET(gtk_widget)) { |
29 DLOG(WARNING) << "no GtkWidget found for that GdkWindow"; | 25 DLOG(WARNING) << "no GtkWidget found for that GdkWindow"; |
30 return NULL; | 26 return NULL; |
31 } | 27 } |
32 WidgetGtk* widget_gtk = WidgetGtk::GetViewForNative(gtk_widget); | 28 WidgetGtk* widget_gtk = WidgetGtk::GetViewForNative(gtk_widget); |
33 | 29 |
34 if (!widget_gtk) { | 30 if (!widget_gtk) { |
35 DLOG(WARNING) << "no WidgetGtk found for that GtkWidget"; | 31 DLOG(WARNING) << "no WidgetGtk found for that GtkWidget"; |
36 return NULL; | 32 return NULL; |
37 } | 33 } |
38 return widget_gtk->GetRootView(); | 34 return widget_gtk->GetRootView(); |
39 } | 35 } |
40 | 36 |
41 #if defined(HAVE_XINPUT2) | |
42 bool X2EventIsTouchEvent(XEvent* xev) { | |
43 // TODO(sad): Determine if the captured event is a touch-event. | |
44 return false; | |
45 } | |
46 #endif // HAVE_XINPUT2 | |
47 | |
48 } // namespace | 37 } // namespace |
49 | 38 |
50 #if defined(HAVE_XINPUT2) | |
51 bool DispatchX2Event(RootView* root, XEvent* xev) { | |
52 if (X2EventIsTouchEvent(xev)) { | |
53 // TODO(sad): Create a TouchEvent, and send it off to |root|. If the event | |
54 // is processed by |root|, then return. Otherwise let it fall through so it | |
55 // can be used (if desired) as a mouse event. | |
56 | |
57 // TouchEvent touch(xev); | |
58 // if (root->OnTouchEvent(touch)) | |
59 // return true; | |
60 } | |
61 | |
62 XGenericEventCookie* cookie = &xev->xcookie; | |
63 | |
64 switch (cookie->evtype) { | |
65 case XI_KeyPress: | |
66 case XI_KeyRelease: { | |
67 // TODO(sad): We don't capture XInput2 events from keyboard yet. | |
68 break; | |
69 } | |
70 case XI_ButtonPress: | |
71 case XI_ButtonRelease: { | |
72 MouseEvent mouseev(xev); | |
73 if (cookie->evtype == XI_ButtonPress) { | |
74 return root->OnMousePressed(mouseev); | |
75 } else { | |
76 root->OnMouseReleased(mouseev, false); | |
77 return true; | |
78 } | |
79 } | |
80 | |
81 case XI_Motion: { | |
82 MouseEvent mouseev(xev); | |
83 if (mouseev.GetType() == Event::ET_MOUSE_DRAGGED) { | |
84 return root->OnMouseDragged(mouseev); | |
85 } else { | |
86 root->OnMouseMoved(mouseev); | |
87 return true; | |
88 } | |
89 break; | |
90 } | |
91 } | |
92 | |
93 return false; | |
94 } | |
95 | |
96 #endif // HAVE_XINPUT2 | |
97 | |
98 bool DispatchXEvent(XEvent* xev) { | 39 bool DispatchXEvent(XEvent* xev) { |
99 GdkDisplay* gdisp = gdk_display_get_default(); | 40 GdkDisplay* gdisp = gdk_display_get_default(); |
100 XID xwindow = xev->xany.window; | 41 GdkWindow* gwind = gdk_window_lookup_for_display(gdisp, xev->xany.window); |
101 | |
102 #if defined(HAVE_XINPUT2) | |
103 if (xev->type == GenericEvent) { | |
104 if (XGetEventData(xev->xgeneric.display, &xev->xcookie)) { | |
105 XGenericEventCookie* cookie = &xev->xcookie; | |
106 XIDeviceEvent* xiev = static_cast<XIDeviceEvent*>(cookie->data); | |
107 xwindow = xiev->event; | |
108 } else { | |
109 DLOG(WARNING) << "Error fetching XGenericEventCookie for event."; | |
110 return false; | |
111 } | |
112 } | |
113 #endif | |
114 | |
115 GdkWindow* gwind = gdk_window_lookup_for_display(gdisp, xwindow); | |
116 | 42 |
117 if (RootView* root = FindRootViewForGdkWindow(gwind)) { | 43 if (RootView* root = FindRootViewForGdkWindow(gwind)) { |
118 switch (xev->type) { | 44 switch (xev->type) { |
119 case KeyPress: | 45 case KeyPress: |
120 case KeyRelease: { | 46 case KeyRelease: { |
121 KeyEvent keyev(xev); | 47 KeyEvent keyev(xev); |
122 | 48 |
123 // If it's a keypress, check to see if it triggers an accelerator. | 49 // If it's a keypress, check to see if it triggers an accelerator. |
124 if (xev->type == KeyPress) { | 50 if (xev->type == KeyPress) { |
125 FocusManager* focus_manager = root->GetFocusManager(); | 51 FocusManager* focus_manager = root->GetFocusManager(); |
(...skipping 18 matching lines...) Expand all Loading... |
144 | 70 |
145 case MotionNotify: { | 71 case MotionNotify: { |
146 MouseEvent mouseev(xev); | 72 MouseEvent mouseev(xev); |
147 if (mouseev.GetType() == Event::ET_MOUSE_DRAGGED) { | 73 if (mouseev.GetType() == Event::ET_MOUSE_DRAGGED) { |
148 return root->OnMouseDragged(mouseev); | 74 return root->OnMouseDragged(mouseev); |
149 } else { | 75 } else { |
150 root->OnMouseMoved(mouseev); | 76 root->OnMouseMoved(mouseev); |
151 return true; | 77 return true; |
152 } | 78 } |
153 } | 79 } |
154 | |
155 #if defined(HAVE_XINPUT2) | |
156 case GenericEvent: { | |
157 bool ret = DispatchX2Event(root, xev); | |
158 XFreeEventData(xev->xgeneric.display, &xev->xcookie); | |
159 return ret; | |
160 } | |
161 #endif | |
162 } | 80 } |
163 } | 81 } |
164 | 82 |
165 return false; | 83 return false; |
166 } | 84 } |
167 | 85 |
168 AcceleratorHandler::AcceleratorHandler() {} | 86 AcceleratorHandler::AcceleratorHandler() {} |
169 | 87 |
170 bool AcceleratorHandler::Dispatch(GdkEvent* event) { | 88 bool AcceleratorHandler::Dispatch(GdkEvent* event) { |
171 gtk_main_do_event(event); | 89 gtk_main_do_event(event); |
172 return true; | 90 return true; |
173 } | 91 } |
174 | 92 |
175 bool AcceleratorHandler::Dispatch(XEvent* xev) { | 93 bool AcceleratorHandler::Dispatch(XEvent* xev) { |
176 return DispatchXEvent(xev); | 94 return DispatchXEvent(xev); |
177 } | 95 } |
178 | 96 |
179 } // namespace views | 97 } // namespace views |
OLD | NEW |