shell.c 31.7 KB
Newer Older
Guido Gunther's avatar
Guido Gunther committed
1
/*
Guido Gunther's avatar
Guido Gunther committed
2
 * Copyright (C) 2018 Purism SPC
Zander Brown's avatar
Zander Brown committed
3
4
5
 *
 * SPDX-License-Identifier: GPL-3.0-or-later
 *
Guido Gunther's avatar
Guido Gunther committed
6
 * Author: Guido Günther <agx@sigxcpu.org>
Guido Gunther's avatar
Guido Gunther committed
7
 *
8
 * Once based on maynard's panel which is
Guido Gunther's avatar
Guido Gunther committed
9
10
11
12
 * Copyright (C) 2014 Collabora Ltd. *
 * Author: Jonny Lamb <jonny.lamb@collabora.co.uk>
 */

13
#define G_LOG_DOMAIN "phosh-shell"
Guido Gunther's avatar
Guido Gunther committed
14

anteater's avatar
anteater committed
15
16
#define WWAN_BACKEND_KEY "wwan-backend"

Guido Gunther's avatar
Guido Gunther committed
17
18
19
#include <stdlib.h>
#include <string.h>

Guido Gunther's avatar
Guido Gunther committed
20
#include <glib-object.h>
Guido Gunther's avatar
Guido Gunther committed
21
#include <glib-unix.h>
Guido Gunther's avatar
Guido Gunther committed
22
#include <glib/gi18n.h>
Guido Gunther's avatar
Guido Gunther committed
23
24
25
#include <gtk/gtk.h>
#include <gdk/gdkwayland.h>

Guido Gunther's avatar
Guido Gunther committed
26
#include "config.h"
Guido Gunther's avatar
Guido Gunther committed
27
#include "shell.h"
28

29
#include "batteryinfo.h"
30
#include "background-manager.h"
anteater's avatar
anteater committed
31
#include "bt-manager.h"
32
#include "fader.h"
Guido Gunther's avatar
Guido Gunther committed
33
#include "feedback-manager.h"
34
#include "home.h"
35
#include "idle-manager.h"
36
#include "keyboard-events.h"
37
#include "lockscreen-manager.h"
38
#include "monitor-manager.h"
39
#include "monitor/monitor.h"
40
#include "notifications/notify-manager.h"
41
#include "notifications/notification-banner.h"
42
#include "osk-manager.h"
43
#include "panel.h"
44
#include "phosh-wayland.h"
45
#include "polkit-auth-agent.h"
Guido Gunther's avatar
Guido Gunther committed
46
47
#include "proximity.h"
#include "sensor-proxy-manager.h"
48
#include "screen-saver-manager.h"
49
#include "session.h"
50
#include "system-prompter.h"
Guido Gunther's avatar
Guido Gunther committed
51
#include "torch-manager.h"
52
#include "util.h"
Guido Gunther's avatar
Guido Gunther committed
53
#include "wifiinfo.h"
54
#include "wwaninfo.h"
anteater's avatar
anteater committed
55
56
57
#include "wwan/phosh-wwan-ofono.h"
#include "wwan/phosh-wwan-mm.h"
#include "wwan/phosh-wwan-backend.h"
Guido Gunther's avatar
Guido Gunther committed
58

Guido Gunther's avatar
Guido Gunther committed
59
60
61
62
63
/**
 * SECTION:shell
 * @short_description: The shell singleton
 * @Title: PhoshShell
 *
Guido Gunther's avatar
Guido Gunther committed
64
 * #PhoshShell is responsible for instantiating the GUI
Guido Gunther's avatar
Guido Gunther committed
65
66
67
68
 * parts of the shell#PhoshPanel, #PhoshHome,… and the managers that
 * interface with DBus #PhoshMonitorManager, #PhoshFeedbackManager, …
 * and coordinates between them.
 */
Guido Gunther's avatar
Guido Gunther committed
69

70
71
enum {
  PHOSH_SHELL_PROP_0,
72
  PHOSH_SHELL_PROP_TRANSFORM,
73
  PHOSH_SHELL_PROP_LOCKED,
74
  PHOSH_SHELL_PROP_PRIMARY_MONITOR,
75
76
77
78
  PHOSH_SHELL_PROP_LAST_PROP
};
static GParamSpec *props[PHOSH_SHELL_PROP_LAST_PROP];

Guido Gunther's avatar
Guido Gunther committed
79
80
typedef struct
{
81
  PhoshLayerSurface *panel;
Guido Gunther's avatar
Guido Gunther committed
82
  PhoshLayerSurface *home;
83
  GPtrArray *faders;              /* for final fade out */
84

85
86
  GtkWidget *notification_banner;

87
  PhoshBackgroundManager *background_manager;
88
  PhoshMonitor *primary_monitor;
89
  PhoshMonitor *builtin_monitor;
90
  PhoshMonitorManager *monitor_manager;
91
  PhoshLockscreenManager *lockscreen_manager;
92
  PhoshIdleManager *idle_manager;
93
  PhoshOskManager  *osk_manager;
94
  PhoshToplevelManager *toplevel_manager;
Guido Gunther's avatar
Guido Gunther committed
95
  PhoshWifiManager *wifi_manager;
96
  PhoshPolkitAuthAgent *polkit_auth_agent;
97
  PhoshScreenSaverManager *screen_saver_manager;
Guido Gunther's avatar
Guido Gunther committed
98
  PhoshNotifyManager *notify_manager;
Guido Gunther's avatar
Guido Gunther committed
99
  PhoshFeedbackManager *feedback_manager;
100
  PhoshBtManager *bt_manager;
anteater's avatar
anteater committed
101
  PhoshWWan *wwan;
Guido Gunther's avatar
Guido Gunther committed
102
  PhoshTorchManager *torch_manager;
103
  PhoshKeyboardEvents *keyboard_events;
Guido Gunther's avatar
Guido Gunther committed
104
105
106
107

  /* sensors */
  PhoshSensorProxyManager *sensor_proxy_manager;
  PhoshProximity *proximity;
108
109

  gboolean startup_finished;
110
  PhoshMonitorTransform transform; /* current rotation of primary monitor */
Guido Gunther's avatar
Guido Gunther committed
111
112
113
114
115
116
117
118
119
120
} PhoshShellPrivate;


typedef struct _PhoshShell
{
  GObject parent;
} PhoshShell;

G_DEFINE_TYPE_WITH_PRIVATE (PhoshShell, phosh_shell, G_TYPE_OBJECT)

Guido Gunther's avatar
Guido Gunther committed
121

Guido Gunther's avatar
Guido Gunther committed
122
static void
123
124
settings_activated_cb (PhoshShell *self,
                       PhoshPanel *window)
Guido Gunther's avatar
Guido Gunther committed
125
{
Guido Gunther's avatar
Guido Gunther committed
126
  PhoshShellPrivate *priv = phosh_shell_get_instance_private (self);
127

128
129
  g_return_if_fail (PHOSH_IS_PANEL (priv->panel));
  phosh_panel_toggle_fold (PHOSH_PANEL(priv->panel));
Guido Gunther's avatar
Guido Gunther committed
130
131
132
}


133
134
135
136
137
138
139
140
141
142
143
144
145
void
phosh_shell_lock (PhoshShell *self)
{
  phosh_shell_set_locked (self, TRUE);
}


void
phosh_shell_unlock (PhoshShell *self)
{
  phosh_shell_set_locked (self, FALSE);
}

146
147
148
149
150
151
152
153
154
155
156
157
158
159
/**
 * phosh_shell_get_locked:
 * @self: The #PhoshShell singleton
 *
 * Returns: %TRUE if the shell is currently locked, otherwise %FALSE.
 */
gboolean
phosh_shell_get_locked (PhoshShell *self)
{
  PhoshShellPrivate *priv = phosh_shell_get_instance_private (self);

  return phosh_lockscreen_manager_get_locked (priv->lockscreen_manager);
}

160
161
/**
 * phosh_shell_set_locked:
162
 * @self: The #PhoshShell singleton
163
 * @locked: %TRUE to lock the shell
164
165
166
167
168
 *
 * Lock the shell. We proxy to lockscreen-manager to avoid
 * that other parts of the shell need to care about this
 * abstraction.
 */
169
void
170
phosh_shell_set_locked (PhoshShell *self, gboolean locked)
171
172
{
  PhoshShellPrivate *priv = phosh_shell_get_instance_private (self);
173
174
175
176
  gboolean current;

  current = phosh_lockscreen_manager_get_locked (priv->lockscreen_manager);

177
  if (current == locked)
178
    return;
179

180
  phosh_lockscreen_manager_set_locked (priv->lockscreen_manager, locked);
181
  g_object_notify_by_pspec (G_OBJECT (self), props[PHOSH_SHELL_PROP_LOCKED]);
Guido Gunther's avatar
Guido Gunther committed
182
183
}

184
185
186
187
188
189
190
191
192
193
194
195
static void
on_home_state_changed (PhoshShell *self, GParamSpec *pspec, PhoshHome *home)
{
  PhoshShellPrivate *priv;
  PhoshHomeState state;

  g_return_if_fail (PHOSH_IS_SHELL (self));
  g_return_if_fail (PHOSH_IS_HOME (home));

  priv = phosh_shell_get_instance_private (self);

  g_object_get (priv->home, "state", &state, NULL);
196
197
  if (state == PHOSH_HOME_STATE_UNFOLDED) {
    phosh_panel_fold (PHOSH_PANEL (priv->panel));
198
    phosh_osk_manager_set_visible (priv->osk_manager, FALSE);
199
  }
200
201
202
}


Guido Gunther's avatar
Guido Gunther committed
203
static void
Guido Gunther's avatar
Guido Gunther committed
204
panels_create (PhoshShell *self)
Guido Gunther's avatar
Guido Gunther committed
205
{
Guido Gunther's avatar
Guido Gunther committed
206
  PhoshShellPrivate *priv = phosh_shell_get_instance_private (self);
207
  PhoshMonitor *monitor;
208
  PhoshWayland *wl = phosh_wayland_get_default ();
Guido Gunther's avatar
Guido Gunther committed
209

210
  monitor = phosh_shell_get_primary_monitor (self);
211
  g_return_if_fail (monitor);
Guido Gunther's avatar
Guido Gunther committed
212

213
214
  priv->panel = PHOSH_LAYER_SURFACE(phosh_panel_new (phosh_wayland_get_zwlr_layer_shell_v1(wl),
                                                     monitor->wl_output));
215
216
  gtk_widget_show (GTK_WIDGET (priv->panel));

Guido Gunther's avatar
Guido Gunther committed
217
218
  priv->home = PHOSH_LAYER_SURFACE(phosh_home_new (phosh_wayland_get_zwlr_layer_shell_v1(wl),
                                                    monitor->wl_output));
219
220
  gtk_widget_show (GTK_WIDGET (priv->home));

Guido Gunther's avatar
Guido Gunther committed
221
  g_signal_connect_swapped (
222
    priv->panel,
Guido Gunther's avatar
Guido Gunther committed
223
224
    "settings-activated",
    G_CALLBACK(settings_activated_cb),
Guido Gunther's avatar
Guido Gunther committed
225
    self);
226
227
228
229
230
231

  g_signal_connect_swapped (
    priv->home,
    "notify::state",
    G_CALLBACK(on_home_state_changed),
    self);
Guido Gunther's avatar
Guido Gunther committed
232
}
Guido Gunther's avatar
Guido Gunther committed
233

234

235
236
237
238
239
240
241
242
243
244
static void
panels_dispose (PhoshShell *self)
{
  PhoshShellPrivate *priv = phosh_shell_get_instance_private (self);

  g_clear_pointer (&priv->panel, phosh_cp_widget_destroy);
  g_clear_pointer (&priv->home, phosh_cp_widget_destroy);
}


Guido Gunther's avatar
Guido Gunther committed
245
static void
Guido Gunther's avatar
Guido Gunther committed
246
css_setup (PhoshShell *self)
Guido Gunther's avatar
Guido Gunther committed
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
{
  GtkCssProvider *provider;
  GFile *file;
  GError *error = NULL;

  provider = gtk_css_provider_new ();
  file = g_file_new_for_uri ("resource:///sm/puri/phosh/style.css");

  if (!gtk_css_provider_load_from_file (provider, file, &error)) {
    g_warning ("Failed to load CSS file: %s", error->message);
    g_clear_error (&error);
    g_object_unref (file);
    return;
  }
  gtk_style_context_add_provider_for_screen (gdk_screen_get_default (),
Guido Gunther's avatar
Guido Gunther committed
262
                                             GTK_STYLE_PROVIDER (provider), 600);
Guido Gunther's avatar
Guido Gunther committed
263
264
265
266
  g_object_unref (file);
}


267
268
269
270
271
272
273
274
275
static void
phosh_shell_set_property (GObject *object,
                          guint property_id,
                          const GValue *value,
                          GParamSpec *pspec)
{
  PhoshShell *self = PHOSH_SHELL (object);

  switch (property_id) {
276
277
  case PHOSH_SHELL_PROP_LOCKED:
    phosh_shell_set_locked (self, g_value_get_boolean (value));
278
    break;
279
280
281
  case PHOSH_SHELL_PROP_PRIMARY_MONITOR:
    phosh_shell_set_primary_monitor (self, g_value_get_object (value));
    break;
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
  default:
    G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
    break;
  }
}


static void
phosh_shell_get_property (GObject *object,
                          guint property_id,
                          GValue *value,
                          GParamSpec *pspec)
{
  PhoshShell *self = PHOSH_SHELL (object);
  PhoshShellPrivate *priv = phosh_shell_get_instance_private(self);

  switch (property_id) {
299
300
  case PHOSH_SHELL_PROP_TRANSFORM:
    g_value_set_enum (value, phosh_monitor_get_transform(priv->primary_monitor));
Guido Gunther's avatar
Guido Gunther committed
301
    break;
302
  case PHOSH_SHELL_PROP_LOCKED:
303
304
    g_value_set_boolean (value,
                         phosh_lockscreen_manager_get_locked (priv->lockscreen_manager));
Guido Gunther's avatar
Guido Gunther committed
305
    break;
306
307
308
  case PHOSH_SHELL_PROP_PRIMARY_MONITOR:
    g_value_set_object (value, phosh_shell_get_primary_monitor (self));
    break;
309
310
311
312
313
314
315
  default:
    G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
    break;
  }
}


Guido Gunther's avatar
Guido Gunther committed
316
static void
317
phosh_shell_dispose (GObject *object)
Guido Gunther's avatar
Guido Gunther committed
318
319
320
321
{
  PhoshShell *self = PHOSH_SHELL (object);
  PhoshShellPrivate *priv = phosh_shell_get_instance_private(self);

Guido Gunther's avatar
Guido Gunther committed
322
323
324
325
326
327
328
  if (priv->sensor_proxy_manager) {
    phosh_dbus_sensor_proxy_call_release_accelerometer_sync (
      PHOSH_DBUS_SENSOR_PROXY(priv->sensor_proxy_manager),
      NULL, NULL);
      g_clear_object (&priv->sensor_proxy_manager);
  }

329
  panels_dispose (self);
330
  g_clear_pointer (&priv->faders, g_ptr_array_unref);
331

332
  g_clear_object (&priv->notification_banner);
333

334
  g_clear_object (&priv->keyboard_events);
335
  /* dispose managers in opposite order of declaration */
Guido Gunther's avatar
Guido Gunther committed
336
  g_clear_object (&priv->torch_manager);
anteater's avatar
anteater committed
337
  g_clear_object (&priv->wwan);
338
339
  g_clear_object (&priv->bt_manager);
  g_clear_object (&priv->feedback_manager);
Guido Gunther's avatar
Guido Gunther committed
340
  g_clear_object (&priv->notify_manager);
341
  g_clear_object (&priv->screen_saver_manager);
342
  g_clear_object (&priv->polkit_auth_agent);
Guido Gunther's avatar
Guido Gunther committed
343
  g_clear_object (&priv->wifi_manager);
344
  g_clear_object (&priv->toplevel_manager);
345
  g_clear_object (&priv->osk_manager);
346
347
348
349
350
  g_clear_object (&priv->idle_manager);
  g_clear_object (&priv->lockscreen_manager);
  g_clear_object (&priv->monitor_manager);
  g_clear_object (&priv->builtin_monitor);
  g_clear_object (&priv->primary_monitor);
351
  g_clear_object (&priv->background_manager);
352
353

  /* sensors */
Guido Gunther's avatar
Guido Gunther committed
354
355
  g_clear_object (&priv->proximity);
  g_clear_object (&priv->sensor_proxy_manager);
356
  phosh_system_prompter_unregister ();
357
  phosh_session_unregister ();
358

359
360
361
362
  G_OBJECT_CLASS (phosh_shell_parent_class)->dispose (object);
}


363
364
365
366
367
368
369
370
371
372
static void
on_num_toplevels_changed (PhoshShell *self, GParamSpec *pspec, PhoshToplevelManager *toplevel_manager)
{
  PhoshShellPrivate *priv;

  g_return_if_fail (PHOSH_IS_SHELL (self));
  g_return_if_fail (PHOSH_IS_TOPLEVEL_MANAGER (toplevel_manager));

  priv = phosh_shell_get_instance_private (self);
  /* all toplevels gone, show the overview */
373
  /* TODO: once we have unfoldable app-drawer unfold that too */
374
375
376
377
378
  if (!phosh_toplevel_manager_get_num_toplevels (toplevel_manager))
    phosh_home_set_state (PHOSH_HOME (priv->home), PHOSH_HOME_STATE_UNFOLDED);
}


379
380
381
382
383
384
385
386
387
388
389
390
391
static void
on_toplevel_added (PhoshShell *self, GParamSpec *pspec, PhoshToplevelManager *toplevel_manager)
{
  PhoshShellPrivate *priv;

  g_return_if_fail (PHOSH_IS_SHELL (self));
  g_return_if_fail (PHOSH_IS_TOPLEVEL_MANAGER (toplevel_manager));

  priv = phosh_shell_get_instance_private (self);
  phosh_home_set_state (PHOSH_HOME (priv->home), PHOSH_HOME_STATE_FOLDED);
}


392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
static void
on_new_notification (PhoshShell         *self,
                     PhoshNotification  *notification,
                     PhoshNotifyManager *manager)
{
  PhoshShellPrivate *priv;

  g_return_if_fail (PHOSH_IS_SHELL (self));
  g_return_if_fail (PHOSH_IS_NOTIFICATION (notification));
  g_return_if_fail (PHOSH_IS_NOTIFY_MANAGER (manager));

  priv = phosh_shell_get_instance_private (self);

  /* Clear existing banner */
  if (priv->notification_banner && GTK_IS_WIDGET (priv->notification_banner)) {
    gtk_widget_destroy (priv->notification_banner);
  }

  if (phosh_notify_manager_get_show_banners (manager) &&
411
412
      !phosh_lockscreen_manager_get_locked (priv->lockscreen_manager) &&
      phosh_panel_get_state (PHOSH_PANEL (priv->panel)) == PHOSH_PANEL_STATE_FOLDED) {
413
414
415
416
417
418
419
420
    g_set_weak_pointer (&priv->notification_banner,
                        phosh_notification_banner_new (notification));

    gtk_widget_show (GTK_WIDGET (priv->notification_banner));
  }
}


Guido Gunther's avatar
Guido Gunther committed
421
422
423
424
425
426
427
428
429
430
static gboolean
on_fade_out_timeout (PhoshShell *self)
{
  PhoshShellPrivate *priv;

  g_return_val_if_fail (PHOSH_IS_SHELL (self), G_SOURCE_REMOVE);

  priv = phosh_shell_get_instance_private (self);

  /* kill all faders if we time out */
431
432
  priv->faders = g_ptr_array_remove_range (priv->faders, 0, priv->faders->len);

Guido Gunther's avatar
Guido Gunther committed
433
434
435
436
  return G_SOURCE_REMOVE;
}


Guido Gunther's avatar
Guido Gunther committed
437
438
439
static gboolean
setup_idle_cb (PhoshShell *self)
{
440
441
  PhoshShellPrivate *priv = phosh_shell_get_instance_private (self);

Guido Gunther's avatar
Guido Gunther committed
442
443
  panels_create (self);
  /* Create background after panel since it needs the panel's size */
444
  priv->background_manager = phosh_background_manager_new ();
Guido Gunther's avatar
Guido Gunther committed
445

446
447
448
449
450
451
452
  g_signal_connect_object (priv->toplevel_manager,
                           "notify::num-toplevels",
                           G_CALLBACK(on_num_toplevels_changed),
                           self,
                           G_CONNECT_SWAPPED);
  on_num_toplevels_changed (self, NULL, priv->toplevel_manager);

453
454
455
456
457
458
  g_signal_connect_object (priv->toplevel_manager,
                           "toplevel-added",
                           G_CALLBACK(on_toplevel_added),
                           self,
                           G_CONNECT_SWAPPED);

459
460
461
462
  /* Screen saver manager needs lock screen manager */
  priv->screen_saver_manager = phosh_screen_saver_manager_get_default (
    priv->lockscreen_manager);

Guido Gunther's avatar
Guido Gunther committed
463
  priv->notify_manager = phosh_notify_manager_get_default ();
464
465
466
467
468
  g_signal_connect_object (priv->notify_manager,
                           "new-notification",
                           G_CALLBACK (on_new_notification),
                           self,
                           G_CONNECT_SWAPPED);
Guido Gunther's avatar
Guido Gunther committed
469

Guido Gunther's avatar
Guido Gunther committed
470
471
472
473
474
475
476
  priv->sensor_proxy_manager = phosh_sensor_proxy_manager_get_default_failable ();
  if (priv->sensor_proxy_manager) {
    priv->proximity = phosh_proximity_new (priv->sensor_proxy_manager,
                                           priv->lockscreen_manager);
    /* TODO: accelerometer */
  }

477
  phosh_session_register (PHOSH_APP_ID);
478

479
  /* If we start rotated, fix this up */
480
481
  if (phosh_shell_get_transform (self) != PHOSH_MONITOR_TRANSFORM_NORMAL)
    phosh_shell_set_transform (self, PHOSH_MONITOR_TRANSFORM_NORMAL);
482

483
484
  priv->startup_finished = TRUE;

Guido Gunther's avatar
Guido Gunther committed
485
486
487
488
  return FALSE;
}


489
490
491
492
493
/* Load all types that might be used in UI files */
static void
type_setup (void)
{
  phosh_battery_info_get_type();
Guido Gunther's avatar
Guido Gunther committed
494
  phosh_wifi_info_get_type();
495
496
497
498
  phosh_wwan_info_get_type();
}


499
500
501
static void
on_builtin_monitor_power_mode_changed (PhoshShell *self, GParamSpec *pspec, PhoshMonitor *monitor)
{
502
  PhoshMonitorPowerSaveMode mode;
503
504
505
506
507

  g_return_if_fail (PHOSH_IS_SHELL (self));
  g_return_if_fail (PHOSH_IS_MONITOR (monitor));

  g_object_get (monitor, "power-mode", &mode, NULL);
508
  if (mode == PHOSH_MONITOR_POWER_SAVE_MODE_OFF)
509
510
511
512
    phosh_shell_lock (self);
}


513
514
515
516
517
static void
on_primary_monitor_configured (PhoshShell   *self,
                               PhoshMonitor *monitor)
{
  PhoshShellPrivate *priv;
518
  PhoshMonitorTransform transform;
519
520
521
522
523

  g_return_if_fail (PHOSH_IS_SHELL (self));
  g_return_if_fail (PHOSH_IS_MONITOR (monitor));

  priv = phosh_shell_get_instance_private (self);
524
525
  transform = phosh_monitor_get_transform (monitor);
  if (transform == priv->transform)
526
527
    return;

528
529
530
  priv->transform = transform;
  g_debug ("Primary monitor transform %d", transform);
  g_object_notify_by_pspec (G_OBJECT (self), props[PHOSH_SHELL_PROP_TRANSFORM]);
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
static void
on_monitor_removed (PhoshShell *self, PhoshMonitor *monitor)
{
  PhoshShellPrivate *priv;

  g_return_if_fail (PHOSH_IS_SHELL (self));
  g_return_if_fail (PHOSH_IS_MONITOR (monitor));
  priv = phosh_shell_get_instance_private (self);

  if (priv->primary_monitor != monitor)
    return;

  g_debug ("Primary monitor removed %p", monitor);

  /* Prefer built in monitor when primary is gone... */
  if (priv->builtin_monitor && monitor != priv->builtin_monitor) {
    phosh_shell_set_primary_monitor (self, priv->builtin_monitor);
    return;
  }

  /* ...just pick the first one available otherwise */
  for (int i = 0; i < phosh_monitor_manager_get_num_monitors (priv->monitor_manager); i++) {
    PhoshMonitor *new_primary = phosh_monitor_manager_get_monitor (priv->monitor_manager, i);
    if (new_primary != monitor) {
      phosh_shell_set_primary_monitor (self, new_primary);
      break;
    }
  }

  g_assert (priv->primary_monitor && priv->primary_monitor != monitor);
}


Guido Gunther's avatar
Guido Gunther committed
567
568
static void
phosh_shell_constructed (GObject *object)
Guido Gunther's avatar
Guido Gunther committed
569
{
Guido Gunther's avatar
Guido Gunther committed
570
571
  PhoshShell *self = PHOSH_SHELL (object);
  PhoshShellPrivate *priv = phosh_shell_get_instance_private (self);
Guido Gunther's avatar
Guido Gunther committed
572

Guido Gunther's avatar
Guido Gunther committed
573
  G_OBJECT_CLASS (phosh_shell_parent_class)->constructed (object);
Guido Gunther's avatar
Guido Gunther committed
574

575
576
577
578
  /* We bind this early since a wl_display_roundtrip () would make us miss
     exising toplevels */
  priv->toplevel_manager = phosh_toplevel_manager_new ();

579
  priv->transform = -1; /* force initial update */
580
  priv->monitor_manager = phosh_monitor_manager_new ();
581
582
583
584
585
  g_signal_connect_swapped (priv->monitor_manager,
                            "monitor-removed",
                            G_CALLBACK (on_monitor_removed),
                            self);

586
587
588
  /* Make sure we processed wayland protocol events */
  phosh_wayland_roundtrip (phosh_wayland_get_default ());

589
  if (phosh_monitor_manager_get_num_monitors(priv->monitor_manager)) {
590
591
    PhoshMonitor *monitor = phosh_monitor_manager_get_monitor (priv->monitor_manager, 0);
    /* Can't invoke phosh_shell_set_primary_monitor () since the shell
Guido Gunther's avatar
Guido Gunther committed
592
       object does not really exist yet but we need the primary monitor
593
594
595
596
597
598
       early for the panels */
    priv->primary_monitor = g_object_ref (monitor);
    g_signal_connect_swapped (priv->primary_monitor,
                              "configured",
                              G_CALLBACK (on_primary_monitor_configured),
                              self);
599
  }
600
601
602
603
604
605

  if (phosh_monitor_is_builtin(priv->primary_monitor))
    priv->builtin_monitor = priv->primary_monitor;
  else
    priv->builtin_monitor = phosh_shell_get_builtin_monitor(self);

606
607
  gtk_icon_theme_add_resource_path (gtk_icon_theme_get_default (),
                                    "/sm/puri/phosh/icons");
Guido Gunther's avatar
Guido Gunther committed
608
  css_setup (self);
609
  type_setup ();
Guido Gunther's avatar
Guido Gunther committed
610

611
  priv->lockscreen_manager = phosh_lockscreen_manager_new ();
612
613
  priv->idle_manager = phosh_idle_manager_get_default();

614
  priv->faders = g_ptr_array_new_with_free_func ((GDestroyNotify) (gtk_widget_destroy));
615

616
  phosh_system_prompter_register ();
617
  priv->polkit_auth_agent = phosh_polkit_auth_agent_new ();
Guido Gunther's avatar
Guido Gunther committed
618

Guido Gunther's avatar
Guido Gunther committed
619
620
  priv->feedback_manager = phosh_feedback_manager_new ();

621
622
623
624
625
626
627
628
  if (priv->builtin_monitor) {
    g_signal_connect_swapped (
      priv->builtin_monitor,
      "notify::power-mode",
      G_CALLBACK(on_builtin_monitor_power_mode_changed),
      self);
  }

629
630
  priv->keyboard_events = phosh_keyboard_events_new ();

Guido Gunther's avatar
Guido Gunther committed
631
  g_idle_add ((GSourceFunc) setup_idle_cb, self);
Guido Gunther's avatar
Guido Gunther committed
632
}
Guido Gunther's avatar
Guido Gunther committed
633

Guido Gunther's avatar
Guido Gunther committed
634
635
636
637
638
639
640

static void
phosh_shell_class_init (PhoshShellClass *klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);

  object_class->constructed = phosh_shell_constructed;
641
642
  object_class->dispose = phosh_shell_dispose;

643
644
645
  object_class->set_property = phosh_shell_set_property;
  object_class->get_property = phosh_shell_get_property;

646
647
648
649
650
651
  props[PHOSH_SHELL_PROP_TRANSFORM] =
    g_param_spec_enum ("transform",
                       "Transform",
                       "Monitor transform of the primary monitor",
                       PHOSH_TYPE_MONITOR_TRANSFORM,
                       PHOSH_MONITOR_TRANSFORM_NORMAL,
652
                       G_PARAM_READABLE | G_PARAM_EXPLICIT_NOTIFY);
653

654
  props[PHOSH_SHELL_PROP_LOCKED] =
Guido Gunther's avatar
Guido Gunther committed
655
656
657
658
659
    g_param_spec_boolean ("locked",
                          "Locked",
                          "Whether the screen is locked",
                          FALSE,
                          G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY);
660

661
662
663
664
665
666
667
  props[PHOSH_SHELL_PROP_PRIMARY_MONITOR] =
    g_param_spec_object ("primary-monitor",
                         "Primary monitor",
                         "The primary monitor",
                         PHOSH_TYPE_MONITOR,
                         G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY);

668
  g_object_class_install_properties (object_class, PHOSH_SHELL_PROP_LAST_PROP, props);
Guido Gunther's avatar
Guido Gunther committed
669
670
671
672
673
674
}


static void
phosh_shell_init (PhoshShell *self)
{
Guido Gunther's avatar
Guido Gunther committed
675
676
677
678
  GtkSettings *gtk_settings;

  gtk_settings = gtk_settings_get_default ();
  g_object_set (G_OBJECT (gtk_settings), "gtk-application-prefer-dark-theme", TRUE, NULL);
Guido Gunther's avatar
Guido Gunther committed
679
}
680
681


682
683
PhoshMonitorTransform
phosh_shell_get_transform (PhoshShell *self)
684
{
685
686
  PhoshShellPrivate *priv;

687
  g_return_val_if_fail (PHOSH_IS_SHELL (self), PHOSH_MONITOR_TRANSFORM_NORMAL);
688
  priv = phosh_shell_get_instance_private (self);
689
690
  g_return_val_if_fail (priv->primary_monitor, PHOSH_MONITOR_TRANSFORM_NORMAL);
  return phosh_monitor_get_transform (priv->primary_monitor);
691
692
693
}


694
void
695
696
phosh_shell_set_transform (PhoshShell *self,
                           PhoshMonitorTransform transform)
697
{
Guido Gunther's avatar
Guido Gunther committed
698
  PhoshShellPrivate *priv = phosh_shell_get_instance_private (self);
699
  PhoshMonitorTransform current;
Guido Gunther's avatar
Guido Gunther committed
700

701
  g_return_if_fail (priv->primary_monitor);
702
703
  current = phosh_monitor_get_transform (priv->primary_monitor);
  if (current == transform)
704
705
    return;

706
707
708
709
710
  phosh_monitor_manager_set_monitor_transform (priv->monitor_manager,
                                               priv->primary_monitor,
                                               transform);
  phosh_monitor_manager_apply_monitor_config (priv->monitor_manager);
  /* Notification change signalled in on_primary_monitor_configured */
711
}
Guido Gunther's avatar
Guido Gunther committed
712
713


714
715
716
717
718
void
phosh_shell_set_primary_monitor (PhoshShell *self, PhoshMonitor *monitor)
{
  PhoshShellPrivate *priv;
  PhoshMonitor *m = NULL;
719
  PhoshMonitorTransform transform;
720
721
722
723
724

  g_return_if_fail (monitor);
  g_return_if_fail (PHOSH_IS_SHELL (self));
  priv = phosh_shell_get_instance_private (self);

725
726
727
  if (monitor == priv->primary_monitor)
    return;

728
729
730
731
732
733
734
  for (int i = 0; i < phosh_monitor_manager_get_num_monitors (priv->monitor_manager); i++) {
    m = phosh_monitor_manager_get_monitor (priv->monitor_manager, i);
    if (monitor == m)
      break;
  }
  g_return_if_fail (monitor == m);

735
736
737
738
  if (priv->primary_monitor)
    g_signal_handlers_disconnect_by_data (priv->primary_monitor, self);
  g_clear_object (&priv->primary_monitor);
  priv->primary_monitor = g_object_ref (monitor);
739
  g_debug ("New primary monitor is %s", monitor->name);
740
741
742
743
744
  g_signal_connect_swapped (priv->primary_monitor,
                            "configured",
                            G_CALLBACK (on_primary_monitor_configured),
                            self);
  /* Catch up if old and new primary monitor's rotation are different */
745
746
  transform = phosh_monitor_get_transform (priv->primary_monitor);
  if (transform != priv->transform)
747
748
    on_primary_monitor_configured (self, priv->primary_monitor);

Guido Gunther's avatar
Guido Gunther committed
749
  /* Move panels to the new monitor by recreating the layer shell surfaces */
750
751
  panels_dispose (self);
  panels_create (self);
752
753

  g_object_notify_by_pspec (G_OBJECT (self), props[PHOSH_SHELL_PROP_PRIMARY_MONITOR]);
754
755
756
}


Guido Gunther's avatar
Guido Gunther committed
757
758
759
760
761
762
763
764
765
PhoshMonitor *
phosh_shell_get_builtin_monitor (PhoshShell *self)
{
  PhoshShellPrivate *priv;
  PhoshMonitor *monitor = NULL;

  g_return_val_if_fail (PHOSH_IS_SHELL (self), NULL);
  priv = phosh_shell_get_instance_private (self);

766
767
768
  if (priv->builtin_monitor)
    return priv->builtin_monitor;

Guido Gunther's avatar
Guido Gunther committed
769
770
771
772
773
774
775
776
777
778
779
780
781
782
  for (int i = 0; i < phosh_monitor_manager_get_num_monitors (priv->monitor_manager); i++) {
    monitor = phosh_monitor_manager_get_monitor (priv->monitor_manager, i);
    if (phosh_monitor_is_builtin (monitor))
      break;
  }

  if (!monitor)
    monitor = phosh_monitor_manager_get_monitor (priv->monitor_manager, 0);
  g_return_val_if_fail (monitor, NULL);

  return monitor;
}


783
PhoshMonitor *
784
phosh_shell_get_primary_monitor (PhoshShell *self)
785
{
786
787
788
789
790
791
  PhoshShellPrivate *priv;
  PhoshMonitor *monitor;

  g_return_val_if_fail (PHOSH_IS_SHELL (self), NULL);
  priv = phosh_shell_get_instance_private (self);

792
793
794
795
  if (priv->primary_monitor)
    return priv->primary_monitor;

  /* When the shell started up we might not have had all monitors */
796
797
798
799
  monitor = phosh_monitor_manager_get_monitor (priv->monitor_manager, 0);
  g_return_val_if_fail (monitor, NULL);

  return monitor;
800
801
802
}


803
804
805
806
807
808
809
810
811
812
813
814
815
PhoshLockscreenManager *
phosh_shell_get_lockscreen_manager (PhoshShell *self)
{
  PhoshShellPrivate *priv;

  g_return_val_if_fail (PHOSH_IS_SHELL (self), NULL);
  priv = phosh_shell_get_instance_private (self);

  g_return_val_if_fail (PHOSH_IS_LOCKSCREEN_MANAGER (priv->lockscreen_manager), NULL);
  return priv->lockscreen_manager;
}


816
817
818
819
820
821
822
823
824
825
826
827
828
PhoshMonitorManager *
phosh_shell_get_monitor_manager (PhoshShell *self)
{
  PhoshShellPrivate *priv;

  g_return_val_if_fail (PHOSH_IS_SHELL (self), NULL);
  priv = phosh_shell_get_instance_private (self);

  g_return_val_if_fail (PHOSH_IS_MONITOR_MANAGER (priv->monitor_manager), NULL);
  return priv->monitor_manager;
}


Guido Gunther's avatar
Guido Gunther committed
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
PhoshWifiManager *
phosh_shell_get_wifi_manager (PhoshShell *self)
{
  PhoshShellPrivate *priv;

  g_return_val_if_fail (PHOSH_IS_SHELL (self), NULL);
  priv = phosh_shell_get_instance_private (self);

  if (!priv->wifi_manager)
      priv->wifi_manager = phosh_wifi_manager_new ();

  g_return_val_if_fail (PHOSH_IS_WIFI_MANAGER (priv->wifi_manager), NULL);
  return priv->wifi_manager;
}

anteater's avatar
anteater committed
844

845
846
847
848
849
850
851
852
853
854
855
856
857
858
PhoshBtManager *
phosh_shell_get_bt_manager (PhoshShell *self)
{
  PhoshShellPrivate *priv;

  g_return_val_if_fail (PHOSH_IS_SHELL (self), NULL);
  priv = phosh_shell_get_instance_private (self);

  if (!priv->bt_manager)
      priv->bt_manager = phosh_bt_manager_new ();

  g_return_val_if_fail (PHOSH_IS_BT_MANAGER (priv->bt_manager), NULL);
  return priv->bt_manager;
}
Guido Gunther's avatar
Guido Gunther committed
859

anteater's avatar
anteater committed
860

861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
PhoshOskManager *
phosh_shell_get_osk_manager (PhoshShell *self)
{
  PhoshShellPrivate *priv;

  g_return_val_if_fail (PHOSH_IS_SHELL (self), NULL);
  priv = phosh_shell_get_instance_private (self);

  if (!priv->osk_manager)
      priv->osk_manager = phosh_osk_manager_new ();

  g_return_val_if_fail (PHOSH_IS_OSK_MANAGER (priv->osk_manager), NULL);
  return priv->osk_manager;
}

anteater's avatar
anteater committed
876

877
878
879
880
881
882
883
884
885
886
887
888
PhoshToplevelManager *
phosh_shell_get_toplevel_manager (PhoshShell *self)
{
  PhoshShellPrivate *priv;

  g_return_val_if_fail (PHOSH_IS_SHELL (self), NULL);
  priv = phosh_shell_get_instance_private (self);

  g_return_val_if_fail (PHOSH_IS_TOPLEVEL_MANAGER (priv->toplevel_manager), NULL);
  return priv->toplevel_manager;
}

anteater's avatar
anteater committed
889

890
891
892
893
894
895
896
897
898
899
900
PhoshFeedbackManager *
phosh_shell_get_feedback_manager (PhoshShell *self)
{
  PhoshShellPrivate *priv;

  g_return_val_if_fail (PHOSH_IS_SHELL (self), NULL);
  priv = phosh_shell_get_instance_private (self);
  g_return_val_if_fail (PHOSH_IS_FEEDBACK_MANAGER (priv->feedback_manager), NULL);

  return priv->feedback_manager;
}
901

anteater's avatar
anteater committed
902

anteater's avatar
anteater committed
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
PhoshWWan *
phosh_shell_get_wwan (PhoshShell *self)
{
  PhoshShellPrivate *priv;

  g_return_val_if_fail (PHOSH_IS_SHELL (self), NULL);
  priv = phosh_shell_get_instance_private (self);

  if (!priv->wwan) {
    g_autoptr (GSettings) settings = g_settings_new ("sm.puri.phosh");
    PhoshWWanBackend backend = g_settings_get_enum (settings, WWAN_BACKEND_KEY);

    switch (backend) {
      default:
      case PHOSH_WWAN_BACKEND_MM:
        priv->wwan = PHOSH_WWAN (phosh_wwan_mm_new());
        break;
      case PHOSH_WWAN_BACKEND_OFONO:
        priv->wwan = PHOSH_WWAN (phosh_wwan_ofono_new());
        break;
    }
  }

  g_return_val_if_fail (PHOSH_IS_WWAN (priv->wwan), NULL);
  return priv->wwan;
}


Guido Gunther's avatar
Guido Gunther committed
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
PhoshTorchManager *
phosh_shell_get_torch_manager (PhoshShell *self)
{
  PhoshShellPrivate *priv;

  g_return_val_if_fail (PHOSH_IS_SHELL (self), NULL);
  priv = phosh_shell_get_instance_private (self);

  if (!priv->torch_manager)
    priv->torch_manager = phosh_torch_manager_new ();

  g_return_val_if_fail (PHOSH_IS_TORCH_MANAGER (priv->torch_manager), NULL);
  return priv->torch_manager;
}

946
947
948
949
950
/**
 * Returns the usable area in pixels usable by a client on the phone
 * display
 */
void
951
phosh_shell_get_usable_area (PhoshShell *self, int *x, int *y, int *width, int *height)
952
{
953
954
  PhoshMonitor *monitor;
  PhoshMonitorMode *mode;
955
956
  int w, h;
  int scale;
957
958

  g_return_if_fail (PHOSH_IS_SHELL (self));
959

960
  monitor = phosh_shell_get_primary_monitor (self);
961
  g_return_if_fail(monitor);
962
  mode = phosh_monitor_get_current_mode (monitor);
963
  g_return_if_fail (mode != NULL);
964

965
  scale = monitor->scale ? monitor->scale : 1;
966
967
968
969
970
971

  g_debug ("Primary monitor %p scale is %d, transform is %d",
           monitor,
           monitor->scale,
           monitor->transform);

972
973
974
975
976
  switch (phosh_monitor_get_transform(monitor)) {
  case PHOSH_MONITOR_TRANSFORM_NORMAL:
  case PHOSH_MONITOR_TRANSFORM_180:
  case PHOSH_MONITOR_TRANSFORM_FLIPPED:
  case PHOSH_MONITOR_TRANSFORM_FLIPPED_180:
977
    w = mode->width / scale;
978
    h = mode->height / scale - PHOSH_PANEL_HEIGHT - PHOSH_HOME_BUTTON_HEIGHT;
979
980
981
    break;
  default:
    w = mode->height / scale;
982
    h = mode->width / scale - PHOSH_PANEL_HEIGHT - PHOSH_HOME_BUTTON_HEIGHT;
983
    break;
984
  }
985

986
987
988
  if (x)
    *x = 0;
  if (y)
989
    *y = PHOSH_PANEL_HEIGHT;
990
991
992
993
  if (width)
    *width = w;
  if (height)
    *height = h;
994
995
996
}


Guido Gunther's avatar
Guido Gunther committed
997
PhoshShell *
998
phosh_shell_get_default (void)
Guido Gunther's avatar
Guido Gunther committed
999
{
1000
1001
1002
  static PhoshShell *instance;

  if (instance == NULL) {
Guido Gunther's avatar
Guido Gunther committed
1003
    g_debug("Creating shell");
1004
1005
1006
1007
    instance = g_object_new (PHOSH_TYPE_SHELL, NULL);
    g_object_add_weak_pointer (G_OBJECT (instance), (gpointer *)&instance);
  }
  return instance;
Guido Gunther's avatar
Guido Gunther committed
1008
}
1009
1010

void
Guido Gunther's avatar
Guido Gunther committed
1011
phosh_shell_fade_out (PhoshShell *self, guint timeout)
1012
1013
1014
{
  PhoshShellPrivate *priv;
  PhoshWayland *wl = phosh_wayland_get_default ();
1015
  PhoshMonitorManager *monitor_manager;
1016
1017
1018

  g_debug ("Fading out...");
  g_return_if_fail (PHOSH_IS_SHELL (self));
1019
1020
  monitor_manager = phosh_shell_get_monitor_manager (self);
  g_return_if_fail (PHOSH_IS_MONITOR_MANAGER (monitor_manager));
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
  priv = phosh_shell_get_instance_private (self);

  for (int i = 0; i < phosh_monitor_manager_get_num_monitors (monitor_manager); i++) {
    PhoshFader *fader;
    PhoshMonitor *monitor = phosh_monitor_manager_get_monitor (monitor_manager, i);

    fader = phosh_fader_new (phosh_wayland_get_zwlr_layer_shell_v1 (wl),
                             monitor->wl_output);
    g_ptr_array_add (priv->faders, fader);
    gtk_widget_show (GTK_WIDGET (fader));
Guido Gunther's avatar
Guido Gunther committed
1031
1032
    if (timeout > 0)
      g_timeout_add_seconds (timeout, (GSourceFunc) on_fade_out_timeout, self);
1033
1034
  }
}
1035
1036
1037

/**
 * phosh_shell_set_power_save:
1038
1039
 * @self: The shell
 * @enable: Wether power save mode should be enabled
1040
1041
1042
1043
1044
1045
1046
1047
1048
 *
 * Enter power saving mode. This currently blanks all monitors.
 */
void
phosh_shell_enable_power_save (PhoshShell *self, gboolean enable)
{
  g_debug ("Entering power save mode");
  g_return_if_fail (PHOSH_IS_SHELL (self));

1049
1050
1051
1052
1053
  /*
   * Locking the outputs instructs g-s-d to tell us to put
   * outputs into power save mode via org.gnome.Mutter.DisplayConfig
   */
  phosh_shell_set_locked(self, enable);
1054
1055
1056

  /* TODO: other means of power saving */
}
1057
1058
1059

/**
 * phosh_shell_started_by_display_manager:
1060
 * @self: The shell
1061
 *
1062
 * Returns: %TRUE if we were started from a display manager. %FALSE otherwise.
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
 */
gboolean
phosh_shell_started_by_display_manager(PhoshShell *self)
{
  g_return_val_if_fail (PHOSH_IS_SHELL (self), FALSE);

  if (!g_strcmp0 (g_getenv ("GDMSESSION"), "phosh"))
    return TRUE;

  return FALSE;
}
1074
1075
1076

/**
 * phosh_shell_is_startup_finished:
1077
 * @self: The shell
1078
 *
1079
 * Returns: %TRUE if the shell finished startup. %FALSE otherwise.
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
 */
gboolean
phosh_shell_is_startup_finished(PhoshShell *self)
{
  PhoshShellPrivate *priv;

  g_return_val_if_fail (PHOSH_IS_SHELL (self), FALSE);
  priv = phosh_shell_get_instance_private (self);

  return priv->startup_finished;
}
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109


void
phosh_shell_add_global_keyboard_action_entries (PhoshShell *self,
                                                const GActionEntry *entries,
                                                gint n_entries,
                                                gpointer user_data)
{
  PhoshShellPrivate *priv;

  g_return_if_fail (PHOSH_IS_SHELL (self));
  priv = phosh_shell_get_instance_private (self);
  g_return_if_fail (priv->keyboard_events);

  g_action_map_add_action_entries (G_ACTION_MAP (priv->keyboard_events),
                                   entries,
                                   n_entries,
                                   user_data);
}
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129


void
phosh_shell_remove_global_keyboard_action_entries (PhoshShell *self,
                                                   const GActionEntry *entries,
                                                   gint n_entries)

{
  PhoshShellPrivate *priv;

  g_return_if_fail (PHOSH_IS_SHELL (self));
  priv = phosh_shell_get_instance_private (self);
  g_return_if_fail (priv->keyboard_events);

  for (int i = 0; i < n_entries; i++) {
    g_action_map_remove_action (G_ACTION_MAP (priv->keyboard_events),
                                entries[i].name);
  }
}