From 6d504e93013db033aecdddd464d164df443e0ca8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Guelfucci?= Date: Fri, 2 Jul 2010 20:05:20 +0200 Subject: [PATCH] Save and restore the numlock state (Bug #3098). --- configure.ac.in | 1 + xfce4-settings-helper/Makefile.am | 4 ++ xfce4-settings-helper/main.c | 7 +++ xfce4-settings-helper/numlock.c | 85 +++++++++++++++++++++++++++++++++++++ xfce4-settings-helper/numlock.h | 28 ++++++++++++ 5 files changed, 125 insertions(+), 0 deletions(-) create mode 100644 xfce4-settings-helper/numlock.c create mode 100644 xfce4-settings-helper/numlock.h diff --git a/configure.ac.in b/configure.ac.in index a7aa821..1ba7521 100644 --- a/configure.ac.in +++ b/configure.ac.in @@ -82,6 +82,7 @@ XDT_CHECK_PACKAGE([LIBXFCE4KBD_PRIVATE], [libxfce4kbd-private-2], [4.7.0]) XDT_CHECK_PACKAGE([XFCONF], [libxfconf-0], [4.6.0]) XDT_CHECK_PACKAGE([DBUS_GLIB], [dbus-glib-1], [0.34]) XDT_CHECK_PACKAGE([LIBWNCK], [libwnck-1.0], [2.12.0]) +XDT_CHECK_PACKAGE([XTEST], [xtst], [1.0.0]) XDT_CHECK_PACKAGE([XI], [xi], [1.2.0], [], [ diff --git a/xfce4-settings-helper/Makefile.am b/xfce4-settings-helper/Makefile.am index 7036699..6c001cf 100644 --- a/xfce4-settings-helper/Makefile.am +++ b/xfce4-settings-helper/Makefile.am @@ -26,6 +26,8 @@ xfce4_settings_helper_SOURCES = \ keyboard-shortcuts.h \ keyboard-layout.c \ keyboard-layout.h \ + numlock.c \ + numlock.h \ pointers.c \ pointers.h \ utils.c \ @@ -47,6 +49,7 @@ xfce4_settings_helper_CFLAGS = \ $(LIBXKLAVIER_CFLAGS) \ $(XI_CFLAGS) \ $(LIBX11_CFLAGS) \ + $(XTEST_CFLAGS) \ $(LIBNOTIFY_CFLAGS) \ $(LIBWNCK_CFLAGS) \ $(PLATFORM_CFLAGS) @@ -67,6 +70,7 @@ xfce4_settings_helper_LDADD = \ $(LIBXKLAVIER_LIBS) \ $(XI_LIBS) \ $(LIBX11_LIBS) \ + $(XTEST_LIBS) \ $(LIBNOTIFY_LIBS) \ $(LIBWNCK_LIBS) diff --git a/xfce4-settings-helper/main.c b/xfce4-settings-helper/main.c index 4d2b9e0..4623bd5 100644 --- a/xfce4-settings-helper/main.c +++ b/xfce4-settings-helper/main.c @@ -56,6 +56,7 @@ #include "keyboard-shortcuts.h" #include "workspaces.h" #include "clipboard-manager.h" +#include "numlock.h" #include "utils.h" #ifdef HAVE_XRANDR @@ -280,6 +281,9 @@ main (gint argc, gchar **argv) clipboard_daemon = NULL; } + /* Restore the last known numlock state */ + xfce_numlock_restore_state (); + /* setup signal handlers to properly quit the main loop */ if (xfce_posix_signal_handler_init (NULL)) { @@ -308,6 +312,9 @@ main (gint argc, gchar **argv) g_object_unref (G_OBJECT (clipboard_daemon)); } + /* Save the numlock state */ + xfce_numlock_save_state (); + /* shutdown xfconf */ xfconf_shutdown (); diff --git a/xfce4-settings-helper/numlock.c b/xfce4-settings-helper/numlock.c new file mode 100644 index 0000000..475f7e6 --- /dev/null +++ b/xfce4-settings-helper/numlock.c @@ -0,0 +1,85 @@ +/* $Id$ */ +/* + * Copyright (c) 2010 Jérôme Guelfucci + * + * 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 of the License, 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 Library General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * + */ + +#ifdef HAVE_CONFIG_H +#include +#endif + +#include +#include +#include + +#include +#include +#include + +#include "numlock.h" + +static gboolean +xfce_numlock_get_state (void) +{ + Display *display; + Atom num_lock; + Bool state; + + display = GDK_DISPLAY (); + + num_lock = XInternAtom (display, "Num Lock", False); + XkbGetNamedIndicator (display, num_lock, NULL, &state, NULL, NULL); + + return state ? TRUE : FALSE; +} + +gboolean +xfce_numlock_save_state (void) +{ + XfconfChannel *channel; + gboolean state; + + channel = xfconf_channel_get ("numlock"); + state = xfce_numlock_get_state (); + + return xfconf_channel_set_bool (channel, "/state", state); +} + +void +xfce_numlock_restore_state (void) +{ + XfconfChannel *channel; + gboolean current_state; + gboolean wanted_state; + + channel = xfconf_channel_get ("numlock"); + + current_state = xfce_numlock_get_state (); + wanted_state = xfconf_channel_get_bool (channel, "/state", TRUE); + + if (current_state != wanted_state) + { + Display *display; + KeyCode code; + + display = GDK_DISPLAY (); + code = XKeysymToKeycode (display, XK_Num_Lock); + + XTestFakeKeyEvent (display, code, True, CurrentTime); + XTestFakeKeyEvent (display, code, False, CurrentTime); + } +} diff --git a/xfce4-settings-helper/numlock.h b/xfce4-settings-helper/numlock.h new file mode 100644 index 0000000..6537110 --- /dev/null +++ b/xfce4-settings-helper/numlock.h @@ -0,0 +1,28 @@ +/* $Id$ */ +/* + * Copyright (c) 2010 Jérôme Guelfucci + * + * 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 of the License, 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 Library General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * + */ + +#ifndef __NUMLOCK_H__ +#define __NUMLOCK_H__ + +gboolean xfce_numlock_save_state (void); +void xfce_numlock_restore_state (void); + +#endif /* !__NUMLOCK_H__ */ + -- 1.7.1