GCC Code Coverage Report


Directory: ./
File: panels/wacom/cc-wacom-stylus-page.c
Date: 2024-05-04 07:58:27
Exec Total Coverage
Lines: 0 175 0.0%
Functions: 0 23 0.0%
Branches: 0 54 0.0%

Line Branch Exec Source
1 /*
2 * Copyright © 2011 Red Hat, Inc.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, see <http://www.gnu.org/licenses/>.
16 *
17 * Authors: Peter Hutterer <peter.hutterer@redhat.com>
18 * Bastien Nocera <hadess@hadess.net>
19 *
20 */
21
22 #include <config.h>
23
24 #include <adwaita.h>
25 #include <glib/gi18n.h>
26 #include <shell/cc-panel.h>
27 #include "cc-wacom-panel.h"
28 #include "cc-wacom-stylus-page.h"
29 #include "cc-wacom-stylus-action-dialog.h"
30 #include "panels/common/cc-list-row.h"
31 #include <gtk/gtk.h>
32
33 #include <string.h>
34
35 struct _CcWacomStylusPage
36 {
37 GtkBox parent_instance;
38
39 CcWacomPanel *panel;
40 GtkWidget *stylus_section;
41 GtkWidget *stylus_icon;
42 GtkWidget *stylus_button1_action_row;
43 GtkWidget *stylus_button2_action_row;
44 GtkWidget *stylus_button3_action_row;
45 GtkLabel *stylus_button1_action_label;
46 GtkLabel *stylus_button2_action_label;
47 GtkLabel *stylus_button3_action_label;
48 GtkWidget *stylus_eraser_pressure;
49 GtkWidget *stylus_tip_pressure_scale;
50 GtkWidget *stylus_eraser_pressure_scale;
51 GtkAdjustment *stylus_tip_pressure_adjustment;
52 GtkAdjustment *stylus_eraser_pressure_adjustment;
53 CcWacomTool *stylus;
54 GSettings *stylus_settings;
55
56 gboolean highlighted;
57 };
58
59 G_DEFINE_TYPE (CcWacomStylusPage, cc_wacom_stylus_page, GTK_TYPE_BOX)
60
61 static void
62 map_pressurecurve (double val, graphene_point_t *p1, graphene_point_t *p2)
63 {
64 g_return_if_fail (val >= 0.0 && val <= 100.0);
65
66 /* The second point's x/y axis is an inverse of the first point's y/x axis,
67 * so that:
68 * 0% maps to 0/100 0/100
69 * 10% maps to 0/80 20/100
70 * 50% maps to 0/0 100/100
71 * 60% maps to 20/0 100/80
72 * 100% maps to 100/0 100/0
73 */
74
75 p1->x = -100 + val * 2;
76 p1->y = 100 - val * 2;
77 p1->x = CLAMP(p1->x, 0, 100);
78 p1->y = CLAMP(p1->y, 0, 100);
79
80 p2->x = 100 - p1->y;
81 p2->y = 100 - p1->x;
82 }
83
84 static void
85 set_pressurecurve (GtkRange *range, GSettings *settings, const gchar *key)
86 {
87 gint slider_val = gtk_range_get_value (range) / 2;
88 graphene_point_t p1, p2;
89 GVariantBuilder builder;
90
91 g_return_if_fail (slider_val >= 0 && slider_val <= 100);
92
93 map_pressurecurve (slider_val, &p1, &p2);
94
95 g_variant_builder_init (&builder, G_VARIANT_TYPE ("ai"));
96 g_variant_builder_add (&builder, "i", (int)p1.x);
97 g_variant_builder_add (&builder, "i", (int)p1.y);
98 g_variant_builder_add (&builder, "i", (int)p2.x);
99 g_variant_builder_add (&builder, "i", (int)p2.y);
100
101 g_settings_set_value (settings, key, g_variant_builder_end (&builder));
102
103 }
104
105 static void
106 on_tip_pressure_value_changed (CcWacomStylusPage *page)
107 {
108 set_pressurecurve (GTK_RANGE (page->stylus_tip_pressure_scale), page->stylus_settings, "pressure-curve");
109 }
110
111 static void
112 on_eraser_pressure_value_changed (CcWacomStylusPage *page)
113 {
114 set_pressurecurve (GTK_RANGE (page->stylus_eraser_pressure_scale), page->stylus_settings, "eraser-pressure-curve");
115 }
116
117 static void
118 set_feel_from_gsettings (GtkAdjustment *adjustment, GSettings *settings, const gchar *key)
119 {
120 GVariant *variant;
121 const gint32 *values;
122 gsize nvalues;
123 int i;
124 graphene_point_t p1, p2;
125
126 variant = g_settings_get_value (settings, key);
127 values = g_variant_get_fixed_array (variant, &nvalues, sizeof (gint32));
128
129 if (nvalues != 4) {
130 g_warning ("Invalid pressure curve format, expected 4 values (got %"G_GSIZE_FORMAT")", nvalues);
131 return;
132 }
133
134 p1 = GRAPHENE_POINT_INIT (values[0], values[1]);
135 p2 = GRAPHENE_POINT_INIT (values[2], values[3]);
136
137 /* Our functions in set_pressurecurve() are lossy thanks to CLAMP so
138 * we calculate a (possibly wrong) slider value from our points, compare
139 * what points that value would produce and if they match - hooray!
140 */
141 for (i = 0; i < 4; i++) {
142 double val;
143
144 switch (i) {
145 case 0:
146 val = (p1.x + 100) / 2.0;
147 break;
148 case 1:
149 val = (-p1.y + 100) / 2.0;
150 break;
151 case 2:
152 val = p2.x / 2.0;
153 break;
154 case 3:
155 val = (-p2.y + 200) / 2.0;
156 break;
157 }
158
159 if (val >= 0.0 && val <= 100.0) {
160 graphene_point_t mapped_p1, mapped_p2;
161
162 map_pressurecurve (val, &mapped_p1, &mapped_p2);
163 if (graphene_point_equal(&p1, &mapped_p1) && graphene_point_equal(&p2, &mapped_p2)) {
164 gtk_adjustment_set_value (adjustment, (int)(val * 2));
165 break;
166 }
167 }
168 }
169 }
170
171 static void
172 cc_wacom_stylus_page_get_property (GObject *object,
173 guint property_id,
174 GValue *value,
175 GParamSpec *pspec)
176 {
177 switch (property_id)
178 {
179 default:
180 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
181 }
182 }
183
184 static void
185 cc_wacom_stylus_page_set_property (GObject *object,
186 guint property_id,
187 const GValue *value,
188 GParamSpec *pspec)
189 {
190 switch (property_id)
191 {
192 default:
193 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
194 }
195 }
196
197 static void
198 present_action_dialog (CcWacomStylusPage *page,
199 guint button,
200 const char *key)
201 {
202 GtkWindow *window;
203 GtkWidget *action_dialog;
204
205 window = GTK_WINDOW (cc_shell_get_toplevel (cc_panel_get_shell (CC_PANEL (page->panel))));
206 action_dialog = cc_wacom_stylus_action_dialog_new (page->stylus_settings,
207 cc_wacom_tool_get_name (page->stylus),
208 button, key);
209
210 gtk_window_set_transient_for (GTK_WINDOW (action_dialog), window);
211 gtk_window_present (GTK_WINDOW (action_dialog));
212 }
213
214 static void
215 on_stylus_button1_action_activated (CcWacomStylusPage *page)
216 {
217 present_action_dialog (page, 1, "button-action");
218 }
219
220 static void
221 on_stylus_button2_action_activated (CcWacomStylusPage *page)
222 {
223 present_action_dialog (page, 2, "secondary-button-action");
224 }
225
226 static void
227 on_stylus_button3_action_activated (CcWacomStylusPage *page)
228 {
229 present_action_dialog (page, 3, "tertiary-button-action");
230 }
231
232 static void
233 cc_wacom_stylus_page_class_init (CcWacomStylusPageClass *klass)
234 {
235 GObjectClass *object_class = G_OBJECT_CLASS (klass);
236 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
237
238 object_class->get_property = cc_wacom_stylus_page_get_property;
239 object_class->set_property = cc_wacom_stylus_page_set_property;
240
241 g_type_ensure (CC_TYPE_LIST_ROW);
242
243 gtk_widget_class_set_template_from_resource (widget_class, "/org/gnome/control-center/wacom/cc-wacom-stylus-page.ui");
244
245 gtk_widget_class_bind_template_child (widget_class, CcWacomStylusPage, stylus_section);
246 gtk_widget_class_bind_template_child (widget_class, CcWacomStylusPage, stylus_icon);
247 gtk_widget_class_bind_template_child (widget_class, CcWacomStylusPage, stylus_button1_action_row);
248 gtk_widget_class_bind_template_child (widget_class, CcWacomStylusPage, stylus_button2_action_row);
249 gtk_widget_class_bind_template_child (widget_class, CcWacomStylusPage, stylus_button3_action_row);
250 gtk_widget_class_bind_template_child (widget_class, CcWacomStylusPage, stylus_button1_action_label);
251 gtk_widget_class_bind_template_child (widget_class, CcWacomStylusPage, stylus_button2_action_label);
252 gtk_widget_class_bind_template_child (widget_class, CcWacomStylusPage, stylus_button3_action_label);
253 gtk_widget_class_bind_template_child (widget_class, CcWacomStylusPage, stylus_eraser_pressure);
254 gtk_widget_class_bind_template_child (widget_class, CcWacomStylusPage, stylus_tip_pressure_scale);
255 gtk_widget_class_bind_template_child (widget_class, CcWacomStylusPage, stylus_eraser_pressure_scale);
256 gtk_widget_class_bind_template_child (widget_class, CcWacomStylusPage, stylus_tip_pressure_adjustment);
257 gtk_widget_class_bind_template_child (widget_class, CcWacomStylusPage, stylus_eraser_pressure_adjustment);
258
259 gtk_widget_class_bind_template_callback (widget_class, on_stylus_button1_action_activated);
260 gtk_widget_class_bind_template_callback (widget_class, on_stylus_button2_action_activated);
261 gtk_widget_class_bind_template_callback (widget_class, on_stylus_button3_action_activated);
262 gtk_widget_class_bind_template_callback (widget_class, on_tip_pressure_value_changed);
263 gtk_widget_class_bind_template_callback (widget_class, on_eraser_pressure_value_changed);
264 }
265
266 static void
267 add_marks (GtkScale *scale)
268 {
269 gtk_scale_add_mark (scale, 100, GTK_POS_BOTTOM, NULL);
270 }
271
272 static void
273 cc_wacom_stylus_page_init (CcWacomStylusPage *page)
274 {
275 gtk_widget_init_template (GTK_WIDGET (page));
276
277 add_marks (GTK_SCALE (page->stylus_tip_pressure_scale));
278 add_marks (GTK_SCALE (page->stylus_eraser_pressure_scale));
279 }
280
281 static void
282 set_icon_name (CcWacomStylusPage *page,
283 const char *icon_name,
284 gboolean use_highlight)
285 {
286 g_autofree gchar *resource = NULL;
287
288 resource = g_strdup_printf ("/org/gnome/control-center/wacom/%s%s.svg",
289 icon_name, use_highlight ? "-highlighted" : "");
290 gtk_picture_set_resource (GTK_PICTURE (page->stylus_icon), resource);
291 }
292
293 static void
294 on_button_action_changed (GSettings *settings,
295 gchar *key,
296 gpointer user_data)
297 {
298 GDesktopStylusButtonAction action = g_settings_get_enum (settings, key);
299 GtkLabel *label = GTK_LABEL (user_data);
300 const char *text = cc_wacom_panel_get_stylus_button_action_label (action);
301
302 if (text)
303 gtk_label_set_label (label, text);
304 }
305
306 GtkWidget *
307 cc_wacom_stylus_page_new (CcWacomPanel *panel,
308 CcWacomTool *stylus)
309 {
310 CcWacomStylusPage *page;
311 guint num_buttons;
312 gboolean has_paired_eraser;
313
314 g_return_val_if_fail (CC_IS_WACOM_TOOL (stylus), NULL);
315
316 page = g_object_new (CC_TYPE_WACOM_STYLUS_PAGE, NULL);
317
318 page->panel = panel;
319 page->stylus = stylus;
320
321 /* Stylus name */
322 adw_preferences_group_set_title (ADW_PREFERENCES_GROUP (page->stylus_section),
323 cc_wacom_tool_get_name (stylus));
324 adw_preferences_group_set_description (ADW_PREFERENCES_GROUP (page->stylus_section),
325 cc_wacom_tool_get_description (stylus));
326
327 /* Icon */
328 set_icon_name (page, cc_wacom_tool_get_icon_name (stylus), FALSE);
329
330 /* Settings */
331 page->stylus_settings = cc_wacom_tool_get_settings (stylus);
332 has_paired_eraser = cc_wacom_tool_get_has_paired_eraser (stylus);
333
334 num_buttons = cc_wacom_tool_get_num_buttons (stylus);
335 gtk_widget_set_visible (page->stylus_button3_action_row,
336 num_buttons >= 3);
337 gtk_widget_set_visible (page->stylus_button2_action_row,
338 num_buttons >= 2);
339 gtk_widget_set_visible (page->stylus_button1_action_row,
340 num_buttons >= 1);
341 gtk_widget_set_visible (page->stylus_eraser_pressure,
342 has_paired_eraser);
343
344 set_feel_from_gsettings (page->stylus_tip_pressure_adjustment,
345 page->stylus_settings, "pressure-curve");
346 set_feel_from_gsettings (page->stylus_eraser_pressure_adjustment,
347 page->stylus_settings, "eraser-pressure-curve");
348
349 g_signal_connect (G_OBJECT (page->stylus_settings),
350 "changed::button-action",
351 G_CALLBACK (on_button_action_changed),
352 page->stylus_button1_action_label);
353 g_signal_connect (G_OBJECT (page->stylus_settings),
354 "changed::secondary-button-action",
355 G_CALLBACK (on_button_action_changed),
356 page->stylus_button2_action_label);
357 g_signal_connect (G_OBJECT (page->stylus_settings),
358 "changed::tertiary-button-action",
359 G_CALLBACK (on_button_action_changed),
360 page->stylus_button3_action_label);
361
362 on_button_action_changed (page->stylus_settings,
363 "button-action",
364 page->stylus_button1_action_label);
365 on_button_action_changed (page->stylus_settings,
366 "secondary-button-action",
367 page->stylus_button2_action_label);
368 on_button_action_changed (page->stylus_settings,
369 "tertiary-button-action",
370 page->stylus_button3_action_label);
371
372 return GTK_WIDGET (page);
373 }
374
375 CcWacomTool *
376 cc_wacom_stylus_page_get_tool (CcWacomStylusPage *page)
377 {
378 return page->stylus;
379 }
380
381 void
382 cc_wacom_stylus_page_set_highlight (CcWacomStylusPage *page,
383 gboolean highlight)
384 {
385 if (page->highlighted != highlight) {
386 set_icon_name (page, cc_wacom_tool_get_icon_name (page->stylus), highlight);
387 page->highlighted = highlight;
388 }
389 }
390
391 const char *
392 cc_wacom_panel_get_stylus_button_action_label (GDesktopStylusButtonAction action)
393 {
394 const char *text = NULL;
395
396 switch (action) {
397 case G_DESKTOP_STYLUS_BUTTON_ACTION_DEFAULT:
398 text = _("_Left Mousebutton Click");
399 break;
400 case G_DESKTOP_STYLUS_BUTTON_ACTION_MIDDLE:
401 text = _("_Middle Mousebutton Click");
402 break;
403 case G_DESKTOP_STYLUS_BUTTON_ACTION_RIGHT:
404 text = _("_Right Mousebutton Click");
405 break;
406 case G_DESKTOP_STYLUS_BUTTON_ACTION_BACK:
407 /* Translators: this is the "go back" action of a button */
408 text = _("_Back");
409 break;
410 case G_DESKTOP_STYLUS_BUTTON_ACTION_FORWARD:
411 /* Translators: this is the "go forward" action of a button */
412 text = _("_Forward");
413 break;
414 }
415
416 return text;
417 }
418