| 1 |
/** |
| 2 |
* This file is a part of the Cairo-Dock project |
| 3 |
* |
| 4 |
* Copyright : (C) see the 'copyright' file. |
| 5 |
* E-mail : see the 'copyright' file. |
| 6 |
* |
| 7 |
* This program is free software; you can redistribute it and/or |
| 8 |
* modify it under the terms of the GNU General Public License |
| 9 |
* as published by the Free Software Foundation; either version 3 |
| 10 |
* of the License, or (at your option) any later version. |
| 11 |
* |
| 12 |
* This program is distributed in the hope that it will be useful, |
| 13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 |
* GNU General Public License for more details. |
| 16 |
* You should have received a copy of the GNU General Public License |
| 17 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 18 |
*/ |
| 19 |
|
| 20 |
#include <stdlib.h> |
| 21 |
#include <math.h> |
| 22 |
#include <string.h> |
| 23 |
#include <X11/XKBlib.h> |
| 24 |
#include <libxklavier/xklavier.h> |
| 25 |
#include <gdk/gdkx.h> |
| 26 |
|
| 27 |
#include "applet-struct.h" |
| 28 |
#include "applet-draw.h" |
| 29 |
#include "applet-xklavier.h" |
| 30 |
|
| 31 |
// If the group changes |
| 32 |
static void _state_changed (XklEngine *pEngine, XklEngineStateChange type, |
| 33 |
gint iGroup, gboolean bRestore) |
| 34 |
{ |
| 35 |
XklState *state = xkl_engine_get_current_state (myData.pEngine); |
| 36 |
cd_debug ("State Changed: %d -> %d (%d) ; %d", myData.iCurrentGroup, state->group, iGroup, state->indicators); |
| 37 |
|
| 38 |
if (type == GROUP_CHANGED && myData.iCurrentGroup != state->group) // new keyboard layout |
| 39 |
{ |
| 40 |
gchar *cShortGroupName = NULL; |
| 41 |
const gchar *cCurrentGroup = NULL; |
| 42 |
|
| 43 |
// Get the current num group |
| 44 |
guint n = xkl_engine_get_num_groups (myData.pEngine); |
| 45 |
g_return_if_fail (n > 0); |
| 46 |
|
| 47 |
int iNewGroup = MAX (0, MIN (n-1, state->group)); // workaround for 64bits to avoid strange numbers in 'state' |
| 48 |
const gchar **pGroupNames = xkl_engine_get_groups_names (myData.pEngine); |
| 49 |
g_return_if_fail (pGroupNames != NULL); |
| 50 |
|
| 51 |
cCurrentGroup = pGroupNames[iNewGroup]; |
| 52 |
g_return_if_fail (cCurrentGroup != NULL); |
| 53 |
|
| 54 |
cd_debug (" group name : %s (%d groups)", cCurrentGroup, n); |
| 55 |
|
| 56 |
// build the displayed group name |
| 57 |
cShortGroupName = g_strndup (cCurrentGroup, myConfig.iNLetters); |
| 58 |
int index = 0; |
| 59 |
int i; |
| 60 |
for (i = 0; i < state->group; i ++) // look for groups before us having the same name. |
| 61 |
{ |
| 62 |
if (strncmp (cCurrentGroup, pGroupNames[i], myConfig.iNLetters) == 0) |
| 63 |
index ++; |
| 64 |
} |
| 65 |
if (index != 0) // add a number if several groups have the same name. |
| 66 |
{ |
| 67 |
gchar *tmp = cShortGroupName; |
| 68 |
cShortGroupName = g_strdup_printf ("%s%d", cShortGroupName, index+1); |
| 69 |
g_free (tmp); |
| 70 |
} |
| 71 |
|
| 72 |
myData.iCurrentGroup = state->group; |
| 73 |
cd_xkbd_update_icon (cCurrentGroup, cShortGroupName, TRUE); |
| 74 |
g_free (cShortGroupName); |
| 75 |
} |
| 76 |
else if (type == INDICATORS_CHANGED) |
| 77 |
cd_debug ("Indicators changed"); // according to libxklavier/tests/test_monitor.c, it should work... but I have to miss something |
| 78 |
} |
| 79 |
|
| 80 |
// A scroll on the icon |
| 81 |
void cd_xkbd_set_prev_next_group (int iDelta) |
| 82 |
{ |
| 83 |
XklState *state = xkl_engine_get_current_state (myData.pEngine); |
| 84 |
|
| 85 |
cd_debug ("keyboard current state : %d;%d +%d", state->group, state->indicators, iDelta); |
| 86 |
|
| 87 |
int i = 0, n = xkl_engine_get_num_groups (myData.pEngine); |
| 88 |
g_return_if_fail (n > 0); |
| 89 |
int iCurrentGroup = MAX (0, MIN (n-1, state->group)); // on blinde car libxklavier peut bugger en 64bits. |
| 90 |
const gchar **pGroupNames = xkl_engine_get_groups_names (myData.pEngine); |
| 91 |
do // on passe au groupe suivant/precedent en sautant les faux (-). |
| 92 |
{ |
| 93 |
i ++; |
| 94 |
iCurrentGroup += iDelta; // xkl_engine_get_next_group ne marche pas. |
| 95 |
if (iCurrentGroup == n) |
| 96 |
iCurrentGroup = 0; |
| 97 |
else if (iCurrentGroup < 0) |
| 98 |
iCurrentGroup = n - 1; |
| 99 |
} while (i < n && (pGroupNames[iCurrentGroup] == NULL || *pGroupNames[iCurrentGroup] == '-')); |
| 100 |
|
| 101 |
state->group = iCurrentGroup; |
| 102 |
cd_debug ("keyboard new state : %d", state->group); |
| 103 |
xkl_engine_allow_one_switch_to_secondary_group (myData.pEngine); // sert a quoi ?? |
| 104 |
|
| 105 |
Window Xid = xkl_engine_get_current_window (myData.pEngine); |
| 106 |
xkl_engine_save_state (myData.pEngine, Xid, state); |
| 107 |
xkl_engine_lock_group (myData.pEngine, state->group); // sert a quoi ?? |
| 108 |
} |
| 109 |
|
| 110 |
// Select the layout from the menu |
| 111 |
void cd_xkbd_set_group (int iNumGroup) |
| 112 |
{ |
| 113 |
XklState *state = xkl_engine_get_current_state (myData.pEngine); |
| 114 |
cd_debug ("keyboard current state : %d;%d", state->group, state->indicators); |
| 115 |
|
| 116 |
state->group = iNumGroup; |
| 117 |
|
| 118 |
Window Xid = xkl_engine_get_current_window (myData.pEngine); |
| 119 |
xkl_engine_allow_one_switch_to_secondary_group (myData.pEngine); // sert a quoi ?? |
| 120 |
xkl_engine_save_state (myData.pEngine, Xid, state); |
| 121 |
xkl_engine_lock_group (myData.pEngine, state->group); // sert a quoi ?? |
| 122 |
} |
| 123 |
|
| 124 |
static guint32 _get_state_indicators () |
| 125 |
{ |
| 126 |
Display *dpy = gdk_x11_get_default_xdisplay (); |
| 127 |
Bool st; |
| 128 |
guint32 indicators; |
| 129 |
Atom capsLock = XInternAtom(dpy, "Caps Lock", False); |
| 130 |
Atom numLock = XInternAtom(dpy, "Num Lock", False); |
| 131 |
// Atom scrollLock = XInternAtom(dpy, "Scroll Lock", False); |
| 132 |
|
| 133 |
XkbGetNamedIndicator (dpy, capsLock, NULL, &st, NULL, NULL); |
| 134 |
indicators = st; |
| 135 |
XkbGetNamedIndicator (dpy, numLock, NULL, &st, NULL, NULL); |
| 136 |
indicators |= st << 1; |
| 137 |
|
| 138 |
return indicators; |
| 139 |
} |
| 140 |
|
| 141 |
// only use to catch new state of the indicator |
| 142 |
gboolean cd_xkbd_keyboard_state_changed (GldiModuleInstance *myApplet, Window *pWindow) |
| 143 |
{ |
| 144 |
CD_APPLET_ENTER; |
| 145 |
///if (pWindow == NULL) |
| 146 |
/// return GLDI_NOTIFICATION_LET_PASS; |
| 147 |
|
| 148 |
// Get the current state |
| 149 |
XklState *state = xkl_engine_get_current_state (myData.pEngine); |
| 150 |
guint32 indicators = _get_state_indicators (); |
| 151 |
|
| 152 |
cd_debug ("group : %d -> %d ; indic : %d -> %d (%d)", |
| 153 |
myData.iCurrentGroup, state->group, |
| 154 |
myData.iCurrentIndic, indicators, state->indicators); |
| 155 |
|
| 156 |
if (myData.iCurrentIndic == indicators) |
| 157 |
CD_APPLET_LEAVE (GLDI_NOTIFICATION_LET_PASS); |
| 158 |
|
| 159 |
// Remember the current state |
| 160 |
myData.iCurrentIndic = indicators; |
| 161 |
|
| 162 |
cd_xkbd_update_icon (NULL, NULL, FALSE); // redraw only the indicators |
| 163 |
CD_APPLET_LEAVE (GLDI_NOTIFICATION_LET_PASS); |
| 164 |
} |
| 165 |
|
| 166 |
static GdkFilterReturn _filter_xevent (GdkXEvent *pGdkXEvent, GdkEvent *pEvent) |
| 167 |
{ |
| 168 |
xkl_engine_filter_events (myData.pEngine, (XEvent*) pGdkXEvent); |
| 169 |
|
| 170 |
return GDK_FILTER_CONTINUE; |
| 171 |
} |
| 172 |
|
| 173 |
void cd_xkbd_force_redraw () |
| 174 |
{ |
| 175 |
// indicator |
| 176 |
Window Xid = xkl_engine_get_current_window (myData.pEngine); |
| 177 |
cd_xkbd_keyboard_state_changed (myApplet, &Xid); // force redraw |
| 178 |
|
| 179 |
// group |
| 180 |
myData.iCurrentGroup = -1; |
| 181 |
_state_changed (myData.pEngine, GROUP_CHANGED, -1, FALSE); |
| 182 |
} |
| 183 |
|
| 184 |
void cd_xkbd_init (Display *pDisplay) |
| 185 |
{ |
| 186 |
myData.pEngine = xkl_engine_get_instance (pDisplay); |
| 187 |
g_return_if_fail (myData.pEngine != NULL); |
| 188 |
|
| 189 |
g_signal_connect (myData.pEngine, "X-state-changed", |
| 190 |
G_CALLBACK(_state_changed), NULL); // notification for the group, we receive nothing for the indicators... |
| 191 |
|
| 192 |
gdk_window_add_filter (NULL, (GdkFilterFunc) _filter_xevent, NULL); // should connect to all type of windows |
| 193 |
/// gdk_window_add_filter (gdk_get_default_root_window (), (GdkFilterFunc) _filter_xevent, NULL); |
| 194 |
|
| 195 |
xkl_engine_start_listen (myData.pEngine, XKLL_TRACK_KEYBOARD_STATE); |
| 196 |
|
| 197 |
cd_xkbd_force_redraw (); |
| 198 |
} |
| 199 |
|
| 200 |
void cd_xkbd_stop () |
| 201 |
{ |
| 202 |
g_return_if_fail (myData.pEngine != NULL); |
| 203 |
|
| 204 |
xkl_engine_stop_listen (myData.pEngine, XKLL_TRACK_KEYBOARD_STATE); |
| 205 |
|
| 206 |
gdk_window_remove_filter (NULL, (GdkFilterFunc) _filter_xevent, NULL); |
| 207 |
/// gdk_window_remove_filter (gdk_get_default_root_window (), (GdkFilterFunc) _filter_xevent, NULL); |
| 208 |
} |