docked-manager.c 9.17 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
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
/*
 * Copyright (C) 2020 Purism SPC
 *
 * SPDX-License-Identifier: GPL-3.0-or-later
 *
 * Author: Guido Günther <agx@sigxcpu.org>
 */

#define G_LOG_DOMAIN "phosh-docked-manager"

#include "config.h"

#include "docked-manager.h"
#include "mode-manager.h"
#include "shell.h"

#define DOCKED_DISABLED_ICON "phone-undocked-symbolic"
#define DOCKED_ENABLED_ICON  "phone-docked-symbolic"

#define PHOC_KEY_MAXIMIZE "auto-maximize"
#define A11Y_KEY_OSK "screen-keyboard-enabled"
#define WM_KEY_LAYOUT "button-layout"
#define GTK_KEY_IS_PHONE "is-phone"

/**
 * SECTION:docked-manager
 * @short_description: Handles 'docking" the phone to additional hardware
 * @Title: PhoshDockedManager
 *
 * #PhoshDockedManager allows to dock the phone to additional hardware
 * and performs the necessary configuration changes
 * (disable OSK, don't maximize windows by default, ...)
 */

enum {
  PROP_0,
  PROP_MODE_MANAGER,
  PROP_ICON_NAME,
  PROP_ENABLED,
  PROP_CAN_DOCK,
  PROP_LAST_PROP
};
static GParamSpec *props[PROP_LAST_PROP];

struct _PhoshDockedManager {
  GObject           parent;

  gboolean          enabled;
  gboolean          can_dock;
  const char       *icon_name;

  PhoshModeManager *mode_manager;

  GSettings        *phoc_settings;
  GSettings        *wm_settings;
  GSettings        *a11y_settings;
  GSettings        *gtk_settings;
};
G_DEFINE_TYPE (PhoshDockedManager, phosh_docked_manager, G_TYPE_OBJECT);


static void
phosh_docked_manager_get_property (GObject    *object,
                                   guint       property_id,
                                   GValue     *value,
                                   GParamSpec *pspec)
{
  PhoshDockedManager *self = PHOSH_DOCKED_MANAGER (object);

  switch (property_id) {
  case PROP_ICON_NAME:
    g_value_set_string (value, self->icon_name);
    break;
  case PROP_CAN_DOCK:
    g_value_set_boolean (value, self->can_dock);
    break;
  case PROP_ENABLED:
    g_value_set_boolean (value, self->enabled);
    break;
  default:
    G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
    break;
  }
}


static void
phosh_docked_manager_set_property (GObject      *object,
                                   guint         property_id,
                                   const GValue *value,
                                   GParamSpec   *pspec)
{
  PhoshDockedManager *self = PHOSH_DOCKED_MANAGER (object);

  switch (property_id) {
  case PROP_MODE_MANAGER:
    /* construct only */
    self->mode_manager = g_value_dup_object (value);
    g_object_notify_by_pspec (G_OBJECT (self), props[PROP_MODE_MANAGER]);
    break;
  case PROP_ENABLED:
    phosh_docked_manager_set_enabled (self, g_value_get_boolean (value));
    break;
  default:
    G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
    break;
  }
}


static void
mode_changed_cb (PhoshDockedManager *self, GParamSpec *pspec, PhoshModeManager *manager)
{
  gboolean can_dock = FALSE;

  g_return_if_fail (PHOSH_IS_DOCKED_MANAGER (self));
  g_return_if_fail (PHOSH_IS_MODE_MANAGER (manager));
  g_return_if_fail (self->mode_manager == manager);

  /*
   * Desktops, laptops and phones with enough external hardware should get floating
   * windows, etc
   */
124
125
  if (phosh_mode_manager_get_mimicry (manager) != PHOSH_MODE_DEVICE_TYPE_PHONE &&
      phosh_mode_manager_get_mimicry (manager) != PHOSH_MODE_DEVICE_TYPE_TABLET)
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
    can_dock = TRUE;

  if (can_dock == self->can_dock)
    return;

  g_debug ("Docked mode possible: %d", can_dock);
  self->can_dock = can_dock;
  g_object_notify_by_pspec (G_OBJECT (self), props[PROP_CAN_DOCK]);

  /* Automatically enable/disable docked mode */
  phosh_docked_manager_set_enabled (self, can_dock);
}


static void
phosh_docked_manager_constructed (GObject *object)
{
  PhoshDockedManager *self = PHOSH_DOCKED_MANAGER (object);
  GSettingsSchemaSource *schema_source = g_settings_schema_source_get_default();
  g_autoptr (GSettingsSchema) schema = NULL;


  G_OBJECT_CLASS (phosh_docked_manager_parent_class)->constructed (object);

  self->phoc_settings = g_settings_new ("sm.puri.phoc");
  self->a11y_settings = g_settings_new ("org.gnome.desktop.a11y.applications");
  self->wm_settings = g_settings_new ("org.gnome.desktop.wm.preferences");

  /* This is a downstream only schema that is supposed to go away once GTK
   * GTK is adaptive (https://source.puri.sm/Librem5/gtk/-/merge_requests/18) */
  schema = g_settings_schema_source_lookup (schema_source, "org.gtk.Settings.Purism", TRUE);
  if (schema && g_settings_schema_has_key (schema, GTK_KEY_IS_PHONE))
    self->gtk_settings = g_settings_new ("org.gtk.Settings.Purism");

  g_object_connect (
    self->mode_manager,
    "swapped-object-signal::notify::device-type", G_CALLBACK (mode_changed_cb), self,
    "swapped-object-signal::notify::mimicry", G_CALLBACK (mode_changed_cb), self,
    NULL);
  mode_changed_cb (self, NULL, self->mode_manager);
}


static void
phosh_docked_manager_dispose (GObject *object)
{
  PhoshDockedManager *self = PHOSH_DOCKED_MANAGER (object);

  g_clear_object (&self->phoc_settings);
  g_clear_object (&self->a11y_settings);
  g_clear_object (&self->wm_settings);
  g_clear_object (&self->gtk_settings);
  g_clear_object (&self->mode_manager);

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


static void
phosh_docked_manager_class_init (PhoshDockedManagerClass *klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);

  object_class->constructed = phosh_docked_manager_constructed;
  object_class->dispose = phosh_docked_manager_dispose;
  object_class->get_property = phosh_docked_manager_get_property;
  object_class->set_property = phosh_docked_manager_set_property;

  props[PROP_MODE_MANAGER] =
    g_param_spec_object ("mode-manager",
                         "Mode manager",
                         "The hw mode object",
                         PHOSH_TYPE_MODE_MANAGER,
                         G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);

  props[PROP_ICON_NAME] =
    g_param_spec_string ("icon-name",
                         "icon name",
                         "The docked icon name",
                         DOCKED_DISABLED_ICON,
                         G_PARAM_READABLE | G_PARAM_EXPLICIT_NOTIFY);

  props[PROP_ENABLED] =
    g_param_spec_boolean ("enabled",
                          "enabled",
                          "Whether docked mode is enabled",
                          FALSE,
                          G_PARAM_READWRITE |
                          G_PARAM_EXPLICIT_NOTIFY |
                          G_PARAM_STATIC_STRINGS);

  props[PROP_CAN_DOCK] =
    g_param_spec_boolean ("can-dock",
                          "Can dock",
                          "Whether the device can be docked",
                          FALSE,
                          G_PARAM_READABLE |
                          G_PARAM_EXPLICIT_NOTIFY |
                          G_PARAM_STATIC_STRINGS);

  g_object_class_install_properties (object_class, PROP_LAST_PROP, props);
}


static void
phosh_docked_manager_init (PhoshDockedManager *self)
{
  self->icon_name = DOCKED_DISABLED_ICON;
}


PhoshDockedManager *
phosh_docked_manager_new (PhoshModeManager *mode_manager)
{
  return PHOSH_DOCKED_MANAGER (g_object_new (PHOSH_TYPE_DOCKED_MANAGER,
                                             "mode-manager", mode_manager,
                                             NULL));
}


const char *
phosh_docked_manager_get_icon_name (PhoshDockedManager *self)
{
  g_return_val_if_fail (PHOSH_IS_DOCKED_MANAGER (self), NULL);

  return self->icon_name;
}


gboolean
phosh_docked_manager_get_enabled (PhoshDockedManager *self)
{
  g_return_val_if_fail (PHOSH_IS_DOCKED_MANAGER (self), FALSE);

  return self->enabled;
}


gboolean
phosh_docked_manager_get_can_dock (PhoshDockedManager *self)
{
  g_return_val_if_fail (PHOSH_IS_DOCKED_MANAGER (self), FALSE);

  return self->can_dock;
}


void
phosh_docked_manager_set_enabled (PhoshDockedManager *self, gboolean enable)
{
  const gchar *icon_name;

  g_return_if_fail (PHOSH_IS_DOCKED_MANAGER (self));

  if (self->enabled == enable)
    return;

  g_object_freeze_notify (G_OBJECT (self));

  self->enabled = enable;
  g_object_notify_by_pspec (G_OBJECT (self), props[PROP_ENABLED]);

  if (enable)
    g_settings_reset (self->wm_settings, WM_KEY_LAYOUT);
  else
    g_settings_set_string (self->wm_settings, WM_KEY_LAYOUT, "appmenu:");

  /* we could bind one boolean property via g_settings_bind but that would spread
   * mode setup over several places */
  g_settings_set_boolean (self->phoc_settings, PHOC_KEY_MAXIMIZE, !enable);
  g_settings_set_boolean (self->a11y_settings, A11Y_KEY_OSK, !enable);
  if (self->gtk_settings)
    g_settings_set_boolean (self->gtk_settings, GTK_KEY_IS_PHONE, !enable);

  /* TODO: Other icons for non phones? */
  icon_name = enable ? DOCKED_ENABLED_ICON : DOCKED_DISABLED_ICON;
  if (icon_name != self->icon_name) {
    self->icon_name = icon_name;
    g_object_notify_by_pspec (G_OBJECT (self), props[PROP_ICON_NAME]);
  }

  g_object_thaw_notify (G_OBJECT (self));

  if (!enable) {
    PhoshShell *shell = phosh_shell_get_default ();
    PhoshMonitor *monitor = phosh_shell_get_builtin_monitor (shell);
    if (monitor)
      phosh_shell_set_primary_monitor (shell, monitor);
  }
  g_debug ("Docked mode %sabled", self->enabled ? "en" : "dis");
}