l10n: Updated Slovak (sk) translation to 100%
[lxde/lxinput.git] / src / lxinput.c
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
35 static char* file = NULL;
36 static GKeyFile* kf;
37
38 static GtkWidget *dlg;
39 static GtkRange *mouse_accel;
40 static GtkRange *mouse_threshold;
41 static GtkToggleButton* mouse_left_handed;
42 static GtkRange *kb_delay;
43 static GtkRange *kb_interval;
44 static GtkToggleButton* kb_beep;
45
46 static int accel = 20, old_accel = 20;
47 static int threshold = 10, old_threshold = 10;
48 static gboolean left_handed = FALSE, old_left_handed = FALSE;
49
50 static int delay = 500, old_delay = 500;
51 static int interval = 30, old_interval = 30;
52 static gboolean beep = TRUE, old_beep = TRUE;
53
54
55 static void on_mouse_accel_changed(GtkRange* range, gpointer user_data)
56 {
57 accel = (int)gtk_range_get_value(range);
58 XChangePointerControl(GDK_DISPLAY(), True, False,
59 accel, 10, 0);
60 }
61
62 static 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
70 static 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
79 static 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
110 static 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
116 static void on_kb_beep_toggle(GtkToggleButton* btn, gpointer user_data)
117 {
118 XKeyboardControl values;
119 beep = gtk_toggle_button_get_active(btn);
120 values.bell_percent = beep ? -1 : 0;
121 XChangeKeyboardControl(GDK_DISPLAY(), KBBellPercent, &values);
122 }
123
124 static 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
131 static 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
139 static void load_settings()
140 {
141 gboolean ret;
142 const char* session_name = g_getenv("DESKTOP_SESSION");
143 /* load settings from current session config files */
144 if(!session_name)
145 session_name = "LXDE";
146 file = g_build_filename( g_get_user_config_dir(), "lxsession", session_name, "desktop.conf", NULL );
147 ret = g_key_file_load_from_file( kf, file, 0, NULL );
148
149 if( ret )
150 {
151 int val;
152 val = g_key_file_get_integer(kf, "Mouse", "AccFactor", NULL);
153 if( val > 0)
154 old_accel = accel = val;
155
156 val = g_key_file_get_integer(kf, "Mouse", "AccThreshold", NULL);
157 if( val > 0)
158 old_threshold = threshold = val;
159
160 old_left_handed = left_handed = g_key_file_get_boolean(kf, "Mouse", "LeftHanded", NULL);
161
162 val = g_key_file_get_integer(kf, "Keyboard", "Delay", NULL);
163 if(val > 0)
164 old_delay = delay = val;
165 val = g_key_file_get_integer(kf, "Keyboard", "Interval", NULL);
166 if(val > 0)
167 old_interval = interval = val;
168
169 if( g_key_file_has_key(kf, "Keyboard", "Beep", NULL ) )
170 old_beep = beep = g_key_file_get_boolean(kf, "Keyboard", "Beep", NULL);
171 }
172 }
173
174 int main(int argc, char** argv)
175 {
176 GtkBuilder* builder;
177 GError* err = NULL;
178 char* str = NULL;
179
180 #ifdef ENABLE_NLS
181 bindtextdomain ( GETTEXT_PACKAGE, PACKAGE_LOCALE_DIR );
182 bind_textdomain_codeset ( GETTEXT_PACKAGE, "UTF-8" );
183 textdomain ( GETTEXT_PACKAGE );
184 #endif
185
186 gtk_init(&argc, &argv);
187
188 gtk_icon_theme_prepend_search_path(gtk_icon_theme_get_default(), PACKAGE_DATA_DIR);
189
190 /* build the UI */
191 builder = gtk_builder_new();
192
193 gtk_builder_add_from_file( builder, PACKAGE_DATA_DIR "/lxinput.ui", NULL );
194 dlg = (GtkWidget*)gtk_builder_get_object( builder, "dlg" );
195 gtk_dialog_set_alternative_button_order( (GtkDialog*)dlg, GTK_RESPONSE_OK, GTK_RESPONSE_CANCEL, -1 );
196
197 mouse_accel = (GtkRange*)gtk_builder_get_object(builder,"mouse_accel");
198 mouse_threshold = (GtkRange*)gtk_builder_get_object(builder,"mouse_threshold");
199 mouse_left_handed = (GtkToggleButton*)gtk_builder_get_object(builder,"left_handed");
200
201 kb_delay = (GtkRange*)gtk_builder_get_object(builder,"kb_delay");
202 kb_interval = (GtkRange*)gtk_builder_get_object(builder,"kb_interval");
203 kb_beep = (GtkToggleButton*)gtk_builder_get_object(builder,"beep");
204
205 g_object_unref( builder );
206
207
208 /* read the config flie */
209 kf = g_key_file_new();
210 load_settings();
211
212 /* init the UI */
213 gtk_range_set_value(mouse_accel, accel);
214 gtk_range_set_value(mouse_threshold, 110-threshold);
215 gtk_toggle_button_set_active(mouse_left_handed, left_handed);
216
217 gtk_range_set_value(kb_delay, delay);
218 gtk_range_set_value(kb_interval, interval);
219 gtk_toggle_button_set_active(kb_beep, beep);
220
221 set_range_stops(mouse_accel, 10);
222 g_signal_connect(mouse_accel, "value-changed", G_CALLBACK(on_mouse_accel_changed), NULL);
223 set_range_stops(mouse_threshold, 10);
224 g_signal_connect(mouse_threshold, "value-changed", G_CALLBACK(on_mouse_threshold_changed), NULL);
225 g_signal_connect(mouse_left_handed, "toggled", G_CALLBACK(on_left_handed_toggle), NULL);
226
227 set_range_stops(kb_delay, 10);
228 g_signal_connect(kb_delay, "value-changed", G_CALLBACK(on_kb_range_changed), &kb_delay);
229 set_range_stops(kb_interval, 10);
230 g_signal_connect(kb_interval, "value-changed", G_CALLBACK(on_kb_range_changed), &kb_interval);
231 g_signal_connect(kb_beep, "toggled", G_CALLBACK(on_kb_beep_toggle), NULL);
232
233 if( gtk_dialog_run( (GtkDialog*)dlg ) == GTK_RESPONSE_OK )
234 {
235 gsize len;
236
237 g_key_file_set_integer(kf, "Mouse", "AccFactor", accel);
238 g_key_file_set_integer(kf, "Mouse", "AccThreshold", threshold);
239 g_key_file_set_integer(kf, "Mouse", "LeftHanded", !!left_handed);
240
241 g_key_file_set_integer(kf, "Keyboard", "Delay", delay);
242 g_key_file_set_integer(kf, "Keyboard", "Interval", interval);
243 g_key_file_set_integer(kf, "Keyboard", "Beep", !!beep);
244
245 if( str = g_key_file_to_data( kf, &len, NULL ) )
246 {
247 if( g_file_set_contents( file, str, len, &err ) )
248 {
249 /* ask the settigns daemon to reload */
250 /* FIXME: is this needed? */
251 /* g_spawn_command_line_sync("lxde-settings-daemon reload", NULL, NULL, NULL, NULL); */
252 }
253 else
254 {
255 g_error_free( err );
256 }
257 g_free(str);
258 }
259 }
260 else
261 {
262 /* restore to original settings */
263
264 /* keyboard */
265 delay = old_delay;
266 interval = old_interval;
267 beep = old_beep;
268 XkbSetAutoRepeatRate(GDK_DISPLAY(), XkbUseCoreKbd, delay, interval);
269 /* FIXME: beep? */
270
271 /* mouse */
272 accel = old_accel;
273 threshold = old_threshold;
274 left_handed = old_left_handed;
275 XChangePointerControl(GDK_DISPLAY(), True, True,
276 accel, 10, (110 - threshold));
277 set_left_handed_mouse();
278 }
279
280 gtk_widget_destroy( dlg );
281
282 g_free( file );
283 g_key_file_free( kf );
284
285 return 0;
286 }