cc-wacom-page.c 38.2 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
/*
 * 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, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 * Authors: Peter Hutterer <peter.hutterer@redhat.com>
 *          Bastien Nocera <hadess@hadess.net>
 *
 */

#include <config.h>

25
26
27
#include <glib/gi18n-lib.h>
#include <gtk/gtk.h>

28
#include "cc-wacom-page.h"
29
#include "cc-wacom-nav-button.h"
30
#include "cc-wacom-mapping-panel.h"
31
#include "cc-wacom-stylus-page.h"
32
#include "gsd-enums.h"
33
#include "gui_gtk.h"
34
35
36
37

#include <string.h>

#define WID(x) (GtkWidget *) gtk_builder_get_object (priv->builder, x)
38
#define CWID(x) (GtkContainer *) gtk_builder_get_object (priv->builder, x)
39
#define MWID(x) (GtkWidget *) gtk_builder_get_object (priv->mapping_builder, x)
40
41
42
43
44
45

G_DEFINE_TYPE (CcWacomPage, cc_wacom_page, GTK_TYPE_BOX)

#define WACOM_PAGE_PRIVATE(o) \
  (G_TYPE_INSTANCE_GET_PRIVATE ((o), CC_TYPE_WACOM_PAGE, CcWacomPagePrivate))

46
47
48
#define THRESHOLD_MISCLICK	15
#define THRESHOLD_DOUBLECLICK	7

49
50
#define ACTION_TYPE_KEY         "action-type"
#define CUSTOM_ACTION_KEY       "custom-action"
51
#define KEY_CUSTOM_ELEVATOR_ACTION "custom-elevator-action"
52
53
54

enum {
	MAPPING_DESCRIPTION_COLUMN,
55
	MAPPING_TYPE_COLUMN,
56
	MAPPING_BUTTON_COLUMN,
57
	MAPPING_BUTTON_DIRECTION,
58
59
60
	MAPPING_N_COLUMNS
};

61
62
63
64
65
66
enum {
	ACTION_NAME_COLUMN,
	ACTION_TYPE_COLUMN,
	ACTION_N_COLUMNS
};

67
68
struct _CcWacomPagePrivate
{
69
	CcWacomPanel   *panel;
70
	GsdWacomDevice *stylus, *eraser, *pad;
71
	GtkBuilder     *builder;
72
	GtkWidget      *nav;
73
	GtkWidget      *notebook;
74
	CalibArea      *area;
75
	GSettings      *wacom_settings;
76
77

	/* Button mapping */
78
	GtkBuilder     *mapping_builder;
79
	GtkWidget      *button_map;
80
	GtkListStore   *action_store;
81
82
83
84

	/* Display mapping */
	GtkWidget      *mapping;
	GtkWidget      *dialog;
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
};

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

/* Tablet mode combo box storage columns */
enum {
	MODENUMBER_COLUMN,
	MODELABEL_COLUMN,
	N_MODECOLUMNS
};

/* Tablet mode options - keep in sync with .ui */
enum {
	MODE_ABSOLUTE, /* stylus + eraser absolute */
	MODE_RELATIVE, /* stylus + eraser relative */
};

107
108
109
110
111
112
113
/* 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 */
};

114
115
116
117
static struct {
       GsdWacomActionType  action_type;
       const gchar        *action_name;
} action_table[] = {
118
119
       { GSD_WACOM_ACTION_TYPE_NONE,           NC_("Wacom action-type", "None")                },
       { GSD_WACOM_ACTION_TYPE_CUSTOM,         NC_("Wacom action-type", "Send Keystroke")      },
120
121
       { GSD_WACOM_ACTION_TYPE_SWITCH_MONITOR, NC_("Wacom action-type", "Switch Monitor")      },
       { GSD_WACOM_ACTION_TYPE_HELP,           NC_("Wacom action-type", "Show On-Screen Help") }
122
123
};

124
125
#define WACOM_C(x) g_dpgettext2(NULL, "Wacom action-type", x)

126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
static void
update_tablet_ui (CcWacomPage *page,
		  int          layout);

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

	if (gsd_wacom_device_is_screen_tablet (device))
		layout = LAYOUT_SCREEN;
	else if (gsd_wacom_device_reversible (device))
		layout = LAYOUT_REVERSIBLE;
	else
		layout = LAYOUT_NORMAL;

	return layout;
}

145
146
147
148
149
150
151
152
153
154
155
156
static void
set_calibration (gint      *cal,
                 gsize      ncal,
                 GSettings *settings)
{
	GVariant    *current; /* current calibration */
	GVariant    *array;   /* new calibration */
	GVariant   **tmp;
	gsize        nvalues;
	int          i;

	current = g_settings_get_value (settings, "area");
Bastien Nocera's avatar
Bastien Nocera committed
157
	g_variant_get_fixed_array (current, &nvalues, sizeof (gint32));
158
	if ((ncal != 4) || (nvalues != 4)) {
Bastien Nocera's avatar
Bastien Nocera committed
159
		g_warning("Unable set set device calibration property. Got %"G_GSIZE_FORMAT" items to put in %"G_GSIZE_FORMAT" slots; expected %d items.\n", ncal, nvalues, 4);
160
161
162
163
164
165
166
167
168
169
170
171
172
		return;
	}

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

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

	g_free (tmp);
}

173
174
175
static void
finish_calibration (CalibArea *area,
		    gpointer   user_data)
176
{
177
178
	CcWacomPage *page = (CcWacomPage *) user_data;
	CcWacomPagePrivate *priv = page->priv;
Bastien Nocera's avatar
Bastien Nocera committed
179
	XYinfo axis;
180
	gboolean swap_xy;
181
	int cal[4];
182

183
	if (calib_area_finish (area, &axis, &swap_xy)) {
Bastien Nocera's avatar
Bastien Nocera committed
184
185
186
187
		cal[0] = axis.x_min;
		cal[1] = axis.y_min;
		cal[2] = axis.x_max;
		cal[3] = axis.y_max;
188
189

		set_calibration(cal, 4, page->priv->wacom_settings);
190
191
	}

192
193
194
195
196
197
198
	calib_area_free (area);
	page->priv->area = NULL;
	gtk_widget_set_sensitive (WID ("button-calibrate"), TRUE);
}

static gboolean
run_calibration (CcWacomPage *page,
199
200
		 gint        *cal,
		 gint         monitor)
201
{
202
203
204
205
	XYinfo              old_axis;
	GdkDevice          *gdk_device;
	CcWacomPagePrivate *priv;
	int                 device_id;
206
207
208
209
210
211
212
213

	g_assert (page->priv->area == NULL);

	old_axis.x_min = cal[0];
	old_axis.y_min = cal[1];
	old_axis.x_max = cal[2];
	old_axis.y_max = cal[3];

214
215
216
217
218
219
220
221
	priv = page->priv;
	g_object_get (priv->stylus, "gdk-device", &gdk_device, NULL);

	if (gdk_device != NULL)
		g_object_get (gdk_device, "device-id", &device_id, NULL);
	else
		device_id = -1;

222
	page->priv->area = calib_area_new (NULL,
223
					   monitor,
224
					   device_id,
225
226
227
					   finish_calibration,
					   page,
					   &old_axis,
228
229
					   THRESHOLD_MISCLICK,
					   THRESHOLD_DOUBLECLICK);
230

231
	return FALSE;
232
233
234
}

static void
235
236
calibrate_button_clicked_cb (GtkButton   *button,
			     CcWacomPage *page)
237
{
Bastien Nocera's avatar
Bastien Nocera committed
238
	int i, calibration[4];
239
	GVariant *variant;
Bastien Nocera's avatar
Bastien Nocera committed
240
	int *current;
241
	gsize ncal;
242
243
244
245
246
247
248
249
250
251
	gint monitor;

	monitor = gsd_wacom_device_get_display_monitor (page->priv->stylus);
	if (monitor < 0) {
		/* 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;
	}
252
253
254

	variant = g_settings_get_value (page->priv->wacom_settings, "area");
	current = (int *) g_variant_get_fixed_array (variant, &ncal, sizeof (gint32));
Bastien Nocera's avatar
Bastien Nocera committed
255

256
257
	if (ncal != 4) {
		g_warning("Device calibration property has wrong length. Got %"G_GSIZE_FORMAT" items; expected %d.\n", ncal, 4);
Bastien Nocera's avatar
Bastien Nocera committed
258
259
260
		g_free (current);
		return;
	}
261

Bastien Nocera's avatar
Bastien Nocera committed
262
263
264
265
266
267
268
269
	for (i = 0; i < 4; i++)
		calibration[i] = current[i];

	if (calibration[0] == -1 &&
	    calibration[1] == -1 &&
	    calibration[2] == -1 &&
	    calibration[3] == -1) {
		gint *device_cal;
270
		device_cal = gsd_wacom_device_get_area (page->priv->stylus);
Bastien Nocera's avatar
Bastien Nocera committed
271
272
273
274
275
		for (i = 0; i < 4; i++)
			calibration[i] = device_cal[i];
		g_free (device_cal);
	}

276
	run_calibration (page, calibration, monitor);
277
	gtk_widget_set_sensitive (GTK_WIDGET (button), FALSE);
Bastien Nocera's avatar
Bastien Nocera committed
278
}
279

280
281
282
283
284
285
286
287
288
289
290
/* 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 (GsdWacomActionType type)
{
	if (type >= G_N_ELEMENTS(action_table))
		return FALSE;
	return TRUE;
}

291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
static char *
get_elevator_shortcut_string (GSettings        *settings,
			      GtkDirectionType  dir)
{
	char **strv, *str;

	strv = g_settings_get_strv (settings, KEY_CUSTOM_ELEVATOR_ACTION);
	if (strv == NULL)
		return NULL;

	if (g_strv_length (strv) >= 1 && dir == GTK_DIR_UP)
		str = g_strdup (strv[0]);
	else if (g_strv_length (strv) >= 2 && dir == GTK_DIR_DOWN)
		str = g_strdup (strv[1]);
	else
		str = NULL;

	g_strfreev (strv);

	return str;
}

313
314
315
316
317
318
319
320
321
static void
accel_set_func (GtkTreeViewColumn *tree_column,
		GtkCellRenderer   *cell,
		GtkTreeModel      *model,
		GtkTreeIter       *iter,
		gpointer           data)
{
	GsdWacomTabletButton *button;
	GsdWacomActionType type;
322
	GtkDirectionType dir;
323
324
325
326
327
328
	char *str;
	guint keyval;
	guint mask;

	gtk_tree_model_get (model, iter,
			    MAPPING_BUTTON_COLUMN, &button,
329
			    MAPPING_BUTTON_DIRECTION, &dir,
330
331
332
333
334
335
336
337
338
339
			    -1);

	if (button == NULL) {
		g_object_set (cell,
			      "visible", FALSE,
			      NULL);
		return;
	}

	if (button->type == WACOM_TABLET_BUTTON_TYPE_HARDCODED) {
Bastien Nocera's avatar
Bastien Nocera committed
340
341
		/* FIXME this should list the name of the button,
		 * Switch Modes Touchring #1 for example */
342
343
344
345
346
347
		g_object_set (cell,
			      "visible", TRUE,
			      "editable", FALSE,
			      "accel-key", 0,
			      "accel-mods", 0,
			      "style", PANGO_STYLE_NORMAL,
348
			      "text", "",
349
350
351
352
353
354
355
356
357
358
			      NULL);
		return;
	}

	if (button->settings == NULL) {
		g_warning ("Button '%s' does not have an associated GSettings", button->id);
		return;
	}

	type = g_settings_get_enum (button->settings, ACTION_TYPE_KEY);
359
	if (type != GSD_WACOM_ACTION_TYPE_CUSTOM) {
360
361
362
363
364
365
		g_object_set (cell,
			      "visible", TRUE,
			      "editable", TRUE,
			      "accel-key", 0,
			      "accel-mods", 0,
			      "style", PANGO_STYLE_NORMAL,
366
			      "text", "",
367
368
369
370
			      NULL);
		return;
	}

371
372
	if (button->type == WACOM_TABLET_BUTTON_TYPE_STRIP ||
	    button->type == WACOM_TABLET_BUTTON_TYPE_RING)
373
374
375
376
377
378
379
380
381
382
383
		str = get_elevator_shortcut_string (button->settings, dir);
	else
		str = g_settings_get_string (button->settings, CUSTOM_ACTION_KEY);

	if (str == NULL || *str == '\0') {
		g_object_set (cell,
			      "visible", TRUE,
			      "editable", TRUE,
			      "accel-key", 0,
			      "accel-mods", 0,
			      "style", PANGO_STYLE_NORMAL,
384
			      "text", C_("Wacom action-type", "None"),
385
386
387
388
			      NULL);
		g_free (str);
		return;
	}
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
	gtk_accelerator_parse (str, &keyval, &mask);
	g_free (str);

	g_object_set (cell,
		      "visible", TRUE,
		      "editable", TRUE,
		      "accel-key", keyval,
		      "accel-mods", mask,
		      "style", PANGO_STYLE_NORMAL,
		      NULL);
}

static gboolean
start_editing_cb (GtkTreeView    *tree_view,
		  GdkEventButton *event,
		  gpointer        user_data)
{
	GtkTreePath *path;
	GtkTreeViewColumn *column;
408
	gboolean handled;
409
410
411
412

	if (event->window != gtk_tree_view_get_bin_window (tree_view))
		return FALSE;

413
	handled = FALSE;
414
415
416
417
418
419
420
421
422
	if (gtk_tree_view_get_path_at_pos (tree_view,
					   (gint) event->x,
					   (gint) event->y,
					   &path, &column,
					   NULL, NULL))
	{
		GtkTreeModel *model;
		GtkTreeIter iter;
		GsdWacomTabletButton *button;
423
424
425
426
		GsdWacomActionType type;

		if (column == gtk_tree_view_get_column (tree_view, MAPPING_TYPE_COLUMN))
			goto out;
427
428
429
430
431
432

		model = gtk_tree_view_get_model (tree_view);
		gtk_tree_model_get_iter (model, &iter, path);
		gtk_tree_model_get (model, &iter,
				    MAPPING_BUTTON_COLUMN, &button,
				    -1);
433
434
435
436
437
438
439
440
441
		if (button == NULL)
			goto out;

		if (button->settings == NULL)
			goto out;

		type = g_settings_get_enum (button->settings, ACTION_TYPE_KEY);
		if (type != GSD_WACOM_ACTION_TYPE_CUSTOM)
			goto out;
442

443
444
445
		gtk_widget_grab_focus (GTK_WIDGET (tree_view));
		gtk_tree_view_set_cursor (tree_view,
					  path,
446
					  gtk_tree_view_get_column (tree_view, MAPPING_BUTTON_COLUMN),
447
					  TRUE);
448
		g_signal_stop_emission_by_name (tree_view, "button_press_event");
449
450
		handled = TRUE;
out:
451
		gtk_tree_path_free (path);
452
	}
453
	return handled;
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
}

static void
start_editing_kb_cb (GtkTreeView *treeview,
                          GtkTreePath *path,
                          GtkTreeViewColumn *column,
                          gpointer user_data)
{
  GtkTreeModel *model;
  GtkTreeIter iter;
  GsdWacomTabletButton *button;

  model = gtk_tree_view_get_model (treeview);
  gtk_tree_model_get_iter (model, &iter, path);
  gtk_tree_model_get (model, &iter,
                      MAPPING_BUTTON_COLUMN, &button,
                      -1);

  gtk_widget_grab_focus (GTK_WIDGET (treeview));
  gtk_tree_view_set_cursor (treeview,
			    path,
475
			    gtk_tree_view_get_column (treeview, MAPPING_BUTTON_COLUMN),
476
477
478
479
480
481
482
483
484
			    TRUE);
}

static void
accel_edited_callback (GtkCellRendererText   *cell,
                       const char            *path_string,
                       guint                  keyval,
                       GdkModifierType        mask,
                       guint                  keycode,
Bastien Nocera's avatar
Bastien Nocera committed
485
                       CcWacomPage           *page)
486
487
488
{
  GtkTreeModel *model;
  GtkTreePath *path = gtk_tree_path_new_from_string (path_string);
Bastien Nocera's avatar
Bastien Nocera committed
489
  GtkTreeView *view;
490
  GtkTreeIter iter;
Bastien Nocera's avatar
Bastien Nocera committed
491
  CcWacomPagePrivate *priv;
492
  GsdWacomTabletButton *button;
493
  GtkDirectionType dir;
494
495
  char *str;

Bastien Nocera's avatar
Bastien Nocera committed
496
497
  priv = page->priv;
  view = GTK_TREE_VIEW (MWID("shortcut_treeview"));
498
499
500
501
502
  model = gtk_tree_view_get_model (view);
  gtk_tree_model_get_iter (model, &iter, path);
  gtk_tree_path_free (path);
  gtk_tree_model_get (model, &iter,
		      MAPPING_BUTTON_COLUMN, &button,
503
		      MAPPING_BUTTON_DIRECTION, &dir,
504
505
506
507
508
509
510
511
512
513
                      -1);

  /* sanity check */
  if (button == NULL)
    return;

  /* CapsLock isn't supported as a keybinding modifier, so keep it from confusing us */
  mask &= ~GDK_LOCK_MASK;

  str = gtk_accelerator_name (keyval, mask);
514

515
516
  if (button->type == WACOM_TABLET_BUTTON_TYPE_STRIP ||
      button->type == WACOM_TABLET_BUTTON_TYPE_RING) {
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
    char *strs[3];
    char **strv;

    strs[2] = NULL;
    strs[0] = strs[1] = "";
    strv = g_settings_get_strv (button->settings, KEY_CUSTOM_ELEVATOR_ACTION);
    if (strv != NULL) {
	    if (g_strv_length (strv) >= 1)
		    strs[0] = strv[0];
	    if (g_strv_length (strv) >= 2)
		    strs[1] = strv[1];
    }

    if (dir == GTK_DIR_UP)
	    strs[0] = str;
    else
	    strs[1] = str;

    g_settings_set_strv (button->settings, KEY_CUSTOM_ELEVATOR_ACTION, (const gchar * const*) strs);
    if (strv != NULL)
	    g_strfreev (strv);
  } else {
    g_settings_set_string (button->settings, CUSTOM_ACTION_KEY, str);
  }
541
542
543
544
545
546
547
  g_settings_set_enum (button->settings, ACTION_TYPE_KEY, GSD_WACOM_ACTION_TYPE_CUSTOM);
  g_free (str);
}

static void
accel_cleared_callback (GtkCellRendererText *cell,
                        const char          *path_string,
Bastien Nocera's avatar
Bastien Nocera committed
548
                        CcWacomPage         *page)
549
{
Bastien Nocera's avatar
Bastien Nocera committed
550
  GtkTreeView *view;
551
552
553
554
  GtkTreePath *path = gtk_tree_path_new_from_string (path_string);
  GtkTreeIter iter;
  GtkTreeModel *model;
  GsdWacomTabletButton *button;
Bastien Nocera's avatar
Bastien Nocera committed
555
  CcWacomPagePrivate *priv;
556
  GtkDirectionType dir;
557

Bastien Nocera's avatar
Bastien Nocera committed
558
559
  priv = page->priv;
  view = GTK_TREE_VIEW (MWID("shortcut_treeview"));
560
561
562
563
564
  model = gtk_tree_view_get_model (view);
  gtk_tree_model_get_iter (model, &iter, path);
  gtk_tree_path_free (path);
  gtk_tree_model_get (model, &iter,
		      MAPPING_BUTTON_COLUMN, &button,
565
		      MAPPING_BUTTON_DIRECTION, &dir,
566
567
568
569
570
571
572
                      -1);

  /* sanity check */
  if (button == NULL)
    return;

  /* Unset the key */
573
574
  if (button->type == WACOM_TABLET_BUTTON_TYPE_STRIP ||
      button->type == WACOM_TABLET_BUTTON_TYPE_RING) {
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
    char *strs[3];
    char **strv;

    strs[2] = NULL;
    strs[0] = strs[1] = "";
    strv = g_settings_get_strv (button->settings, KEY_CUSTOM_ELEVATOR_ACTION);
    if (strv != NULL) {
	    if (g_strv_length (strv) >= 1)
		    strs[0] = strv[0];
	    if (g_strv_length (strv) >= 2)
		    strs[1] = strv[1];
    }

    if (dir == GTK_DIR_UP)
	    strs[0] = "";
    else
	    strs[1] = "";

    if (*strs[0] == '\0' && *strs[1] == '\0')
	    g_settings_set_enum (button->settings, ACTION_TYPE_KEY, GSD_WACOM_ACTION_TYPE_NONE);
    g_settings_set_strv (button->settings, KEY_CUSTOM_ELEVATOR_ACTION, (const gchar * const*) strs);
    if (strv != NULL)
	    g_strfreev (strv);
  } else {
	  g_settings_set_enum (button->settings, ACTION_TYPE_KEY, GSD_WACOM_ACTION_TYPE_NONE);
	  g_settings_set_string (button->settings, CUSTOM_ACTION_KEY, "");
  }
}

static void
add_button_to_store (GtkListStore         *model,
		     GsdWacomTabletButton *button,
607
608
		     GtkDirectionType      dir,
		     GsdWacomActionType    type)
609
610
611
612
613
{
	GtkTreeIter new_row;
	char *dir_name;

	if (dir == GTK_DIR_UP || dir == GTK_DIR_DOWN) {
614
615
616
		if (button->type == WACOM_TABLET_BUTTON_TYPE_RING) {
			dir_name = g_strdup_printf ("%s (%s)",
						    button->name,
617
						    dir == GTK_DIR_UP ? "↺" : "↻");
618
619
620
		} else {
			dir_name = g_strdup_printf ("%s (%s)",
						    button->name,
621
						    dir == GTK_DIR_UP ? C_("Wacom tablet button", "Up") : C_("Wacom tablet button", "Down"));
622
		}
623
624
625
626
	} else {
		dir_name = NULL;
	}

627
	if (action_type_is_valid (type) == FALSE)
628
629
		type = GSD_WACOM_ACTION_TYPE_NONE;

630
631
632
	gtk_list_store_append (model, &new_row);
	gtk_list_store_set (model, &new_row,
			    MAPPING_DESCRIPTION_COLUMN, dir_name ? dir_name : button->name,
633
			    MAPPING_TYPE_COLUMN, WACOM_C(action_table[type].action_name),
634
635
636
637
			    MAPPING_BUTTON_COLUMN, button,
			    MAPPING_BUTTON_DIRECTION, dir,
			    -1);
	g_free (dir_name);
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
static void
action_set_func (GtkTreeViewColumn *tree_column,
		 GtkCellRenderer   *cell,
		 GtkTreeModel      *model,
		 GtkTreeIter       *iter,
		 gpointer           data)
{
	GsdWacomTabletButton *button;
	GsdWacomActionType type;

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

	if (button == NULL) {
		g_object_set (cell, "visible", FALSE, NULL);
		return;
	}

	if (button->type == WACOM_TABLET_BUTTON_TYPE_HARDCODED) {
		g_object_set (cell,
			      "visible", TRUE,
			      "editable", FALSE,
			      "style", PANGO_STYLE_NORMAL,
			      "text", _("Switch Modes"),
			      NULL);
		return;
	}

667
668
	if (button->type == WACOM_TABLET_BUTTON_TYPE_STRIP ||
	    button->type == WACOM_TABLET_BUTTON_TYPE_RING) {
669
670
671
672
		g_object_set (cell,
			      "visible", TRUE,
			      "editable", FALSE,
			      "style", PANGO_STYLE_NORMAL,
673
			      "text", WACOM_C(action_table[GSD_WACOM_ACTION_TYPE_CUSTOM].action_name),
674
675
676
677
678
679
680
681
682
683
			      NULL);
		return;
	}

	if (button->settings == NULL) {
		g_warning ("Button '%s' does not have an associated GSettings", button->id);
		return;
	}

	type = g_settings_get_enum (button->settings, ACTION_TYPE_KEY);
684
	if (action_type_is_valid (type) == FALSE)
685
686
		type = GSD_WACOM_ACTION_TYPE_NONE;

687
688
689
690
	g_object_set (cell,
		      "visible", TRUE,
		      "editable", TRUE,
		      "style", PANGO_STYLE_NORMAL,
691
		      "text",  WACOM_C(action_table[type].action_name),
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
		      NULL);
}

static void
combo_action_cell_changed (GtkCellRendererCombo *cell,
                           const gchar          *path_string,
                           GtkTreeIter          *new_iter,
                           CcWacomPage          *page)
{
	GtkTreeView          *tree_view;
	GtkTreePath          *path;
	GtkTreeModel         *model;
	CcWacomPagePrivate   *priv;
	GsdWacomActionType    type;
	GtkTreeIter           iter;
	GsdWacomTabletButton *button;

	priv = page->priv;
	tree_view = GTK_TREE_VIEW (MWID("shortcut_treeview"));
	model = gtk_tree_view_get_model (tree_view);
	path = gtk_tree_path_new_from_string (path_string);

	gtk_tree_model_get (GTK_TREE_MODEL (priv->action_store), new_iter, ACTION_TYPE_COLUMN, &type, -1);
	gtk_tree_model_get_iter (model, &iter, path);
716
	gtk_list_store_set (GTK_LIST_STORE (model), &iter, MAPPING_TYPE_COLUMN, WACOM_C(action_table[type].action_name), -1);
717
718
719
720
721
722
723
724
725
726
727
728
	gtk_tree_path_free (path);

	gtk_tree_model_get (model, &iter, MAPPING_BUTTON_COLUMN, &button, -1);
	if (button == NULL)
		return;
	if (button->settings == NULL)
		return;
	g_settings_set_enum (button->settings, ACTION_TYPE_KEY, type);

	gtk_widget_grab_focus (GTK_WIDGET (tree_view));
}

729
730
731
732
733
734
735
736
static void
setup_mapping_treeview (CcWacomPage *page)
{
	CcWacomPagePrivate *priv;
	GtkTreeView *treeview;
	GtkCellRenderer *renderer;
	GtkTreeViewColumn *column;
	GtkListStore *model;
737
	GtkTreeIter iter;
738
	GList *list, *l;
739
	gint i;
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761

	priv = page->priv;
	treeview = GTK_TREE_VIEW(MWID ("shortcut_treeview"));

	g_signal_connect (treeview, "button_press_event",
			  G_CALLBACK (start_editing_cb), page);
	g_signal_connect (treeview, "row-activated",
			  G_CALLBACK (start_editing_kb_cb), page);

	renderer = gtk_cell_renderer_text_new ();
	g_object_set (G_OBJECT (renderer), "ellipsize", PANGO_ELLIPSIZE_END, NULL);

	column = gtk_tree_view_column_new_with_attributes (_("Button"),
							   renderer,
							   "text", MAPPING_DESCRIPTION_COLUMN,
							   NULL);
	gtk_tree_view_column_set_resizable (column, FALSE);
	gtk_tree_view_column_set_expand (column, TRUE);

	gtk_tree_view_append_column (treeview, column);
	gtk_tree_view_column_set_sort_column_id (column, MAPPING_DESCRIPTION_COLUMN);

762
763
764
765
766
767
768
	priv->action_store = gtk_list_store_new (ACTION_N_COLUMNS, G_TYPE_STRING, G_TYPE_INT);
	for (i = 0; i < G_N_ELEMENTS (action_table); i++) {
		/* Screen tablets cannot switch monitors (as the monitor is the tablet) */
		if (action_table[i].action_type == GSD_WACOM_ACTION_TYPE_SWITCH_MONITOR &&
		    gsd_wacom_device_is_screen_tablet (priv->stylus))
			continue;

769
770
771
772
773
		/* Do not list on-screen help if libwacom do no provide a layout */
		if (action_table[i].action_type == GSD_WACOM_ACTION_TYPE_HELP &&
		    gsd_wacom_device_get_layout_path (priv->stylus) == NULL)
			continue;

774
775
		gtk_list_store_append (priv->action_store, &iter);
		gtk_list_store_set (priv->action_store, &iter,
776
		                    ACTION_NAME_COLUMN, WACOM_C(action_table[i].action_name),
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
		                    ACTION_TYPE_COLUMN, action_table[i].action_type, -1);
	}
	renderer = gtk_cell_renderer_combo_new ();
	g_object_set (renderer,
                      "text-column", ACTION_NAME_COLUMN,
                      "has-entry", FALSE,
                      "model", priv->action_store,
                      "editable", TRUE,
                      NULL);
	g_signal_connect (renderer, "changed",
	                  G_CALLBACK (combo_action_cell_changed), page);

	column = gtk_tree_view_column_new_with_attributes (_("Type"),
							   renderer,
							   "text", MAPPING_TYPE_COLUMN,
							   NULL);
	gtk_tree_view_column_set_cell_data_func (column, renderer, action_set_func, NULL, NULL);
	gtk_tree_view_column_set_resizable (column, FALSE);
	gtk_tree_view_column_set_expand (column, FALSE);

	gtk_tree_view_append_column (treeview, column);

799
800
801
802
803
804
	renderer = (GtkCellRenderer *) g_object_new (GTK_TYPE_CELL_RENDERER_ACCEL,
						     "accel-mode", GTK_CELL_RENDERER_ACCEL_MODE_OTHER,
						     NULL);

	g_signal_connect (renderer, "accel_edited",
			  G_CALLBACK (accel_edited_callback),
Bastien Nocera's avatar
Bastien Nocera committed
805
			  page);
806
807
	g_signal_connect (renderer, "accel_cleared",
			  G_CALLBACK (accel_cleared_callback),
Bastien Nocera's avatar
Bastien Nocera committed
808
			  page);
809
810
811
812
813
814
815
816

	column = gtk_tree_view_column_new_with_attributes (_("Action"), renderer, NULL);
	gtk_tree_view_column_set_cell_data_func (column, renderer, accel_set_func, NULL, NULL);
	gtk_tree_view_column_set_resizable (column, FALSE);
	gtk_tree_view_column_set_expand (column, FALSE);

	gtk_tree_view_append_column (treeview, column);

817
	model = gtk_list_store_new (MAPPING_N_COLUMNS, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_POINTER, G_TYPE_INT);
818
819
820
821
822
823
	gtk_tree_view_set_model (treeview, GTK_TREE_MODEL (model));

	/* Fill it up! */
	list = gsd_wacom_device_get_buttons (page->priv->pad);
	for (l = list; l != NULL; l = l->next) {
		GsdWacomTabletButton *button = l->data;
824
825
826
827
		GsdWacomActionType type = GSD_WACOM_ACTION_TYPE_NONE;

		if (button->settings)
			type = g_settings_get_enum (button->settings, ACTION_TYPE_KEY);
828

829
830
		if (button->type == WACOM_TABLET_BUTTON_TYPE_STRIP ||
		    button->type == WACOM_TABLET_BUTTON_TYPE_RING) {
831
832
			add_button_to_store (model, button, GTK_DIR_UP, GSD_WACOM_ACTION_TYPE_CUSTOM);
			add_button_to_store (model, button, GTK_DIR_DOWN, GSD_WACOM_ACTION_TYPE_CUSTOM);
833
		} else {
834
			add_button_to_store (model, button, 0, type);
835
		}
836
837
838
839
840
	}
	g_list_free (list);
	g_object_unref (model);
}

841
842
843
844
845
846
847
848
849
850
851
852
853
static void
button_mapping_dialog_closed (GtkDialog   *dialog,
			      int          response_id,
			      CcWacomPage *page)
{
	CcWacomPagePrivate *priv;

	priv = page->priv;
	gtk_widget_destroy (MWID ("button-mapping-dialog"));
	g_object_unref (page->priv->mapping_builder);
	page->priv->mapping_builder = NULL;
}

854
855
856
857
858
859
static void
map_buttons_button_clicked_cb (GtkButton   *button,
			       CcWacomPage *page)
{
	GError *error = NULL;
	GtkWidget *dialog;
860
	CcWacomPagePrivate *priv;
Bastien Nocera's avatar
Bastien Nocera committed
861
	GtkWidget *toplevel;
862
863

	priv = page->priv;
864

Bastien Nocera's avatar
Bastien Nocera committed
865
866
867
	g_assert (priv->mapping_builder == NULL);
	priv->mapping_builder = gtk_builder_new ();
	gtk_builder_add_from_file (priv->mapping_builder,
868
869
870
871
872
				   GNOMECC_UI_DIR "/button-mapping.ui",
				   &error);

	if (error != NULL) {
		g_warning ("Error loading UI file: %s", error->message);
Bastien Nocera's avatar
Bastien Nocera committed
873
874
		g_object_unref (priv->mapping_builder);
		priv->mapping_builder = NULL;
875
876
877
878
		g_error_free (error);
		return;
	}

879
880
	setup_mapping_treeview (page);

881
	dialog = MWID ("button-mapping-dialog");
Bastien Nocera's avatar
Bastien Nocera committed
882
883
	toplevel = gtk_widget_get_toplevel (GTK_WIDGET (page));
	gtk_window_set_transient_for (GTK_WINDOW (dialog), GTK_WINDOW (toplevel));
884
	gtk_window_set_modal (GTK_WINDOW (dialog), TRUE);
885
886
887
	g_signal_connect (G_OBJECT (dialog), "response",
			  G_CALLBACK (button_mapping_dialog_closed), page);

888
	gtk_widget_show (dialog);
889
890
891

	priv->button_map = dialog;
	g_object_add_weak_pointer (G_OBJECT (dialog), (gpointer *) &priv->button_map);
892
893
}

894
895
896
897
898
899
static void
display_mapping_dialog_closed (GtkDialog   *dialog,
			       int          response_id,
			       CcWacomPage *page)
{
	CcWacomPagePrivate *priv;
900
	int layout;
901
902
903
904
905

	priv = page->priv;
	gtk_widget_destroy (priv->dialog);
	priv->dialog = NULL;
	priv->mapping = NULL;
906
907
	layout = get_layout_type (priv->stylus);
	update_tablet_ui (page, layout);
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
}

static void
display_mapping_button_clicked_cb (GtkButton   *button,
				   CcWacomPage *page)
{
	CcWacomPagePrivate *priv;

	priv = page->priv;

	g_assert (priv->mapping == NULL);

	priv->dialog = gtk_dialog_new_with_buttons (_("Display Mapping"),
						    GTK_WINDOW (gtk_widget_get_toplevel (GTK_WIDGET (page))),
						    GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
						    GTK_STOCK_CLOSE,
						    GTK_RESPONSE_ACCEPT,
						    NULL);
	priv->mapping = cc_wacom_mapping_panel_new ();
	cc_wacom_mapping_panel_set_device (CC_WACOM_MAPPING_PANEL (priv->mapping),
					   priv->stylus);
	gtk_container_add (GTK_CONTAINER (gtk_dialog_get_content_area (GTK_DIALOG (priv->dialog))),
			   priv->mapping);
	g_signal_connect (G_OBJECT (priv->dialog), "response",
			  G_CALLBACK (display_mapping_dialog_closed), page);
	gtk_widget_show_all (priv->dialog);
934
935

	g_object_add_weak_pointer (G_OBJECT (priv->mapping), (gpointer *) &priv->dialog);
936
937
}

938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
static void
tabletmode_changed_cb (GtkComboBox *combo, gpointer user_data)
{
	CcWacomPagePrivate	*priv	= CC_WACOM_PAGE(user_data)->priv;
	GtkListStore		*liststore;
	GtkTreeIter		iter;
	gint			mode;
	gboolean		is_absolute;

	if (!gtk_combo_box_get_active_iter (combo, &iter))
		return;

	liststore = GTK_LIST_STORE (WID ("liststore-tabletmode"));
	gtk_tree_model_get (GTK_TREE_MODEL (liststore), &iter,
			    MODENUMBER_COLUMN, &mode,
			    -1);

	is_absolute = (mode == MODE_ABSOLUTE);
	g_settings_set_boolean (priv->wacom_settings, "is-absolute", is_absolute);
}

959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
static const gchar*
opposite_rotation (const gchar *rotation)
{
	/* Order matters here, if not found we return "none"  */
	static const gchar *rotations[] = { "half", "cw", "none", "ccw" };
	guint i, n;

	n = G_N_ELEMENTS (rotations);
	for (i = 0; i < n; i++) {
		if (strcmp (rotation, rotations[i]) == 0)
			break;
	}

	return rotations[(i + n / 2) % n];
}

975
976
977
978
static void
left_handed_toggled_cb (GtkSwitch *sw, GParamSpec *pspec, gpointer *user_data)
{
	CcWacomPagePrivate	*priv = CC_WACOM_PAGE(user_data)->priv;
979
980
	GsdWacomDevice          *device = priv->stylus;
	GsdWacomRotation 	display_rotation;
981
982
	const gchar*		rotation;

983
984
985
986
	display_rotation = gsd_wacom_device_get_display_rotation (device);
	rotation = gsd_wacom_device_rotation_type_to_name (display_rotation);
	if (gtk_switch_get_active (sw))
		rotation = opposite_rotation (rotation);
987
988
989
990
991
992
993
994

	g_settings_set_string (priv->wacom_settings, "rotation", rotation);
}

static void
set_left_handed_from_gsettings (CcWacomPage *page)
{
	CcWacomPagePrivate	*priv = CC_WACOM_PAGE(page)->priv;
995
996
	GsdWacomDevice          *device = priv->stylus;
	GsdWacomRotation 	display_rotation;
997
998
	const gchar*		rotation;

999
	display_rotation = gsd_wacom_device_get_display_rotation (device);
1000
	rotation = g_settings_get_string (priv->wacom_settings, "rotation");
For faster browsing, not all history is shown. View entire blame