OLD | NEW |
| (Empty) |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "ui/wayland/wayland_input_device.h" | |
6 | |
7 #include <X11/extensions/XKBcommon.h> | |
8 #include <wayland-client.h> | |
9 | |
10 #include "base/wayland/wayland_event.h" | |
11 #include "ui/wayland/wayland_widget.h" | |
12 #include "ui/wayland/wayland_window.h" | |
13 | |
14 using namespace base::wayland; | |
15 | |
16 namespace ui { | |
17 | |
18 WaylandInputDevice::WaylandInputDevice( | |
19 wl_display* display, | |
20 uint32_t id) | |
21 : input_device_(static_cast<wl_input_device*>( | |
22 wl_display_bind(display, id, &wl_input_device_interface))), | |
23 pointer_focus_(NULL), | |
24 keyboard_focus_(NULL), | |
25 keyboard_modifiers_(0) { | |
26 | |
27 // List of callback functions for input device events. | |
28 static const struct wl_input_device_listener kInputDeviceListener = { | |
29 WaylandInputDevice::OnMotionNotify, | |
30 WaylandInputDevice::OnButtonNotify, | |
31 WaylandInputDevice::OnKeyNotify, | |
32 WaylandInputDevice::OnPointerFocus, | |
33 WaylandInputDevice::OnKeyboardFocus, | |
34 }; | |
35 | |
36 wl_input_device_add_listener(input_device_, &kInputDeviceListener, this); | |
37 wl_input_device_set_user_data(input_device_, this); | |
38 | |
39 struct xkb_rule_names names; | |
40 names.rules = "evdev"; | |
41 names.model = "pc105"; | |
42 names.layout = "us"; | |
43 names.variant = ""; | |
44 names.options = ""; | |
45 | |
46 xkb_ = xkb_compile_keymap_from_rules(&names); | |
47 } | |
48 | |
49 WaylandInputDevice::~WaylandInputDevice() { | |
50 if (input_device_) | |
51 wl_input_device_destroy(input_device_); | |
52 } | |
53 | |
54 void WaylandInputDevice::Attach(wl_buffer* buffer, int32_t x, int32_t y) { | |
55 wl_input_device_attach(input_device_, last_event_time_, buffer, x, y); | |
56 } | |
57 | |
58 void WaylandInputDevice::OnMotionNotify(void* data, | |
59 wl_input_device* input_device, | |
60 uint32_t time, | |
61 int32_t x, | |
62 int32_t y, | |
63 int32_t sx, | |
64 int32_t sy) { | |
65 WaylandInputDevice* device = static_cast<WaylandInputDevice*>(data); | |
66 WaylandWindow* window = device->pointer_focus_; | |
67 | |
68 device->last_event_time_ = time; | |
69 device->global_position_.SetPoint(x, y); | |
70 device->surface_position_.SetPoint(sx, sy); | |
71 | |
72 WaylandEvent event; | |
73 event.type = WAYLAND_MOTION; | |
74 event.motion.time = time; | |
75 event.motion.modifiers = device->keyboard_modifiers_; | |
76 event.motion.x = sx; | |
77 event.motion.y = sy; | |
78 | |
79 window->widget()->OnMotionNotify(event); | |
80 } | |
81 | |
82 void WaylandInputDevice::OnButtonNotify(void* data, | |
83 wl_input_device* input_device, | |
84 uint32_t time, | |
85 uint32_t button, | |
86 uint32_t state) { | |
87 WaylandInputDevice* device = static_cast<WaylandInputDevice*>(data); | |
88 WaylandWindow* window = device->pointer_focus_; | |
89 | |
90 device->last_event_time_ = time; | |
91 | |
92 WaylandEvent event; | |
93 event.type = WAYLAND_BUTTON; | |
94 event.button.time = time; | |
95 event.button.button = button; | |
96 event.button.state = state; | |
97 event.button.modifiers = device->keyboard_modifiers_; | |
98 event.button.x = device->surface_position_.x(); | |
99 event.button.y = device->surface_position_.y(); | |
100 | |
101 window->widget()->OnButtonNotify(event); | |
102 } | |
103 | |
104 void WaylandInputDevice::OnKeyNotify(void* data, | |
105 wl_input_device* input_device, | |
106 uint32_t time, | |
107 uint32_t key, | |
108 uint32_t state) { | |
109 WaylandInputDevice* device = static_cast<WaylandInputDevice*>(data); | |
110 WaylandWindow* window = device->keyboard_focus_; | |
111 struct xkb_desc *xkb = device->xkb_; | |
112 | |
113 device->last_event_time_ = time; | |
114 | |
115 WaylandEvent event; | |
116 event.type = WAYLAND_KEY; | |
117 event.key.time = time; | |
118 event.key.key = key; | |
119 event.key.state = state; | |
120 | |
121 uint32_t code = key + xkb->min_key_code; | |
122 uint32_t level = 0; | |
123 if ((device->keyboard_modifiers_ & XKB_COMMON_SHIFT_MASK) && | |
124 XkbKeyGroupWidth(xkb, code, 0) > 1) { | |
125 level = 1; | |
126 } | |
127 | |
128 event.key.sym = XkbKeySymEntry(xkb, code, level, 0); | |
129 if (state) | |
130 device->keyboard_modifiers_ |= xkb->map->modmap[code]; | |
131 else | |
132 device->keyboard_modifiers_ &= ~xkb->map->modmap[code]; | |
133 | |
134 event.key.modifiers = device->keyboard_modifiers_; | |
135 | |
136 window->widget()->OnKeyNotify(event); | |
137 } | |
138 | |
139 void WaylandInputDevice::OnPointerFocus(void* data, | |
140 wl_input_device* input_device, | |
141 uint32_t time, | |
142 wl_surface* surface, | |
143 int32_t x, | |
144 int32_t y, | |
145 int32_t sx, | |
146 int32_t sy) { | |
147 WaylandInputDevice* device = static_cast<WaylandInputDevice*>(data); | |
148 WaylandWindow* window = device->pointer_focus_; | |
149 | |
150 device->last_event_time_ = time; | |
151 | |
152 WaylandEvent event; | |
153 event.type = WAYLAND_POINTER_FOCUS; | |
154 event.pointer_focus.time = time; | |
155 event.pointer_focus.x = sx; | |
156 event.pointer_focus.y = sy; | |
157 | |
158 // If we have a window, then this means it loses focus | |
159 if (window) { | |
160 event.pointer_focus.state = 0; | |
161 device->pointer_focus_ = NULL; | |
162 window->widget()->OnPointerFocus(event); | |
163 } | |
164 | |
165 // If we have a surface, then a new window is in focus | |
166 if (surface) { | |
167 event.pointer_focus.state = 1; | |
168 window = static_cast<WaylandWindow*>(wl_surface_get_user_data(surface)); | |
169 device->pointer_focus_ = window; | |
170 window->widget()->OnPointerFocus(event); | |
171 } | |
172 } | |
173 | |
174 void WaylandInputDevice::OnKeyboardFocus(void* data, | |
175 wl_input_device* input_device, | |
176 uint32_t time, | |
177 wl_surface* surface, | |
178 wl_array* keys) { | |
179 WaylandInputDevice* device = static_cast<WaylandInputDevice*>(data); | |
180 WaylandWindow* window = device->keyboard_focus_; | |
181 struct xkb_desc* xkb = device->xkb_; | |
182 | |
183 device->last_event_time_ = time; | |
184 | |
185 WaylandEvent event; | |
186 event.type = WAYLAND_KEYBOARD_FOCUS; | |
187 event.keyboard_focus.time = time; | |
188 device->keyboard_modifiers_ = 0; | |
189 | |
190 uint32_t* codes = static_cast<uint32_t*>(keys->data); | |
191 int codes_size = keys->size / sizeof(uint32_t); | |
192 for (int i = 0; i < codes_size; i++) { | |
193 uint32_t code = codes[i] + xkb->min_key_code; | |
194 device->keyboard_modifiers_ |= xkb->map->modmap[code]; | |
195 } | |
196 event.keyboard_focus.modifiers = device->keyboard_modifiers_; | |
197 | |
198 // If there is a window, then it loses focus | |
199 if (window) { | |
200 event.keyboard_focus.state = 0; | |
201 device->keyboard_focus_ = NULL; | |
202 window->widget()->OnKeyboardFocus(event); | |
203 } | |
204 | |
205 // If we have a surface, then a window gains focus | |
206 if (surface) { | |
207 event.keyboard_focus.state = 1; | |
208 window = static_cast<WaylandWindow*>(wl_surface_get_user_data(surface)); | |
209 device->keyboard_focus_ = window; | |
210 window->widget()->OnKeyboardFocus(event); | |
211 } | |
212 } | |
213 | |
214 } // namespace ui | |
OLD | NEW |