gdkmireventsource.c 25.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
/*
 * Copyright © 2014 Canonical Ltd
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation; either version 2 of
 * the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
 */

#include "config.h"

#include "gdkinternals.h"
#include "gdkdisplayprivate.h"
#include "gdkmir.h"
#include "gdkmir-private.h"

25
#include <mir_toolkit/events/window_placement.h>
26

27
#define NANO_TO_MILLI(x) ((x) / 1000000)
28

29 30 31 32 33 34 35 36
struct _GdkMirWindowReference {
  GdkMirEventSource *source;
  GdkWindow         *window;
  gint               ref_count;
};

typedef struct {
  GdkMirWindowReference *window_ref;
37
  const MirEvent        *event;
38 39 40 41 42 43 44 45
} GdkMirQueuedEvent;

struct _GdkMirEventSource
{
  GSource parent_instance;

  GMutex mir_event_lock;
  GQueue mir_events;
46
  gboolean log_events;
47 48 49 50 51 52 53 54 55 56 57

  GdkDisplay *display;
};

static void
send_event (GdkWindow *window, GdkDevice *device, GdkEvent *event)
{
  GdkDisplay *display;
  GList *node;

  gdk_event_set_device (event, device);
58
  gdk_event_set_source_device (event, device);
59
  gdk_event_set_screen (event, gdk_display_get_default_screen (gdk_window_get_display (window)));
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
  event->any.window = g_object_ref (window);

  display = gdk_window_get_display (window);
  node = _gdk_event_queue_append (display, event);
  _gdk_windowing_got_event (display, node, event, _gdk_display_get_next_serial (display));
}

static void
set_key_event_string (GdkEventKey *event)
{
  gunichar c = 0;

  if (event->keyval != GDK_KEY_VoidSymbol)
    c = gdk_keyval_to_unicode (event->keyval);

  if (c)
    {
      gchar buf[7];
      gint len;
      gsize bytes_written;

      /* Apply the control key - Taken from Xlib
       */
      if (event->state & GDK_CONTROL_MASK)
        {
          if ((c >= '@' && c < '\177') || c == ' ') c &= 0x1F;
          else if (c == '2')
            {
              event->string = g_memdup ("\0\0", 2);
              event->length = 1;
              buf[0] = '\0';
              return;
            }
          else if (c >= '3' && c <= '7') c -= ('3' - '\033');
          else if (c == '8') c = '\177';
          else if (c == '/') c = '_' & 0x1F;
        }

      len = g_unichar_to_utf8 (c, buf);
      buf[len] = '\0';

      event->string = g_locale_from_utf8 (buf, len,
                                          NULL, &bytes_written,
                                          NULL);
      if (event->string)
        event->length = bytes_written;
    }
  else if (event->keyval == GDK_KEY_Escape)
    {
      event->length = 1;
      event->string = g_strdup ("\033");
    }
  else if (event->keyval == GDK_KEY_Return ||
           event->keyval == GDK_KEY_KP_Enter)
    {
      event->length = 1;
      event->string = g_strdup ("\r");
    }

  if (!event->string)
    {
      event->length = 0;
      event->string = g_strdup ("");
    }
}

static void
127
generate_key_event (GdkWindow *window, GdkEventType type, guint state, guint keyval, guint16 keycode, gboolean is_modifier, guint32 event_time)
128 129
{
  GdkEvent *event;
130 131 132
  GdkDisplay *display;
  GdkSeat *seat;
  GdkDevice *keyboard;
133 134 135 136 137

  event = gdk_event_new (type);
  event->key.state = state;
  event->key.keyval = keyval;
  event->key.hardware_keycode = keycode + 8;
138
  gdk_event_set_scancode (event, keycode + 8);
139
  event->key.is_modifier = is_modifier;
140
  event->key.time = event_time;
141 142
  set_key_event_string (&event->key);

143 144 145 146 147
  display = gdk_window_get_display (window);
  seat = gdk_display_get_default_seat (display);
  keyboard = gdk_seat_get_keyboard (seat);

  send_event (window, keyboard, event);
148 149 150 151 152
}

static GdkDevice *
get_pointer (GdkWindow *window)
{
153 154 155 156 157 158 159 160 161
  GdkDisplay *display;
  GdkSeat *seat;
  GdkDevice *pointer;

  display = gdk_window_get_display (window);
  seat = gdk_display_get_default_seat (display);
  pointer = gdk_seat_get_pointer (seat);

  return pointer;
162 163 164
}

static void
165
generate_button_event (GdkWindow *window, GdkEventType type, gdouble x, gdouble y, guint button, guint state, guint32 event_time)
166 167 168 169 170 171 172 173
{
  GdkEvent *event;

  event = gdk_event_new (type);
  event->button.x = x;
  event->button.y = y;
  event->button.state = state;
  event->button.button = button;
174
  event->button.time = event_time;
175 176 177 178 179

  send_event (window, get_pointer (window), event);
}

static void
180
generate_scroll_event (GdkWindow *window, gdouble x, gdouble y, gdouble delta_x, gdouble delta_y, guint state, guint32 event_time)
181 182 183 184 185 186 187
{
  GdkEvent *event;

  event = gdk_event_new (GDK_SCROLL);
  event->scroll.x = x;
  event->scroll.y = y;
  event->scroll.state = state;
188
  event->scroll.time = event_time;
189

190 191 192 193 194 195 196 197 198 199 200
  if (ABS (delta_x) == 1 && delta_y == 0)
    {
      event->scroll.direction = (delta_x < 0) ? GDK_SCROLL_LEFT : GDK_SCROLL_RIGHT;
    }
  else if (ABS (delta_y) == 1 && delta_x == 0)
    {
      event->scroll.direction = (delta_y < 0) ? GDK_SCROLL_DOWN : GDK_SCROLL_UP;
    }
  else
    {
      event->scroll.direction = GDK_SCROLL_SMOOTH;
201
      event->scroll.delta_x = delta_x;
202 203 204
      event->scroll.delta_y = -delta_y;
    }

205 206 207 208
  send_event (window, get_pointer (window), event);
}

static void
209
generate_motion_event (GdkWindow *window, gdouble x, gdouble y, guint state, guint32 event_time)
210 211 212 213 214 215 216 217
{
  GdkEvent *event;

  event = gdk_event_new (GDK_MOTION_NOTIFY);
  event->motion.x = x;
  event->motion.y = y;
  event->motion.state = state;
  event->motion.is_hint = FALSE;
218
  event->motion.time = event_time;
219 220 221 222 223

  send_event (window, get_pointer (window), event);
}

static void
224
generate_crossing_event (GdkWindow *window, GdkEventType type, gdouble x, gdouble y, guint32 event_time)
225 226 227 228 229 230 231 232 233
{
  GdkEvent *event;

  event = gdk_event_new (type);
  event->crossing.x = x;
  event->crossing.y = y;
  event->crossing.mode = GDK_CROSSING_NORMAL;
  event->crossing.detail = GDK_NOTIFY_ANCESTOR;
  event->crossing.focus = TRUE;
234
  event->crossing.time = event_time;
235 236 237 238 239 240 241 242 243 244

  send_event (window, get_pointer (window), event);
}

static void
generate_focus_event (GdkWindow *window, gboolean focused)
{
  GdkEvent *event;

  if (focused)
William Hua's avatar
William Hua committed
245 246 247 248
    {
      gdk_synthesize_window_state (window, 0, GDK_WINDOW_STATE_FOCUSED);
      _gdk_mir_display_focus_window (gdk_window_get_display (window), window);
    }
249
  else
William Hua's avatar
William Hua committed
250 251 252 253
    {
      gdk_synthesize_window_state (window, GDK_WINDOW_STATE_FOCUSED, 0);
      _gdk_mir_display_unfocus_window (gdk_window_get_display (window), window);
    }
254 255 256 257 258 259 260 261 262

  event = gdk_event_new (GDK_FOCUS_CHANGE);
  event->focus_change.send_event = FALSE;
  event->focus_change.in = focused;

  send_event (window, get_pointer (window), event);
}

static guint
263
get_modifier_state (unsigned int modifiers, guint button_state)
264
{
265
  guint modifier_state = button_state;
266

267 268 269
  if ((modifiers & (mir_input_event_modifier_alt |
                    mir_input_event_modifier_alt_left |
                    mir_input_event_modifier_alt_right)) != 0)
270
    modifier_state |= GDK_MOD1_MASK;
271 272 273
  if ((modifiers & (mir_input_event_modifier_shift |
                    mir_input_event_modifier_shift_left |
                    mir_input_event_modifier_shift_right)) != 0)
274
    modifier_state |= GDK_SHIFT_MASK;
275 276 277
  if ((modifiers & (mir_input_event_modifier_ctrl |
                    mir_input_event_modifier_ctrl_left |
                    mir_input_event_modifier_ctrl_right)) != 0)
278
    modifier_state |= GDK_CONTROL_MASK;
279 280 281 282 283
  if ((modifiers & (mir_input_event_modifier_meta |
                    mir_input_event_modifier_meta_left |
                    mir_input_event_modifier_meta_right)) != 0)
    modifier_state |= GDK_META_MASK;
  if ((modifiers & mir_input_event_modifier_caps_lock) != 0)
284 285 286 287 288 289
    modifier_state |= GDK_LOCK_MASK;

  return modifier_state;
}

static void
290
handle_key_event (GdkWindow *window, const MirInputEvent *event)
291
{
292
  const MirKeyboardEvent *keyboard_event = mir_input_event_get_keyboard_event (event);
293
  GdkMirWindowImpl *impl = GDK_MIR_WINDOW_IMPL (window->impl);
294
  GdkKeymap *keymap;
295
  guint modifier_state;
296
  guint button_state;
297

298 299 300
  if (!keyboard_event)
    return;

301 302 303 304 305 306 307 308 309 310 311
  _gdk_mir_window_impl_get_cursor_state (impl, NULL, NULL, NULL, &button_state);
  modifier_state = get_modifier_state (mir_keyboard_event_modifiers (keyboard_event), button_state);
  keymap = gdk_keymap_get_for_display (gdk_window_get_display (window));

  generate_key_event (window,
                      mir_keyboard_event_action (keyboard_event) == mir_keyboard_action_up ? GDK_KEY_RELEASE : GDK_KEY_PRESS,
                      modifier_state,
                      mir_keyboard_event_key_code (keyboard_event),
                      mir_keyboard_event_scan_code (keyboard_event),
                      _gdk_mir_keymap_key_is_modifier (keymap, mir_keyboard_event_key_code (keyboard_event)),
                      NANO_TO_MILLI (mir_input_event_get_event_time (event)));
312 313
}

William Hua's avatar
William Hua committed
314 315 316 317 318 319 320 321 322 323 324
static void
handle_touch_event (GdkWindow           *window,
                    const MirTouchEvent *mir_touch_event)
{
  const MirInputEvent *mir_input_event = mir_touch_event_input_event (mir_touch_event);
  guint n = mir_touch_event_point_count (mir_touch_event);
  GdkEvent *gdk_event;
  guint i;

  for (i = 0; i < n; i++)
    {
325 326 327 328 329 330 331
      MirTouchAction action = mir_touch_event_action (mir_touch_event, i);
      if (action == mir_touch_action_up)
        gdk_event = gdk_event_new (GDK_TOUCH_END);
      else if (action == mir_touch_action_down)
        gdk_event = gdk_event_new (GDK_TOUCH_BEGIN);
      else
        gdk_event = gdk_event_new (GDK_TOUCH_UPDATE);
William Hua's avatar
William Hua committed
332 333 334 335 336 337 338 339 340

      gdk_event->touch.window = window;
      gdk_event->touch.sequence = GINT_TO_POINTER (mir_touch_event_id (mir_touch_event, i));
      gdk_event->touch.time = mir_input_event_get_event_time (mir_input_event);
      gdk_event->touch.state = get_modifier_state (mir_touch_event_modifiers (mir_touch_event), 0);
      gdk_event->touch.x = mir_touch_event_axis_value (mir_touch_event, i, mir_touch_axis_x);
      gdk_event->touch.y = mir_touch_event_axis_value (mir_touch_event, i, mir_touch_axis_y);
      gdk_event->touch.x_root = mir_touch_event_axis_value (mir_touch_event, i, mir_touch_axis_x);
      gdk_event->touch.y_root = mir_touch_event_axis_value (mir_touch_event, i, mir_touch_axis_y);
341
      gdk_event->touch.emulating_pointer = TRUE;
342
      gdk_event_set_pointer_emulated (gdk_event, TRUE);
William Hua's avatar
William Hua committed
343 344 345 346 347

      send_event (window, get_pointer (window), gdk_event);
    }
}

348
static guint
349
get_button_state (const MirPointerEvent *event)
350
{
351 352 353 354 355 356 357 358 359 360
  guint state = 0;

  if (mir_pointer_event_button_state (event, mir_pointer_button_primary)) /* left */
    state |= GDK_BUTTON1_MASK;
  if (mir_pointer_event_button_state (event, mir_pointer_button_secondary)) /* right */
    state |= GDK_BUTTON3_MASK;
  if (mir_pointer_event_button_state (event, mir_pointer_button_tertiary)) /* middle */
    state |= GDK_BUTTON2_MASK;

  return state;
361 362
}

363
static void
364
handle_motion_event (GdkWindow *window, const MirInputEvent *event)
365
{
366
  const MirPointerEvent *pointer_event = mir_input_event_get_pointer_event (event);
367 368 369
  GdkMirWindowImpl *impl = GDK_MIR_WINDOW_IMPL (window->impl);
  gdouble x, y;
  gboolean cursor_inside;
370
  guint button_state;
371
  guint new_button_state;
372
  guint modifier_state;
373
  guint32 event_time;
374
  GdkEventType event_type;
375 376 377 378
  guint changed_button_state;

  if (!pointer_event)
    return;
379 380

  _gdk_mir_window_impl_get_cursor_state (impl, &x, &y, &cursor_inside, &button_state);
381 382
  new_button_state = get_button_state (pointer_event);
  modifier_state = get_modifier_state (mir_pointer_event_modifiers (pointer_event), new_button_state);
383
  event_time = NANO_TO_MILLI (mir_input_event_get_event_time (event));
384

385 386
  if (window)
    {
387 388 389 390 391
      gdouble new_x;
      gdouble new_y;
      gdouble hscroll;
      gdouble vscroll;

392 393
      /* Update which window has focus */
      _gdk_mir_pointer_set_location (get_pointer (window), x, y, window, modifier_state);
394
      switch (mir_pointer_event_action (pointer_event))
395
        {
396 397 398
        case mir_pointer_action_button_up:
        case mir_pointer_action_button_down:
          event_type = mir_pointer_event_action (pointer_event) == mir_pointer_action_button_down ? GDK_BUTTON_PRESS : GDK_BUTTON_RELEASE;
399 400
          changed_button_state = button_state ^ new_button_state;
          if (changed_button_state == 0 || (changed_button_state & GDK_BUTTON1_MASK) != 0)
401
            generate_button_event (window, event_type, x, y, GDK_BUTTON_PRIMARY, modifier_state, event_time);
402
          if ((changed_button_state & GDK_BUTTON2_MASK) != 0)
403
            generate_button_event (window, event_type, x, y, GDK_BUTTON_MIDDLE, modifier_state, event_time);
404 405 406
          if ((changed_button_state & GDK_BUTTON3_MASK) != 0)
            generate_button_event (window, event_type, x, y, GDK_BUTTON_SECONDARY, modifier_state, event_time);
          button_state = new_button_state;
407
          break;
408 409 410 411 412 413
        case mir_pointer_action_motion:
          new_x = mir_pointer_event_axis_value (pointer_event, mir_pointer_axis_x);
          new_y = mir_pointer_event_axis_value (pointer_event, mir_pointer_axis_y);
          hscroll = mir_pointer_event_axis_value (pointer_event, mir_pointer_axis_hscroll);
          vscroll = mir_pointer_event_axis_value (pointer_event, mir_pointer_axis_vscroll);

414
          if (hscroll != 0.0 || vscroll != 0.0)
415 416
            generate_scroll_event (window, x, y, hscroll, vscroll, modifier_state, event_time);
          if (ABS (new_x - x) > 0.5 || ABS (new_y - y) > 0.5)
417 418 419 420 421
            {
              generate_motion_event (window, new_x, new_y, modifier_state, event_time);
              x = new_x;
              y = new_y;
            }
422

423
          break;
424 425 426 427 428 429 430
        case mir_pointer_action_enter:
          if (!cursor_inside)
            {
              cursor_inside = TRUE;
              generate_crossing_event (window, GDK_ENTER_NOTIFY, x, y, event_time);
            }
          break;
431
        case mir_pointer_action_leave:
432 433 434 435 436 437
          if (cursor_inside)
            {
              cursor_inside = FALSE;
              generate_crossing_event (window, GDK_LEAVE_NOTIFY, x, y, event_time);
            }
          break;
438 439
        default:
          break;
440 441 442 443
        }

      _gdk_mir_window_impl_set_cursor_state (impl, x, y, cursor_inside, button_state);
    }
444 445 446
}

static void
447 448
handle_window_event (GdkWindow            *window,
                     const MirWindowEvent *event)
449 450
{
  GdkMirWindowImpl *impl = GDK_MIR_WINDOW_IMPL (window->impl);
451
  MirWindowState state;
452

453
  switch (mir_window_event_get_attribute (event))
454
    {
455 456
    case mir_window_attrib_type:
      _gdk_mir_window_impl_set_window_type (impl, mir_window_event_get_attribute_value (event));
457
      break;
458 459 460
    case mir_window_attrib_state:
      state = mir_window_event_get_attribute_value (event);
      _gdk_mir_window_impl_set_window_state (impl, state);
461 462 463

      switch (state)
        {
464 465
        case mir_window_state_restored:
        case mir_window_state_hidden:
466 467 468 469 470 471
          gdk_synthesize_window_state (window,
                                       GDK_WINDOW_STATE_ICONIFIED |
                                       GDK_WINDOW_STATE_MAXIMIZED |
                                       GDK_WINDOW_STATE_FULLSCREEN,
                                       0);
          break;
472
        case mir_window_state_minimized:
473 474 475 476 477
          gdk_synthesize_window_state (window,
                                       GDK_WINDOW_STATE_MAXIMIZED |
                                       GDK_WINDOW_STATE_FULLSCREEN,
                                       GDK_WINDOW_STATE_ICONIFIED);
          break;
478 479 480
        case mir_window_state_maximized:
        case mir_window_state_vertmaximized:
        case mir_window_state_horizmaximized:
481 482 483 484 485
          gdk_synthesize_window_state (window,
                                       GDK_WINDOW_STATE_ICONIFIED |
                                       GDK_WINDOW_STATE_FULLSCREEN,
                                       GDK_WINDOW_STATE_MAXIMIZED);
          break;
486
        case mir_window_state_fullscreen:
487 488 489 490 491 492 493 494 495
          gdk_synthesize_window_state (window,
                                       GDK_WINDOW_STATE_ICONIFIED |
                                       GDK_WINDOW_STATE_MAXIMIZED,
                                       GDK_WINDOW_STATE_FULLSCREEN);
          break;
        default:
          break;
        }

496
      break;
497
    case mir_window_attrib_swapinterval:
498
      break;
499 500
    case mir_window_attrib_focus:
      generate_focus_event (window, mir_window_event_get_attribute_value (event) != 0);
501 502 503 504 505 506
      break;
    default:
      break;
    }
}

507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532
static void
generate_configure_event (GdkWindow *window,
                          gint       width,
                          gint       height)
{
  GdkEvent *event;

  event = gdk_event_new (GDK_CONFIGURE);
  event->configure.send_event = FALSE;
  event->configure.width = width;
  event->configure.height = height;

  send_event (window, get_pointer (window), event);
}

static void
handle_resize_event (GdkWindow            *window,
                     const MirResizeEvent *event)
{
  window->width = mir_resize_event_get_width (event);
  window->height = mir_resize_event_get_height (event);
  _gdk_window_update_size (window);

  generate_configure_event (window, mir_resize_event_get_width (event), mir_resize_event_get_height (event));
}

533
static void
William Hua's avatar
William Hua committed
534
handle_close_event (GdkWindow *window)
535 536 537 538 539
{
  send_event (window, get_pointer (window), gdk_event_new (GDK_DESTROY));
  gdk_window_destroy_notify (window);
}

540
static void
541 542
handle_window_output_event (GdkWindow                  *window,
                            const MirWindowOutputEvent *event)
543
{
544
  _gdk_mir_window_set_scale (window, mir_window_output_event_get_scale (event));
545 546
}

547
static void
548 549
handle_window_placement_event (GdkWindow                     *window,
                               const MirWindowPlacementEvent *event)
550
{
551
  _gdk_mir_window_set_final_rect (window, mir_window_placement_get_relative_position (event));
552 553
}

554 555 556
typedef struct
{
  GdkWindow *window;
557
  MirEvent *event;
558 559 560 561 562 563 564
} EventData;

static void
gdk_mir_event_source_queue_event (GdkDisplay     *display,
                                  GdkWindow      *window,
                                  const MirEvent *event)
{
565 566
  const MirInputEvent *input_event;

567
  // FIXME: Only generate events if the window wanted them?
568
  switch (mir_event_get_type (event))
569
    {
570 571 572 573 574 575 576 577 578
    case mir_event_type_input:
      input_event = mir_event_get_input_event (event);

      switch (mir_input_event_get_type (input_event))
        {
        case mir_input_event_type_key:
          handle_key_event (window, input_event);
          break;
        case mir_input_event_type_touch:
William Hua's avatar
William Hua committed
579
          handle_touch_event (window, mir_input_event_get_touch_event (input_event));
580 581 582 583
          break;
        case mir_input_event_type_pointer:
          handle_motion_event (window, input_event);
          break;
584 585
        default:
          break;
586 587 588
        }

      break;
589
    case mir_event_type_key:
590
      handle_key_event (window, mir_event_get_input_event (event));
591 592
      break;
    case mir_event_type_motion:
593
      handle_motion_event (window, mir_event_get_input_event (event));
594
      break;
595 596
    case mir_event_type_window:
      handle_window_event (window, mir_event_get_window_event (event));
597 598
      break;
    case mir_event_type_resize:
599
      handle_resize_event (window, mir_event_get_resize_event (event));
600
      break;
601 602 603 604
    case mir_event_type_prompt_session_state_change:
      break;
    case mir_event_type_orientation:
      break;
605
    case mir_event_type_close_window:
William Hua's avatar
William Hua committed
606
      handle_close_event (window);
607
      break;
608 609
    case mir_event_type_keymap:
      break;
610 611
    case mir_event_type_window_output:
      handle_window_output_event (window, mir_event_get_window_output_event (event));
612
      break;
613 614
    case mir_event_type_input_device_state:
      break;
615 616
    case mir_event_type_window_placement:
      handle_window_placement_event (window, mir_event_get_window_placement_event (event));
617
      break;
618
    default:
619
      g_warning ("Ignoring unknown Mir event %d", mir_event_get_type (event));
620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639
      break;
    }
}

static GdkMirQueuedEvent *
gdk_mir_event_source_take_queued_event (GdkMirEventSource *source)
{
  GdkMirQueuedEvent *queued_event;

  g_mutex_lock (&source->mir_event_lock);
  queued_event = g_queue_pop_head (&source->mir_events);
  g_mutex_unlock (&source->mir_event_lock);

  return queued_event;
}

static void
gdk_mir_queued_event_free (GdkMirQueuedEvent *event)
{
  _gdk_mir_window_reference_unref (event->window_ref);
640
  mir_event_unref (event->event);
641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656
  g_slice_free (GdkMirQueuedEvent, event);
}

static void
gdk_mir_event_source_convert_events (GdkMirEventSource *source)
{
  GdkMirQueuedEvent *event;

  while ((event = gdk_mir_event_source_take_queued_event (source)))
    {
      GdkWindow *window = event->window_ref->window;

      /* The window may have been destroyed in the main thread while the
       * event was being dispatched...
       */
      if (window != NULL)
657
        {
658
          if (source->log_events)
659
            _gdk_mir_print_event (event->event);
660

661
          gdk_mir_event_source_queue_event (source->display, window, event->event);
662
        }
663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749
      else
        g_warning ("window was destroyed before event arrived...");

      gdk_mir_queued_event_free (event);
    }
}

static gboolean
gdk_mir_event_source_prepare (GSource *g_source,
                              gint    *timeout)
{
  GdkMirEventSource *source = (GdkMirEventSource *) g_source;
  gboolean mir_events_in_queue;

  if (_gdk_event_queue_find_first (source->display))
   return TRUE;

  g_mutex_lock (&source->mir_event_lock);
  mir_events_in_queue = g_queue_get_length (&source->mir_events) > 0;
  g_mutex_unlock (&source->mir_event_lock);

  return mir_events_in_queue;
}

static gboolean
gdk_mir_event_source_check (GSource *g_source)
{
  return gdk_mir_event_source_prepare (g_source, NULL);
}

static gboolean
gdk_mir_event_source_dispatch (GSource     *g_source,
                               GSourceFunc  callback,
                               gpointer     user_data)
{
  GdkMirEventSource *source = (GdkMirEventSource *) g_source;
  GdkEvent *event;

  /* First, run the queue of events from the thread */
  gdk_mir_event_source_convert_events (source);

  /* Next, dispatch one single event from the display's queue.
   *
   * If there is more than one event then we will soon find ourselves
   * back here again.
   */

  gdk_threads_enter ();

  event = gdk_display_get_event (source->display);

  if (event)
    {
      _gdk_event_emit (event);

      gdk_event_free (event);
    }

  gdk_threads_leave ();

  return TRUE;
}

static void
gdk_mir_event_source_finalize (GSource *g_source)
{
  GdkMirEventSource *source = (GdkMirEventSource *) g_source;
  GdkMirQueuedEvent *event;

  while ((event = gdk_mir_event_source_take_queued_event (source)))
    gdk_mir_queued_event_free (event);

  g_mutex_clear (&source->mir_event_lock);
}

static GSourceFuncs gdk_mir_event_source_funcs = {
  gdk_mir_event_source_prepare,
  gdk_mir_event_source_check,
  gdk_mir_event_source_dispatch,
  gdk_mir_event_source_finalize
};

GdkMirEventSource *
_gdk_mir_event_source_new (GdkDisplay *display)
{
  GdkMirEventSource *source;
  GSource *g_source;
750
  char *name;
751 752

  g_source = g_source_new (&gdk_mir_event_source_funcs, sizeof (GdkMirEventSource));
753 754 755 756 757
  name = g_strdup_printf ("GDK Mir Event source (%s)", gdk_display_get_name (display));
  g_source_set_name (g_source, name);
  g_free (name);
  g_source_set_priority (g_source, GDK_PRIORITY_EVENTS);
  g_source_set_can_recurse (g_source, TRUE);
758 759 760 761 762
  g_source_attach (g_source, NULL);

  source = (GdkMirEventSource *) g_source;
  g_mutex_init (&source->mir_event_lock);
  source->display = display;
763
  source->log_events = (g_getenv ("GDK_MIR_LOG_EVENTS") != NULL);
764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830

  return source;
}

GdkMirWindowReference *
_gdk_mir_event_source_get_window_reference (GdkWindow *window)
{
  static GQuark win_ref_quark;
  GdkMirWindowReference *ref;

  if G_UNLIKELY (!win_ref_quark)
    win_ref_quark = g_quark_from_string ("GdkMirEventSource window reference");

  ref = g_object_get_qdata (G_OBJECT (window), win_ref_quark);

  if (!ref)
    {
      GdkMirEventSource *source;

      source = _gdk_mir_display_get_event_source (gdk_window_get_display (window));
      g_source_ref ((GSource *) source);

      ref = g_slice_new (GdkMirWindowReference);
      ref->window = window;
      ref->source = source;
      ref->ref_count = 0;
      g_object_add_weak_pointer (G_OBJECT (window), (gpointer *) &ref->window);

      g_object_set_qdata_full (G_OBJECT (window), win_ref_quark,
                               ref, (GDestroyNotify) _gdk_mir_window_reference_unref);
    }

  g_atomic_int_inc (&ref->ref_count);

  return ref;
}

void
_gdk_mir_window_reference_unref (GdkMirWindowReference *ref)
{
  if (g_atomic_int_dec_and_test (&ref->ref_count))
    {
      if (ref->window)
        g_object_remove_weak_pointer (G_OBJECT (ref->window), (gpointer *) &ref->window);

      g_source_unref ((GSource *) ref->source);

      g_slice_free (GdkMirWindowReference, ref);
    }
}

void
_gdk_mir_event_source_queue (GdkMirWindowReference *window_ref,
                             const MirEvent        *event)
{
  GdkMirEventSource *source = window_ref->source;
  GdkMirQueuedEvent *queued_event;

  /* We are in the wrong thread right now.  We absolutely cannot touch
   * the window.
   *
   * We can do pretty much anything we want with the source, though...
   */

  queued_event = g_slice_new (GdkMirQueuedEvent);
  g_atomic_int_inc (&window_ref->ref_count);
  queued_event->window_ref = window_ref;
831
  queued_event->event = mir_event_ref (event);
832 833 834 835 836 837 838

  g_mutex_lock (&source->mir_event_lock);
  g_queue_push_tail (&source->mir_events, queued_event);
  g_mutex_unlock (&source->mir_event_lock);

  g_main_context_wakeup (NULL);
}