summaryrefslogtreecommitdiff
path: root/libxklavier/xklavier_xmm.c
blob: 093e3a31765e5547ddebcbc1bb9aa63e54d2e995 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
#include <time.h>
#include <stdlib.h>
#include <string.h>

#include <X11/Xatom.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Xlibint.h>
#include <X11/keysym.h>

#include "config.h"

#include "xklavier_private.h"
#include "xklavier_private_xmm.h"

#define SHORTCUT_OPTION_PREFIX "grp:"

gchar* current_xmm_rules = NULL;

XklConfigRec current_xmm_config;

Atom xmm_state_atom;

const gchar **xkl_xmm_groups_get_names( void )
{
  return (const gchar **)current_xmm_config.layouts;
}

void xkl_xmm_shortcuts_grab( void )
{
  const XmmShortcut *shortcut;
  const XmmSwitchOption *option = xkl_xmm_shortcut_get_current();
  
  xkl_debug( 150, "Found shortcut option: %p\n", option );
  if( option == NULL )
    return;
  
  shortcut = option->shortcuts;
  while (shortcut->keysym != XK_VoidSymbol)
  {
    int keycode = XKeysymToKeycode( xkl_display, shortcut->keysym );
    xkl_xmm_grab_ignoring_indicators( keycode, 
                                      shortcut->modifiers );
    shortcut++;
  }
}

void xkl_xmm_shortcuts_ungrab( void )
{
  const XmmShortcut *shortcut;
  const XmmSwitchOption *option = xkl_xmm_shortcut_get_current();
  
  if( option == NULL )
    return;
  
  shortcut = option->shortcuts;
  while (shortcut->keysym != XK_VoidSymbol)
  {
    int keycode = XKeysymToKeycode( xkl_display, shortcut->keysym );
    xkl_xmm_ungrab_ignoring_indicators( keycode, 
                                        shortcut->modifiers );
    shortcut++;
  }
}

XmmSwitchOption *xkl_xmm_shortcut_get_current( void )
{
  const gchar* option_name = xkl_xmm_shortcut_get_current_option_name();
  XmmSwitchOption *switch_option = all_switch_options;
  xkl_debug( 150, "Configured switch option: [%s]\n", option_name );
  if( option_name == NULL )
    return NULL;
  while( switch_option->option_name != NULL )
  {
    if( !g_ascii_strcasecmp( switch_option->option_name, option_name ) )
      return switch_option;
    switch_option++;
  }
  return NULL;
}

const gchar* xkl_xmm_shortcut_get_current_option_name( void )
{
  gchar** option = current_xmm_config.options;
  do
  {
    /* starts with "grp:" */
    if( strstr( *option, SHORTCUT_OPTION_PREFIX ) != NULL )
    {
      return *option + sizeof SHORTCUT_OPTION_PREFIX - 1;
    }
  } while ( *(++option) != NULL );
  return NULL;
}

const XmmSwitchOption *xkl_xmm_switch_option_find( gint keycode, 
                                                   guint state, 
                                                   gint* current_shortcut_rv )
{
  const XmmSwitchOption *rv = xkl_xmm_shortcut_get_current();
  
  if( rv != NULL )
  {
    XmmShortcut *sc = rv->shortcuts;
    while (sc->keysym != XK_VoidSymbol)
    {
      if( ( XKeysymToKeycode( xkl_display, sc->keysym ) == keycode ) &&
          ( ( state & sc->modifiers ) == sc->modifiers ) )
      {
        return rv;
      }
      sc++;
    }
  }
  return NULL;
}

gint xkl_xmm_listen_resume( void )
{
  if( xkl_listener_type & XKLL_MANAGE_LAYOUTS )
    xkl_xmm_shortcuts_grab();
  return 0;
}

gint xkl_xmm_listen_pause( void )
{
  if( xkl_listener_type & XKLL_MANAGE_LAYOUTS )
    xkl_xmm_shortcuts_ungrab();
  return 0;
}

guint xkl_xmm_groups_get_max_num( void )
{
  return 0;
}

guint xkl_xmm_groups_get_num( void )
{
  gint rv = 0;
  gchar ** p = current_xmm_config.layouts;
  while( *p++ != NULL ) rv++;
  return rv;
}
  
void xkl_xmm_free_all_info( void )
{
  if( current_xmm_rules != NULL )
  {
    g_free( current_xmm_rules );
    current_xmm_rules = NULL;
  }
  xkl_config_rec_reset( &current_xmm_config );
}

gboolean xkl_xmm_if_cached_info_equals_actual( void )
{
  return FALSE;
}

gboolean xkl_xmm_load_all_info(  )
{
  return xkl_config_get_full_from_server( &current_xmm_rules,
                                          &current_xmm_config );
}

void xkl_xmm_state_get_server( XklState * state )
{
  unsigned char *propval = NULL;
  Atom actual_type;
  int actual_format;
  unsigned long bytes_remaining;
  unsigned long actual_items;
  int result;
  
  memset( state, 0, sizeof( *state ) );

  result = XGetWindowProperty( xkl_display, xkl_root_window,
                               xmm_state_atom, 0L, 1L, 
                               False, XA_INTEGER, &actual_type,
                               &actual_format, &actual_items,
                               &bytes_remaining, &propval );
  
  if( Success == result )
  {
    if( actual_format == 32 || actual_items == 1 )
    {
      state->group = *(CARD32*)propval;
    } else
    {  
      xkl_debug( 160, "Could not get the xmodmap current group\n" );
    }
    XFree( propval );
  } else
  {
    xkl_debug( 160, "Could not get the xmodmap current group: %d\n", result );
  }
}

void xkl_xmm_group_actualize( gint group )
{
  char cmd[1024];
  int res;
  const gchar* layout_name = NULL;
  
  if( xkl_xmm_groups_get_num() < group )
    return;
  
  layout_name = current_xmm_config.layouts[group];
  
  snprintf( cmd, sizeof cmd, 
            "xmodmap %s/xmodmap.%s",
            XMODMAP_BASE, layout_name );

  res = system( cmd );
  if( res > 0 )
  {
    xkl_debug( 0, "xmodmap error %d\n", res );
  } else if( res < 0 )
  {
    xkl_debug( 0, "Could not execute xmodmap: %d\n", res );
  }
  XSync( xkl_display, False );
}

void xkl_xmm_group_lock( gint group )
{
  CARD32 propval;

  if( xkl_xmm_groups_get_num() < group )
    return;
  
  /* updating the status property */
  propval = group;
  XChangeProperty( xkl_display, xkl_root_window, xmm_state_atom, 
                   XA_INTEGER, 32, PropModeReplace, 
                   (unsigned char*)&propval, 1 );
  XSync( xkl_display, False );
}

gint xkl_xmm_init( void )
{
  static XklVTable xkl_xmm_vtable =
  {
    "xmodmap",
    XKLF_MULTIPLE_LAYOUTS_SUPPORTED |
      XKLF_REQUIRES_MANUAL_LAYOUT_MANAGEMENT, 
    xkl_xmm_config_activate,
    xkl_xmm_config_init,
    xkl_xmm_config_registry_load,
    NULL, /* no write_file */

    xkl_xmm_groups_get_names,
    xkl_xmm_groups_get_max_num,
    xkl_xmm_groups_get_num,
    xkl_xmm_group_lock,

    xkl_xmm_process_x_event,
    xkl_xmm_free_all_info,
    xkl_xmm_if_cached_info_equals_actual,
    xkl_xmm_load_all_info,
    xkl_xmm_state_get_server,
    xkl_xmm_listen_pause,
    xkl_xmm_listen_resume,
    NULL, /* no indicators_set */
  };

  if( getenv( "XKL_XMODMAP_DISABLE" ) != NULL )
    return -1;

  xkl_xmm_vtable.base_config_atom =
    XInternAtom( xkl_display, "_XMM_NAMES", False );
  xkl_xmm_vtable.backup_config_atom =
    XInternAtom( xkl_display, "_XMM_NAMES_BACKUP", False );

  xmm_state_atom =
    XInternAtom( xkl_display, "_XMM_STATE", False );
  
  xkl_xmm_vtable.default_model = "generic";
  xkl_xmm_vtable.default_layout = "us";

  xkl_vtable = &xkl_xmm_vtable;

  return 0;
}