| 1 |
/* |
| 2 |
* Clutter. |
| 3 |
* |
| 4 |
* An OpenGL based 'interactive canvas' library. |
| 5 |
* |
| 6 |
* Copyright (C) 2014 Canonical Ltd. |
| 7 |
* |
| 8 |
* This library is free software; you can redistribute it and/or |
| 9 |
* modify it under the terms of the GNU Lesser General Public |
| 10 |
* License as published by the Free Software Foundation; either |
| 11 |
* version 2 of the License, or (at your option) any later version. |
| 12 |
* |
| 13 |
* This library is distributed in the hope that it will be useful, |
| 14 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 16 |
* Lesser General Public License for more details. |
| 17 |
* |
| 18 |
* You should have received a copy of the GNU Lesser General Public |
| 19 |
* License along with this library. If not, see <http://www.gnu.org/licenses/>. |
| 20 |
* |
| 21 |
* Authors: |
| 22 |
* Marco Trevisan <[email protected]> |
| 23 |
*/ |
| 24 |
|
| 25 |
#ifdef HAVE_CONFIG_H |
| 26 |
#include "config.h" |
| 27 |
#endif |
| 28 |
|
| 29 |
#include "clutter-mir.h" |
| 30 |
#include "clutter-private.h" |
| 31 |
#include "clutter-event-private.h" |
| 32 |
#include "clutter-stage-mir.h" |
| 33 |
#include "clutter-stage-private.h" |
| 34 |
#include "clutter-backend-mir-priv.h" |
| 35 |
#include "clutter-device-manager-private.h" |
| 36 |
#include "evdev/clutter-xkb-utils.h" |
| 37 |
|
| 38 |
#include "clutter-event-mir.h" |
| 39 |
|
| 40 |
#define NANO_TO_MILLI(x) ((x) / 1000000) |
| 41 |
|
| 42 |
/* Using the clutter threads lock would cause a dead-lock when resizing */ |
| 43 |
static GMutex mir_event_lock; |
| 44 |
|
| 45 |
static gboolean |
| 46 |
clutter_event_source_mir_check (GSource *source) |
| 47 |
{ |
| 48 |
gboolean retval; |
| 49 |
|
| 50 |
g_mutex_lock (&mir_event_lock); |
| 51 |
|
| 52 |
retval = clutter_events_pending (); |
| 53 |
|
| 54 |
g_mutex_unlock (&mir_event_lock); |
| 55 |
|
| 56 |
return retval; |
| 57 |
} |
| 58 |
|
| 59 |
static gboolean |
| 60 |
clutter_event_source_mir_prepare (GSource *source, gint *timeout) |
| 61 |
{ |
| 62 |
*timeout = -1; |
| 63 |
return clutter_event_source_mir_check (source); |
| 64 |
} |
| 65 |
|
| 66 |
static gboolean |
| 67 |
clutter_event_source_mir_dispatch (GSource *source, |
| 68 |
GSourceFunc callback, |
| 69 |
gpointer data) |
| 70 |
{ |
| 71 |
ClutterEvent *event; |
| 72 |
|
| 73 |
g_mutex_lock (&mir_event_lock); |
| 74 |
_clutter_threads_acquire_lock (); |
| 75 |
|
| 76 |
event = clutter_event_get (); |
| 77 |
|
| 78 |
if (event) |
| 79 |
{ |
| 80 |
/* forward the event into clutter for emission etc. */ |
| 81 |
_clutter_stage_queue_event (event->any.stage, event, FALSE); |
| 82 |
} |
| 83 |
|
| 84 |
_clutter_threads_release_lock (); |
| 85 |
g_mutex_unlock (&mir_event_lock); |
| 86 |
|
| 87 |
return TRUE; |
| 88 |
} |
| 89 |
|
| 90 |
static void |
| 91 |
clutter_event_source_mir_finalize (GSource *source) |
| 92 |
{ |
| 93 |
g_mutex_clear (&mir_event_lock); |
| 94 |
} |
| 95 |
|
| 96 |
static GSourceFuncs clutter_event_source_mir_funcs = { |
| 97 |
clutter_event_source_mir_prepare, |
| 98 |
clutter_event_source_mir_check, |
| 99 |
clutter_event_source_mir_dispatch, |
| 100 |
clutter_event_source_mir_finalize |
| 101 |
}; |
| 102 |
|
| 103 |
GSource * |
| 104 |
_clutter_event_source_mir_new (void) |
| 105 |
{ |
| 106 |
GSource *source; |
| 107 |
|
| 108 |
source = g_source_new (&clutter_event_source_mir_funcs, sizeof (GSource)); |
| 109 |
|
| 110 |
g_mutex_init (&mir_event_lock); |
| 111 |
g_source_set_priority (source, CLUTTER_PRIORITY_EVENTS); |
| 112 |
g_source_attach (source, NULL); |
| 113 |
|
| 114 |
return source; |
| 115 |
} |
| 116 |
|
| 117 |
|
| 118 |
static ClutterModifierType |
| 119 |
translate_mir_modifier (unsigned int key_modifiers, MirMotionButton button_state) |
| 120 |
{ |
| 121 |
ClutterModifierType clutter_modifiers = 0; |
| 122 |
|
| 123 |
if (key_modifiers == mir_key_modifier_none && button_state == 0) |
| 124 |
return clutter_modifiers; |
| 125 |
|
| 126 |
if (key_modifiers & mir_key_modifier_alt) |
| 127 |
clutter_modifiers |= CLUTTER_MOD1_MASK; |
| 128 |
|
| 129 |
if (key_modifiers & mir_key_modifier_shift) |
| 130 |
clutter_modifiers |= CLUTTER_SHIFT_MASK; |
| 131 |
|
| 132 |
if (key_modifiers & mir_key_modifier_ctrl) |
| 133 |
clutter_modifiers |= CLUTTER_CONTROL_MASK; |
| 134 |
|
| 135 |
if (key_modifiers & mir_key_modifier_meta) |
| 136 |
clutter_modifiers |= CLUTTER_META_MASK; |
| 137 |
|
| 138 |
if (key_modifiers & mir_key_modifier_caps_lock) |
| 139 |
clutter_modifiers |= CLUTTER_LOCK_MASK; |
| 140 |
|
| 141 |
if (button_state & mir_motion_button_primary) |
| 142 |
clutter_modifiers |= CLUTTER_BUTTON1_MASK; |
| 143 |
|
| 144 |
if (button_state & mir_motion_button_secondary) |
| 145 |
clutter_modifiers |= CLUTTER_BUTTON3_MASK; |
| 146 |
|
| 147 |
if (button_state & mir_motion_button_tertiary) |
| 148 |
clutter_modifiers |= CLUTTER_BUTTON2_MASK; |
| 149 |
|
| 150 |
return clutter_modifiers; |
| 151 |
} |
| 152 |
|
| 153 |
static gunichar |
| 154 |
get_unicode_value (int32_t key_code) |
| 155 |
{ |
| 156 |
gunichar unicode = '\0'; |
| 157 |
char text[8]; |
| 158 |
int size; |
| 159 |
|
| 160 |
size = xkb_keysym_to_utf8 (key_code, text, sizeof (text)); |
| 161 |
|
| 162 |
if (size > 0) |
| 163 |
{ |
| 164 |
unicode = g_utf8_get_char_validated (text, size); |
| 165 |
|
| 166 |
if (unicode == -1 || unicode == -2) |
| 167 |
unicode = '\0'; |
| 168 |
} |
| 169 |
|
| 170 |
return unicode; |
| 171 |
} |
| 172 |
|
| 173 |
void |
| 174 |
_clutter_mir_handle_event (ClutterBackend *backend, |
| 175 |
MirSurface *surface, |
| 176 |
MirEvent *mir_event) |
| 177 |
{ |
| 178 |
ClutterStageManager *stage_manager; |
| 179 |
ClutterInputDevice *device = NULL; |
| 180 |
ClutterStage *stage = NULL; |
| 181 |
ClutterEvent *event = NULL; |
| 182 |
ClutterModifierType modifiers; |
| 183 |
MirMotionButton button_state; |
| 184 |
MirMotionPointer *pointer; |
| 185 |
const GSList *l; |
| 186 |
|
| 187 |
stage_manager = clutter_stage_manager_get_default (); |
| 188 |
|
| 189 |
for (l = clutter_stage_manager_peek_stages (stage_manager); l; l = l->next) |
| 190 |
{ |
| 191 |
ClutterStage* tmp_stage = l->data; |
| 192 |
|
| 193 |
if (CLUTTER_IS_STAGE (tmp_stage) && |
| 194 |
clutter_mir_stage_get_mir_surface (tmp_stage) == surface) |
| 195 |
{ |
| 196 |
stage = tmp_stage; |
| 197 |
break; |
| 198 |
} |
| 199 |
} |
| 200 |
|
| 201 |
if (!stage) |
| 202 |
return; |
| 203 |
|
| 204 |
g_mutex_lock (&mir_event_lock); |
| 205 |
|
| 206 |
button_state = CLUTTER_STAGE_MIR (stage)->button_state; |
| 207 |
|
| 208 |
switch (mir_event->type) |
| 209 |
{ |
| 210 |
case mir_event_type_key: |
| 211 |
if (mir_event->key.action == mir_key_action_multiple) |
| 212 |
break; |
| 213 |
|
| 214 |
device = clutter_device_manager_get_core_device (backend->device_manager, |
| 215 |
CLUTTER_KEYBOARD_DEVICE); |
| 216 |
|
| 217 |
event = clutter_event_new (mir_event->key.action == mir_key_action_down ? |
| 218 |
CLUTTER_KEY_PRESS : CLUTTER_KEY_RELEASE); |
| 219 |
|
| 220 |
modifiers = translate_mir_modifier (mir_event->key.modifiers, button_state); |
| 221 |
event->key.time = NANO_TO_MILLI (mir_event->key.event_time); |
| 222 |
event->key.modifier_state = modifiers; |
| 223 |
event->key.keyval = mir_event->key.key_code; |
| 224 |
event->key.hardware_keycode = mir_event->key.scan_code + 8; |
| 225 |
event->key.unicode_value = get_unicode_value (mir_event->key.key_code); |
| 226 |
break; |
| 227 |
|
| 228 |
case mir_event_type_motion: |
| 229 |
pointer = mir_event->motion.pointer_coordinates; |
| 230 |
device = clutter_device_manager_get_core_device (backend->device_manager, |
| 231 |
CLUTTER_POINTER_DEVICE); |
| 232 |
|
| 233 |
/* We need to send an ENTER event again if the stage is not focused anymore */ |
| 234 |
if (mir_event->motion.action != mir_motion_action_hover_enter && |
| 235 |
mir_event->motion.action != mir_motion_action_hover_exit && |
| 236 |
!_clutter_input_device_get_stage (device)) |
| 237 |
{ |
| 238 |
ClutterEvent *new_event = clutter_event_new (CLUTTER_ENTER); |
| 239 |
modifiers = translate_mir_modifier (mir_event->motion.modifiers, |
| 240 |
button_state); |
| 241 |
|
| 242 |
clutter_event_set_time (new_event, NANO_TO_MILLI (mir_event->motion.event_time)); |
| 243 |
clutter_event_set_state (new_event, modifiers); |
| 244 |
clutter_event_set_coords (new_event, pointer->x, pointer->y); |
| 245 |
|
| 246 |
_clutter_input_device_set_stage (device, stage); |
| 247 |
|
| 248 |
clutter_event_set_stage (new_event, stage); |
| 249 |
clutter_event_set_device (new_event, device); |
| 250 |
clutter_event_set_source_device (new_event, device); |
| 251 |
|
| 252 |
_clutter_event_push (new_event, FALSE); |
| 253 |
} |
| 254 |
|
| 255 |
switch (mir_event->motion.action) |
| 256 |
{ |
| 257 |
case mir_motion_action_down: |
| 258 |
case mir_motion_action_pointer_down: |
| 259 |
case mir_motion_action_up: |
| 260 |
case mir_motion_action_pointer_up: |
| 261 |
event = clutter_event_new ((mir_event->motion.action == |
| 262 |
mir_motion_action_down || |
| 263 |
mir_event->motion.action == |
| 264 |
mir_motion_action_pointer_down) ? |
| 265 |
CLUTTER_BUTTON_PRESS : |
| 266 |
CLUTTER_BUTTON_RELEASE); |
| 267 |
|
| 268 |
event->button.button = CLUTTER_BUTTON_PRIMARY; |
| 269 |
event->button.click_count = 1; |
| 270 |
|
| 271 |
button_state ^= mir_event->motion.button_state; |
| 272 |
|
| 273 |
if (button_state == 0 || (button_state & mir_motion_button_primary)) |
| 274 |
event->button.button = CLUTTER_BUTTON_PRIMARY; |
| 275 |
else if (button_state & mir_motion_button_secondary) |
| 276 |
event->button.button = CLUTTER_BUTTON_SECONDARY; |
| 277 |
else if (button_state & mir_motion_button_tertiary) |
| 278 |
event->button.button = CLUTTER_BUTTON_MIDDLE; |
| 279 |
else if (button_state & mir_motion_button_back) |
| 280 |
event->button.button = 8; |
| 281 |
else if (button_state & mir_motion_button_forward) |
| 282 |
event->button.button = 9; |
| 283 |
|
| 284 |
button_state = mir_event->motion.button_state; |
| 285 |
CLUTTER_STAGE_MIR (stage)->button_state = button_state; |
| 286 |
|
| 287 |
break; |
| 288 |
case mir_motion_action_scroll: |
| 289 |
event = clutter_event_new (CLUTTER_SCROLL); |
| 290 |
if (ABS (pointer->hscroll) == 1 && pointer->vscroll == 0) |
| 291 |
{ |
| 292 |
clutter_event_set_scroll_direction (event, pointer->hscroll < 0 ? |
| 293 |
CLUTTER_SCROLL_LEFT : |
| 294 |
CLUTTER_SCROLL_RIGHT); |
| 295 |
} |
| 296 |
else if (ABS (pointer->vscroll) == 1 && pointer->hscroll == 0) |
| 297 |
{ |
| 298 |
clutter_event_set_scroll_direction (event, pointer->vscroll < 0 ? |
| 299 |
CLUTTER_SCROLL_DOWN : |
| 300 |
CLUTTER_SCROLL_UP); |
| 301 |
} |
| 302 |
else |
| 303 |
{ |
| 304 |
clutter_event_set_scroll_delta (event, -pointer->hscroll, -pointer->vscroll); |
| 305 |
} |
| 306 |
break; |
| 307 |
|
| 308 |
case mir_motion_action_move: |
| 309 |
case mir_motion_action_hover_move: |
| 310 |
event = clutter_event_new (CLUTTER_MOTION); |
| 311 |
break; |
| 312 |
|
| 313 |
case mir_motion_action_hover_enter: |
| 314 |
event = clutter_event_new (CLUTTER_ENTER); |
| 315 |
_clutter_input_device_set_stage (device, stage); |
| 316 |
break; |
| 317 |
|
| 318 |
case mir_motion_action_hover_exit: |
| 319 |
event = clutter_event_new (CLUTTER_LEAVE); |
| 320 |
_clutter_input_device_set_stage (device, NULL); |
| 321 |
break; |
| 322 |
} |
| 323 |
|
| 324 |
if (event) |
| 325 |
{ |
| 326 |
modifiers = translate_mir_modifier (mir_event->motion.modifiers, |
| 327 |
button_state); |
| 328 |
|
| 329 |
clutter_event_set_time (event, NANO_TO_MILLI (mir_event->motion.event_time)); |
| 330 |
clutter_event_set_state (event, modifiers); |
| 331 |
clutter_event_set_coords (event, pointer->x, pointer->y); |
| 332 |
} |
| 333 |
|
| 334 |
break; |
| 335 |
|
| 336 |
case mir_event_type_surface: |
| 337 |
switch (mir_event->surface.attrib) |
| 338 |
{ |
| 339 |
case mir_surface_attrib_state: |
| 340 |
if (mir_event->surface.value == mir_surface_state_fullscreen) |
| 341 |
{ |
| 342 |
_clutter_stage_update_state (stage, |
| 343 |
0, |
| 344 |
CLUTTER_STAGE_STATE_FULLSCREEN); |
| 345 |
} |
| 346 |
else |
| 347 |
{ |
| 348 |
_clutter_stage_update_state (stage, |
| 349 |
CLUTTER_STAGE_STATE_FULLSCREEN, |
| 350 |
0); |
| 351 |
} |
| 352 |
break; |
| 353 |
|
| 354 |
case mir_surface_attrib_focus: |
| 355 |
if (mir_event->surface.value == mir_surface_focused) |
| 356 |
{ |
| 357 |
_clutter_stage_update_state (stage, |
| 358 |
0, |
| 359 |
CLUTTER_STAGE_STATE_ACTIVATED); |
| 360 |
} |
| 361 |
else /* if (mir_event->surface.value == mir_surface_unfocused) */ |
| 362 |
{ |
| 363 |
_clutter_stage_update_state (stage, |
| 364 |
CLUTTER_STAGE_STATE_ACTIVATED, |
| 365 |
0); |
| 366 |
} |
| 367 |
break; |
| 368 |
|
| 369 |
default: |
| 370 |
break; |
| 371 |
} |
| 372 |
break; |
| 373 |
|
| 374 |
case mir_event_type_close_surface: |
| 375 |
event = clutter_event_new (CLUTTER_DESTROY_NOTIFY); |
| 376 |
break; |
| 377 |
|
| 378 |
default: |
| 379 |
break; |
| 380 |
} |
| 381 |
|
| 382 |
if (event) |
| 383 |
{ |
| 384 |
clutter_event_set_stage (event, stage); |
| 385 |
clutter_event_set_device (event, device); |
| 386 |
clutter_event_set_source_device (event, device); |
| 387 |
|
| 388 |
_clutter_event_push (event, FALSE); |
| 389 |
} |
| 390 |
|
| 391 |
g_mutex_unlock (&mir_event_lock); |
| 392 |
|
| 393 |
if (event) |
| 394 |
g_main_context_wakeup (NULL); |
| 395 |
} |