summaryrefslogtreecommitdiff
path: root/src/core/meta-context.c
blob: 7bb0e5f46094887ebc95bcb54086d33087388373 (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
/*
 * Copyright (C) 2019 Red Hat Inc.
 *
 * 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.
 *
 */

#include "config.h"

#include "core/meta-context-private.h"

#include <locale.h>

#include "backends/meta-backend-private.h"
#include "compositor/meta-plugin-manager.h"
#include "core/display-private.h"
#include "core/prefs-private.h"
#include "core/util-private.h"

#ifdef HAVE_WAYLAND
#include "wayland/meta-wayland.h"
#endif

enum
{
  PROP_0,

  PROP_NAME,

  N_PROPS
};

static GParamSpec *obj_props[N_PROPS];

typedef enum _MetaContextState
{
  META_CONTEXT_STATE_INIT,
  META_CONTEXT_STATE_CONFIGURED,
  META_CONTEXT_STATE_SETUP,
  META_CONTEXT_STATE_STARTED,
  META_CONTEXT_STATE_RUNNING,
  META_CONTEXT_STATE_TERMINATED,
} MetaContextState;

typedef struct _MetaContextPrivate
{
  char *name;
  char *plugin_name;
  GType plugin_gtype;
  char *gnome_wm_keybindings;

  MetaContextState state;

  GOptionContext *option_context;

  MetaBackend *backend;
  MetaDisplay *display;
#ifdef HAVE_WAYLAND
  MetaWaylandCompositor *wayland_compositor;
#endif

  GMainLoop *main_loop;
  GError *termination_error;
} MetaContextPrivate;

G_DEFINE_TYPE_WITH_PRIVATE (MetaContext, meta_context, G_TYPE_OBJECT)

void
meta_context_add_option_entries (MetaContext        *context,
                                 const GOptionEntry *entries,
                                 const char         *translation_domain)
{
  MetaContextPrivate *priv = meta_context_get_instance_private (context);

  g_warn_if_fail (priv->state == META_CONTEXT_STATE_INIT);

  g_option_context_add_main_entries (priv->option_context,
                                     entries,
                                     translation_domain);
}

void
meta_context_add_option_group (MetaContext  *context,
                               GOptionGroup *group)
{
  MetaContextPrivate *priv = meta_context_get_instance_private (context);

  g_warn_if_fail (priv->state == META_CONTEXT_STATE_INIT);
  g_return_if_fail (priv->option_context);

  g_option_context_add_group (priv->option_context, group);
}

void
meta_context_set_plugin_gtype (MetaContext *context,
                               GType        plugin_gtype)
{
  MetaContextPrivate *priv = meta_context_get_instance_private (context);

  g_return_if_fail (priv->state <= META_CONTEXT_STATE_CONFIGURED);
  g_return_if_fail (!priv->plugin_name);

  priv->plugin_gtype = plugin_gtype;
}

void
meta_context_set_plugin_name (MetaContext *context,
                              const char  *plugin_name)
{
  MetaContextPrivate *priv = meta_context_get_instance_private (context);

  g_return_if_fail (priv->state <= META_CONTEXT_STATE_CONFIGURED);
  g_return_if_fail (priv->plugin_gtype == G_TYPE_NONE);

  priv->plugin_name = g_strdup (plugin_name);
}

void
meta_context_set_gnome_wm_keybindings (MetaContext *context,
                                       const char  *wm_keybindings)
{
  MetaContextPrivate *priv = meta_context_get_instance_private (context);

  g_return_if_fail (priv->state <= META_CONTEXT_STATE_CONFIGURED);

  g_clear_pointer (&priv->gnome_wm_keybindings, g_free);
  priv->gnome_wm_keybindings = g_strdup (wm_keybindings);
}

const char *
meta_context_get_gnome_wm_keybindings (MetaContext *context)
{
  MetaContextPrivate *priv = meta_context_get_instance_private (context);

  return priv->gnome_wm_keybindings;
}

void
meta_context_notify_ready (MetaContext *context)
{
  MetaContextPrivate *priv = meta_context_get_instance_private (context);

  g_return_if_fail (priv->state == META_CONTEXT_STATE_STARTED ||
                    priv->state == META_CONTEXT_STATE_RUNNING);

  META_CONTEXT_GET_CLASS (context)->notify_ready (context);
}

const char *
meta_context_get_name (MetaContext *context)
{
  MetaContextPrivate *priv = meta_context_get_instance_private (context);

  return priv->name;
}

/**
 * meta_context_get_backend:
 * @context: The #MetaContext
 *
 * Returns: (transfer none): the #MetaBackend
 */
MetaBackend *
meta_context_get_backend (MetaContext *context)
{
  MetaContextPrivate *priv = meta_context_get_instance_private (context);

  return priv->backend;
}

/**
 * meta_context_get_display:
 * @context: The #MetaContext
 *
 * Returns: (transfer none): the #MetaDisplay
 */
MetaDisplay *
meta_context_get_display (MetaContext *context)
{
  MetaContextPrivate *priv = meta_context_get_instance_private (context);

  return priv->display;
}

#ifdef HAVE_WAYLAND
MetaWaylandCompositor *
meta_context_get_wayland_compositor (MetaContext *context)
{
  MetaContextPrivate *priv = meta_context_get_instance_private (context);

  return priv->wayland_compositor;
}
#endif

MetaCompositorType
meta_context_get_compositor_type (MetaContext *context)
{
  return META_CONTEXT_GET_CLASS (context)->get_compositor_type (context);
}

gboolean
meta_context_is_replacing (MetaContext *context)
{
  return META_CONTEXT_GET_CLASS (context)->is_replacing (context);
}

MetaX11DisplayPolicy
meta_context_get_x11_display_policy (MetaContext *context)
{
  return META_CONTEXT_GET_CLASS (context)->get_x11_display_policy (context);
}

static gboolean
meta_context_real_configure (MetaContext   *context,
                             int           *argc,
                             char        ***argv,
                             GError       **error)
{
  MetaContextPrivate *priv = meta_context_get_instance_private (context);
  g_autoptr (GOptionContext) option_context = NULL;

  if (!priv->option_context)
    {
      g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
                   "Tried to configure multiple times");
      return FALSE;
    }

  option_context = g_steal_pointer (&priv->option_context);
  return g_option_context_parse (option_context, argc, argv, error);
}

gboolean
meta_context_configure (MetaContext   *context,
                        int           *argc,
                        char        ***argv,
                        GError       **error)
{
  MetaContextPrivate *priv = meta_context_get_instance_private (context);
  MetaCompositorType compositor_type;

  g_warn_if_fail (priv->state == META_CONTEXT_STATE_INIT);

  if (!META_CONTEXT_GET_CLASS (context)->configure (context, argc, argv, error))
    {
      priv->state = META_CONTEXT_STATE_TERMINATED;
      return FALSE;
    }

  compositor_type = meta_context_get_compositor_type (context);
  switch (compositor_type)
    {
    case META_COMPOSITOR_TYPE_WAYLAND:
      meta_set_is_wayland_compositor (TRUE);
      break;
    case META_COMPOSITOR_TYPE_X11:
      meta_set_is_wayland_compositor (FALSE);
      break;
    }

  priv->state = META_CONTEXT_STATE_CONFIGURED;

  return TRUE;
}

static const char *
compositor_type_to_description (MetaCompositorType compositor_type)
{
  switch (compositor_type)
    {
    case META_COMPOSITOR_TYPE_WAYLAND:
      return "Wayland display server";
    case META_COMPOSITOR_TYPE_X11:
      return "X11 window and compositing manager";
    }

  g_assert_not_reached ();
}

static void
init_introspection (MetaContext *context)
{
#ifdef HAVE_INTROSPECTION
  g_irepository_prepend_search_path (MUTTER_PKGLIBDIR);
#endif
}

static gboolean
meta_context_real_setup (MetaContext  *context,
                         GError      **error)
{
  MetaContextPrivate *priv = meta_context_get_instance_private (context);
  MetaBackend *backend;

  backend = META_CONTEXT_GET_CLASS (context)->create_backend (context, error);
  if (!backend)
    return FALSE;

  priv->backend = backend;
  return TRUE;
}

gboolean
meta_context_setup (MetaContext  *context,
                    GError      **error)
{
  MetaContextPrivate *priv = meta_context_get_instance_private (context);
  MetaCompositorType compositor_type;

  g_warn_if_fail (priv->state == META_CONTEXT_STATE_CONFIGURED);

  if (!priv->plugin_name && priv->plugin_gtype == G_TYPE_NONE)
    {
      priv->state = META_CONTEXT_STATE_TERMINATED;
      g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
                   "No compositor plugin set");
      return FALSE;
    }

  meta_init_debug_utils ();

  compositor_type = meta_context_get_compositor_type (context);
  g_message ("Running %s (using mutter %s) as a %s",
             priv->name, VERSION,
             compositor_type_to_description (compositor_type));

  if (priv->plugin_name)
    meta_plugin_manager_load (priv->plugin_name);
  else
    meta_plugin_manager_set_plugin_type (priv->plugin_gtype);

  init_introspection (context);

  if (!META_CONTEXT_GET_CLASS (context)->setup (context, error))
    {
      priv->state = META_CONTEXT_STATE_TERMINATED;
      return FALSE;
    }

  priv->state = META_CONTEXT_STATE_SETUP;
  return TRUE;
}

gboolean
meta_context_start (MetaContext  *context,
                    GError      **error)
{
  MetaContextPrivate *priv = meta_context_get_instance_private (context);

  g_warn_if_fail (priv->state == META_CONTEXT_STATE_SETUP);

  meta_prefs_init ();

#ifdef HAVE_WAYLAND
  if (meta_context_get_compositor_type (context) ==
      META_COMPOSITOR_TYPE_WAYLAND)
    priv->wayland_compositor = meta_wayland_compositor_new (context);
#endif

  priv->display = meta_display_new (context, error);
  if (!priv->display)
    {
      priv->state = META_CONTEXT_STATE_TERMINATED;
      return FALSE;
    }

  priv->main_loop = g_main_loop_new (NULL, FALSE);

  priv->state = META_CONTEXT_STATE_STARTED;

  return TRUE;
}

gboolean
meta_context_run_main_loop (MetaContext  *context,
                            GError      **error)
{
  MetaContextPrivate *priv = meta_context_get_instance_private (context);

  g_warn_if_fail (priv->state == META_CONTEXT_STATE_STARTED);
  if (!priv->main_loop)
    {
      priv->state = META_CONTEXT_STATE_TERMINATED;
      g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
                   "Tried to run main loop without having started");
      return FALSE;
    }

  priv->state = META_CONTEXT_STATE_RUNNING;
  g_main_loop_run (priv->main_loop);
  priv->state = META_CONTEXT_STATE_TERMINATED;
  g_clear_pointer (&priv->main_loop, g_main_loop_unref);

  if (priv->termination_error)
    {
      g_propagate_error (error, g_steal_pointer (&priv->termination_error));
      return FALSE;
    }

  return TRUE;
}

void
meta_context_terminate (MetaContext *context)
{
  MetaContextPrivate *priv = meta_context_get_instance_private (context);

  g_warn_if_fail (priv->state == META_CONTEXT_STATE_RUNNING);
  g_warn_if_fail (g_main_loop_is_running (priv->main_loop));

  g_main_loop_quit (priv->main_loop);
}

void
meta_context_terminate_with_error (MetaContext *context,
                                   GError      *error)
{
  MetaContextPrivate *priv = meta_context_get_instance_private (context);

  priv->termination_error = g_steal_pointer (&error);
  meta_context_terminate (context);
}

void
meta_context_destroy (MetaContext *context)
{
  g_object_run_dispose (G_OBJECT (context));
  g_object_unref (context);
}

static void
meta_context_get_property (GObject    *object,
                           guint       prop_id,
                           GValue     *value,
                           GParamSpec *pspec)
{
  MetaContext *context = META_CONTEXT (object);
  MetaContextPrivate *priv = meta_context_get_instance_private (context);

  switch (prop_id)
    {
    case PROP_NAME:
      g_value_set_string (value, priv->name);
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}

static void
meta_context_set_property (GObject      *object,
                           guint         prop_id,
                           const GValue *value,
                           GParamSpec   *pspec)
{
  MetaContext *context = META_CONTEXT (object);
  MetaContextPrivate *priv = meta_context_get_instance_private (context);

  switch (prop_id)
    {
    case PROP_NAME:
      priv->name = g_value_dup_string (value);
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}

static void
meta_context_dispose (GObject *object)
{
  MetaContext *context = META_CONTEXT (object);
  MetaContextPrivate *priv = meta_context_get_instance_private (context);

  if (priv->backend)
    meta_backend_prepare_shutdown (priv->backend);

#ifdef HAVE_WAYLAND
  if (priv->wayland_compositor)
    meta_wayland_compositor_prepare_shutdown (priv->wayland_compositor);
#endif

  if (priv->display)
    meta_display_close (priv->display, META_CURRENT_TIME);
  g_clear_object (&priv->display);

#ifdef HAVE_WAYLAND
  g_clear_object (&priv->wayland_compositor);
#endif

  g_clear_pointer (&priv->backend, meta_backend_destroy);

  g_clear_pointer (&priv->option_context, g_option_context_free);
  g_clear_pointer (&priv->main_loop, g_main_loop_unref);

  G_OBJECT_CLASS (meta_context_parent_class)->dispose (object);
}

static void
meta_context_finalize (GObject *object)
{
  MetaContext *context = META_CONTEXT (object);
  MetaContextPrivate *priv = meta_context_get_instance_private (context);

  g_clear_pointer (&priv->gnome_wm_keybindings, g_free);
  g_clear_pointer (&priv->plugin_name, g_free);
  g_clear_pointer (&priv->name, g_free);

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

static void
meta_context_class_init (MetaContextClass *klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);

  object_class->get_property = meta_context_get_property;
  object_class->set_property = meta_context_set_property;
  object_class->dispose = meta_context_dispose;
  object_class->finalize = meta_context_finalize;

  klass->configure = meta_context_real_configure;
  klass->setup = meta_context_real_setup;

  obj_props[PROP_NAME] =
    g_param_spec_string ("name",
                         "name",
                         "Human readable name",
                         NULL,
                         G_PARAM_READWRITE |
                         G_PARAM_CONSTRUCT_ONLY |
                         G_PARAM_STATIC_STRINGS);
  g_object_class_install_properties (object_class, N_PROPS, obj_props);
}

static void
meta_context_init (MetaContext *context)
{
  MetaContextPrivate *priv = meta_context_get_instance_private (context);

  priv->plugin_gtype = G_TYPE_NONE;
  priv->gnome_wm_keybindings = g_strdup ("Mutter");

  if (!setlocale (LC_ALL, ""))
    g_warning ("Locale not understood by C library");
  bindtextdomain (GETTEXT_PACKAGE, MUTTER_LOCALEDIR);
  bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");

  priv->option_context = g_option_context_new (NULL);
  g_option_context_set_main_group (priv->option_context,
                                   g_option_group_new (NULL, NULL, NULL,
                                                       context, NULL));
}