summaryrefslogtreecommitdiffstats
path: root/panels/mouse/cc-mouse-test.c
blob: 145cb74bb64aadf02084eddc26c9fe298c4da85f (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
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
 *
 * Copyright (C) 2012 Red Hat, Inc.
 *
 * Written by: Ondrej Holy <oholy@redhat.com>,
 *
 * 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, 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/>.
 */

#include <config.h>

#include <glib/gi18n.h>
#include <string.h>
#include <gdk/gdk.h>
#include <gdk/x11/gdkx.h>
#include <math.h>

#include "cc-mouse-test.h"

#include <sys/types.h>
#include <sys/stat.h>

/* Click test button sizes. */
#define SHADOW_SIZE (10.0 / 180 * size)
#define SHADOW_SHIFT_Y (-1.0 / 180 * size)
#define SHADOW_OPACITY (0.15 / 180 * size)
#define OUTER_CIRCLE_SIZE (22.0 / 180 * size)
#define ANNULUS_SIZE (6.0 / 180 * size)
#define INNER_CIRCLE_SIZE (52.0 / 180 * size)

static void setup_information_label (CcMouseTest *self);
static void setup_scroll_image (CcMouseTest *self);

enum
{
	DOUBLE_CLICK_TEST_OFF,
	DOUBLE_CLICK_TEST_MAYBE,
	DOUBLE_CLICK_TEST_ON,
	DOUBLE_CLICK_TEST_STILL_ON,
	DOUBLE_CLICK_TEST_ALMOST_THERE,
	DOUBLE_CLICK_TEST_GEGL
};

struct _CcMouseTest
{
	AdwBin parent_instance;

	GtkWidget *button_drawing_area;
	GtkWidget *information_label;
	GtkWidget *image;
	GtkWidget *scrolled_window_adjustment;
	GtkWidget *viewport;

	guint32 double_click_timestamp;
	gint double_click_state;
	gint button_state;

	GSettings *mouse_settings;

	gint information_label_timeout_id;
	gint button_drawing_area_timeout_id;
	gint scroll_image_timeout_id;
};

G_DEFINE_TYPE (CcMouseTest, cc_mouse_test, ADW_TYPE_BIN);

/* Timeout for the double click test */

static gboolean
test_maybe_timeout (CcMouseTest *self)
{
	self->double_click_state = DOUBLE_CLICK_TEST_OFF;

	gtk_widget_queue_draw (self->button_drawing_area);

	self->button_drawing_area_timeout_id = 0;

	return FALSE;
}

/* Timeout for the information label */

static gboolean
information_label_timeout (CcMouseTest *self)
{
	setup_information_label (self);

	self->information_label_timeout_id = 0;

	return FALSE;
}

/* Timeout for the scroll image */

static gboolean
scroll_image_timeout (CcMouseTest *self)
{
	setup_scroll_image (self);

	self->scroll_image_timeout_id = 0;

	return FALSE;
}

/* Set information label */

static void
setup_information_label (CcMouseTest *self)
{
	const gchar *message = NULL;
	g_autofree gchar *label_text = NULL;
	gboolean double_click;

	if (self->information_label_timeout_id != 0) {
		g_source_remove (self->information_label_timeout_id);
		self->information_label_timeout_id = 0;
	}

	if (self->double_click_state == DOUBLE_CLICK_TEST_OFF) {
		gtk_label_set_label (GTK_LABEL (self->information_label), _("Try clicking, double clicking, scrolling"));
		return;
	}

	if (self->double_click_state == DOUBLE_CLICK_TEST_GEGL) {
		message = _("Five clicks, GEGL time!");
	} else {
		double_click = (self->double_click_state >= DOUBLE_CLICK_TEST_ON);
		switch (self->button_state) {
		case 1:
			message = (double_click) ? _("Double click, primary button") : _("Single click, primary button");
			break;
		case 2:
			message = (double_click) ? _("Double click, middle button") : _("Single click, middle button");
			break;
		case 3:
			message = (double_click) ? _("Double click, secondary button") : _("Single click, secondary button");
			break;
		}
	}

	label_text = g_strconcat ("<b>", message, "</b>", NULL);
	gtk_label_set_markup (GTK_LABEL (self->information_label), label_text);

	self->information_label_timeout_id = g_timeout_add (2500, (GSourceFunc) information_label_timeout, self);
}

/* Update scroll image */

static void
setup_scroll_image (CcMouseTest *self)
{
	const char *resource;

	if (self->scroll_image_timeout_id != 0) {
		g_source_remove (self->scroll_image_timeout_id);
		self->scroll_image_timeout_id = 0;
	}

	if (self->double_click_state == DOUBLE_CLICK_TEST_GEGL)
		resource = "/org/gnome/control-center/mouse/scroll-test-gegl.svg";
	else
		resource = "/org/gnome/control-center/mouse/scroll-test.svg";
	gtk_picture_set_resource (GTK_PICTURE (self->image), resource);

	if (self->double_click_state != DOUBLE_CLICK_TEST_GEGL)
		return;

	self->scroll_image_timeout_id = g_timeout_add (5000, (GSourceFunc) scroll_image_timeout, self);
}

/* Callback issued when the user clicks the double click testing area. */

static void
button_drawing_area_button_pressed_cb (GtkGestureClick *click_gesture,
                                       gint             n_press,
                                       gdouble          x,
                                       gdouble          y,
                                       CcMouseTest     *self)
{
	guint32 event_time;
	guint current_button;
	gint double_click_time;

	current_button = gtk_gesture_single_get_current_button (GTK_GESTURE_SINGLE (click_gesture));

	if (current_button > 3)
		return;

	double_click_time = g_settings_get_int (self->mouse_settings, "double-click");

	if (self->button_drawing_area_timeout_id != 0) {
		g_source_remove (self->button_drawing_area_timeout_id);
		self->button_drawing_area_timeout_id = 0;
	}

	/* Ignore fake double click using different buttons. */
	if (self->double_click_state != DOUBLE_CLICK_TEST_OFF && self->button_state != current_button)
		self->double_click_state = DOUBLE_CLICK_TEST_OFF;

	event_time = gtk_event_controller_get_current_event_time (GTK_EVENT_CONTROLLER (click_gesture));
	switch (self->double_click_state) {
	case DOUBLE_CLICK_TEST_OFF:
		self->double_click_state = DOUBLE_CLICK_TEST_MAYBE;
		self->button_drawing_area_timeout_id = g_timeout_add (double_click_time, (GSourceFunc) test_maybe_timeout, self);
		break;
	case DOUBLE_CLICK_TEST_MAYBE:
	case DOUBLE_CLICK_TEST_ON:
	case DOUBLE_CLICK_TEST_STILL_ON:
	case DOUBLE_CLICK_TEST_ALMOST_THERE:
		if (event_time - self->double_click_timestamp < double_click_time) {
			self->double_click_state++;
			self->button_drawing_area_timeout_id = g_timeout_add (2500, (GSourceFunc) test_maybe_timeout, self);
		} else {
			test_maybe_timeout (self);
		}
		break;
	case DOUBLE_CLICK_TEST_GEGL:
		self->double_click_state = DOUBLE_CLICK_TEST_OFF;
		break;
	}

	self->double_click_timestamp = event_time;

	gtk_widget_queue_draw (self->button_drawing_area);

	self->button_state = current_button;
	setup_information_label (self);
	setup_scroll_image (self);
}

static void
button_drawing_area_draw_func (GtkDrawingArea *drawing_area,
                               cairo_t        *cr,
                               int             width,
                               int             height,
                               gpointer        user_data)
{
	CcMouseTest *self = CC_MOUSE_TEST (user_data);
	gdouble center_x, center_y, size;
	GdkRGBA inner_color, outer_color;
	cairo_pattern_t *pattern;

	size = MAX (MIN (width, height), 1);
	center_x = width / 2.0;
	center_y = height / 2.0;

	switch (self->double_click_state) {
	case DOUBLE_CLICK_TEST_ON:
	case DOUBLE_CLICK_TEST_STILL_ON:
	case DOUBLE_CLICK_TEST_ALMOST_THERE:
	case DOUBLE_CLICK_TEST_GEGL:
		gdk_rgba_parse (&outer_color, "#729fcf");
		gdk_rgba_parse (&inner_color, "#729fcf");
		break;
	case DOUBLE_CLICK_TEST_MAYBE:
		gdk_rgba_parse (&outer_color, "#729fcf");
		gdk_rgba_parse (&inner_color, "#ffffff");
		break;
	case DOUBLE_CLICK_TEST_OFF:
		gdk_rgba_parse (&outer_color, "#ffffff");
		gdk_rgba_parse (&inner_color, "#ffffff");
		break;
	}

	/* Draw shadow. */
	cairo_rectangle (cr, center_x - size / 2,  center_y - size / 2, size, size);
	pattern = cairo_pattern_create_radial (center_x, center_y, 0, center_x, center_y, size);
	cairo_pattern_add_color_stop_rgba (pattern, 0.5 - SHADOW_SIZE / size, 0, 0, 0, SHADOW_OPACITY);
	cairo_pattern_add_color_stop_rgba (pattern, 0.5, 0, 0, 0, 0);
	cairo_set_source (cr, pattern);
	cairo_fill (cr);

	/* Draw outer circle. */
	cairo_set_line_width (cr, OUTER_CIRCLE_SIZE);
	cairo_arc (cr, center_x, center_y + SHADOW_SHIFT_Y,
		   INNER_CIRCLE_SIZE + ANNULUS_SIZE + OUTER_CIRCLE_SIZE / 2,
		   0, 2 * G_PI);
	gdk_cairo_set_source_rgba (cr, &outer_color);
	cairo_stroke (cr);

	/* Draw inner circle. */
	cairo_set_line_width (cr, 0);
	cairo_arc (cr, center_x, center_y + SHADOW_SHIFT_Y,
		   INNER_CIRCLE_SIZE,
		   0, 2 * G_PI);
	gdk_cairo_set_source_rgba (cr, &inner_color);
	cairo_fill (cr);
}

static void
setup_dialog (CcMouseTest *self)
{
	GtkAdjustment *adjustment;
	GtkStyleProvider *provider;

	adjustment = GTK_ADJUSTMENT (self->scrolled_window_adjustment);
	gtk_adjustment_set_value (adjustment,
				  gtk_adjustment_get_upper (adjustment));

	provider = GTK_STYLE_PROVIDER (gtk_css_provider_new ());
	gtk_css_provider_load_from_data (GTK_CSS_PROVIDER (provider), "* {background: #26a269;}", -1);
	gtk_style_context_add_provider (gtk_widget_get_style_context (self->viewport),
					provider,
					GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
	g_object_unref (provider);
}

static void
cc_mouse_test_finalize (GObject *object)
{
	CcMouseTest *self = CC_MOUSE_TEST (object);

	g_clear_object (&self->mouse_settings);

	if (self->information_label_timeout_id != 0) {
		g_source_remove (self->information_label_timeout_id);
		self->information_label_timeout_id = 0;
	}

	if (self->scroll_image_timeout_id != 0) {
		g_source_remove (self->scroll_image_timeout_id);
		self->scroll_image_timeout_id = 0;
	}

	if (self->button_drawing_area_timeout_id != 0) {
		g_source_remove (self->button_drawing_area_timeout_id);
		self->button_drawing_area_timeout_id = 0;
	}

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

static void
cc_mouse_test_class_init (CcMouseTestClass *klass)
{
	GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
	GObjectClass *object_class = G_OBJECT_CLASS (klass);

	object_class->finalize = cc_mouse_test_finalize;

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

	gtk_widget_class_bind_template_child (widget_class, CcMouseTest, button_drawing_area);
	gtk_widget_class_bind_template_child (widget_class, CcMouseTest, information_label);
	gtk_widget_class_bind_template_child (widget_class, CcMouseTest, image);
	gtk_widget_class_bind_template_child (widget_class, CcMouseTest, scrolled_window_adjustment);
	gtk_widget_class_bind_template_child (widget_class, CcMouseTest, viewport);

	gtk_widget_class_bind_template_callback (widget_class, button_drawing_area_button_pressed_cb);
}

static void
cc_mouse_test_init (CcMouseTest *self)
{
	gtk_widget_init_template (GTK_WIDGET (self));

	gtk_drawing_area_set_draw_func (GTK_DRAWING_AREA (self->button_drawing_area),
					button_drawing_area_draw_func,
					self, NULL);

	self->double_click_timestamp = 0;
	self->double_click_state = DOUBLE_CLICK_TEST_OFF;
	self->button_state = 0;

	self->mouse_settings = g_settings_new ("org.gnome.desktop.peripherals.mouse");

	self->information_label_timeout_id = 0;
	self->button_drawing_area_timeout_id = 0;
	self->scroll_image_timeout_id = 0;

	setup_dialog (self);
}

GtkWidget *
cc_mouse_test_new (void)
{
	return (GtkWidget *) g_object_new (CC_TYPE_MOUSE_TEST, NULL);
}