1 /* -*- mode: c; style: linux -*- */
2 
3 /* mouse-properties-capplet.c
4  * Copyright (C) 2001 Red Hat, Inc.
5  * Copyright (C) 2001 Ximian, Inc.
6  *
7  * Written by: Jonathon Blandford <jrb@redhat.com>,
8  *             Bradford Hovinen <hovinen@ximian.com>,
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2, or (at your option)
13  * any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
23  * 02110-1301, USA.
24  */
25 
26 #include <config.h>
27 
28 #include <glib/gi18n.h>
29 #include <string.h>
30 #include <gio/gio.h>
31 #include <gdk/gdkx.h>
32 #include <math.h>
33 
34 #include "capplet-util.h"
35 #include "activate-settings-daemon.h"
36 #include "msd-input-helper.h"
37 
38 #include <X11/Xatom.h>
39 #include <X11/extensions/XInput.h>
40 
41 enum
42 {
43 	DOUBLE_CLICK_TEST_OFF,
44 	DOUBLE_CLICK_TEST_MAYBE,
45 	DOUBLE_CLICK_TEST_ON
46 };
47 
48 typedef enum {
49 	ACCEL_PROFILE_DEFAULT,
50 	ACCEL_PROFILE_ADAPTIVE,
51 	ACCEL_PROFILE_FLAT
52 } AccelProfile;
53 
54 #define MOUSE_SCHEMA "org.mate.peripherals-mouse"
55 #define INTERFACE_SCHEMA "org.mate.interface"
56 #define DOUBLE_CLICK_KEY "double-click"
57 
58 #define TOUCHPAD_SCHEMA "org.mate.peripherals-touchpad"
59 
60 /* State in testing the double-click speed. Global for a great deal of
61  * convenience
62  */
63 static gint double_click_state = DOUBLE_CLICK_TEST_OFF;
64 
65 static GSettings *mouse_settings = NULL;
66 static GSettings *interface_settings = NULL;
67 static GSettings *touchpad_settings = NULL;
68 
69 /* Double Click handling */
70 
71 struct test_data_t
72 {
73 	gint *timeout_id;
74 	GtkWidget *image;
75 };
76 
77 /* Timeout for the double click test */
78 
79 static gboolean
test_maybe_timeout(struct test_data_t * data)80 test_maybe_timeout (struct test_data_t *data)
81 {
82 	double_click_state = DOUBLE_CLICK_TEST_OFF;
83 
84 	gtk_image_set_from_resource (GTK_IMAGE (data->image), "/org/mate/mcc/mouse/double-click-off.svg");
85 
86 	*data->timeout_id = 0;
87 
88 	return FALSE;
89 }
90 
91 /* Callback issued when the user clicks the double click testing area. */
92 
93 static gboolean
event_box_button_press_event(GtkWidget * widget,GdkEventButton * event,gpointer user_data)94 event_box_button_press_event (GtkWidget   *widget,
95 			      GdkEventButton *event,
96 			      gpointer user_data)
97 {
98 	gint                       double_click_time;
99 	static struct test_data_t  data;
100 	static gint                test_on_timeout_id     = 0;
101 	static gint                test_maybe_timeout_id  = 0;
102 	static guint32             double_click_timestamp = 0;
103 	GtkWidget                 *image;
104 
105 	if (event->type != GDK_BUTTON_PRESS)
106 		return FALSE;
107 
108 	image = g_object_get_data (G_OBJECT (widget), "image");
109 
110 	double_click_time = g_settings_get_int (mouse_settings, DOUBLE_CLICK_KEY);
111 
112 	if (test_maybe_timeout_id != 0) {
113 		g_source_remove  (test_maybe_timeout_id);
114 		test_maybe_timeout_id = 0;
115 	}
116 	if (test_on_timeout_id != 0) {
117 		g_source_remove (test_on_timeout_id);
118 		test_on_timeout_id = 0;
119 	}
120 
121 	switch (double_click_state) {
122 	case DOUBLE_CLICK_TEST_OFF:
123 		double_click_state = DOUBLE_CLICK_TEST_MAYBE;
124 		data.image = image;
125 		data.timeout_id = &test_maybe_timeout_id;
126 		test_maybe_timeout_id = g_timeout_add (double_click_time, (GSourceFunc) test_maybe_timeout, &data);
127 		break;
128 	case DOUBLE_CLICK_TEST_MAYBE:
129 		if (event->time - double_click_timestamp < double_click_time) {
130 			double_click_state = DOUBLE_CLICK_TEST_ON;
131 			data.image = image;
132 			data.timeout_id = &test_on_timeout_id;
133 			test_on_timeout_id = g_timeout_add (2500, (GSourceFunc) test_maybe_timeout, &data);
134 		}
135 		break;
136 	case DOUBLE_CLICK_TEST_ON:
137 		double_click_state = DOUBLE_CLICK_TEST_OFF;
138 		break;
139 	}
140 
141 	double_click_timestamp = event->time;
142 
143 	switch (double_click_state) {
144 	case DOUBLE_CLICK_TEST_ON:
145 		gtk_image_set_from_resource (GTK_IMAGE (image), "/org/mate/mcc/mouse/double-click-on.svg");
146 		break;
147 	case DOUBLE_CLICK_TEST_MAYBE:
148 		gtk_image_set_from_resource (GTK_IMAGE (image), "/org/mate/mcc/mouse/double-click-maybe.svg");
149 		break;
150 	case DOUBLE_CLICK_TEST_OFF:
151 		gtk_image_set_from_resource (GTK_IMAGE (image), "/org/mate/mcc/mouse/double-click-off.svg");
152 		break;
153 	}
154 
155 	return TRUE;
156 }
157 
158 static void
orientation_radio_button_release_event(GtkWidget * widget,GdkEventButton * event)159 orientation_radio_button_release_event (GtkWidget   *widget,
160 				        GdkEventButton *event)
161 {
162 	gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (widget), TRUE);
163 }
164 
165 static void
orientation_radio_button_toggled(GtkToggleButton * togglebutton,GtkBuilder * dialog)166 orientation_radio_button_toggled (GtkToggleButton *togglebutton,
167 				        GtkBuilder *dialog)
168 {
169 	gboolean left_handed;
170 	left_handed = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (WID ("left_handed_radio")));
171 	g_settings_set_boolean (mouse_settings, "left-handed", left_handed);
172 }
173 
174 static void
synaptics_check_capabilities(GtkBuilder * dialog)175 synaptics_check_capabilities (GtkBuilder *dialog)
176 {
177 	GdkDisplay *display;
178 	int numdevices, i;
179 	XDeviceInfo *devicelist;
180 	Atom realtype, prop;
181 	int realformat;
182 	unsigned long nitems, bytes_after;
183 	unsigned char *data;
184 
185 	display = gdk_display_get_default ();
186 	prop = XInternAtom (GDK_DISPLAY_XDISPLAY(display), "Synaptics Capabilities", True);
187 	if (!prop)
188 		return;
189 
190 	devicelist = XListInputDevices (GDK_DISPLAY_XDISPLAY(display), &numdevices);
191 	for (i = 0; i < numdevices; i++) {
192 		if (devicelist[i].use != IsXExtensionPointer)
193 			continue;
194 
195 		gdk_x11_display_error_trap_push (display);
196 		XDevice *device = XOpenDevice (GDK_DISPLAY_XDISPLAY(display),
197 					       devicelist[i].id);
198 		if (gdk_x11_display_error_trap_pop (display))
199 			continue;
200 
201 		gdk_x11_display_error_trap_push (display);
202 		if ((XGetDeviceProperty (GDK_DISPLAY_XDISPLAY(display), device, prop, 0, 2, False,
203 					 XA_INTEGER, &realtype, &realformat, &nitems,
204 					 &bytes_after, &data) == Success) && (realtype != None)) {
205 			/* Property data is booleans for has_left, has_middle,
206 			 * has_right, has_double, has_triple */
207 			if (!data[0]) {
208 				gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (WID ("tap_to_click_toggle")), TRUE);
209 				gtk_widget_set_sensitive (WID ("tap_to_click_toggle"), FALSE);
210 			}
211 
212 			XFree (data);
213 		}
214 
215 		gdk_x11_display_error_trap_pop_ignored (display);
216 
217 		XCloseDevice (GDK_DISPLAY_XDISPLAY(display), device);
218 	}
219 	XFreeDeviceList (devicelist);
220 }
221 
222 static void
accel_profile_combobox_changed_callback(GtkWidget * combobox,void * data)223 accel_profile_combobox_changed_callback (GtkWidget *combobox, void *data)
224 {
225 	AccelProfile value = gtk_combo_box_get_active (GTK_COMBO_BOX (combobox));
226 	g_settings_set_enum (mouse_settings, (const gchar *) "accel-profile", value);
227 }
228 
229 static void
comboxbox_changed(GtkWidget * combobox,GtkBuilder * dialog,const char * key)230 comboxbox_changed (GtkWidget *combobox, GtkBuilder *dialog, const char *key)
231 {
232 	gint value = gtk_combo_box_get_active (GTK_COMBO_BOX (combobox));
233 	gint value2, value3;
234 	GtkLabel *warn = GTK_LABEL (WID ("multi_finger_warning"));
235 
236 	g_settings_set_int (touchpad_settings, (const gchar *) key, value);
237 
238 	/* Show warning if some multi-finger click emulation is enabled. */
239 	value2 = g_settings_get_int (touchpad_settings, "two-finger-click");
240 	value3 = g_settings_get_int (touchpad_settings, "three-finger-click");
241 	gtk_widget_set_opacity (GTK_WIDGET (warn), (value2 || value3)?  1.0: 0.0);
242 }
243 
244 static void
comboxbox_two_finger_changed_callback(GtkWidget * combobox,void * data)245 comboxbox_two_finger_changed_callback (GtkWidget *combobox, void *data)
246 {
247 	comboxbox_changed (combobox, GTK_BUILDER (data), "two-finger-click");
248 }
249 
250 static void
comboxbox_three_finger_changed_callback(GtkWidget * combobox,void * data)251 comboxbox_three_finger_changed_callback (GtkWidget *combobox, void *data)
252 {
253 	comboxbox_changed (combobox, GTK_BUILDER (data), "three-finger-click");
254 }
255 
256 /* Set up the property editors in the dialog. */
257 static void
setup_dialog(GtkBuilder * dialog)258 setup_dialog (GtkBuilder *dialog)
259 {
260 	GtkRadioButton    *radio;
261 
262 	/* Orientation radio buttons */
263 	radio = GTK_RADIO_BUTTON (WID ("left_handed_radio"));
264 	gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON(radio),
265 		g_settings_get_boolean(mouse_settings, "left-handed"));
266 	/* explicitly connect to button-release so that you can change orientation with either button */
267 	g_signal_connect (WID ("right_handed_radio"), "button_release_event",
268 		G_CALLBACK (orientation_radio_button_release_event), NULL);
269 	g_signal_connect (WID ("left_handed_radio"), "button_release_event",
270 		G_CALLBACK (orientation_radio_button_release_event), NULL);
271 	g_signal_connect (WID ("left_handed_radio"), "toggled",
272 		G_CALLBACK (orientation_radio_button_toggled), dialog);
273 
274 	/* Locate pointer toggle */
275 	g_settings_bind (mouse_settings, "locate-pointer", WID ("locate_pointer_toggle"),
276 		"active", G_SETTINGS_BIND_DEFAULT);
277 
278 	/* Middle Button Emulation */
279 	g_settings_bind (mouse_settings, "middle-button-enabled", WID ("middle_button_emulation_toggle"),
280 		"active", G_SETTINGS_BIND_DEFAULT);
281 
282 	/* Middle Button Paste */
283 	g_settings_bind (interface_settings, "gtk-enable-primary-paste", WID ("middle_button_paste_toggle"),
284 		"active", G_SETTINGS_BIND_DEFAULT);
285 
286 
287 	/* Double-click time */
288 	g_settings_bind (mouse_settings, DOUBLE_CLICK_KEY,
289 		gtk_range_get_adjustment (GTK_RANGE (WID ("delay_scale"))), "value",
290 		G_SETTINGS_BIND_DEFAULT);
291 
292 	gtk_image_set_from_resource (GTK_IMAGE (WID ("double_click_image")), "/org/mate/mcc/mouse/double-click-off.svg");
293 	g_object_set_data (G_OBJECT (WID ("double_click_eventbox")), "image", WID ("double_click_image"));
294 	g_signal_connect (WID ("double_click_eventbox"), "button_press_event",
295 			  G_CALLBACK (event_box_button_press_event), NULL);
296 
297 	/* speed */
298 	g_settings_bind (mouse_settings, "motion-acceleration",
299 		gtk_range_get_adjustment (GTK_RANGE (WID ("accel_scale"))), "value",
300 		G_SETTINGS_BIND_DEFAULT);
301 	g_settings_bind (mouse_settings, "motion-threshold",
302 		gtk_range_get_adjustment (GTK_RANGE (WID ("sensitivity_scale"))), "value",
303 		G_SETTINGS_BIND_DEFAULT);
304 
305 	g_signal_connect (WID ("mouse_accel_profile"), "changed",
306 			  G_CALLBACK (accel_profile_combobox_changed_callback), NULL);
307 	gtk_combo_box_set_active (GTK_COMBO_BOX (WID ("mouse_accel_profile")),
308 				  g_settings_get_enum (mouse_settings, "accel-profile"));
309 
310 	/* DnD threshold */
311 	g_settings_bind (mouse_settings, "drag-threshold",
312 		gtk_range_get_adjustment (GTK_RANGE (WID ("drag_threshold_scale"))), "value",
313 		G_SETTINGS_BIND_DEFAULT);
314 
315 	/* Trackpad page */
316 	if (touchpad_is_present () == FALSE)
317 		gtk_notebook_remove_page (GTK_NOTEBOOK (WID ("prefs_widget")), -1);
318 	else {
319 		g_settings_bind (touchpad_settings, "touchpad-enabled",
320 			WID ("touchpad_enable"), "active",
321 			G_SETTINGS_BIND_DEFAULT);
322 		g_settings_bind (touchpad_settings, "touchpad-enabled",
323 			WID ("vbox_touchpad_general"), "sensitive",
324 			G_SETTINGS_BIND_DEFAULT);
325 		g_settings_bind (touchpad_settings, "touchpad-enabled",
326 			WID ("vbox_touchpad_scrolling"), "sensitive",
327 			G_SETTINGS_BIND_DEFAULT);
328 		g_settings_bind (touchpad_settings, "touchpad-enabled",
329 			WID ("vbox_touchpad_pointer_speed"), "sensitive",
330 			G_SETTINGS_BIND_DEFAULT);
331 		g_settings_bind (touchpad_settings, "disable-while-typing",
332 			WID ("disable_w_typing_toggle"), "active",
333 			G_SETTINGS_BIND_DEFAULT);
334 		g_settings_bind (touchpad_settings, "tap-to-click",
335 			WID ("tap_to_click_toggle"), "active",
336 			G_SETTINGS_BIND_DEFAULT);
337 		g_settings_bind (touchpad_settings, "vertical-edge-scrolling", WID ("vert_edge_scroll_toggle"), "active", G_SETTINGS_BIND_DEFAULT);
338 		g_settings_bind (touchpad_settings, "horizontal-edge-scrolling", WID ("horiz_edge_scroll_toggle"), "active", G_SETTINGS_BIND_DEFAULT);
339 		g_settings_bind (touchpad_settings, "vertical-two-finger-scrolling", WID ("vert_twofinger_scroll_toggle"), "active", G_SETTINGS_BIND_DEFAULT);
340 		g_settings_bind (touchpad_settings, "horizontal-two-finger-scrolling", WID ("horiz_twofinger_scroll_toggle"), "active", G_SETTINGS_BIND_DEFAULT);
341 		g_settings_bind (touchpad_settings, "natural-scroll", WID ("natural_scroll_toggle"), "active", G_SETTINGS_BIND_DEFAULT);
342 
343 		char * emulation_values[] = { _("Disabled"), _("Left button"), _("Middle button"), _("Right button") };
344 
345 		GtkWidget *two_click_comboxbox = gtk_combo_box_text_new ();
346 		GtkWidget *three_click_comboxbox = gtk_combo_box_text_new ();
347 		gtk_box_pack_start (GTK_BOX (WID ("hbox_two_finger_click")), two_click_comboxbox, FALSE, FALSE, 6);
348 		gtk_box_pack_start (GTK_BOX (WID ("hbox_three_finger_click")), three_click_comboxbox, FALSE, FALSE, 6);
349 		int i;
350 		for (i=0; i<4; i++) {
351 			gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT (two_click_comboxbox), emulation_values[i]);
352 			gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT (three_click_comboxbox), emulation_values[i]);
353 		}
354 
355 		g_signal_connect (two_click_comboxbox, "changed", G_CALLBACK (comboxbox_two_finger_changed_callback), dialog);
356 		g_signal_connect (three_click_comboxbox, "changed", G_CALLBACK (comboxbox_three_finger_changed_callback), dialog);
357 		gtk_combo_box_set_active (GTK_COMBO_BOX (two_click_comboxbox), g_settings_get_int (touchpad_settings, "two-finger-click"));
358 		gtk_combo_box_set_active (GTK_COMBO_BOX (three_click_comboxbox), g_settings_get_int (touchpad_settings, "three-finger-click"));
359 		gtk_widget_show (two_click_comboxbox);
360 		gtk_widget_show (three_click_comboxbox);
361 
362 		/* speed */
363 		g_settings_bind (touchpad_settings, "motion-acceleration",
364 			gtk_range_get_adjustment (GTK_RANGE (WID ("touchpad_accel_scale"))), "value",
365 			G_SETTINGS_BIND_DEFAULT);
366 		g_settings_bind (touchpad_settings, "motion-threshold",
367 			gtk_range_get_adjustment (GTK_RANGE (WID ("touchpad_sensitivity_scale"))), "value",
368 			G_SETTINGS_BIND_DEFAULT);
369 
370 		synaptics_check_capabilities (dialog);
371 	}
372 
373 }
374 
375 /* Callback issued when a button is clicked on the dialog */
376 
377 static void
dialog_response_cb(GtkDialog * dialog,gint response_id,gpointer data)378 dialog_response_cb (GtkDialog *dialog, gint response_id, gpointer data)
379 {
380 	if (response_id == GTK_RESPONSE_HELP)
381 		capplet_help (GTK_WINDOW (dialog),
382 			      "goscustperiph-5");
383 	else
384 		gtk_main_quit ();
385 }
386 
387 int
main(int argc,char ** argv)388 main (int argc, char **argv)
389 {
390 	GtkBuilder     *dialog;
391 	GtkWidget      *dialog_win, *w;
392 	gchar *start_page = NULL;
393 
394 	GOptionContext *context;
395 	GOptionEntry cap_options[] = {
396 		{"show-page", 'p', G_OPTION_FLAG_IN_MAIN,
397 		 G_OPTION_ARG_STRING,
398 		 &start_page,
399 		 /* TRANSLATORS: don't translate the terms in brackets */
400 		 N_("Specify the name of the page to show (general)"),
401 		 N_("page") },
402 		{NULL}
403 	};
404 
405 	context = g_option_context_new (_("- MATE Mouse Preferences"));
406 	g_option_context_add_main_entries (context, cap_options, GETTEXT_PACKAGE);
407 	capplet_init (context, &argc, &argv);
408 
409 	activate_settings_daemon ();
410 
411 	mouse_settings = g_settings_new (MOUSE_SCHEMA);
412 	interface_settings = g_settings_new (INTERFACE_SCHEMA);
413 	touchpad_settings = g_settings_new (TOUCHPAD_SCHEMA);
414 
415 	dialog = gtk_builder_new_from_resource ("/org/mate/mcc/mouse/mate-mouse-properties.ui");
416 
417 	setup_dialog (dialog);
418 
419 	dialog_win = WID ("mouse_properties_dialog");
420 	g_signal_connect (dialog_win, "response",
421 			  G_CALLBACK (dialog_response_cb), NULL);
422 
423 	GtkNotebook* nb = GTK_NOTEBOOK (WID ("prefs_widget"));
424 	gtk_widget_add_events (GTK_WIDGET (nb), GDK_SCROLL_MASK);
425 	g_signal_connect (GTK_WIDGET (nb),
426 	                  "scroll-event",
427 	                  G_CALLBACK (capplet_notebook_scroll_event_cb),
428 	                  NULL);
429 
430 
431 	if (start_page != NULL) {
432 		gchar *page_name;
433 
434 		page_name = g_strconcat (start_page, "_vbox", NULL);
435 		g_free (start_page);
436 
437 		w = WID (page_name);
438 		if (w != NULL) {
439 			gint pindex;
440 
441 			pindex = gtk_notebook_page_num (nb, w);
442 			if (pindex != -1)
443 				gtk_notebook_set_current_page (nb, pindex);
444 		}
445 		g_free (page_name);
446 	}
447 
448 	capplet_set_icon (dialog_win, "input-mouse");
449 	gtk_widget_show (dialog_win);
450 
451 	gtk_main ();
452 
453 	g_object_unref (dialog);
454 	g_object_unref (mouse_settings);
455 	g_object_unref (interface_settings);
456 	g_object_unref (touchpad_settings);
457 
458 	return 0;
459 }
460