summaryrefslogtreecommitdiff
path: root/src/backends/native/meta-backend-native.c
blob: d285d61f28acf03217d75da6ef1c37e45d26f3e4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
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
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
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
750
751
752
753
754
755
756
757
758
759
760
761
762
763
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
831
832
833
834
/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */

/*
 * Copyright (C) 2014 Red Hat
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program 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
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 * 02111-1307, USA.
 *
 * Written by:
 *     Jasper St. Pierre <jstpierre@mecheye.net>
 */

/**
 * SECTION:meta-backend-native
 * @title: MetaBackendNative
 * @short_description: A native (KMS/evdev) MetaBackend
 *
 * MetaBackendNative is an implementation of #MetaBackend that uses "native"
 * technologies like DRM/KMS and libinput/evdev to perform the necessary
 * functions.
 */

#include "config.h"

#include "backends/native/meta-backend-native.h"
#include "backends/native/meta-backend-native-private.h"

#include <sched.h>
#include <stdlib.h>

#include "backends/meta-cursor-tracker-private.h"
#include "backends/meta-idle-monitor-private.h"
#include "backends/meta-logical-monitor.h"
#include "backends/meta-monitor-manager-private.h"
#include "backends/meta-pointer-constraint.h"
#include "backends/meta-settings-private.h"
#include "backends/meta-stage-private.h"
#include "backends/native/meta-barrier-native.h"
#include "backends/native/meta-clutter-backend-native.h"
#include "backends/native/meta-cursor-renderer-native.h"
#include "backends/native/meta-input-settings-native.h"
#include "backends/native/meta-kms.h"
#include "backends/native/meta-kms-device.h"
#include "backends/native/meta-launcher.h"
#include "backends/native/meta-monitor-manager-kms.h"
#include "backends/native/meta-renderer-native.h"
#include "backends/native/meta-stage-native.h"
#include "clutter/evdev/clutter-evdev.h"
#include "core/meta-border.h"
#include "meta/main.h"

struct _MetaBackendNative
{
  MetaBackend parent;

  MetaLauncher *launcher;
  MetaUdev *udev;
  MetaKms *kms;
  MetaBarrierManagerNative *barrier_manager;

  guint udev_device_added_handler_id;
};

static GInitableIface *initable_parent_iface;

static void
initable_iface_init (GInitableIface *initable_iface);

G_DEFINE_TYPE_WITH_CODE (MetaBackendNative, meta_backend_native, META_TYPE_BACKEND,
                         G_IMPLEMENT_INTERFACE (G_TYPE_INITABLE,
                                                initable_iface_init))

static void
disconnect_udev_device_added_handler (MetaBackendNative *native);

static void
meta_backend_native_finalize (GObject *object)
{
  MetaBackendNative *native = META_BACKEND_NATIVE (object);

  if (native->udev_device_added_handler_id)
    disconnect_udev_device_added_handler (native);

  g_clear_object (&native->udev);
  g_clear_object (&native->kms);
  meta_launcher_free (native->launcher);

  G_OBJECT_CLASS (meta_backend_native_parent_class)->finalize (object);
}

static void
constrain_to_barriers (ClutterInputDevice *device,
                       guint32             time,
                       float              *new_x,
                       float              *new_y)
{
  MetaBackendNative *native = META_BACKEND_NATIVE (meta_get_backend ());

  meta_barrier_manager_native_process (native->barrier_manager,
                                       device,
                                       time,
                                       new_x, new_y);
}

static void
constrain_to_client_constraint (ClutterInputDevice *device,
                                guint32             time,
                                float               prev_x,
                                float               prev_y,
                                float              *x,
                                float              *y)
{
  MetaBackend *backend = meta_get_backend ();
  MetaPointerConstraint *constraint =
    meta_backend_get_client_pointer_constraint (backend);

  if (!constraint)
    return;

  meta_pointer_constraint_constrain (constraint, device,
                                     time, prev_x, prev_y, x, y);
}

/*
 * The pointer constrain code is mostly a rip-off of the XRandR code from Xorg.
 * (from xserver/randr/rrcrtc.c, RRConstrainCursorHarder)
 *
 * Copyright © 2006 Keith Packard
 * Copyright 2010 Red Hat, Inc
 *
 */

static void
constrain_all_screen_monitors (ClutterInputDevice *device,
                               MetaMonitorManager *monitor_manager,
                               float              *x,
                               float              *y)
{
  ClutterPoint current;
  float cx, cy;
  GList *logical_monitors, *l;

  clutter_input_device_get_coords (device, NULL, &current);

  cx = current.x;
  cy = current.y;

  /* if we're trying to escape, clamp to the CRTC we're coming from */

  logical_monitors =
    meta_monitor_manager_get_logical_monitors (monitor_manager);
  for (l = logical_monitors; l; l = l->next)
    {
      MetaLogicalMonitor *logical_monitor = l->data;
      int left, right, top, bottom;

      left = logical_monitor->rect.x;
      right = left + logical_monitor->rect.width;
      top = logical_monitor->rect.y;
      bottom = top + logical_monitor->rect.height;

      if ((cx >= left) && (cx < right) && (cy >= top) && (cy < bottom))
        {
          if (*x < left)
            *x = left;
          if (*x >= right)
            *x = right - 1;
          if (*y < top)
            *y = top;
          if (*y >= bottom)
            *y = bottom - 1;

          return;
        }
    }
}

static void
pointer_constrain_callback (ClutterInputDevice *device,
                            guint32             time,
                            float               prev_x,
                            float               prev_y,
                            float              *new_x,
                            float              *new_y,
                            gpointer            user_data)
{
  MetaBackend *backend = meta_get_backend ();
  MetaMonitorManager *monitor_manager =
    meta_backend_get_monitor_manager (backend);

  /* Constrain to barriers */
  constrain_to_barriers (device, time, new_x, new_y);

  /* Constrain to pointer lock */
  constrain_to_client_constraint (device, time, prev_x, prev_y, new_x, new_y);

  /* if we're moving inside a monitor, we're fine */
  if (meta_monitor_manager_get_logical_monitor_at (monitor_manager,
                                                   *new_x, *new_y))
    return;

  /* if we're trying to escape, clamp to the CRTC we're coming from */
  constrain_all_screen_monitors (device, monitor_manager, new_x, new_y);
}

static void
relative_motion_across_outputs (MetaMonitorManager *monitor_manager,
                                MetaLogicalMonitor *current,
                                float               cur_x,
                                float               cur_y,
                                float              *dx_inout,
                                float              *dy_inout)
{
  MetaLogicalMonitor *cur = current;
  float x = cur_x, y = cur_y;
  float dx = *dx_inout, dy = *dy_inout;
  MetaDisplayDirection direction = -1;

  while (cur)
    {
      MetaLine2 left, right, top, bottom, motion;
      MetaVector2 intersection;

      motion = (MetaLine2) {
          .a = { x, y },
            .b = { x + (dx * cur->scale), y + (dy * cur->scale) }
      };
      left = (MetaLine2) {
            { cur->rect.x, cur->rect.y },
              { cur->rect.x, cur->rect.y + cur->rect.height }
      };
      right = (MetaLine2) {
            { cur->rect.x + cur->rect.width, cur->rect.y },
              { cur->rect.x + cur->rect.width, cur->rect.y + cur->rect.height }
      };
      top = (MetaLine2) {
            { cur->rect.x, cur->rect.y },
              { cur->rect.x + cur->rect.width, cur->rect.y }
      };
      bottom = (MetaLine2) {
            { cur->rect.x, cur->rect.y + cur->rect.height },
              { cur->rect.x + cur->rect.width, cur->rect.y + cur->rect.height }
      };

      if (direction != META_DISPLAY_RIGHT &&
          meta_line2_intersects_with (&motion, &left, &intersection))
        direction = META_DISPLAY_LEFT;
      else if (direction != META_DISPLAY_LEFT &&
               meta_line2_intersects_with (&motion, &right, &intersection))
        direction = META_DISPLAY_RIGHT;
      else if (direction != META_DISPLAY_DOWN &&
               meta_line2_intersects_with (&motion, &top, &intersection))
        direction = META_DISPLAY_UP;
      else if (direction != META_DISPLAY_UP &&
               meta_line2_intersects_with (&motion, &bottom, &intersection))
        direction = META_DISPLAY_DOWN;
      else
        {
          /* We reached the dest logical monitor */
          x = motion.b.x;
          y = motion.b.y;
          break;
        }

      x = intersection.x;
      y = intersection.y;
      dx -= intersection.x - motion.a.x;
      dy -= intersection.y - motion.a.y;

      cur = meta_monitor_manager_get_logical_monitor_neighbor (monitor_manager,
                                                               cur, direction);
    }

  *dx_inout = x - cur_x;
  *dy_inout = y - cur_y;
}

static void
relative_motion_filter (ClutterInputDevice *device,
                        float               x,
                        float               y,
                        float              *dx,
                        float              *dy,
                        gpointer            user_data)
{
  MetaMonitorManager *monitor_manager = user_data;
  MetaLogicalMonitor *logical_monitor, *dest_logical_monitor;
  float new_dx, new_dy;

  if (meta_is_stage_views_scaled ())
    return;

  logical_monitor = meta_monitor_manager_get_logical_monitor_at (monitor_manager,
                                                                 x, y);
  if (!logical_monitor)
    return;

  new_dx = (*dx) * logical_monitor->scale;
  new_dy = (*dy) * logical_monitor->scale;

  dest_logical_monitor = meta_monitor_manager_get_logical_monitor_at (monitor_manager,
                                                                      x + new_dx,
                                                                      y + new_dy);
  if (dest_logical_monitor &&
      dest_logical_monitor != logical_monitor)
    {
      /* If we are crossing monitors, attempt to bisect the distance on each
       * axis and apply the relative scale for each of them.
       */
      new_dx = *dx;
      new_dy = *dy;
      relative_motion_across_outputs (monitor_manager, logical_monitor,
                                      x, y, &new_dx, &new_dy);
    }

  *dx = new_dx;
  *dy = new_dy;
}

static ClutterBackend *
meta_backend_native_create_clutter_backend (MetaBackend *backend)
{
  return g_object_new (META_TYPE_CLUTTER_BACKEND_NATIVE, NULL);
}

static void
meta_backend_native_post_init (MetaBackend *backend)
{
  ClutterDeviceManager *manager = clutter_device_manager_get_default ();
  MetaSettings *settings = meta_backend_get_settings (backend);

  META_BACKEND_CLASS (meta_backend_native_parent_class)->post_init (backend);

  clutter_evdev_set_pointer_constrain_callback (manager, pointer_constrain_callback,
                                                NULL, NULL);
  clutter_evdev_set_relative_motion_filter (manager, relative_motion_filter,
                                            meta_backend_get_monitor_manager (backend));

  if (meta_settings_is_experimental_feature_enabled (settings,
                                                     META_EXPERIMENTAL_FEATURE_RT_SCHEDULER))
    {
      int retval;
      struct sched_param sp = {
        .sched_priority = sched_get_priority_min (SCHED_RR)
      };

      retval = sched_setscheduler (0, SCHED_RR | SCHED_RESET_ON_FORK, &sp);

      if (retval != 0)
        g_warning ("Failed to set RT scheduler: %m");
    }
}

static MetaMonitorManager *
meta_backend_native_create_monitor_manager (MetaBackend *backend,
                                            GError     **error)
{
  return g_initable_new (META_TYPE_MONITOR_MANAGER_KMS, NULL, error,
                         "backend", backend,
                         NULL);
}

static MetaCursorRenderer *
meta_backend_native_create_cursor_renderer (MetaBackend *backend)
{
  return META_CURSOR_RENDERER (meta_cursor_renderer_native_new (backend));
}

static MetaRenderer *
meta_backend_native_create_renderer (MetaBackend *backend,
                                     GError     **error)
{
  MetaBackendNative *native = META_BACKEND_NATIVE (backend);
  MetaRendererNative *renderer_native;

  renderer_native = meta_renderer_native_new (native, error);
  if (!renderer_native)
    return NULL;

  return META_RENDERER (renderer_native);
}

static MetaInputSettings *
meta_backend_native_create_input_settings (MetaBackend *backend)
{
  return g_object_new (META_TYPE_INPUT_SETTINGS_NATIVE, NULL);
}

static void
meta_backend_native_warp_pointer (MetaBackend *backend,
                                  int          x,
                                  int          y)
{
  ClutterDeviceManager *manager = clutter_device_manager_get_default ();
  ClutterInputDevice *device = clutter_device_manager_get_core_device (manager, CLUTTER_POINTER_DEVICE);
  MetaCursorTracker *cursor_tracker = meta_backend_get_cursor_tracker (backend);

  /* XXX */
  guint32 time_ = 0;

  /* Warp the input device pointer state. */
  clutter_evdev_warp_pointer (device, time_, x, y);

  /* Warp displayed pointer cursor. */
  meta_cursor_tracker_update_position (cursor_tracker, x, y);
}

static MetaLogicalMonitor *
meta_backend_native_get_current_logical_monitor (MetaBackend *backend)
{
  MetaCursorTracker *cursor_tracker = meta_backend_get_cursor_tracker (backend);
  MetaMonitorManager *monitor_manager =
    meta_backend_get_monitor_manager (backend);
  int x, y;

  meta_cursor_tracker_get_pointer (cursor_tracker, &x, &y, NULL);
  return meta_monitor_manager_get_logical_monitor_at (monitor_manager, x, y);
}

static void
meta_backend_native_set_keymap (MetaBackend *backend,
                                const char  *layouts,
                                const char  *variants,
                                const char  *options)
{
  ClutterDeviceManager *manager = clutter_device_manager_get_default ();
  struct xkb_rule_names names;
  struct xkb_keymap *keymap;
  struct xkb_context *context;

  names.rules = DEFAULT_XKB_RULES_FILE;
  names.model = DEFAULT_XKB_MODEL;
  names.layout = layouts;
  names.variant = variants;
  names.options = options;

  context = xkb_context_new (XKB_CONTEXT_NO_FLAGS);
  keymap = xkb_keymap_new_from_names (context, &names, XKB_KEYMAP_COMPILE_NO_FLAGS);
  xkb_context_unref (context);

  clutter_evdev_set_keyboard_map (manager, keymap);

  meta_backend_notify_keymap_changed (backend);

  xkb_keymap_unref (keymap);
}

static struct xkb_keymap *
meta_backend_native_get_keymap (MetaBackend *backend)
{
  ClutterDeviceManager *manager = clutter_device_manager_get_default ();
  return clutter_evdev_get_keyboard_map (manager);
}

static xkb_layout_index_t
meta_backend_native_get_keymap_layout_group (MetaBackend *backend)
{
  ClutterDeviceManager *manager = clutter_device_manager_get_default ();

  return clutter_evdev_get_keyboard_layout_index (manager);
}

static void
meta_backend_native_lock_layout_group (MetaBackend *backend,
                                       guint        idx)
{
  ClutterDeviceManager *manager = clutter_device_manager_get_default ();
  xkb_layout_index_t old_idx;

  old_idx = meta_backend_native_get_keymap_layout_group (backend);
  if (old_idx == idx)
    return;

  clutter_evdev_set_keyboard_layout_index (manager, idx);
  meta_backend_notify_keymap_layout_group_changed (backend, idx);
}

static void
meta_backend_native_set_numlock (MetaBackend *backend,
                                 gboolean     numlock_state)
{
  ClutterDeviceManager *manager = clutter_device_manager_get_default ();
  clutter_evdev_set_keyboard_numlock (manager, numlock_state);
}

static gboolean
meta_backend_native_get_relative_motion_deltas (MetaBackend *backend,
                                                const        ClutterEvent *event,
                                                double       *dx,
                                                double       *dy,
                                                double       *dx_unaccel,
                                                double       *dy_unaccel)
{
  return clutter_evdev_event_get_relative_motion (event,
                                                  dx, dy,
                                                  dx_unaccel, dy_unaccel);
}

static void
meta_backend_native_update_screen_size (MetaBackend *backend,
                                        int width, int height)
{
  ClutterBackend *clutter_backend = meta_backend_get_clutter_backend (backend);
  MetaStageNative *stage_native;
  ClutterActor *stage = meta_backend_get_stage (backend);

  stage_native = meta_clutter_backend_native_get_stage_native (clutter_backend);
  meta_stage_native_rebuild_views (stage_native);

  clutter_actor_set_size (stage, width, height);
}

static MetaGpuKms *
create_gpu_from_udev_device (MetaBackendNative  *native,
                             GUdevDevice        *device,
                             GError            **error)
{
  MetaKmsDeviceFlag flags = META_KMS_DEVICE_FLAG_NONE;
  const char *device_path;
  MetaKmsDevice *kms_device;

  if (meta_is_udev_device_platform_device (device))
    flags |= META_KMS_DEVICE_FLAG_PLATFORM_DEVICE;

  if (meta_is_udev_device_boot_vga (device))
    flags |= META_KMS_DEVICE_FLAG_BOOT_VGA;

  device_path = g_udev_device_get_device_file (device);

  kms_device = meta_kms_create_device (native->kms, device_path, flags,
                                       error);
  if (!kms_device)
    return NULL;

  return meta_gpu_kms_new (native, kms_device, error);
}

static void
on_udev_device_added (MetaUdev          *udev,
                      GUdevDevice       *device,
                      MetaBackendNative *native)
{
  MetaBackend *backend = META_BACKEND (native);
  g_autoptr (GError) error = NULL;
  const char *device_path;
  MetaGpuKms *new_gpu_kms;
  GList *gpus, *l;

  if (!meta_udev_is_drm_device (udev, device))
    return;

  device_path = g_udev_device_get_device_file (device);

  gpus = meta_backend_get_gpus (backend);;
  for (l = gpus; l; l = l->next)
    {
      MetaGpuKms *gpu_kms = l->data;

      if (!g_strcmp0 (device_path, meta_gpu_kms_get_file_path (gpu_kms)))
        {
          g_warning ("Failed to hotplug secondary gpu '%s': %s",
                     device_path, "device already present");
          return;
        }
    }

  new_gpu_kms = create_gpu_from_udev_device (native, device, &error);
  if (!new_gpu_kms)
    {
      g_warning ("Failed to hotplug secondary gpu '%s': %s",
                 device_path, error->message);
      g_error_free (error);
      return;
    }

  meta_backend_add_gpu (backend, META_GPU (new_gpu_kms));
}

static void
connect_udev_device_added_handler (MetaBackendNative *native)
{
  native->udev_device_added_handler_id =
    g_signal_connect (native->udev, "device-added",
                      G_CALLBACK (on_udev_device_added), native);
}

static void
disconnect_udev_device_added_handler (MetaBackendNative *native)
{
  g_signal_handler_disconnect (native->udev,
                               native->udev_device_added_handler_id);
  native->udev_device_added_handler_id = 0;
}

static gboolean
init_gpus (MetaBackendNative  *native,
           GError            **error)
{
  MetaBackend *backend = META_BACKEND (native);
  MetaUdev *udev = meta_backend_native_get_udev (native);
  GList *devices;
  GList *l;

  devices = meta_udev_list_drm_devices (udev, error);
  if (!devices)
    return FALSE;

  for (l = devices; l; l = l->next)
    {
      GUdevDevice *device = l->data;
      MetaGpuKms *gpu_kms;
      GError *local_error = NULL;

      gpu_kms = create_gpu_from_udev_device (native, device, &local_error);

      if (!gpu_kms)
        {
          g_warning ("Failed to open gpu '%s': %s",
                     g_udev_device_get_device_file (device),
                     local_error->message);
          g_clear_error (&local_error);
          continue;
        }

      meta_backend_add_gpu (backend, META_GPU (gpu_kms));
    }

  g_list_free_full (devices, g_object_unref);

  if (g_list_length (meta_backend_get_gpus (backend)) == 0)
    {
      g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND,
                   "No GPUs found");
      return FALSE;
    }

  connect_udev_device_added_handler (native);

  return TRUE;
}

static gboolean
meta_backend_native_initable_init (GInitable     *initable,
                                   GCancellable  *cancellable,
                                   GError       **error)
{
  MetaBackendNative *native = META_BACKEND_NATIVE (initable);

  if (!meta_is_stage_views_enabled ())
    {
      g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
                   "The native backend requires stage views");
      return FALSE;
    }

  native->launcher = meta_launcher_new (error);
  if (!native->launcher)
    return FALSE;

  native->udev = meta_udev_new (native);
  native->barrier_manager = meta_barrier_manager_native_new ();

  native->kms = meta_kms_new (META_BACKEND (native), error);
  if (!native->kms)
    return FALSE;

  if (!init_gpus (native, error))
    return FALSE;

  return initable_parent_iface->init (initable, cancellable, error);
}

static void
initable_iface_init (GInitableIface *initable_iface)
{
  initable_parent_iface = g_type_interface_peek_parent (initable_iface);

  initable_iface->init = meta_backend_native_initable_init;
}

static void
meta_backend_native_class_init (MetaBackendNativeClass *klass)
{
  MetaBackendClass *backend_class = META_BACKEND_CLASS (klass);
  GObjectClass *object_class = G_OBJECT_CLASS (klass);

  object_class->finalize = meta_backend_native_finalize;

  backend_class->create_clutter_backend = meta_backend_native_create_clutter_backend;

  backend_class->post_init = meta_backend_native_post_init;

  backend_class->create_monitor_manager = meta_backend_native_create_monitor_manager;
  backend_class->create_cursor_renderer = meta_backend_native_create_cursor_renderer;
  backend_class->create_renderer = meta_backend_native_create_renderer;
  backend_class->create_input_settings = meta_backend_native_create_input_settings;

  backend_class->warp_pointer = meta_backend_native_warp_pointer;

  backend_class->get_current_logical_monitor = meta_backend_native_get_current_logical_monitor;

  backend_class->set_keymap = meta_backend_native_set_keymap;
  backend_class->get_keymap = meta_backend_native_get_keymap;
  backend_class->get_keymap_layout_group = meta_backend_native_get_keymap_layout_group;
  backend_class->lock_layout_group = meta_backend_native_lock_layout_group;
  backend_class->get_relative_motion_deltas = meta_backend_native_get_relative_motion_deltas;
  backend_class->update_screen_size = meta_backend_native_update_screen_size;
  backend_class->set_numlock = meta_backend_native_set_numlock;
}

static void
meta_backend_native_init (MetaBackendNative *native)
{
}

MetaLauncher *
meta_backend_native_get_launcher (MetaBackendNative *native)
{
  return native->launcher;
}

MetaUdev *
meta_backend_native_get_udev (MetaBackendNative *native)
{
  return native->udev;
}

MetaKms *
meta_backend_native_get_kms (MetaBackendNative *native)
{
  return native->kms;
}

gboolean
meta_activate_vt (int vt, GError **error)
{
  MetaBackend *backend = meta_get_backend ();
  MetaBackendNative *native = META_BACKEND_NATIVE (backend);
  MetaLauncher *launcher = meta_backend_native_get_launcher (native);

  return meta_launcher_activate_vt (launcher, vt, error);
}

MetaBarrierManagerNative *
meta_backend_native_get_barrier_manager (MetaBackendNative *native)
{
  return native->barrier_manager;
}

/**
 * meta_activate_session:
 *
 * Tells mutter to activate the session. When mutter is a
 * display server, this tells logind to switch over to
 * the new session.
 */
gboolean
meta_activate_session (void)
{
  GError *error = NULL;
  MetaBackend *backend = meta_get_backend ();

  /* Do nothing. */
  if (!META_IS_BACKEND_NATIVE (backend))
    return TRUE;

  MetaBackendNative *native = META_BACKEND_NATIVE (backend);

  if (!meta_launcher_activate_session (native->launcher, &error))
    {
      g_warning ("Could not activate session: %s\n", error->message);
      g_error_free (error);
      return FALSE;
    }

  return TRUE;
}

void
meta_backend_native_pause (MetaBackendNative *native)
{
  MetaBackend *backend = META_BACKEND (native);
  ClutterStage *stage = CLUTTER_STAGE (meta_backend_get_stage (backend));
  MetaMonitorManager *monitor_manager =
    meta_backend_get_monitor_manager (backend);
  MetaMonitorManagerKms *monitor_manager_kms =
    META_MONITOR_MANAGER_KMS (monitor_manager);

  clutter_evdev_release_devices ();
  clutter_stage_freeze_updates (stage);

  disconnect_udev_device_added_handler (native);

  meta_monitor_manager_kms_pause (monitor_manager_kms);
}

void meta_backend_native_resume (MetaBackendNative *native)
{
  MetaBackend *backend = META_BACKEND (native);
  ClutterStage *stage = CLUTTER_STAGE (meta_backend_get_stage (backend));
  MetaMonitorManager *monitor_manager =
    meta_backend_get_monitor_manager (backend);
  MetaMonitorManagerKms *monitor_manager_kms =
    META_MONITOR_MANAGER_KMS (monitor_manager);
  MetaInputSettings *input_settings;
  MetaIdleMonitor *idle_monitor;

  meta_monitor_manager_kms_resume (monitor_manager_kms);

  connect_udev_device_added_handler (native);

  clutter_evdev_reclaim_devices ();
  clutter_stage_thaw_updates (stage);

  clutter_actor_queue_redraw (CLUTTER_ACTOR (stage));

  idle_monitor = meta_backend_get_idle_monitor (backend, 0);
  meta_idle_monitor_reset_idletime (idle_monitor);

  input_settings = meta_backend_get_input_settings (backend);
  meta_input_settings_maybe_restore_numlock_state (input_settings);
}