summaryrefslogtreecommitdiff
path: root/libxklavier/xklavier_config_xkb.c
blob: 25a9a7343e84e63aa45d1217c7d547e75ee9a33b (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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
#include <errno.h>
#include <string.h>
#include <locale.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/param.h>
#include <sys/wait.h>

#include <sys/types.h>
#include <fcntl.h>

#include <libxml/xpath.h>

#include "config.h"

#include "xklavier_private.h"
#include "xklavier_private_xkb.h"

#ifdef XKB_HEADERS_PRESENT
#include <X11/extensions/XKBfile.h>
#include <X11/extensions/XKM.h>
#endif

/* For "bad" X servers we hold our own copy */
#define XML_CFG_FALLBACK_PATH ( DATA_DIR "/xfree86.xml" )

#define XKBCOMP ( XKB_BASE "/xkbcomp" )

#define XK_XKB_KEYS
#include <X11/keysymdef.h>

#ifdef XKB_HEADERS_PRESENT
XkbRF_VarDefsRec _xklVarDefs;
static XkbRF_RulesPtr _xklRules;
static XkbComponentNamesRec componentNames;

static char *locale;
#endif

#ifdef XKB_HEADERS_PRESENT
static XkbRF_RulesPtr _XklLoadRulesSet( void )
{
  char fileName[MAXPATHLEN] = "";
  char* rf = _XklGetRulesSetName( XKB_DEFAULT_RULESET );

  _xklRules = NULL;
  if( rf == NULL )
  {
    _xklLastErrorMsg = "Could not find the XKB rules set";
    return NULL;
  }

  locale = setlocale( LC_ALL, NULL );
  if( locale != NULL )
    locale = strdup( locale );

  snprintf( fileName, sizeof fileName, XKB_BASE "/rules/%s", rf );
  XklDebug( 160, "Loading rules from [%s]\n", fileName );

  _xklRules = XkbRF_Load( fileName, locale, True, True );

  if( _xklRules == NULL )
  {
    _xklLastErrorMsg = "Could not load rules";
    return NULL;
  }
  return _xklRules;
}
#endif

#ifdef XKB_HEADERS_PRESENT
static void _XklFreeRulesSet( void )
{
  if ( _xklRules )
    XkbRF_Free( _xklRules, True );
}
#endif

void _XklXkbConfigInit( void )
{
#ifdef XKB_HEADERS_PRESENT
  XkbInitAtoms( NULL );
#endif
}

Bool _XklXkbConfigLoadRegistry( void )
{
  struct stat statBuf;
  char fileName[MAXPATHLEN] = "";
  char* rf = _XklGetRulesSetName( XKB_DEFAULT_RULESET );

  if ( rf == NULL )
    return False;

  snprintf( fileName, sizeof fileName, XKB_BASE "/rules/%s.xml", rf );

  if( stat( fileName, &statBuf ) != 0 )
  {
    strncpy( fileName, XML_CFG_FALLBACK_PATH, sizeof fileName );
    fileName[ MAXPATHLEN - 1 ] = '\0';
  }

  return XklConfigLoadRegistryFromFile( fileName );
}

#ifdef XKB_HEADERS_PRESENT
static Bool _XklConfigPrepareBeforeKbd( const XklConfigRecPtr data )
{
  XkbRF_RulesPtr rulesPtr = _XklLoadRulesSet();

  memset( &_xklVarDefs, 0, sizeof( _xklVarDefs ) );

  if( !rulesPtr )
    return False;

  _xklVarDefs.model = ( char * ) data->model;

  if( data->layouts != NULL )
    _xklVarDefs.layout = _XklConfigRecMergeLayouts( data );

  if( data->variants != NULL )
    _xklVarDefs.variant = _XklConfigRecMergeVariants( data );

  if( data->options != NULL )
    _xklVarDefs.options = _XklConfigRecMergeOptions( data );

  if( !XkbRF_GetComponents( rulesPtr, &_xklVarDefs, &componentNames ) )
  {
    _xklLastErrorMsg = "Could not translate rules into components";
    return False;
  }

  if ( _xklDebugLevel >= 200 )
  {
    XklDebug( 200, "keymap: %s\n", componentNames.keymap );
    XklDebug( 200, "keycodes: %s\n", componentNames.keycodes );
    XklDebug( 200, "compat: %s\n", componentNames.compat );
    XklDebug( 200, "types: %s\n", componentNames.types );
    XklDebug( 200, "symbols: %s\n", componentNames.symbols );
    XklDebug( 200, "geometry: %s\n", componentNames.geometry );
  }
  return True;
}

static void _XklConfigCleanAfterKbd(  )
{
  _XklFreeRulesSet();

  free( locale );
  locale = NULL;

  free( _xklVarDefs.layout );
  free( _xklVarDefs.variant );
  free( _xklVarDefs.options );
  memset( &_xklVarDefs, 0, sizeof( _xklVarDefs ) );

  free(componentNames.keymap);
  free(componentNames.keycodes);
  free(componentNames.compat);
  free(componentNames.types);
  free(componentNames.symbols);
  free(componentNames.geometry);
}

static XkbDescPtr _XklConfigGetKeyboard( Bool activate )
{
  XkbDescPtr xkb = NULL;
#if 0
  xkb = XkbGetKeyboardByName( _xklDpy, 
                              XkbUseCoreKbd, 
                              &componentNames,
                              XkbGBN_AllComponentsMask &
                              ( ~XkbGBN_GeometryMask ), 
                              XkbGBN_AllComponentsMask &
                              ( ~XkbGBN_GeometryMask ), 
                              activate );
#else
  char xkmFN[L_tmpnam];
  char xkbFN[L_tmpnam];
  FILE* tmpxkm;
  XkbFileInfo result;
  int xkmloadres;

  if ( tmpnam( xkmFN ) != NULL && 
       tmpnam( xkbFN ) != NULL )
  {
    pid_t cpid, pid;
    int status = 0;
    FILE *tmpxkb;

    XklDebug( 150, "tmp XKB/XKM file names: [%s]/[%s]\n", xkbFN, xkmFN );
    if( (tmpxkb = fopen( xkbFN, "w" )) != NULL )
    {
      fprintf( tmpxkb, "xkb_keymap {\n" );
      fprintf( tmpxkb, "        xkb_keycodes  { include \"%s\" };\n", componentNames.keycodes );
      fprintf( tmpxkb, "        xkb_types     { include \"%s\" };\n", componentNames.types );
      fprintf( tmpxkb, "        xkb_compat    { include \"%s\" };\n", componentNames.compat );
      fprintf( tmpxkb, "        xkb_symbols   { include \"%s\" };\n", componentNames.symbols );
      fprintf( tmpxkb, "        xkb_geometry  { include \"%s\" };\n", componentNames.geometry );
      fprintf( tmpxkb, "};\n" );
      fclose( tmpxkb );
    
      XklDebug( 150, "xkb_keymap {\n"
        "        xkb_keycodes  { include \"%s\" };\n"
        "        xkb_types     { include \"%s\" };\n"
        "        xkb_compat    { include \"%s\" };\n"
        "        xkb_symbols   { include \"%s\" };\n"
        "        xkb_geometry  { include \"%s\" };\n};\n", 
        componentNames.keycodes,
        componentNames.types,
        componentNames.compat,
        componentNames.symbols,
        componentNames.geometry );
       
      cpid=fork();
      switch( cpid )
      {
        case -1:
          XklDebug( 0, "Could not fork: %d\n", errno );
          break;
        case 0:
          /* child */
          XklDebug( 160, "Executing %s\n", XKBCOMP );
          execl( XKBCOMP, XKBCOMP, "-I", "-I" XKB_BASE, "-xkm", xkbFN, xkmFN, NULL );
          XklDebug( 0, "Could not exec %s: %d\n", XKBCOMP, errno );
          exit( 1 );
        default:
          /* parent */
          pid = wait( &status );
          XklDebug( 150, "Return status of %d (well, started %d): %d\n", pid, cpid, status );
          memset( (char *)&result, 0, sizeof(result) );
          result.xkb = XkbAllocKeyboard();

          if( Success == XkbChangeKbdDisplay( _xklDpy, &result ) )
          {
            XklDebug( 150, "Hacked the kbddesc - set the display...\n" );
            if( (tmpxkm = fopen( xkmFN, "r" )) != NULL )
            {
              xkmloadres = XkmReadFile( tmpxkm, XkmKeymapLegal, XkmKeymapLegal, &result);
              XklDebug( 150, "Loaded %s output as XKM file, got %d (comparing to %d)\n", 
                        XKBCOMP, (int)xkmloadres, (int)XkmKeymapLegal );
              if ( (int)xkmloadres != (int)XkmKeymapLegal )
              {
                XklDebug( 150, "Loaded legal keymap\n" );
                if( activate )
                {
                  XklDebug( 150, "Activating it...\n" );
                  if( XkbWriteToServer(&result) )
                  {
                     XklDebug( 150, "Updating the keyboard...\n" );
                     xkb = result.xkb;
                  } else
                  {
                     XklDebug( 0, "Could not write keyboard description to the server\n" );
                  }
                } else /* no activate, just load */
                  xkb = result.xkb;
              } else /* could not load properly */
              {
                XklDebug( 0, "Could not load %s output as XKM file, got %d (asked %d)\n", 
                             XKBCOMP, (int)xkmloadres, (int)XkmKeymapLegal );
              }
              fclose( tmpxkm );
              XklDebug( 160, "Unlinking the temporary xkm file %s\n", xkmFN );
              if ( remove( xkmFN ) == -1 )
                XklDebug( 0, "Could not unlink the temporary xkm file %s: %d\n", 
                             xkmFN, errno );
            } else /* could not open the file */
            {
              XklDebug( 0, "Could not open the temporary xkm file %s\n", xkmFN );
            }
          } else /* could not assign to display */
          {
            XklDebug( 0, "Could not change the keyboard description to display\n" );
          } 
          if ( xkb == NULL )
            XkbFreeKeyboard( result.xkb, XkbAllComponentsMask, True );
          break;
      }
      XklDebug( 160, "Unlinking the temporary xkb file %s\n", xkbFN );
      if ( remove( xkbFN ) == -1 )
        XklDebug( 0, "Could not unlink the temporary xkb file %s: %d\n", 
                  xkbFN, errno );
    } else /* could not open input tmp file */
    {
      XklDebug( 0, "Could not open tmp XKB file [%s]: %d\n", xkbFN, errno );
    }
  } else
  {
    XklDebug( 0, "Could not get tmp names\n" );
  }

#endif
  return xkb;
}
#endif

/* check only client side support */
Bool _XklXkbConfigMultipleLayoutsSupported( void )
{
  enum { NON_SUPPORTED, SUPPORTED, UNCHECKED };

  static int supportState = UNCHECKED;

  if( supportState == UNCHECKED )
  {
#ifdef XKB_HEADERS_PRESENT
    XkbRF_RulesPtr rulesPtr;
#endif
    XklDebug( 100, "!!! Checking multiple layouts support\n" );
    supportState = NON_SUPPORTED;
#ifdef XKB_HEADERS_PRESENT
    rulesPtr = _XklLoadRulesSet();
    if( rulesPtr )
    {
      XkbRF_VarDefsRec varDefs;
      XkbComponentNamesRec cNames;
      memset( &varDefs, 0, sizeof( varDefs ) );

      varDefs.model = "pc105";
      varDefs.layout = "a,b";
      varDefs.variant = "";
      varDefs.options = "";

      if( XkbRF_GetComponents( rulesPtr, &varDefs, &cNames ) )
      {
        XklDebug( 100, "!!! Multiple layouts ARE supported\n" );
        supportState = SUPPORTED;
      } else
      {
        XklDebug( 100, "!!! Multiple layouts ARE NOT supported\n" );
      }
      free(cNames.keymap);
      free(cNames.keycodes);
      free(cNames.compat);
      free(cNames.types);
      free(cNames.symbols);
      free(cNames.geometry);

      _XklFreeRulesSet();
    }
#endif
  }
  return supportState == SUPPORTED;
}

Bool _XklXkbConfigActivate( const XklConfigRecPtr data )
{
  Bool rv = False;
#if 0
  {
  int i;
  XklDebug( 150, "New model: [%s]\n", data->model );
  XklDebug( 150, "New layouts: %p\n", data->layouts );
  for( i = data->numLayouts; --i >= 0; )
    XklDebug( 150, "New layout[%d]: [%s]\n", i, data->layouts[i] );
  XklDebug( 150, "New variants: %p\n", data->variants );
  for( i = data->numVariants; --i >= 0; )
    XklDebug( 150, "New variant[%d]: [%s]\n", i, data->variants[i] );
  XklDebug( 150, "New options: %p\n", data->options );
  for( i = data->numOptions; --i >= 0; )
    XklDebug( 150, "New option[%d]: [%s]\n", i, data->options[i] );
  }
#endif

#ifdef XKB_HEADERS_PRESENT
  if( _XklConfigPrepareBeforeKbd( data ) )
  {
    XkbDescPtr xkb;
    xkb = _XklConfigGetKeyboard( True );
    if( xkb != NULL )
    {
      if( XklSetNamesProp
          ( xklVTable->baseConfigAtom, _XklGetRulesSetName( XKB_DEFAULT_RULESET ), data ) )
          /* We do not need to check the result of _XklGetRulesSetName - 
             because PrepareBeforeKbd did it for us */
        rv = True;
      else
        _xklLastErrorMsg = "Could not set names property";
      XkbFreeKeyboard( xkb, XkbAllComponentsMask, True );
    } else
    {
      _xklLastErrorMsg = "Could not load keyboard description";
    }
  }
  _XklConfigCleanAfterKbd(  );
#endif
  return rv;
}

Bool _XklXkbConfigWriteFile( const char *fileName, 
                             const XklConfigRecPtr data,
                             const Bool binary )
{
  Bool rv = False;

#ifdef XKB_HEADERS_PRESENT
  FILE *output = fopen( fileName, "w" );
  XkbFileInfo dumpInfo;

  if( output == NULL )
  {
    _xklLastErrorMsg = "Could not open the XKB file";
    return False;
  }

  if( _XklConfigPrepareBeforeKbd( data ) )
  {
    XkbDescPtr xkb;
    xkb = _XklConfigGetKeyboard( False );
    if( xkb != NULL )
    {
      dumpInfo.defined = 0;
      dumpInfo.xkb = xkb;
      dumpInfo.type = XkmKeymapFile;
      if( binary )
        rv = XkbWriteXKMFile( output, &dumpInfo );
      else
        rv = XkbWriteXKBFile( output, &dumpInfo, True, NULL, NULL );

      XkbFreeKeyboard( xkb, XkbGBN_AllComponentsMask, True );
    } else
      _xklLastErrorMsg = "Could not load keyboard description";
  }
  _XklConfigCleanAfterKbd(  );
  fclose( output );
#endif
  return rv;
}