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.
5 #include "ui/ozone/platform/caca/caca_event_source.h"
10 #include "base/message_loop/message_loop.h"
11 #include "ui/events/event.h"
12 #include "ui/events/event_utils.h"
13 #include "ui/events/keycodes/keyboard_codes.h"
14 #include "ui/ozone/platform/caca/caca_window.h"
20 ui::KeyboardCode
GetKeyboardCode(const caca_event_t
& event
) {
21 // List of special mappings the Caca provides.
22 static const ui::KeyboardCode kCacaKeyMap
[] = {
75 int key_code
= caca_get_event_key_ch(&event
);
76 if (key_code
>= 'a' && key_code
<= 'z')
77 return static_cast<ui::KeyboardCode
>(key_code
- ('a' - 'A'));
78 if (key_code
>= '0' && key_code
<= 'Z')
79 return static_cast<ui::KeyboardCode
>(key_code
);
80 if (static_cast<unsigned int>(key_code
) < arraysize(kCacaKeyMap
))
81 return kCacaKeyMap
[key_code
];
83 return ui::VKEY_UNKNOWN
;
86 int ModifierFromKey(const caca_event_t
& event
) {
87 int key_code
= caca_get_event_key_ch(&event
);
88 if (key_code
>= 'A' && key_code
<= 'Z')
89 return ui::EF_SHIFT_DOWN
;
90 if ((key_code
>= CACA_KEY_CTRL_A
&& key_code
<= CACA_KEY_CTRL_G
) ||
91 (key_code
>= CACA_KEY_CTRL_J
&& key_code
<= CACA_KEY_CTRL_L
) ||
92 (key_code
>= CACA_KEY_CTRL_N
&& key_code
<= CACA_KEY_CTRL_R
) ||
93 (key_code
>= CACA_KEY_CTRL_T
&& key_code
<= CACA_KEY_CTRL_Z
))
94 return ui::EF_CONTROL_DOWN
;
99 int ModifierFromButton(const caca_event_t
& event
) {
100 switch (caca_get_event_mouse_button(&event
)) {
102 return ui::EF_LEFT_MOUSE_BUTTON
;
104 return ui::EF_RIGHT_MOUSE_BUTTON
;
106 return ui::EF_MIDDLE_MOUSE_BUTTON
;
111 // Translate coordinates to bitmap coordinates.
112 gfx::PointF
TranslateLocation(const gfx::PointF
& location
, CacaWindow
* window
) {
113 gfx::Size physical_size
= window
->physical_size();
114 gfx::Size bitmap_size
= window
->bitmap_size();
116 location
.x() * bitmap_size
.width() / physical_size
.width(),
117 location
.y() * bitmap_size
.height() / physical_size
.height());
120 ui::EventType
GetEventTypeFromNative(const caca_event_t
& event
) {
121 switch (caca_get_event_type(&event
)) {
122 case CACA_EVENT_KEY_PRESS
:
123 return ui::ET_KEY_PRESSED
;
124 case CACA_EVENT_KEY_RELEASE
:
125 return ui::ET_KEY_RELEASED
;
126 case CACA_EVENT_MOUSE_PRESS
:
127 return ui::ET_MOUSE_PRESSED
;
128 case CACA_EVENT_MOUSE_RELEASE
:
129 return ui::ET_MOUSE_RELEASED
;
130 case CACA_EVENT_MOUSE_MOTION
:
131 return ui::ET_MOUSE_MOVED
;
133 return ui::ET_UNKNOWN
;
139 CacaEventSource::CacaEventSource() {
142 CacaEventSource::~CacaEventSource() {
145 void CacaEventSource::TryProcessingEvent(CacaWindow
* window
) {
146 if (!window
->display())
150 int event_mask
= CACA_EVENT_KEY_PRESS
| CACA_EVENT_KEY_RELEASE
|
151 CACA_EVENT_MOUSE_PRESS
| CACA_EVENT_MOUSE_RELEASE
|
152 CACA_EVENT_MOUSE_MOTION
| CACA_EVENT_RESIZE
|
155 if (!caca_get_event(window
->display(), event_mask
, &event
, 0))
158 switch (caca_get_event_type(&event
)) {
159 case CACA_EVENT_KEY_PRESS
:
160 case CACA_EVENT_KEY_RELEASE
:
161 case CACA_EVENT_MOUSE_PRESS
:
162 case CACA_EVENT_MOUSE_RELEASE
:
163 case CACA_EVENT_MOUSE_MOTION
:
164 OnInputEvent(&event
, window
);
166 case CACA_EVENT_RESIZE
:
167 window
->OnCacaResize();
169 case CACA_EVENT_QUIT
:
170 window
->OnCacaQuit();
177 void CacaEventSource::OnInputEvent(caca_event_t
* event
, CacaWindow
* window
) {
178 ui::EventType type
= GetEventTypeFromNative(*event
);
179 bool pressed
= type
== ui::ET_KEY_PRESSED
|| type
== ui::ET_MOUSE_PRESSED
;
182 case ui::ET_KEY_PRESSED
:
183 case ui::ET_KEY_RELEASED
: {
185 modifier_flags_
|= ModifierFromKey(*event
);
187 modifier_flags_
&= ~ModifierFromKey(*event
);
189 ui::KeyEvent
key_event(
190 type
, GetKeyboardCode(*event
), modifier_flags_
);
191 window
->OnCacaEvent(&key_event
);
194 case ui::ET_MOUSE_MOVED
:
195 last_cursor_location_
.SetPoint(caca_get_event_mouse_x(event
),
196 caca_get_event_mouse_y(event
));
197 // Update cursor location.
198 caca_gotoxy(caca_get_canvas(window
->display()),
199 last_cursor_location_
.x(),
200 last_cursor_location_
.y());
203 case ui::ET_MOUSE_PRESSED
:
204 case ui::ET_MOUSE_RELEASED
: {
206 int changed_flags
= 0;
207 if (type
!= ui::ET_MOUSE_MOVED
) {
209 changed_flags
= ModifierFromButton(*event
);
210 modifier_flags_
|= changed_flags
;
212 modifier_flags_
&= ~changed_flags
;
214 // On release the button pressed is removed from |modifier_flags_|,
215 // but sending the event needs it set.
216 flags
= modifier_flags_
| changed_flags
;
218 gfx::PointF location
= TranslateLocation(last_cursor_location_
, window
);
219 ui::MouseEvent
mouse_event(type
, location
, location
, EventTimeForNow(),
220 flags
, changed_flags
);
221 window
->OnCacaEvent(&mouse_event
);