summaryrefslogtreecommitdiff
path: root/panels/wacom/cc-wacom-page.c
blob: 0753e6e8f3525bdea00413127b7ba001500e4e8e (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
/*
 * Copyright © 2011 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, see <http://www.gnu.org/licenses/>.
 *
 * Authors: Peter Hutterer <peter.hutterer@redhat.com>
 *          Bastien Nocera <hadess@hadess.net>
 *
 */

#include <config.h>

#ifdef FAKE_AREA
#include <gdk/gdk.h>
#endif /* FAKE_AREA */

#include <glib/gi18n-lib.h>
#include <gtk/gtk.h>
#include <gdesktop-enums.h>
#ifdef GDK_WINDOWING_X11
#include <gdk/x11/gdkx.h>
#endif
#ifdef GDK_WINDOWING_WAYLAND
#include <gdk/wayland/gdkwayland.h>
#endif

#include "cc-wacom-device.h"
#include "cc-wacom-button-row.h"
#include "cc-wacom-page.h"
#include "cc-wacom-stylus-page.h"
#include "gsd-enums.h"
#include "calibrator-gui.h"
#include "gsd-input-helper.h"

#include <string.h>

#define MWID(x) (GtkWidget *) gtk_builder_get_object (page->mapping_builder, x)

#define THRESHOLD_MISCLICK	15
#define THRESHOLD_DOUBLECLICK	7

struct _CcWacomPage
{
	GtkBox          parent_instance;

	CcWacomPanel   *panel;
	CcWacomDevice  *stylus;
	GList          *pads;
	CcCalibArea    *area;
	GSettings      *wacom_settings;

	GtkWidget      *tablet_section;
	GtkWidget      *tablet_icon;
	GtkWidget      *tablet_display;
	GtkWidget      *tablet_calibrate;
	GtkWidget      *tablet_map_buttons;
	GtkWidget      *tablet_mode;
	GtkWidget      *tablet_mode_switch;
	GtkWidget      *tablet_left_handed;
	GtkWidget      *tablet_left_handed_switch;
	GtkWidget      *tablet_aspect_ratio;
	GtkWidget      *tablet_aspect_ratio_switch;
	GtkWidget      *display_section;

	GnomeRRScreen  *rr_screen;

	/* Button mapping */
	GtkBuilder     *mapping_builder;
	GtkWindow      *button_map;
	GtkListStore   *action_store;

	GCancellable   *cancellable;

	/* To reach other grouped devices */
	GsdDeviceManager *manager;
};

G_DEFINE_TYPE (CcWacomPage, cc_wacom_page, GTK_TYPE_BOX)

/* Different types of layout for the tablet config */
enum {
	LAYOUT_NORMAL,        /* tracking mode, button mapping */
	LAYOUT_REVERSIBLE,    /* tracking mode, button mapping, left-hand orientation */
	LAYOUT_SCREEN        /* button mapping, calibration, display resolution */
};

static int
get_layout_type (CcWacomDevice *device)
{
	int layout;

	if (cc_wacom_device_get_integration_flags (device) &
	    (WACOM_DEVICE_INTEGRATED_DISPLAY | WACOM_DEVICE_INTEGRATED_SYSTEM))
		layout = LAYOUT_SCREEN;
	else if (cc_wacom_device_is_reversible (device))
		layout = LAYOUT_REVERSIBLE;
	else
		layout = LAYOUT_NORMAL;

	return layout;
}

static void
set_calibration (CcWacomDevice  *device,
                 gdouble        *cal,
                 gsize           ncal,
                 GSettings      *settings)
{
	GVariant    *current; /* current calibration */
	GVariant    *array;   /* new calibration */
	g_autofree GVariant   **tmp = NULL;
	gsize        nvalues;
	gint         i;

	current = g_settings_get_value (settings, "area");
	g_variant_get_fixed_array (current, &nvalues, sizeof (gdouble));
	if ((ncal != 4) || (nvalues != 4)) {
		g_warning("Unable to set device calibration property. Got %"G_GSIZE_FORMAT" items to put in %"G_GSIZE_FORMAT" slots; expected %d items.\n", ncal, nvalues, 4);
		return;
	}

	tmp = g_malloc (nvalues * sizeof (GVariant*));
	for (i = 0; i < ncal; i++)
		tmp[i] = g_variant_new_double (cal[i]);

	array = g_variant_new_array (G_VARIANT_TYPE_DOUBLE, tmp, nvalues);
	g_settings_set_value (settings, "area", array);

	g_debug ("Setting area to %f, %f, %f, %f (left/right/top/bottom)",
		 cal[0], cal[1], cal[2], cal[3]);
}

static void
finish_calibration (CcCalibArea *area,
		    gpointer     user_data)
{
	CcWacomPage *page = (CcWacomPage *) user_data;
	XYinfo axis;
	gdouble cal[4];

	if (cc_calib_area_finish (area)) {
		cc_calib_area_get_padding (area, &axis);
		cal[0] = axis.x_min;
		cal[1] = axis.x_max;
		cal[2] = axis.y_min;
		cal[3] = axis.y_max;

		set_calibration (page->stylus,
				 cal, 4, page->wacom_settings);
	} else {
		/* Reset the old values */
		GVariant *old_calibration;

		old_calibration = g_object_get_data (G_OBJECT (page), "old-calibration");
		g_settings_set_value (page->wacom_settings, "area", old_calibration);
		g_object_set_data (G_OBJECT (page), "old-calibration", NULL);
	}

	cc_calib_area_free (area);
	page->area = NULL;
	gtk_widget_set_sensitive (page->tablet_calibrate, TRUE);
}

static GdkDevice *
cc_wacom_page_get_gdk_device (CcWacomPage *page)
{
	GsdDevice *gsd_device;
	GdkDevice *gdk_device = NULL;
	GdkDisplay *display;
	GdkSeat *seat;
	g_autoptr(GList) slaves = NULL;
	GList *l;

	gsd_device = cc_wacom_device_get_device (page->stylus);
	g_return_val_if_fail (GSD_IS_DEVICE (gsd_device), NULL);

	display = gtk_widget_get_display (GTK_WIDGET (page));
	seat = gdk_display_get_default_seat (display);
	slaves = gdk_seat_get_devices (seat, GDK_SEAT_CAPABILITY_TABLET_STYLUS);

	for (l = slaves; l && !gdk_device; l = l->next) {
		g_autofree gchar *device_node = NULL;

		if (gdk_device_get_source (l->data) != GDK_SOURCE_PEN)
			continue;

#ifdef GDK_WINDOWING_X11
		if (GDK_IS_X11_DISPLAY (display))
			device_node = xdevice_get_device_node (gdk_x11_device_get_id (l->data));
#endif
#ifdef GDK_WINDOWING_WAYLAND
		if (GDK_IS_WAYLAND_DISPLAY (display))
			device_node = g_strdup (gdk_wayland_device_get_node_path (l->data));
#endif

		if (g_strcmp0 (device_node, gsd_device_get_device_file (gsd_device)) == 0)
			gdk_device = l->data;
	}

	return gdk_device;
}

static gboolean
run_calibration (CcWacomPage *page,
		 GVariant    *old_calibration,
		 gdouble     *cal,
		 GdkMonitor  *monitor)
{
	g_assert (page->area == NULL);

	page->area = cc_calib_area_new (NULL,
                                        monitor,
                                        cc_wacom_page_get_gdk_device (page),
                                        finish_calibration,
                                        page,
                                        THRESHOLD_MISCLICK,
                                        THRESHOLD_DOUBLECLICK);

	g_object_set_data_full (G_OBJECT (page),
				"old-calibration",
				old_calibration,
				(GDestroyNotify) g_variant_unref);

	return FALSE;
}

static void
calibrate (CcWacomPage *page)
{
	int i;
	GVariant *old_calibration, *array;
	g_autofree GVariant **tmp = NULL;
	g_autofree gdouble *calibration = NULL;
	gsize ncal;
	g_autoptr(GdkMonitor) monitor = NULL;
  GListModel *monitors;
	GdkDisplay *display;
	g_autoptr(GnomeRRScreen) rr_screen = NULL;
	GnomeRROutput *output;
	g_autoptr(GError) error = NULL;
	gint x, y;

	display = gdk_display_get_default ();
	rr_screen = gnome_rr_screen_new (display, &error);
	if (error) {
		g_warning ("Could not connect to display manager: %s", error->message);
		return;
	}

	output = cc_wacom_device_get_output (page->stylus, rr_screen);
	gnome_rr_output_get_position (output, &x, &y);

  monitors = gdk_display_get_monitors (display);
  for (i = 0; i < g_list_model_get_n_items (monitors); i++) {
    g_autoptr(GdkMonitor) m = g_list_model_get_item (monitors, i);
    GdkRectangle geometry;

    gdk_monitor_get_geometry (m, &geometry);
    if (gdk_rectangle_contains_point (&geometry, x, y))
      {
        monitor = g_steal_pointer (&m);
        break;
      }
  }

	if (!monitor) {
		/* The display the tablet should be mapped to could not be located.
		 * This shouldn't happen if the EDID data is good...
		 */
		g_critical("Output associated with the tablet is not connected. Unable to calibrate.");
		return;
	}

	old_calibration = g_settings_get_value (page->wacom_settings, "area");
	g_variant_get_fixed_array (old_calibration, &ncal, sizeof (gdouble));

	if (ncal != 4) {
		g_warning("Device calibration property has wrong length. Got %"G_GSIZE_FORMAT" items; expected %d.\n", ncal, 4);
		return;
	}

	calibration = g_new0 (gdouble, ncal);

	/* Reset the current values, to avoid old calibrations
	 * from interfering with the calibration */
	tmp = g_malloc (ncal * sizeof (GVariant*));
	for (i = 0; i < ncal; i++) {
		calibration[i] = 0.0;
		tmp[i] = g_variant_new_double (calibration[i]);
	}

	array = g_variant_new_array (G_VARIANT_TYPE_DOUBLE, tmp, ncal);
	g_settings_set_value (page->wacom_settings, "area", array);

	run_calibration (page, old_calibration, calibration, monitor);
	gtk_widget_set_sensitive (page->tablet_calibrate, FALSE);
}

static void
on_calibrate_activated (CcWacomPage *self)
{
	calibrate (self);
}

/* This avoids us crashing when a newer version of
 * gnome-control-center has been used, and we load up an
 * old one, as the action type if unknown to the old g-c-c */
static gboolean
action_type_is_valid (GDesktopPadButtonAction action)
{
	if (action >= G_N_ELEMENTS (action_table))
		return FALSE;
	return TRUE;
}

static void
create_row_from_button (GtkWidget *list_box,
			guint      button,
			GSettings *settings)
{
	gtk_list_box_append (GTK_LIST_BOX (list_box),
                       cc_wacom_button_row_new (button, settings));
}

static void
setup_button_mapping (CcWacomPage *page)
{
	GDesktopPadButtonAction action;
	CcWacomDevice *pad;
	GtkWidget *list_box;
	guint i, n_buttons;
	GSettings *settings;

	list_box = MWID ("shortcuts_list");
	pad = page->pads->data;
	n_buttons = cc_wacom_device_get_num_buttons (pad);

	for (i = 0; i < n_buttons; i++) {
		settings = cc_wacom_device_get_button_settings (pad, i);
		if (!settings)
			continue;

		action = g_settings_get_enum (settings, "action");
		if (!action_type_is_valid (action))
			continue;

		create_row_from_button (list_box, i, settings);
	}
}

static void
button_mapping_dialog_closed (CcWacomPage *page)
{
	gtk_window_destroy (GTK_WINDOW (MWID ("button-mapping-dialog")));
	g_clear_object (&page->mapping_builder);
}

static void
show_button_mapping_dialog (CcWacomPage *page)
{
	GtkWidget          *toplevel;
	g_autoptr(GError)   error = NULL;
	GtkWidget          *dialog;

	g_assert (page->mapping_builder == NULL);
	page->mapping_builder = gtk_builder_new ();
	gtk_builder_add_from_resource (page->mapping_builder,
                                       "/org/gnome/control-center/wacom/button-mapping.ui",
                                       &error);

	if (error != NULL) {
		g_warning ("Error loading UI file: %s", error->message);
		g_clear_object (&page->mapping_builder);
		return;
	}

	setup_button_mapping (page);

	dialog = MWID ("button-mapping-dialog");
	toplevel = GTK_WIDGET (gtk_widget_get_native (GTK_WIDGET (page)));
	gtk_window_set_transient_for (GTK_WINDOW (dialog), GTK_WINDOW (toplevel));
	gtk_window_set_modal (GTK_WINDOW (dialog), TRUE);
	g_signal_connect_object (dialog, "response",
	                         G_CALLBACK (button_mapping_dialog_closed), page, G_CONNECT_SWAPPED);

	gtk_widget_show (dialog);

	page->button_map = GTK_WINDOW (dialog);
	g_object_add_weak_pointer (G_OBJECT (dialog), (gpointer *) &page->button_map);
}

static void
set_osd_visibility_cb (GObject      *source_object,
		       GAsyncResult *res,
		       gpointer      data)
{
	g_autoptr(GError) error = NULL;
	GVariant    *result;
	CcWacomPage *page;

	page = CC_WACOM_PAGE (data);

	result = g_dbus_proxy_call_finish (G_DBUS_PROXY (source_object), res, &error);

	if (result == NULL) {
		if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED)) {
			g_printerr ("Error setting OSD's visibility: %s\n", error->message);
			show_button_mapping_dialog (page);
		} else {
			return;
		}
	}
}

static void
set_osd_visibility (CcWacomPage *page)
{
	GDBusProxy         *proxy;
	GsdDevice          *gsd_device;
	const gchar        *device_path;

	proxy = cc_wacom_panel_get_gsd_wacom_bus_proxy (page->panel);

	/* Pick the first device, the OSD may change later between them */
	gsd_device = cc_wacom_device_get_device (page->pads->data);

	device_path = gsd_device_get_device_file (gsd_device);

	if (proxy == NULL) {
		show_button_mapping_dialog (page);
		return;
	}

	g_dbus_proxy_call (proxy,
			   "Show",
			   g_variant_new ("(ob)", device_path, TRUE),
			   G_DBUS_CALL_FLAGS_NONE,
			   -1,
			   page->cancellable,
			   set_osd_visibility_cb,
			   page);
}

static void
on_map_buttons_activated (CcWacomPage *self)
{
	set_osd_visibility (self);
}

static void
on_display_selected (GtkWidget   *widget,
		     GParamSpec  *pspec,
		     CcWacomPage *page)
{
	GListModel *list;
	g_autoptr (GObject) obj = NULL;
	GVariant *variant;
	gint idx;

	list = adw_combo_row_get_model (ADW_COMBO_ROW (widget));
	idx = adw_combo_row_get_selected (ADW_COMBO_ROW (widget));
	obj = g_list_model_get_item (list, idx);

	variant = g_object_get_data (obj, "value-output");

	if (variant)
		g_settings_set_value (page->wacom_settings, "output", g_variant_ref (variant));
	else
		g_settings_reset (page->wacom_settings, "output");

	gtk_widget_set_sensitive (page->tablet_calibrate, variant == NULL);
}

/* Boilerplate code goes below */

static void
cc_wacom_page_get_property (GObject    *object,
                             guint       property_id,
                             GValue     *value,
                             GParamSpec *pspec)
{
	switch (property_id)
	{
		default:
			G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
	}
}

static void
cc_wacom_page_set_property (GObject      *object,
                             guint         property_id,
                             const GValue *value,
                             GParamSpec   *pspec)
{
	switch (property_id)
	{
		default:
			G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
	}
}

static void
cc_wacom_page_dispose (GObject *object)
{
	CcWacomPage *self = CC_WACOM_PAGE (object);

	g_cancellable_cancel (self->cancellable);
	g_clear_object (&self->cancellable);
	g_clear_pointer (&self->area, cc_calib_area_free);
	g_clear_pointer (&self->button_map, gtk_window_destroy);
	g_list_free_full (self->pads, g_object_unref);
	g_clear_object (&self->rr_screen);
	self->pads = NULL;

	self->panel = NULL;

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

static void
cc_wacom_page_class_init (CcWacomPageClass *klass)
{
	GObjectClass *object_class = G_OBJECT_CLASS (klass);
	GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);

	object_class->get_property = cc_wacom_page_get_property;
	object_class->set_property = cc_wacom_page_set_property;
	object_class->dispose = cc_wacom_page_dispose;

	gtk_widget_class_set_template_from_resource (widget_class, "/org/gnome/control-center/wacom/cc-wacom-page.ui");

	gtk_widget_class_bind_template_child (widget_class, CcWacomPage, tablet_section);
	gtk_widget_class_bind_template_child (widget_class, CcWacomPage, tablet_icon);
	gtk_widget_class_bind_template_child (widget_class, CcWacomPage, tablet_display);
	gtk_widget_class_bind_template_child (widget_class, CcWacomPage, tablet_calibrate);
	gtk_widget_class_bind_template_child (widget_class, CcWacomPage, tablet_map_buttons);
	gtk_widget_class_bind_template_child (widget_class, CcWacomPage, tablet_mode);
	gtk_widget_class_bind_template_child (widget_class, CcWacomPage, tablet_mode_switch);
	gtk_widget_class_bind_template_child (widget_class, CcWacomPage, tablet_left_handed);
	gtk_widget_class_bind_template_child (widget_class, CcWacomPage, tablet_left_handed_switch);
	gtk_widget_class_bind_template_child (widget_class, CcWacomPage, tablet_aspect_ratio);
	gtk_widget_class_bind_template_child (widget_class, CcWacomPage, tablet_aspect_ratio_switch);
	gtk_widget_class_bind_template_child (widget_class, CcWacomPage, display_section);

	gtk_widget_class_bind_template_callback (widget_class, on_map_buttons_activated);
	gtk_widget_class_bind_template_callback (widget_class, on_calibrate_activated);
	gtk_widget_class_bind_template_callback (widget_class, on_display_selected);
}

static void
update_displays_model (CcWacomPage *page)
{
	g_autoptr (GtkStringList) list = NULL;
	GnomeRROutput **outputs, *cur_output;
	int i, idx = 0, cur = -1, automatic_item = -1;
	g_autoptr (GObject) obj = NULL;
	GVariant *variant;

	outputs = gnome_rr_screen_list_outputs (page->rr_screen);
	list = gtk_string_list_new (NULL);
	cur_output = cc_wacom_device_get_output (page->stylus,
						 page->rr_screen);

	for (i = 0; outputs[i] != NULL; i++) {
		GnomeRROutput *output = outputs[i];
		GnomeRRCrtc *crtc = gnome_rr_output_get_crtc (output);
		g_autofree gchar *text = NULL;
		g_autofree gchar *vendor = NULL;
		g_autofree gchar *product = NULL;
		g_autofree gchar *serial = NULL;
		const gchar *name, *disp_name;

		/* Output is turned on? */
		if (!crtc || gnome_rr_crtc_get_current_mode (crtc) == NULL)
			continue;

		if (output == cur_output)
			cur = idx;

		name = gnome_rr_output_get_name (output);
		disp_name = gnome_rr_output_get_display_name (output);
		text = g_strdup_printf ("%s (%s)", name, disp_name);

		gnome_rr_output_get_ids_from_edid (output,
						   &vendor,
						   &product,
						   &serial);
		variant = g_variant_new_strv ((const gchar *[]) { vendor, product, serial }, 3);

		gtk_string_list_append (list, text);
		obj = g_list_model_get_item (G_LIST_MODEL (list), idx);
		g_object_set_data_full (G_OBJECT (obj), "value-output",
					variant, (GDestroyNotify) g_variant_unref);
		idx++;
	}

	/* All displays item */
	gtk_string_list_append (list, _("All Displays"));
	variant = g_variant_new_strv ((const gchar *[]) { "", "", "" }, 3);
	obj = g_list_model_get_item (G_LIST_MODEL (list), idx);
	g_object_set_data_full (G_OBJECT (obj), "value-output",
				variant, (GDestroyNotify) g_variant_unref);
	if (cur_output == NULL)
		cur = idx;

	/* "Automatic" item */
	if (get_layout_type (page->stylus) == LAYOUT_SCREEN) {
		g_autoptr (GVariant) user_value = NULL;

		idx++;
		gtk_string_list_append (list, _("Automatic"));
		automatic_item = idx;

		user_value = g_settings_get_user_value (page->wacom_settings, "output");
		if (!user_value)
			cur = idx;
	}

	g_signal_handlers_block_by_func (page->tablet_display, on_display_selected, page);
	adw_combo_row_set_model (ADW_COMBO_ROW (page->tablet_display), G_LIST_MODEL (list));
	adw_combo_row_set_selected (ADW_COMBO_ROW (page->tablet_display), cur);
	g_signal_handlers_unblock_by_func (page->tablet_display, on_display_selected, page);

	gtk_widget_set_sensitive (page->tablet_calibrate, cur == automatic_item);
}

static void
cc_wacom_page_init (CcWacomPage *page)
{
	g_autoptr (GError) error = NULL;

	gtk_widget_init_template (GTK_WIDGET (page));
	page->rr_screen = gnome_rr_screen_new (gdk_display_get_default (), &error);

	if (error)
		g_warning ("Could not get RR screen: %s", error->message);

	g_signal_connect_object (page->rr_screen, "changed",
				 G_CALLBACK (update_displays_model),
				 page, G_CONNECT_SWAPPED);
}

static void
set_icon_name (CcWacomPage *page,
	       GtkWidget   *widget,
	       const char  *icon_name)
{
	g_autofree gchar *resource = NULL;

	resource = g_strdup_printf ("/org/gnome/control-center/wacom/%s.svg", icon_name);
	gtk_picture_set_resource (GTK_PICTURE (widget), resource);
}

static gboolean
has_monitor (CcWacomPage *page)
{
	WacomIntegrationFlags integration_flags;

	integration_flags = cc_wacom_device_get_integration_flags (page->stylus);

	return ((integration_flags &
		 (WACOM_DEVICE_INTEGRATED_DISPLAY | WACOM_DEVICE_INTEGRATED_SYSTEM)) != 0);
}

static void
update_pad_availability (CcWacomPage *page)
{
	gtk_widget_set_visible (page->tablet_map_buttons, page->pads != NULL);
}

static void
check_add_pad (CcWacomPage *page,
	       GsdDevice   *gsd_device)
{
	g_autoptr(CcWacomDevice) wacom_device = NULL;

	if ((gsd_device_get_device_type (gsd_device) & GSD_DEVICE_TYPE_PAD) == 0)
		return;

	if (!gsd_device_shares_group (cc_wacom_device_get_device (page->stylus),
				      gsd_device))
		return;

	wacom_device = cc_wacom_device_new (gsd_device);
	if (!wacom_device)
		return;

	page->pads = g_list_prepend (page->pads, g_steal_pointer (&wacom_device));
	update_pad_availability (page);
}

static void
check_remove_pad (CcWacomPage *page,
		  GsdDevice   *gsd_device)
{
	GList *l;

	if ((gsd_device_get_device_type (gsd_device) & GSD_DEVICE_TYPE_PAD) == 0)
		return;

	for (l = page->pads; l; l = l->next) {
		CcWacomDevice *wacom_device = l->data;
		if (cc_wacom_device_get_device (wacom_device) == gsd_device) {
			page->pads = g_list_delete_link (page->pads, l);
			g_object_unref (wacom_device);
		}
	}

	update_pad_availability (page);
}

static GVariant *
tablet_mode_bind_set (const GValue       *value,
		      const GVariantType *expected_type,
		      gpointer            user_data)
{
	gboolean setting;

	setting = g_value_get_boolean (value);

	return g_variant_new_string (setting ? "absolute" : "relative");
}

static gboolean
tablet_mode_bind_get (GValue   *value,
		      GVariant *variant,
		      gpointer  user_data)
{
	g_value_set_boolean (value,
			     g_strcmp0 (g_variant_get_string (variant, NULL),
					"absolute") == 0);
	return TRUE;
}

GtkWidget *
cc_wacom_page_new (CcWacomPanel  *panel,
		   CcWacomDevice *stylus)
{
	g_autoptr (GList) pads = NULL;
	CcWacomPage *page;
	GList *l;

	g_return_val_if_fail (CC_IS_WACOM_DEVICE (stylus), NULL);

	page = g_object_new (CC_TYPE_WACOM_PAGE, NULL);

	page->panel = panel;
	page->stylus = stylus;

	gtk_widget_set_visible (page->tablet_left_handed,
				get_layout_type (stylus) == LAYOUT_REVERSIBLE);
	gtk_widget_set_visible (page->tablet_calibrate,
				get_layout_type (stylus) == LAYOUT_SCREEN);

	/* FIXME move this to construct */
	page->wacom_settings  = cc_wacom_device_get_settings (stylus);

	/* Tablet name */
	adw_preferences_group_set_title (ADW_PREFERENCES_GROUP (page->tablet_section),
					 cc_wacom_device_get_name (stylus));
	adw_preferences_group_set_description (ADW_PREFERENCES_GROUP (page->tablet_section),
					       cc_wacom_device_get_description (stylus));

	g_settings_bind_with_mapping (page->wacom_settings, "mapping",
				      page->tablet_mode_switch, "active",
				      G_SETTINGS_BIND_DEFAULT,
				      tablet_mode_bind_get,
				      tablet_mode_bind_set,
				      NULL, NULL);
	g_settings_bind_with_mapping (page->wacom_settings, "mapping",
				      page->display_section, "sensitive",
				      G_SETTINGS_BIND_DEFAULT,
				      tablet_mode_bind_get,
				      tablet_mode_bind_set,
				      NULL, NULL);
	g_settings_bind (page->wacom_settings, "left-handed",
			 page->tablet_left_handed_switch, "active",
			 G_SETTINGS_BIND_DEFAULT);
	g_settings_bind (page->wacom_settings, "keep-aspect",
			 page->tablet_aspect_ratio_switch, "active",
			 G_SETTINGS_BIND_DEFAULT);

	/* Tablet icon */
	set_icon_name (page, page->tablet_icon, cc_wacom_device_get_icon_name (stylus));

	/* Listen to changes in related/paired pads */
	page->manager = gsd_device_manager_get ();
	g_signal_connect_object (G_OBJECT (page->manager), "device-added",
				 G_CALLBACK (check_add_pad), page,
				 G_CONNECT_SWAPPED);
	g_signal_connect_object (G_OBJECT (page->manager), "device-removed",
				 G_CALLBACK (check_remove_pad), page,
				 G_CONNECT_SWAPPED);

	pads = gsd_device_manager_list_devices (page->manager, GSD_DEVICE_TYPE_PAD);
	for (l = pads; l ; l = l->next)
		check_add_pad (page, l->data);

	update_pad_availability (page);
	update_displays_model (page);

	return GTK_WIDGET (page);
}

void
cc_wacom_page_calibrate (CcWacomPage *page)
{
	g_return_if_fail (CC_IS_WACOM_PAGE (page));

	calibrate (page);
}

gboolean
cc_wacom_page_can_calibrate (CcWacomPage *page)
{
	g_return_val_if_fail (CC_IS_WACOM_PAGE (page),
			      FALSE);

	return has_monitor (page);
}