OLD | NEW |
| (Empty) |
1 // Copyright 2014 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/ozone/platform/caca/caca_event_source.h" | |
6 | |
7 #include <caca.h> | |
8 | |
9 #include "base/bind.h" | |
10 #include "base/macros.h" | |
11 #include "base/message_loop/message_loop.h" | |
12 #include "base/strings/string_util.h" | |
13 #include "ui/events/event.h" | |
14 #include "ui/events/event_utils.h" | |
15 #include "ui/events/keycodes/keyboard_codes.h" | |
16 #include "ui/ozone/platform/caca/caca_window.h" | |
17 | |
18 namespace ui { | |
19 | |
20 namespace { | |
21 | |
22 ui::KeyboardCode GetKeyboardCode(const caca_event_t& event) { | |
23 // List of special mappings the Caca provides. | |
24 static const ui::KeyboardCode kCacaKeyMap[] = { | |
25 ui::VKEY_UNKNOWN, | |
26 ui::VKEY_A, | |
27 ui::VKEY_B, | |
28 ui::VKEY_C, | |
29 ui::VKEY_D, | |
30 ui::VKEY_E, | |
31 ui::VKEY_F, | |
32 ui::VKEY_G, | |
33 ui::VKEY_BACK, | |
34 ui::VKEY_TAB, | |
35 ui::VKEY_J, | |
36 ui::VKEY_K, | |
37 ui::VKEY_L, | |
38 ui::VKEY_RETURN, | |
39 ui::VKEY_N, | |
40 ui::VKEY_O, | |
41 ui::VKEY_P, | |
42 ui::VKEY_Q, | |
43 ui::VKEY_R, | |
44 ui::VKEY_PAUSE, | |
45 ui::VKEY_T, | |
46 ui::VKEY_U, | |
47 ui::VKEY_V, | |
48 ui::VKEY_W, | |
49 ui::VKEY_X, | |
50 ui::VKEY_Y, | |
51 ui::VKEY_Z, | |
52 ui::VKEY_ESCAPE, | |
53 ui::VKEY_DELETE, | |
54 ui::VKEY_UP, | |
55 ui::VKEY_DOWN, | |
56 ui::VKEY_LEFT, | |
57 ui::VKEY_RIGHT, | |
58 ui::VKEY_INSERT, | |
59 ui::VKEY_HOME, | |
60 ui::VKEY_END, | |
61 ui::VKEY_PRIOR, | |
62 ui::VKEY_NEXT, | |
63 ui::VKEY_F1, | |
64 ui::VKEY_F2, | |
65 ui::VKEY_F3, | |
66 ui::VKEY_F4, | |
67 ui::VKEY_F5, | |
68 ui::VKEY_F6, | |
69 ui::VKEY_F7, | |
70 ui::VKEY_F8, | |
71 ui::VKEY_F9, | |
72 ui::VKEY_F10, | |
73 ui::VKEY_F11, | |
74 ui::VKEY_F12, | |
75 }; | |
76 | |
77 int key_code = caca_get_event_key_ch(&event); | |
78 if (base::IsAsciiLower(key_code)) | |
79 return static_cast<ui::KeyboardCode>(key_code - ('a' - 'A')); | |
80 if (key_code >= '0' && key_code <= 'Z') | |
81 return static_cast<ui::KeyboardCode>(key_code); | |
82 if (static_cast<unsigned int>(key_code) < arraysize(kCacaKeyMap)) | |
83 return kCacaKeyMap[key_code]; | |
84 | |
85 return ui::VKEY_UNKNOWN; | |
86 } | |
87 | |
88 int ModifierFromKey(const caca_event_t& event) { | |
89 int key_code = caca_get_event_key_ch(&event); | |
90 if (base::IsAsciiUpper(key_code)) | |
91 return ui::EF_SHIFT_DOWN; | |
92 if ((key_code >= CACA_KEY_CTRL_A && key_code <= CACA_KEY_CTRL_G) || | |
93 (key_code >= CACA_KEY_CTRL_J && key_code <= CACA_KEY_CTRL_L) || | |
94 (key_code >= CACA_KEY_CTRL_N && key_code <= CACA_KEY_CTRL_R) || | |
95 (key_code >= CACA_KEY_CTRL_T && key_code <= CACA_KEY_CTRL_Z)) | |
96 return ui::EF_CONTROL_DOWN; | |
97 | |
98 return ui::EF_NONE; | |
99 } | |
100 | |
101 int ModifierFromButton(const caca_event_t& event) { | |
102 switch (caca_get_event_mouse_button(&event)) { | |
103 case 1: | |
104 return ui::EF_LEFT_MOUSE_BUTTON; | |
105 case 2: | |
106 return ui::EF_RIGHT_MOUSE_BUTTON; | |
107 case 3: | |
108 return ui::EF_MIDDLE_MOUSE_BUTTON; | |
109 } | |
110 return 0; | |
111 } | |
112 | |
113 // Translate coordinates to bitmap coordinates. | |
114 gfx::PointF TranslateLocation(const gfx::PointF& location, CacaWindow* window) { | |
115 gfx::Size physical_size = window->physical_size(); | |
116 gfx::Size bitmap_size = window->bitmap_size(); | |
117 return gfx::PointF( | |
118 location.x() * bitmap_size.width() / physical_size.width(), | |
119 location.y() * bitmap_size.height() / physical_size.height()); | |
120 } | |
121 | |
122 ui::EventType GetEventTypeFromNative(const caca_event_t& event) { | |
123 switch (caca_get_event_type(&event)) { | |
124 case CACA_EVENT_KEY_PRESS: | |
125 return ui::ET_KEY_PRESSED; | |
126 case CACA_EVENT_KEY_RELEASE: | |
127 return ui::ET_KEY_RELEASED; | |
128 case CACA_EVENT_MOUSE_PRESS: | |
129 return ui::ET_MOUSE_PRESSED; | |
130 case CACA_EVENT_MOUSE_RELEASE: | |
131 return ui::ET_MOUSE_RELEASED; | |
132 case CACA_EVENT_MOUSE_MOTION: | |
133 return ui::ET_MOUSE_MOVED; | |
134 default: | |
135 return ui::ET_UNKNOWN; | |
136 } | |
137 } | |
138 | |
139 } // namespace | |
140 | |
141 CacaEventSource::CacaEventSource() { | |
142 } | |
143 | |
144 CacaEventSource::~CacaEventSource() { | |
145 } | |
146 | |
147 void CacaEventSource::TryProcessingEvent(CacaWindow* window) { | |
148 if (!window->display()) | |
149 return; | |
150 | |
151 caca_event_t event; | |
152 int event_mask = CACA_EVENT_KEY_PRESS | CACA_EVENT_KEY_RELEASE | | |
153 CACA_EVENT_MOUSE_PRESS | CACA_EVENT_MOUSE_RELEASE | | |
154 CACA_EVENT_MOUSE_MOTION | CACA_EVENT_RESIZE | | |
155 CACA_EVENT_QUIT; | |
156 | |
157 if (!caca_get_event(window->display(), event_mask, &event, 0)) | |
158 return; | |
159 | |
160 switch (caca_get_event_type(&event)) { | |
161 case CACA_EVENT_KEY_PRESS: | |
162 case CACA_EVENT_KEY_RELEASE: | |
163 case CACA_EVENT_MOUSE_PRESS: | |
164 case CACA_EVENT_MOUSE_RELEASE: | |
165 case CACA_EVENT_MOUSE_MOTION: | |
166 OnInputEvent(&event, window); | |
167 break; | |
168 case CACA_EVENT_RESIZE: | |
169 window->OnCacaResize(); | |
170 break; | |
171 case CACA_EVENT_QUIT: | |
172 window->OnCacaQuit(); | |
173 break; | |
174 default: | |
175 NOTIMPLEMENTED(); | |
176 } | |
177 } | |
178 | |
179 void CacaEventSource::OnInputEvent(caca_event_t* event, CacaWindow* window) { | |
180 ui::EventType type = GetEventTypeFromNative(*event); | |
181 bool pressed = type == ui::ET_KEY_PRESSED || type == ui::ET_MOUSE_PRESSED; | |
182 | |
183 switch (type) { | |
184 case ui::ET_KEY_PRESSED: | |
185 case ui::ET_KEY_RELEASED: { | |
186 if (pressed) | |
187 modifier_flags_ |= ModifierFromKey(*event); | |
188 else | |
189 modifier_flags_ &= ~ModifierFromKey(*event); | |
190 | |
191 ui::KeyEvent key_event( | |
192 type, GetKeyboardCode(*event), modifier_flags_); | |
193 window->OnCacaEvent(&key_event); | |
194 break; | |
195 } | |
196 case ui::ET_MOUSE_MOVED: | |
197 last_cursor_location_.SetPoint(caca_get_event_mouse_x(event), | |
198 caca_get_event_mouse_y(event)); | |
199 // Update cursor location. | |
200 caca_gotoxy(caca_get_canvas(window->display()), | |
201 last_cursor_location_.x(), | |
202 last_cursor_location_.y()); | |
203 | |
204 // fallthrough | |
205 case ui::ET_MOUSE_PRESSED: | |
206 case ui::ET_MOUSE_RELEASED: { | |
207 int flags = 0; | |
208 int changed_flags = 0; | |
209 if (type != ui::ET_MOUSE_MOVED) { | |
210 if (pressed) { | |
211 changed_flags = ModifierFromButton(*event); | |
212 modifier_flags_ |= changed_flags; | |
213 } else { | |
214 modifier_flags_ &= ~changed_flags; | |
215 } | |
216 // On release the button pressed is removed from |modifier_flags_|, | |
217 // but sending the event needs it set. | |
218 flags = modifier_flags_ | changed_flags; | |
219 } | |
220 gfx::PointF location = TranslateLocation(last_cursor_location_, window); | |
221 ui::MouseEvent mouse_event(type, gfx::Point(), gfx::Point(), | |
222 EventTimeForNow(), flags, changed_flags); | |
223 mouse_event.set_location_f(location); | |
224 mouse_event.set_root_location_f(location); | |
225 window->OnCacaEvent(&mouse_event); | |
226 break; | |
227 } | |
228 default: | |
229 NOTIMPLEMENTED(); | |
230 break; | |
231 } | |
232 } | |
233 | |
234 } // namespace ui | |
OLD | NEW |