Apply Project Standard Autogen
[lxde/lxinput.git] / src / lxinput.c
CommitLineData
14f5ccb1
HJYP
1/*
2 * lxinput.c
3 *
4 * Copyright 2009 PCMan <pcman.tw@gmail.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19 * MA 02110-1301, USA.
20 */
21
22#ifdef HAVE_CONFIG_H
23#include <config.h>
24#endif
25
26#include <gtk/gtk.h>
27#include <glib/gi18n.h>
28#include <string.h>
29#include <math.h>
30
31#include <gdk/gdkx.h>
32#include <X11/Xlib.h>
33#include <X11/XKBlib.h>
34
c1d928f0 35static char* file = NULL;
14f5ccb1 36static GKeyFile* kf;
c1d928f0 37
14f5ccb1
HJYP
38static GtkWidget *dlg;
39static GtkRange *mouse_accel;
40static GtkRange *mouse_threshold;
41static GtkToggleButton* mouse_left_handed;
42static GtkRange *kb_delay;
43static GtkRange *kb_interval;
44static GtkToggleButton* kb_beep;
45
46static int accel = 20, old_accel = 20;
47static int threshold = 10, old_threshold = 10;
48static gboolean left_handed = FALSE, old_left_handed = FALSE;
49
50static int delay = 500, old_delay = 500;
51static int interval = 30, old_interval = 30;
52static gboolean beep = TRUE, old_beep = TRUE;
53
54
55static void on_mouse_accel_changed(GtkRange* range, gpointer user_data)
56{
57 accel = (int)gtk_range_get_value(range);
14f5ccb1
HJYP
58 XChangePointerControl(GDK_DISPLAY(), True, False,
59 accel, 10, 0);
60}
61
62static void on_mouse_threshold_changed(GtkRange* range, gpointer user_data)
63{
64 /* threshold = sensitivity - 100. The lower the threshold, the higher the sensitivity */
65 threshold = 110 - (int)gtk_range_get_value(range);
66 XChangePointerControl(GDK_DISPLAY(), False, True,
67 0, 10, threshold);
68}
69
70static void on_kb_range_changed(GtkRange* range, int* val)
71{
72 *val = (int)gtk_range_get_value(range);
73 /* apply keyboard values */
74 XkbSetAutoRepeatRate(GDK_DISPLAY(), XkbUseCoreKbd, delay, interval);
75}
76
77/* This function is taken from Gnome's control-center 2.6.0.3 (gnome-settings-mouse.c) and was modified*/
78#define DEFAULT_PTR_MAP_SIZE 128
79static void set_left_handed_mouse()
80{
81 unsigned char *buttons;
82 gint n_buttons, i;
83 gint idx_1 = 0, idx_3 = 1;
84
85 buttons = g_alloca (DEFAULT_PTR_MAP_SIZE);
86 n_buttons = XGetPointerMapping (GDK_DISPLAY(), buttons, DEFAULT_PTR_MAP_SIZE);
87 if (n_buttons > DEFAULT_PTR_MAP_SIZE)
88 {
89 buttons = g_alloca (n_buttons);
90 n_buttons = XGetPointerMapping (GDK_DISPLAY(), buttons, n_buttons);
91 }
92
93 for (i = 0; i < n_buttons; i++)
94 {
95 if (buttons[i] == 1)
96 idx_1 = i;
97 else if (buttons[i] == ((n_buttons < 3) ? 2 : 3))
98 idx_3 = i;
99 }
100
101 if ((left_handed && idx_1 < idx_3) ||
102 (!left_handed && idx_1 > idx_3))
103 {
104 buttons[idx_1] = ((n_buttons < 3) ? 2 : 3);
105 buttons[idx_3] = 1;
106 XSetPointerMapping (GDK_DISPLAY(), buttons, n_buttons);
107 }
108}
109
110static void on_left_handed_toggle(GtkToggleButton* btn, gpointer user_data)
111{
112 left_handed = gtk_toggle_button_get_active(btn);
113 set_left_handed_mouse(left_handed);
114}
115
116static void on_kb_beep_toggle(GtkToggleButton* btn, gpointer user_data)
117{
1de468f5 118 XKeyboardControl values;
14f5ccb1 119 beep = gtk_toggle_button_get_active(btn);
1de468f5
HJYP
120 values.bell_percent = beep ? -1 : 0;
121 XChangeKeyboardControl(GDK_DISPLAY(), KBBellPercent, &values);
14f5ccb1
HJYP
122}
123
124static gboolean on_change_val(GtkRange *range, GtkScrollType scroll,
125 gdouble value, gpointer user_data)
126{
127 int interval = GPOINTER_TO_INT(user_data);
128 return FALSE;
129}
130
131static void set_range_stops(GtkRange* range, int interval )
132{
133/*
134 g_signal_connect(range, "change-value",
135 G_CALLBACK(on_change_val), GINT_TO_POINTER(interval));
136*/
137}
138
139static void load_settings()
140{
c1d928f0
HJYP
141 /* load settings form LXDE config files */
142 file = g_build_filename( g_get_user_config_dir(), "lxde/config", NULL );
143 gboolean ret = g_key_file_load_from_file( kf, file, 0, NULL );
14f5ccb1 144
c1d928f0
HJYP
145 if( ret )
146 {
147 int val;
148 val = g_key_file_get_integer(kf, "Mouse", "AccFactor", NULL);
149 if( val > 0)
150 old_accel = accel = val;
151
152 val = g_key_file_get_integer(kf, "Mouse", "AccThreshold", NULL);
153 if( val > 0)
154 old_threshold = threshold = val;
155
156 old_left_handed = left_handed = g_key_file_get_boolean(kf, "Mouse", "LeftHanded", NULL);
157
158 val = g_key_file_get_integer(kf, "Keyboard", "Delay", NULL);
159 if(val > 0)
160 old_delay = delay = val;
161 val = g_key_file_get_integer(kf, "Keyboard", "Interval", NULL);
162 if(val > 0)
163 old_interval = interval = val;
164
165 if( g_key_file_has_key(kf, "Keyboard", "Beep", NULL ) )
166 old_beep = beep = g_key_file_get_boolean(kf, "Keyboard", "Beep", NULL);
167 }
14f5ccb1
HJYP
168}
169
170int main(int argc, char** argv)
171{
172 GtkBuilder* builder;
173 GError* err = NULL;
174 char* str = NULL;
175
176#ifdef ENABLE_NLS
177 bindtextdomain ( GETTEXT_PACKAGE, PACKAGE_LOCALE_DIR );
178 bind_textdomain_codeset ( GETTEXT_PACKAGE, "UTF-8" );
179 textdomain ( GETTEXT_PACKAGE );
180#endif
181
182 gtk_init(&argc, &argv);
183
60142a83 184 gtk_icon_theme_prepend_search_path(gtk_icon_theme_get_default(), PACKAGE_DATA_DIR);
185
14f5ccb1
HJYP
186 /* build the UI */
187 builder = gtk_builder_new();
188
189 gtk_builder_add_from_file( builder, PACKAGE_DATA_DIR "/lxinput.ui", NULL );
190 dlg = (GtkWidget*)gtk_builder_get_object( builder, "dlg" );
191 gtk_dialog_set_alternative_button_order( (GtkDialog*)dlg, GTK_RESPONSE_OK, GTK_RESPONSE_CANCEL, -1 );
192
193 mouse_accel = (GtkRange*)gtk_builder_get_object(builder,"mouse_accel");
194 mouse_threshold = (GtkRange*)gtk_builder_get_object(builder,"mouse_threshold");
195 mouse_left_handed = (GtkToggleButton*)gtk_builder_get_object(builder,"left_handed");
196
197 kb_delay = (GtkRange*)gtk_builder_get_object(builder,"kb_delay");
198 kb_interval = (GtkRange*)gtk_builder_get_object(builder,"kb_interval");
199 kb_beep = (GtkToggleButton*)gtk_builder_get_object(builder,"beep");
200
201 g_object_unref( builder );
202
c1d928f0
HJYP
203
204 /* read the config flie */
205 kf = g_key_file_new();
206 load_settings();
207
208 /* init the UI */
14f5ccb1
HJYP
209 gtk_range_set_value(mouse_accel, accel);
210 gtk_range_set_value(mouse_threshold, 110-threshold);
211 gtk_toggle_button_set_active(mouse_left_handed, left_handed);
212
213 gtk_range_set_value(kb_delay, delay);
214 gtk_range_set_value(kb_interval, interval);
215 gtk_toggle_button_set_active(kb_beep, beep);
216
14f5ccb1
HJYP
217 set_range_stops(mouse_accel, 10);
218 g_signal_connect(mouse_accel, "value-changed", G_CALLBACK(on_mouse_accel_changed), NULL);
219 set_range_stops(mouse_threshold, 10);
220 g_signal_connect(mouse_threshold, "value-changed", G_CALLBACK(on_mouse_threshold_changed), NULL);
221 g_signal_connect(mouse_left_handed, "toggled", G_CALLBACK(on_left_handed_toggle), NULL);
222
223 set_range_stops(kb_delay, 10);
224 g_signal_connect(kb_delay, "value-changed", G_CALLBACK(on_kb_range_changed), &kb_delay);
225 set_range_stops(kb_interval, 10);
226 g_signal_connect(kb_interval, "value-changed", G_CALLBACK(on_kb_range_changed), &kb_interval);
227 g_signal_connect(kb_beep, "toggled", G_CALLBACK(on_kb_beep_toggle), NULL);
228
14f5ccb1
HJYP
229 if( gtk_dialog_run( (GtkDialog*)dlg ) == GTK_RESPONSE_OK )
230 {
231 gsize len;
c1d928f0
HJYP
232
233 g_key_file_set_integer(kf, "Mouse", "AccFactor", accel);
234 g_key_file_set_integer(kf, "Mouse", "AccThreshold", threshold);
1de468f5 235 g_key_file_set_integer(kf, "Mouse", "LeftHanded", !!left_handed);
c1d928f0
HJYP
236
237 g_key_file_set_integer(kf, "Keyboard", "Delay", delay);
238 g_key_file_set_integer(kf, "Keyboard", "Interval", interval);
1de468f5 239 g_key_file_set_integer(kf, "Keyboard", "Beep", !!beep);
c1d928f0 240
14f5ccb1
HJYP
241 if( str = g_key_file_to_data( kf, &len, NULL ) )
242 {
c1d928f0
HJYP
243 if( g_file_set_contents( file, str, len, &err ) )
244 {
245 /* ask the settigns daemon to reload */
246 /* FIXME: is this needed? */
247 /* g_spawn_command_line_sync("lxde-settings-daemon reload", NULL, NULL, NULL, NULL); */
248 }
249 else
14f5ccb1
HJYP
250 {
251 g_error_free( err );
252 }
14f5ccb1
HJYP
253 g_free(str);
254 }
255 }
256 else
257 {
258 /* restore to original settings */
259
260 /* keyboard */
261 delay = old_delay;
262 interval = old_interval;
263 beep = old_beep;
264 XkbSetAutoRepeatRate(GDK_DISPLAY(), XkbUseCoreKbd, delay, interval);
265 /* FIXME: beep? */
266
267 /* mouse */
268 accel = old_accel;
269 threshold = old_threshold;
270 left_handed = old_left_handed;
271 XChangePointerControl(GDK_DISPLAY(), True, True,
272 accel, 10, (110 - threshold));
273 set_left_handed_mouse();
274 }
275
276 gtk_widget_destroy( dlg );
277
c1d928f0 278 g_free( file );
14f5ccb1
HJYP
279 g_key_file_free( kf );
280
281 return 0;
282}