1
/* -*- mode: c; style: linux -*- */
3
/* mouse-properties-capplet.c
4
* Copyright (C) 2001 Red Hat, Inc.
5
* Copyright (C) 2001 Ximian, Inc.
7
* Written by: Jonathon Blandford <jrb@redhat.com>,
8
* Bradford Hovinen <hovinen@ximian.com>,
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)
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.
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., 59 Temple Place - Suite 330, Boston, MA
28
#include <glib/gi18n.h>
30
#include <gconf/gconf-client.h>
34
#include "capplet-util.h"
35
#include "gconf-property-editor.h"
36
#include "activate-settings-daemon.h"
37
#include "capplet-stock-icons.h"
38
#include "gnome-mouse-accessibility.h"
40
#include <sys/types.h>
44
#include <X11/Xatom.h>
45
#include <X11/extensions/XInput.h>
49
#include <X11/Xcursor/Xcursor.h>
54
DOUBLE_CLICK_TEST_OFF,
55
DOUBLE_CLICK_TEST_MAYBE,
59
/* We use this in at least half a dozen places, so it makes sense just to
62
#define DOUBLE_CLICK_KEY "/desktop/gnome/peripherals/mouse/double_click"
64
/* State in testing the double-click speed. Global for a great deal of
67
static gint double_click_state = DOUBLE_CLICK_TEST_OFF;
69
/* normalization routines */
70
/* All of our scales but double_click are on the range 1->10 as a result, we
71
* have a few routines to convert from whatever the gconf key is to our range.
74
double_click_from_gconf (GConfPropertyEditor *peditor, const GConfValue *value)
76
GConfValue *new_value;
78
new_value = gconf_value_new (GCONF_VALUE_INT);
79
gconf_value_set_int (new_value, CLAMP ((int) floor ((gconf_value_get_int (value) + 50) / 100.0) * 100, 100, 1000));
84
get_default_mouse_info (int *default_numerator, int *default_denominator, int *default_threshold)
86
int numerator, denominator;
88
int tmp_num, tmp_den, tmp_threshold;
90
/* Query X for the default value */
91
XGetPointerControl (GDK_DISPLAY (), &numerator, &denominator,
93
XChangePointerControl (GDK_DISPLAY (), True, True, -1, -1, -1);
94
XGetPointerControl (GDK_DISPLAY (), &tmp_num, &tmp_den, &tmp_threshold);
95
XChangePointerControl (GDK_DISPLAY (), True, True, numerator, denominator, threshold);
97
if (default_numerator)
98
*default_numerator = tmp_num;
100
if (default_denominator)
101
*default_denominator = tmp_den;
103
if (default_threshold)
104
*default_threshold = tmp_threshold;
109
motion_acceleration_from_gconf (GConfPropertyEditor *peditor,
110
const GConfValue *value)
112
GConfValue *new_value;
113
gfloat motion_acceleration;
115
new_value = gconf_value_new (GCONF_VALUE_FLOAT);
117
if (gconf_value_get_float (value) == -1.0) {
118
int numerator, denominator;
120
get_default_mouse_info (&numerator, &denominator, NULL);
122
motion_acceleration = CLAMP ((gfloat)(numerator / denominator), 0.2, 6.0);
125
motion_acceleration = CLAMP (gconf_value_get_float (value), 0.2, 6.0);
128
if (motion_acceleration >= 1)
129
gconf_value_set_float (new_value, motion_acceleration + 4);
131
gconf_value_set_float (new_value, motion_acceleration * 5);
137
motion_acceleration_to_gconf (GConfPropertyEditor *peditor,
138
const GConfValue *value)
140
GConfValue *new_value;
141
gfloat motion_acceleration;
143
new_value = gconf_value_new (GCONF_VALUE_FLOAT);
144
motion_acceleration = CLAMP (gconf_value_get_float (value), 1.0, 10.0);
146
if (motion_acceleration < 5)
147
gconf_value_set_float (new_value, motion_acceleration / 5.0);
149
gconf_value_set_float (new_value, motion_acceleration - 4);
155
threshold_from_gconf (GConfPropertyEditor *peditor,
156
const GConfValue *value)
158
GConfValue *new_value;
160
new_value = gconf_value_new (GCONF_VALUE_FLOAT);
162
if (gconf_value_get_int (value) == -1) {
165
get_default_mouse_info (NULL, NULL, &threshold);
166
gconf_value_set_float (new_value, CLAMP (threshold, 1, 10));
169
gconf_value_set_float (new_value, CLAMP (gconf_value_get_int (value), 1, 10));
176
drag_threshold_from_gconf (GConfPropertyEditor *peditor,
177
const GConfValue *value)
179
GConfValue *new_value;
181
new_value = gconf_value_new (GCONF_VALUE_FLOAT);
183
gconf_value_set_float (new_value, CLAMP (gconf_value_get_int (value), 1, 10));
188
/* Double Click handling */
196
/* Timeout for the double click test */
199
test_maybe_timeout (struct test_data_t *data)
201
double_click_state = DOUBLE_CLICK_TEST_OFF;
203
gtk_image_set_from_stock (GTK_IMAGE (data->image),
204
MOUSE_DBLCLCK_OFF, mouse_capplet_dblclck_icon_get_size());
206
*data->timeout_id = 0;
211
/* Callback issued when the user clicks the double click testing area. */
214
event_box_button_press_event (GtkWidget *widget,
215
GdkEventButton *event,
216
GConfChangeSet *changeset)
218
gint double_click_time;
220
static struct test_data_t data;
221
static gint test_on_timeout_id = 0;
222
static gint test_maybe_timeout_id = 0;
223
static guint32 double_click_timestamp = 0;
227
if (event->type != GDK_BUTTON_PRESS)
230
image = g_object_get_data (G_OBJECT (widget), "image");
232
if (!(changeset && gconf_change_set_check_value (changeset, DOUBLE_CLICK_KEY, &value))) {
233
client = gconf_client_get_default();
234
double_click_time = gconf_client_get_int (client, DOUBLE_CLICK_KEY, NULL);
235
g_object_unref (client);
238
double_click_time = gconf_value_get_int (value);
240
if (test_maybe_timeout_id != 0)
241
g_source_remove (test_maybe_timeout_id);
242
if (test_on_timeout_id != 0)
243
g_source_remove (test_on_timeout_id);
245
switch (double_click_state) {
246
case DOUBLE_CLICK_TEST_OFF:
247
double_click_state = DOUBLE_CLICK_TEST_MAYBE;
249
data.timeout_id = &test_maybe_timeout_id;
250
test_maybe_timeout_id = g_timeout_add (double_click_time, (GtkFunction) test_maybe_timeout, &data);
252
case DOUBLE_CLICK_TEST_MAYBE:
253
if (event->time - double_click_timestamp < double_click_time) {
254
double_click_state = DOUBLE_CLICK_TEST_ON;
256
data.timeout_id = &test_on_timeout_id;
257
test_on_timeout_id = g_timeout_add (2500, (GtkFunction) test_maybe_timeout, &data);
260
case DOUBLE_CLICK_TEST_ON:
261
double_click_state = DOUBLE_CLICK_TEST_OFF;
265
double_click_timestamp = event->time;
267
switch (double_click_state) {
268
case DOUBLE_CLICK_TEST_ON:
269
gtk_image_set_from_stock (GTK_IMAGE (image),
270
MOUSE_DBLCLCK_ON, mouse_capplet_dblclck_icon_get_size());
272
case DOUBLE_CLICK_TEST_MAYBE:
273
gtk_image_set_from_stock (GTK_IMAGE (image),
274
MOUSE_DBLCLCK_MAYBE, mouse_capplet_dblclck_icon_get_size());
276
case DOUBLE_CLICK_TEST_OFF:
277
gtk_image_set_from_stock (GTK_IMAGE (image),
278
MOUSE_DBLCLCK_OFF, mouse_capplet_dblclck_icon_get_size());
286
orientation_radio_button_release_event (GtkWidget *widget,
287
GdkEventButton *event)
289
gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (widget), TRUE);
293
left_handed_from_gconf (GConfPropertyEditor *peditor,
294
const GConfValue *value)
296
GConfValue *new_value;
298
new_value = gconf_value_new (GCONF_VALUE_INT);
300
gconf_value_set_int (new_value, gconf_value_get_bool (value));
306
left_handed_to_gconf (GConfPropertyEditor *peditor,
307
const GConfValue *value)
309
GConfValue *new_value;
311
new_value = gconf_value_new (GCONF_VALUE_BOOL);
313
gconf_value_set_bool (new_value, gconf_value_get_int (value) == 1);
319
scrollmethod_changed_event (GConfPropertyEditor *peditor,
321
const GConfValue *value,
324
GtkToggleButton *disabled = GTK_TOGGLE_BUTTON (WID ("scroll_disabled_radio"));
326
gtk_widget_set_sensitive (WID ("horiz_scroll_toggle"),
327
!gtk_toggle_button_get_active (disabled));
331
synaptics_check_capabilities (GtkBuilder *dialog)
335
XDeviceInfo *devicelist;
338
unsigned long nitems, bytes_after;
341
prop = XInternAtom (GDK_DISPLAY (), "Synaptics Capabilities", True);
345
devicelist = XListInputDevices (GDK_DISPLAY (), &numdevices);
346
for (i = 0; i < numdevices; i++) {
347
if (devicelist[i].use != IsXExtensionPointer)
350
gdk_error_trap_push ();
351
XDevice *device = XOpenDevice (GDK_DISPLAY (),
353
if (gdk_error_trap_pop ())
356
gdk_error_trap_push ();
357
if ((XGetDeviceProperty (GDK_DISPLAY (), device, prop, 0, 2, False,
358
XA_INTEGER, &realtype, &realformat, &nitems,
359
&bytes_after, &data) == Success) && (realtype != None)) {
360
/* Property data is booleans for has_left, has_middle, has_right, has_double, has_triple.
361
* Newer drivers (X.org/kerrnel) will also include has_pressure and has_width. */
363
gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (WID ("tap_to_click_toggle")), TRUE);
364
gtk_widget_set_sensitive (WID ("tap_to_click_toggle"), FALSE);
367
/* Disable two finger scrolling unless the hardware supports
368
* double touch or can emulate it based on finger width. */
369
if (!(data[3] ||(nitems >= 6 && data[5])))
370
gtk_widget_set_sensitive (WID ("scroll_twofinger_radio"), FALSE);
374
gdk_error_trap_pop ();
376
XCloseDevice (GDK_DISPLAY (), device);
378
XFreeDeviceList (devicelist);
383
find_synaptics (void)
385
gboolean ret = FALSE;
388
XDeviceInfo *devicelist;
391
unsigned long nitems, bytes_after;
393
XExtensionVersion *version;
395
/* Input device properties require version 1.5 or higher */
396
version = XGetExtensionVersion (GDK_DISPLAY (), "XInputExtension");
397
if (!version->present ||
398
(version->major_version * 1000 + version->minor_version) < 1005) {
403
prop = XInternAtom (GDK_DISPLAY (), "Synaptics Off", True);
407
devicelist = XListInputDevices (GDK_DISPLAY (), &numdevices);
408
for (i = 0; i < numdevices; i++) {
409
if (devicelist[i].use != IsXExtensionPointer)
412
gdk_error_trap_push();
413
XDevice *device = XOpenDevice (GDK_DISPLAY (),
415
if (gdk_error_trap_pop ())
418
gdk_error_trap_push ();
419
if ((XGetDeviceProperty (GDK_DISPLAY (), device, prop, 0, 1, False,
420
XA_INTEGER, &realtype, &realformat, &nitems,
421
&bytes_after, &data) == Success) && (realtype != None)) {
425
gdk_error_trap_pop ();
427
XCloseDevice (GDK_DISPLAY (), device);
434
XFreeDeviceList (devicelist);
439
/* Set up the property editors in the dialog. */
441
setup_dialog (GtkBuilder *dialog, GConfChangeSet *changeset)
443
GtkRadioButton *radio;
446
/* Orientation radio buttons */
447
radio = GTK_RADIO_BUTTON (WID ("left_handed_radio"));
448
peditor = gconf_peditor_new_select_radio
449
(changeset, "/desktop/gnome/peripherals/mouse/left_handed", gtk_radio_button_get_group (radio),
450
"conv-to-widget-cb", left_handed_from_gconf,
451
"conv-from-widget-cb", left_handed_to_gconf,
453
/* explicitly connect to button-release so that you can change orientation with either button */
454
g_signal_connect (WID ("right_handed_radio"), "button_release_event",
455
G_CALLBACK (orientation_radio_button_release_event), NULL);
456
g_signal_connect (WID ("left_handed_radio"), "button_release_event",
457
G_CALLBACK (orientation_radio_button_release_event), NULL);
459
/* Locate pointer toggle */
460
peditor = gconf_peditor_new_boolean
461
(changeset, "/desktop/gnome/peripherals/mouse/locate_pointer", WID ("locate_pointer_toggle"), NULL);
463
/* Double-click time */
464
peditor = gconf_peditor_new_numeric_range
465
(changeset, DOUBLE_CLICK_KEY, WID ("delay_scale"),
466
"conv-to-widget-cb", double_click_from_gconf,
468
gtk_image_set_from_stock (GTK_IMAGE (WID ("double_click_image")), MOUSE_DBLCLCK_OFF, mouse_capplet_dblclck_icon_get_size ());
469
g_object_set_data (G_OBJECT (WID ("double_click_eventbox")), "image", WID ("double_click_image"));
470
g_signal_connect (WID ("double_click_eventbox"), "button_press_event",
471
G_CALLBACK (event_box_button_press_event), changeset);
474
gconf_peditor_new_numeric_range
475
(changeset, "/desktop/gnome/peripherals/mouse/motion_acceleration", WID ("accel_scale"),
476
"conv-to-widget-cb", motion_acceleration_from_gconf,
477
"conv-from-widget-cb", motion_acceleration_to_gconf,
480
gconf_peditor_new_numeric_range
481
(changeset, "/desktop/gnome/peripherals/mouse/motion_threshold", WID ("sensitivity_scale"),
482
"conv-to-widget-cb", threshold_from_gconf,
486
gconf_peditor_new_numeric_range
487
(changeset, "/desktop/gnome/peripherals/mouse/drag_threshold", WID ("drag_threshold_scale"),
488
"conv-to-widget-cb", drag_threshold_from_gconf,
492
if (find_synaptics () == FALSE)
493
gtk_notebook_remove_page (GTK_NOTEBOOK (WID ("prefs_widget")), -1);
495
gconf_peditor_new_boolean
496
(changeset, "/desktop/gnome/peripherals/touchpad/disable_while_typing", WID ("disable_w_typing_toggle"), NULL);
497
gconf_peditor_new_boolean
498
(changeset, "/desktop/gnome/peripherals/touchpad/tap_to_click", WID ("tap_to_click_toggle"), NULL);
499
gconf_peditor_new_boolean
500
(changeset, "/desktop/gnome/peripherals/touchpad/horiz_scroll_enabled", WID ("horiz_scroll_toggle"), NULL);
501
radio = GTK_RADIO_BUTTON (WID ("scroll_disabled_radio"));
502
peditor = gconf_peditor_new_select_radio
503
(changeset, "/desktop/gnome/peripherals/touchpad/scroll_method", gtk_radio_button_get_group (radio),
506
synaptics_check_capabilities (dialog);
507
scrollmethod_changed_event (GCONF_PROPERTY_EDITOR (peditor), NULL, NULL, dialog);
508
g_signal_connect (peditor, "value-changed",
509
G_CALLBACK (scrollmethod_changed_event), dialog);
514
/* Construct the dialog */
520
GtkSizeGroup *size_group;
521
GError *error = NULL;
523
dialog = gtk_builder_new ();
524
gtk_builder_add_from_file (dialog, GNOMECC_UI_DIR "/gnome-mouse-properties.ui", &error);
526
g_warning ("Error loading UI file: %s", error->message);
530
size_group = gtk_size_group_new (GTK_SIZE_GROUP_HORIZONTAL);
531
gtk_size_group_add_widget (size_group, WID ("acceleration_label"));
532
gtk_size_group_add_widget (size_group, WID ("sensitivity_label"));
533
gtk_size_group_add_widget (size_group, WID ("threshold_label"));
534
gtk_size_group_add_widget (size_group, WID ("timeout_label"));
536
size_group = gtk_size_group_new (GTK_SIZE_GROUP_HORIZONTAL);
537
gtk_size_group_add_widget (size_group, WID ("acceleration_fast_label"));
538
gtk_size_group_add_widget (size_group, WID ("sensitivity_high_label"));
539
gtk_size_group_add_widget (size_group, WID ("threshold_large_label"));
540
gtk_size_group_add_widget (size_group, WID ("timeout_long_label"));
542
size_group = gtk_size_group_new (GTK_SIZE_GROUP_HORIZONTAL);
543
gtk_size_group_add_widget (size_group, WID ("acceleration_slow_label"));
544
gtk_size_group_add_widget (size_group, WID ("sensitivity_low_label"));
545
gtk_size_group_add_widget (size_group, WID ("threshold_small_label"));
546
gtk_size_group_add_widget (size_group, WID ("timeout_short_label"));
548
size_group = gtk_size_group_new (GTK_SIZE_GROUP_HORIZONTAL);
549
gtk_size_group_add_widget (size_group, WID ("simulated_delay_label"));
550
gtk_size_group_add_widget (size_group, WID ("dwell_delay_label"));
551
gtk_size_group_add_widget (size_group, WID ("dwell_threshold_label"));
553
size_group = gtk_size_group_new (GTK_SIZE_GROUP_HORIZONTAL);
554
gtk_size_group_add_widget (size_group, WID ("simulated_delay_short_label"));
555
gtk_size_group_add_widget (size_group, WID ("dwell_delay_short_label"));
556
gtk_size_group_add_widget (size_group, WID ("dwell_threshold_small_label"));
558
size_group = gtk_size_group_new (GTK_SIZE_GROUP_HORIZONTAL);
559
gtk_size_group_add_widget (size_group, WID ("simulated_delay_long_label"));
560
gtk_size_group_add_widget (size_group, WID ("dwell_delay_long_label"));
561
gtk_size_group_add_widget (size_group, WID ("dwell_threshold_large_label"));
566
/* Callback issued when a button is clicked on the dialog */
569
dialog_response_cb (GtkDialog *dialog, gint response_id, GConfChangeSet *changeset)
571
if (response_id == GTK_RESPONSE_HELP)
572
capplet_help (GTK_WINDOW (dialog),
579
main (int argc, char **argv)
583
GtkWidget *dialog_win, *w;
584
gchar *start_page = NULL;
586
GOptionContext *context;
587
GOptionEntry cap_options[] = {
588
{"show-page", 'p', G_OPTION_FLAG_IN_MAIN,
591
/* TRANSLATORS: don't translate the terms in brackets */
592
N_("Specify the name of the page to show (general|accessibility)"),
597
context = g_option_context_new (_("- GNOME Mouse Preferences"));
598
g_option_context_add_main_entries (context, cap_options, GETTEXT_PACKAGE);
599
capplet_init (context, &argc, &argv);
601
capplet_init_stock_icons ();
603
activate_settings_daemon ();
605
client = gconf_client_get_default ();
606
gconf_client_add_dir (client, "/desktop/gnome/peripherals/mouse", GCONF_CLIENT_PRELOAD_ONELEVEL, NULL);
607
gconf_client_add_dir (client, "/desktop/gnome/peripherals/touchpad", GCONF_CLIENT_PRELOAD_ONELEVEL, NULL);
609
dialog = create_dialog ();
612
setup_dialog (dialog, NULL);
613
setup_accessibility (dialog, client);
615
dialog_win = WID ("mouse_properties_dialog");
616
g_signal_connect (dialog_win, "response",
617
G_CALLBACK (dialog_response_cb), NULL);
619
if (start_page != NULL) {
622
page_name = g_strconcat (start_page, "_vbox", NULL);
630
nb = GTK_NOTEBOOK (WID ("prefs_widget"));
631
pindex = gtk_notebook_page_num (nb, w);
633
gtk_notebook_set_current_page (nb, pindex);
638
capplet_set_icon (dialog_win, "input-mouse");
639
gtk_widget_show (dialog_win);
643
g_object_unref (dialog);
646
g_object_unref (client);