summaryrefslogtreecommitdiffstats
path: root/panels/wacom/cc-wacom-stylus-page.c
blob: 2fc6c5adb3bf94b119ff3edd269d134262b1914d (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
/*
 * 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>

#include <glib/gi18n.h>
#include "cc-wacom-stylus-page.h"
#include "cc-wacom-nav-button.h"
#include <gtk/gtk.h>
#include <gdesktop-enums.h>

#include <string.h>

#define WID(x) (GtkWidget *) gtk_builder_get_object (page->builder, x)
#define CWID(x) (GtkContainer *) gtk_builder_get_object (page->builder, x)

struct _CcWacomStylusPage
{
	GtkBox          parent_instance;

	CcWacomTool    *stylus;
	GtkBuilder     *builder;
	GtkWidget      *nav;
	GSettings      *stylus_settings;
};

G_DEFINE_TYPE (CcWacomStylusPage, cc_wacom_stylus_page, GTK_TYPE_BOX)

/* Button combo box storage columns */
enum {
	BUTTONNUMBER_COLUMN,
	BUTTONNAME_COLUMN,
	N_BUTTONCOLUMNS
};

/* GSettings stores pressurecurve as 4 values like the driver. We map slider
 * scale to these values given the array below. These settings were taken from
 * wacomcpl, where they've been around for years.
 */
#define N_PRESSURE_CURVES 7
static const gint32 PRESSURE_CURVES[N_PRESSURE_CURVES][4] = {
		{	0,	75,	25,	100	},	/* soft */
		{	0,	50,	50,	100	},
		{	0,	25,	75,	100	},
		{	0,	0,	100,	100	},	/* neutral */
		{	25,	0,	100,	75	},
		{	50,	0,	100,	50	},
		{	75,	0,	100,	25	}	/* firm */
};

static void
set_pressurecurve (GtkRange *range, GSettings *settings, const gchar *key)
{
	gint		slider_val = gtk_range_get_value (range);
	GVariant	*values[4],
			*array;
	int		i;

	for (i = 0; i < G_N_ELEMENTS (values); i++)
		values[i] = g_variant_new_int32 (PRESSURE_CURVES[slider_val][i]);

	array = g_variant_new_array (G_VARIANT_TYPE_INT32, values, G_N_ELEMENTS (values));

	g_settings_set_value (settings, key, array);
}

static void
tip_feel_value_changed_cb (CcWacomStylusPage *page)
{
	set_pressurecurve (GTK_RANGE (WID ("scale-tip-feel")), page->stylus_settings, "pressure-curve");
}

static void
eraser_feel_value_changed_cb (CcWacomStylusPage *page)
{
	set_pressurecurve (GTK_RANGE (WID ("scale-eraser-feel")), page->stylus_settings, "eraser-pressure-curve");
}

static void
set_feel_from_gsettings (GtkAdjustment *adjustment, GSettings *settings, const gchar *key)
{
	GVariant	*variant;
	const gint32	*values;
	gsize		nvalues;
	int		i;

	variant = g_settings_get_value (settings, key);
	values = g_variant_get_fixed_array (variant, &nvalues, sizeof (gint32));

	if (nvalues != 4) {
		g_warning ("Invalid pressure curve format, expected 4 values (got %"G_GSIZE_FORMAT")", nvalues);
		return;
	}

	for (i = 0; i < N_PRESSURE_CURVES; i++) {
		if (memcmp (PRESSURE_CURVES[i], values, sizeof (gint32) * 4) == 0) {
			gtk_adjustment_set_value (adjustment, i);
			break;
		}
	}
}

static void
set_button_mapping_from_gsettings (GtkComboBox *combo, GSettings* settings, const gchar *key)
{
	GDesktopStylusButtonAction action;
	GtkTreeModel	*model;
	GtkTreeIter	 iter;
	gboolean	 valid;

	action = g_settings_get_enum (settings, key);
	model = gtk_combo_box_get_model (combo);
	valid = gtk_tree_model_get_iter_first (model, &iter);

	while (valid) {
		gint button;

		gtk_tree_model_get (model, &iter,
				    BUTTONNUMBER_COLUMN, &button,
				    -1);

		/* Currently button values match logical X buttons. If we
		 * introduce things like double-click, this code must
		 * change. Recommendation: use negative buttons numbers for
		 * special ones.
		 */

		if (button == action) {
			gtk_combo_box_set_active_iter (combo, &iter);
			break;
		}

		valid = gtk_tree_model_iter_next (model, &iter);
	}
}

static void
button_changed_cb (CcWacomStylusPage *page)
{
	GtkTreeIter		iter;
	GtkListStore		*liststore;
	gint			mapping_b2,
				mapping_b3,
				mapping_b4;

	if (!gtk_combo_box_get_active_iter (GTK_COMBO_BOX (WID ("combo-bottombutton")), &iter))
		return;

	liststore = GTK_LIST_STORE (WID ("liststore-buttons"));
	gtk_tree_model_get (GTK_TREE_MODEL (liststore), &iter,
			    BUTTONNUMBER_COLUMN, &mapping_b2,
			    -1);

	if (cc_wacom_tool_get_num_buttons (page->stylus) > 1) {
		if (!gtk_combo_box_get_active_iter (GTK_COMBO_BOX (WID ("combo-topbutton")), &iter))
			return;

		gtk_tree_model_get (GTK_TREE_MODEL (liststore), &iter,
				    BUTTONNUMBER_COLUMN, &mapping_b3,
				    -1);
	} else {
		mapping_b3 = 0;
	}

	if (cc_wacom_tool_get_num_buttons (page->stylus) > 2) {
		if (!gtk_combo_box_get_active_iter (GTK_COMBO_BOX (WID ("combo-thirdbutton")), &iter))
			return;

		gtk_tree_model_get (GTK_TREE_MODEL (liststore), &iter,
				    BUTTONNUMBER_COLUMN, &mapping_b4,
				    -1);
	} else {
		mapping_b4 = 0;
	}

	g_settings_set_enum (page->stylus_settings, "button-action", mapping_b2);
	g_settings_set_enum (page->stylus_settings, "secondary-button-action", mapping_b3);
	g_settings_set_enum (page->stylus_settings, "tertiary-button-action", mapping_b4);
}

static void
combobox_text_cellrenderer (GtkComboBox *combo, int name_column)
{
	GtkCellRenderer	*renderer;

	renderer = gtk_cell_renderer_text_new ();
	gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo), renderer, TRUE);
	gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo), renderer,
					"text", BUTTONNAME_COLUMN, NULL);
}

/* Boilerplate code goes below */

static void
cc_wacom_stylus_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_stylus_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_stylus_page_dispose (GObject *object)
{
	CcWacomStylusPage *page = CC_WACOM_STYLUS_PAGE (object);

	g_clear_object (&page->builder);

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

static void
cc_wacom_stylus_page_class_init (CcWacomStylusPageClass *klass)
{
	GObjectClass *object_class = G_OBJECT_CLASS (klass);

	object_class->get_property = cc_wacom_stylus_page_get_property;
	object_class->set_property = cc_wacom_stylus_page_set_property;
	object_class->dispose = cc_wacom_stylus_page_dispose;
}

static void
add_marks (GtkScale *scale)
{
	gint i;

	for (i = 0; i < N_PRESSURE_CURVES; i++)
		gtk_scale_add_mark (scale, i, GTK_POS_BOTTOM, NULL);
}

static void
cc_wacom_stylus_page_init (CcWacomStylusPage *page)
{
	g_autoptr(GError) error = NULL;
	GtkComboBox *combo;
	GtkWidget *box;
	char *objects[] = {
		"stylus-grid",
		"liststore-buttons",
		"adjustment-tip-feel",
		"adjustment-eraser-feel",
		NULL
	};

	page->builder = gtk_builder_new ();

	gtk_builder_add_objects_from_resource (page->builder,
                                               "/org/gnome/control-center/wacom/wacom-stylus-page.ui",
                                               objects,
                                               &error);
	if (error != NULL) {
		g_warning ("Error loading UI file: %s", error->message);
		return;
	}

	box = WID ("stylus-grid");
	gtk_container_add (GTK_CONTAINER (page), box);
	gtk_widget_set_vexpand (GTK_WIDGET (box), TRUE);

	add_marks (GTK_SCALE (WID ("scale-tip-feel")));
	add_marks (GTK_SCALE (WID ("scale-eraser-feel")));

	g_signal_connect_object (WID ("scale-tip-feel"), "value-changed",
                                 G_CALLBACK (tip_feel_value_changed_cb), page, G_CONNECT_SWAPPED);
	g_signal_connect_object (WID ("scale-eraser-feel"), "value-changed",
                                 G_CALLBACK (eraser_feel_value_changed_cb), page, G_CONNECT_SWAPPED);

	combo = GTK_COMBO_BOX (WID ("combo-topbutton"));
	combobox_text_cellrenderer (combo, BUTTONNAME_COLUMN);
	g_signal_connect_object (combo, "changed",
                                 G_CALLBACK (button_changed_cb), page, G_CONNECT_SWAPPED);

	combo = GTK_COMBO_BOX (WID ("combo-bottombutton"));
	combobox_text_cellrenderer (combo, BUTTONNAME_COLUMN);
	g_signal_connect_object (combo, "changed",
                                 G_CALLBACK (button_changed_cb), page, G_CONNECT_SWAPPED);

	combo = GTK_COMBO_BOX (WID ("combo-thirdbutton"));
	combobox_text_cellrenderer (combo, BUTTONNAME_COLUMN);
	g_signal_connect_object (G_OBJECT (combo), "changed",
                                 G_CALLBACK (button_changed_cb), page, G_CONNECT_SWAPPED);

	page->nav = cc_wacom_nav_button_new ();
        gtk_widget_set_halign (page->nav, GTK_ALIGN_END);
        gtk_widget_set_margin_start (page->nav, 10);
	gtk_widget_show (page->nav);
	gtk_container_add (CWID ("navigation-placeholder"), page->nav);
}

static void
set_icon_name (CcWacomStylusPage *page,
	       const char  *widget_name,
	       const char  *icon_name)
{
	g_autofree gchar *resource = NULL;

	resource = g_strdup_printf ("/org/gnome/control-center/wacom/%s.svg", icon_name);
	gtk_image_set_from_resource (GTK_IMAGE (WID (widget_name)), resource);
}

/* Different types of layout for the stylus config */
enum {
	LAYOUT_NORMAL,                      /* eraser, 2 buttons, tip */
	LAYOUT_INKING,                      /* tip */
	LAYOUT_AIRBRUSH,                    /* eraser, 1 button, tip */
	LAYOUT_GENERIC_2_BUTTONS_NO_ERASER, /* 2 buttons, tip, no eraser */
	LAYOUT_3DPEN,                       /* 3 buttons, tip, no eraser */
	LAYOUT_OTHER
};

static void
remove_buttons (CcWacomStylusPage *page, int n)
{
	if (n < 3) {
		gtk_widget_destroy (WID ("combo-thirdbutton"));
		gtk_widget_destroy (WID ("label-third-button"));
	}
	if (n < 2) {
		gtk_widget_destroy (WID ("combo-topbutton"));
		gtk_widget_destroy (WID ("label-top-button"));
		gtk_label_set_text (GTK_LABEL (WID ("label-lower-button")), _("Button"));
	}
	if (n < 1) {
		gtk_widget_destroy (WID ("combo-bottombutton"));
		gtk_widget_destroy (WID ("label-lower-button"));
	}
}

static void
remove_eraser (CcWacomStylusPage *page)
{
	gtk_widget_destroy (WID ("eraser-box"));
	gtk_widget_destroy (WID ("label-eraser-feel"));
}

static void
update_stylus_ui (CcWacomStylusPage *page,
		  int                layout)
{
	switch (layout) {
	case LAYOUT_NORMAL:
		remove_buttons (page, 2);
		break;
	case LAYOUT_INKING:
		remove_buttons (page, 0);
		remove_eraser (page);
		gtk_container_child_set (CWID ("stylus-controls-grid"),
					 WID ("label-tip-feel"),
					 "top_attach", 0, NULL);
		gtk_container_child_set (CWID ("stylus-controls-grid"),
					 WID ("box-tip-feel"),
					 "top_attach", 0, NULL);
		break;
	case LAYOUT_AIRBRUSH:
		remove_buttons (page, 1);
		gtk_container_child_set (CWID ("stylus-controls-grid"),
					 WID ("label-lower-button"),
					 "top_attach", 1, NULL);
		gtk_container_child_set (CWID ("stylus-controls-grid"),
					 WID ("combo-bottombutton"),
					 "top_attach", 1, NULL);
		gtk_container_child_set (CWID ("stylus-controls-grid"),
					 WID ("label-tip-feel"),
					 "top_attach", 2, NULL);
		gtk_container_child_set (CWID ("stylus-controls-grid"),
					 WID ("box-tip-feel"),
					 "top_attach", 2, NULL);
		break;
	case LAYOUT_GENERIC_2_BUTTONS_NO_ERASER:
		remove_buttons (page, 2);
		remove_eraser (page);
		break;
	case LAYOUT_3DPEN:
		remove_buttons (page, 3);
		remove_eraser (page);
		break;
	case LAYOUT_OTHER:
		/* We already warn about it in cc_wacom_stylus_page_new () */
		break;
	}
}

GtkWidget *
cc_wacom_stylus_page_new (CcWacomTool *stylus)
{
	CcWacomStylusPage *page;
	guint num_buttons;
	int layout;
	gboolean has_eraser;

	g_return_val_if_fail (CC_IS_WACOM_TOOL (stylus), NULL);

	page = g_object_new (CC_TYPE_WACOM_STYLUS_PAGE, NULL);

	page->stylus = stylus;

	/* Icon */
	set_icon_name (page, "image-stylus", cc_wacom_tool_get_icon_name (stylus));

	/* Settings */
	page->stylus_settings = cc_wacom_tool_get_settings (stylus);
	has_eraser = cc_wacom_tool_get_has_eraser (stylus);

	/* Stylus name */
	gtk_label_set_text (GTK_LABEL (WID ("label-stylus")), cc_wacom_tool_get_name (stylus));

	num_buttons = cc_wacom_tool_get_num_buttons (stylus);
	if (num_buttons == 0 && !has_eraser)
		layout = LAYOUT_INKING;
	else if (num_buttons == 2 && has_eraser)
		layout = LAYOUT_NORMAL;
	else if (num_buttons == 1 && has_eraser)
		layout = LAYOUT_AIRBRUSH;
	else if (num_buttons == 2 && !has_eraser)
		layout = LAYOUT_GENERIC_2_BUTTONS_NO_ERASER;
	else if (num_buttons == 3 && !has_eraser)
		layout = LAYOUT_3DPEN;
	else {
		layout = LAYOUT_OTHER;
		remove_buttons (page, num_buttons);

		/* Gray out eraser if not available */
		gtk_widget_set_sensitive (WID ("eraser-box"), has_eraser);
		gtk_widget_set_sensitive (WID ("label-eraser-feel"), has_eraser);

		g_warning ("The layout of this page is not known, %d buttons, %s eraser",
			   num_buttons, has_eraser ? "with" : "without");
	}

	update_stylus_ui (page, layout);

	if (num_buttons >= 3)
		set_button_mapping_from_gsettings (GTK_COMBO_BOX (WID ("combo-thirdbutton")),
						   page->stylus_settings, "tertiary-button-action");
	if (num_buttons >= 2)
		set_button_mapping_from_gsettings (GTK_COMBO_BOX (WID ("combo-topbutton")),
						   page->stylus_settings, "secondary-button-action");
	if (num_buttons >= 1)
		set_button_mapping_from_gsettings (GTK_COMBO_BOX (WID ("combo-bottombutton")),
						   page->stylus_settings, "button-action");
	set_feel_from_gsettings (GTK_ADJUSTMENT (WID ("adjustment-tip-feel")),
				 page->stylus_settings, "pressure-curve");

	if (has_eraser)
		set_feel_from_gsettings (GTK_ADJUSTMENT (WID ("adjustment-eraser-feel")),
					 page->stylus_settings, "eraser-pressure-curve");

	return GTK_WIDGET (page);
}

CcWacomTool *
cc_wacom_stylus_page_get_tool (CcWacomStylusPage *page)
{
	return page->stylus;
}

void
cc_wacom_stylus_page_set_navigation (CcWacomStylusPage *page,
				     GtkNotebook *notebook)
{
	g_return_if_fail (CC_IS_WACOM_STYLUS_PAGE (page));

	g_object_set (G_OBJECT (page->nav),
		      "notebook", notebook,
		      "ignore-first", TRUE,
		      NULL);
}