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