summaryrefslogtreecommitdiffstats
path: root/panels/wacom/cc-wacom-tool.c
blob: 5633f5bdef667e0c43415507fcc5d0fd6d65b4c5 (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
/*
 * Copyright © 2016 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: Carlos Garnacho <carlosg@gnome.org>
 *
 */

#include "config.h"

#include "cc-wacom-tool.h"

#include <glib/gi18n.h>

enum {
	PROP_0,
	PROP_SERIAL,
	PROP_ID,
	PROP_DEVICE,
	N_PROPS
};

static GParamSpec *props[N_PROPS] = { 0 };

typedef struct _CcWacomTool CcWacomTool;

struct _CcWacomTool {
	GObject parent_instance;
	guint64 serial;
	guint64 id;

	CcWacomDevice *device; /* Only set for tools with no serial */

	GSettings *settings;
	const WacomStylus *wstylus;
};

static void cc_wacom_tool_initable_iface_init (GInitableIface *iface);

G_DEFINE_TYPE_WITH_CODE (CcWacomTool, cc_wacom_tool, G_TYPE_OBJECT,
			 G_IMPLEMENT_INTERFACE (G_TYPE_INITABLE,
						cc_wacom_tool_initable_iface_init))

static void
cc_wacom_tool_init (CcWacomTool *tool)
{
}

static void
cc_wacom_tool_set_property (GObject      *object,
			    guint         prop_id,
			    const GValue *value,
			    GParamSpec   *pspec)
{
	CcWacomTool *tool = CC_WACOM_TOOL (object);

	switch (prop_id) {
	case PROP_SERIAL:
		tool->serial = g_value_get_uint64 (value);
		break;
	case PROP_ID:
		tool->id = g_value_get_uint64 (value);
		break;
	case PROP_DEVICE:
		tool->device = g_value_get_object (value);
		break;
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
		break;
	}
}

static void
cc_wacom_tool_get_property (GObject    *object,
			    guint       prop_id,
			    GValue     *value,
			    GParamSpec *pspec)
{
	CcWacomTool *tool = CC_WACOM_TOOL (object);

	switch (prop_id) {
	case PROP_SERIAL:
		g_value_set_uint64 (value, tool->serial);
		break;
	case PROP_ID:
		g_value_set_uint64 (value, tool->id);
		break;
	case PROP_DEVICE:
		g_value_set_object (value, tool->device);
		break;
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
		break;
	}
}

static void
cc_wacom_tool_finalize (GObject *object)
{
	CcWacomTool *tool = CC_WACOM_TOOL (object);

	g_clear_object (&tool->settings);

	G_OBJECT_CLASS (cc_wacom_tool_parent_class)->finalize (object);
}

static void
cc_wacom_tool_class_init (CcWacomToolClass *klass)
{
	GObjectClass *object_class = G_OBJECT_CLASS (klass);

	object_class->set_property = cc_wacom_tool_set_property;
	object_class->get_property = cc_wacom_tool_get_property;
	object_class->finalize = cc_wacom_tool_finalize;

	props[PROP_SERIAL] =
		g_param_spec_uint64 ("serial",
				     "serial",
				     "serial",
				     0, G_MAXUINT64, 0,
				     G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY);
	props[PROP_ID] =
		g_param_spec_uint64 ("id",
				     "id",
				     "id",
				     0, G_MAXUINT64, 0,
				     G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY);
	props[PROP_DEVICE] =
		g_param_spec_object ("device",
				     "device",
				     "device",
				     CC_TYPE_WACOM_DEVICE,
				     G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY);

	g_object_class_install_properties (object_class, N_PROPS, props);
}

static gboolean
cc_wacom_tool_initable_init (GInitable     *initable,
			     GCancellable  *cancellable,
			     GError       **error)
{
	CcWacomTool *tool = CC_WACOM_TOOL (initable);
	WacomDeviceDatabase *wacom_db;
	gchar *path;

	wacom_db = cc_wacom_device_database_get ();

	if (tool->id == 0 && tool->device) {
		const gint *ids;
		gint n_supported;

		ids = cc_wacom_device_get_supported_tools (tool->device, &n_supported);
		if (n_supported > 0)
			tool->id = ids[0];
	}

	if (tool->id == 0)
		tool->wstylus = libwacom_stylus_get_for_id (wacom_db, 0xfffff);
	else
		tool->wstylus = libwacom_stylus_get_for_id (wacom_db, tool->id);

	if (!tool->wstylus) {
		g_set_error (error, 0, 0, "Stylus description not found");
		return FALSE;
	}

	if (tool->serial == 0) {
		const gchar *vendor, *product;
		GsdDevice *gsd_device;

		gsd_device = cc_wacom_device_get_device (tool->device);
		gsd_device_get_device_ids (gsd_device, &vendor, &product);
		path = g_strdup_printf ("/org/gnome/desktop/peripherals/stylus/default-%s:%s/",
					vendor, product);
        } else {
		path = g_strdup_printf ("/org/gnome/desktop/peripherals/stylus/%lx/", tool->serial);
        }

	tool->settings = g_settings_new_with_path ("org.gnome.desktop.peripherals.tablet.stylus",
						   path);
	g_free (path);

	return TRUE;
}

static void
cc_wacom_tool_initable_iface_init (GInitableIface *iface)
{
	iface->init = cc_wacom_tool_initable_init;
}

CcWacomTool *
cc_wacom_tool_new (guint64        serial,
		   guint64        id,
		   CcWacomDevice *device)
{
	g_return_val_if_fail (serial != 0 || CC_IS_WACOM_DEVICE (device), NULL);

	return g_initable_new (CC_TYPE_WACOM_TOOL,
			       NULL, NULL,
			       "serial", serial,
			       "id", id,
			       "device", device,
			       NULL);
}

guint64
cc_wacom_tool_get_serial (CcWacomTool *tool)
{
	g_return_val_if_fail (CC_IS_WACOM_TOOL (tool), 0);

	return tool->serial;
}

guint64
cc_wacom_tool_get_id (CcWacomTool *tool)
{
	g_return_val_if_fail (CC_IS_WACOM_TOOL (tool), 0);

	return tool->id;
}

const gchar *
cc_wacom_tool_get_name (CcWacomTool *tool)
{
	g_return_val_if_fail (CC_IS_WACOM_TOOL (tool), NULL);

	return libwacom_stylus_get_name (tool->wstylus);
}

static const char *
get_icon_name_from_type (const WacomStylus *wstylus)
{
	WacomStylusType type = libwacom_stylus_get_type (wstylus);

	switch (type) {
	case WSTYLUS_INKING:
	case WSTYLUS_STROKE:
		/* The stroke pen is the same as the inking pen with
		 * a different nib */
		return "wacom-stylus-inking";
	case WSTYLUS_AIRBRUSH:
		return "wacom-stylus-airbrush";
	case WSTYLUS_MARKER:
		return "wacom-stylus-art-pen";
	case WSTYLUS_CLASSIC:
		return "wacom-stylus-classic";
#ifdef HAVE_WACOM_3D_STYLUS
	case WSTYLUS_3D:
		return "wacom-stylus-3btn-no-eraser";
#endif
	default:
		if (!libwacom_stylus_has_eraser (wstylus)) {
			if (libwacom_stylus_get_num_buttons (wstylus) >= 3)
				return "wacom-stylus-3btn-no-eraser";
			else
				return "wacom-stylus-no-eraser";
		}
		else {
			if (libwacom_stylus_get_num_buttons (wstylus) >= 3)
				return "wacom-stylus-3btn";
			else
				return "wacom-stylus";
		}
	}
}

const gchar *
cc_wacom_tool_get_icon_name (CcWacomTool *tool)
{
	g_return_val_if_fail (CC_IS_WACOM_TOOL (tool), NULL);

	return get_icon_name_from_type (tool->wstylus);
}

GSettings *
cc_wacom_tool_get_settings (CcWacomTool *tool)
{
	g_return_val_if_fail (CC_IS_WACOM_TOOL (tool), NULL);

	return tool->settings;
}

guint
cc_wacom_tool_get_num_buttons (CcWacomTool *tool)
{
	g_return_val_if_fail (CC_IS_WACOM_TOOL (tool), 0);

	return libwacom_stylus_get_num_buttons (tool->wstylus);
}

gboolean
cc_wacom_tool_get_has_eraser (CcWacomTool *tool)
{
	g_return_val_if_fail (CC_IS_WACOM_TOOL (tool), FALSE);

	return libwacom_stylus_is_eraser (tool->wstylus);
}

const gchar *
cc_wacom_tool_get_description (CcWacomTool *tool)
{
	WacomAxisTypeFlags axes;

	axes = libwacom_stylus_get_axes (tool->wstylus);

	if ((~axes & (WACOM_AXIS_TYPE_TILT | WACOM_AXIS_TYPE_PRESSURE | WACOM_AXIS_TYPE_SLIDER)) == 0)
		return _("Airbrush stylus with pressure, tilt, and integrated slider");
	else if ((~axes & (WACOM_AXIS_TYPE_TILT | WACOM_AXIS_TYPE_PRESSURE | WACOM_AXIS_TYPE_ROTATION_Z)) == 0)
		return _("Airbrush stylus with pressure, tilt, and rotation");
	else if ((~axes & (WACOM_AXIS_TYPE_TILT | WACOM_AXIS_TYPE_PRESSURE)) == 0)
		return _("Standard stylus with pressure and tilt");
	else if ((~axes & WACOM_AXIS_TYPE_PRESSURE) == 0)
		return _("Standard stylus with pressure");

	return NULL;
}