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