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
|
#include <errno.h>
#include <string.h>
#include <locale.h>
#include <X11/Xlib.h>
#include <X11/Xatom.h>
#include <libxml/xpath.h>
#include "config.h"
#include "xklavier.h"
#include "xklavier_config.h"
#include "xklavier_private.h"
void XklConfigRecInit( XklConfigRecPtr data )
{
/* clear the structure VarDefsPtr... */
memset( ( void * ) data, 0, sizeof( XklConfigRec ) );
}
static Bool PtrsEqual( char* p1, char* p2 )
{
if ( p1 == p2 )
return True;
if ( ( p1 == NULL && p2 != NULL ) ||
( p1 != NULL && p2 == NULL ) )
return False;
return !strcmp( p1, p2 );
}
static Bool ListsEqual( int numItems1, char** items1,
int numItems2, char** items2 )
{
int i;
if ( numItems1 != numItems2 )
return False;
if ( items1 == items2 )
return True;
for( i = numItems1; --i >= 0; )
if ( !PtrsEqual( *items1++ , *items2++ ) )
return False;
return True;
}
static Bool _XklGetDefaultNamesProp( char **rulesFileOut, XklConfigRecPtr data )
{
if ( rulesFileOut != NULL )
*rulesFileOut = strdup( XKB_DEFAULT_RULESET );
data->model = strdup( xklVTable->defaultModel );
/* keeping Nvariants = Nlayouts */
data->numLayouts = data->numVariants = 1;
data->layouts = malloc( sizeof( char * ) );
data->layouts[0] = strdup( xklVTable->defaultLayout );
data->variants = malloc( sizeof( char * ) );
data->variants[0] = strdup( "" );
data->numOptions = 0;
data->options = NULL;
return True;
}
Bool _XklConfigGetFullFromServer( char **rulesFileOut, XklConfigRecPtr data )
{
Bool rv =
XklGetNamesProp( xklVTable->baseConfigAtom, rulesFileOut, data );
if( !rv )
rv = _XklGetDefaultNamesProp( rulesFileOut, data );
return rv;
}
Bool XklConfigRecEquals( XklConfigRecPtr data1, XklConfigRecPtr data2 )
{
if ( data1 == data2 )
return True;
if ( !PtrsEqual( data1->model, data2->model ) )
return False;
if ( !ListsEqual( data1->numLayouts, data1->layouts,
data2->numLayouts, data2->layouts ) )
return False;
if ( !ListsEqual( data1->numVariants, data1->variants,
data2->numVariants, data2->variants ) )
return False;
return ListsEqual( data1->numOptions, data1->options,
data2->numOptions, data2->options );
}
void XklConfigRecDestroy( XklConfigRecPtr data )
{
int i;
char **p;
if( data->model != NULL )
free( data->model );
if( ( p = data->layouts ) != NULL )
{
for( i = data->numLayouts; --i >= 0; )
free( *p++ );
free( data->layouts );
}
if( ( p = data->variants ) != NULL )
{
for( i = data->numVariants; --i >= 0; )
free( *p++ );
free( data->variants );
}
if( ( p = data->options ) != NULL )
{
for( i = data->numOptions; --i >= 0; )
free( *p++ );
free( data->options );
}
}
void XklConfigRecReset( XklConfigRecPtr data )
{
XklConfigRecDestroy( data );
XklConfigRecInit( data );
}
Bool XklConfigGetFromServer( XklConfigRecPtr data )
{
return _XklConfigGetFullFromServer( NULL, data );
}
Bool XklConfigGetFromBackup( XklConfigRecPtr data )
{
Bool rv =
XklGetNamesProp( xklVTable->backupConfigAtom, NULL, data );
return rv;
}
Bool XklBackupNamesProp( )
{
Bool rv = True;
char *rf = NULL;
XklConfigRec data;
Bool cgp = False;
XklConfigRecInit( &data );
if( XklGetNamesProp
( xklVTable->backupConfigAtom, NULL, &data ) )
{
XklConfigRecDestroy( &data );
return True;
}
/* "backup" property is not defined */
XklConfigRecReset( &data );
cgp = _XklConfigGetFullFromServer( &rf, &data );
if ( cgp )
{
#if 0
int i;
XklDebug( 150, "Original model: [%s]\n", data.model );
XklDebug( 150, "Original layouts(%d):\n", data.numLayouts );
for( i = data.numLayouts; --i >= 0; )
XklDebug( 150, "%d: [%s]\n", i, data.layouts[i] );
XklDebug( 150, "Original variants(%d):\n", data.numVariants );
for( i = data.numVariants; --i >= 0; )
XklDebug( 150, "%d: [%s]\n", i, data.variants[i] );
XklDebug( 150, "Original options(%d):\n", data.numOptions );
for( i = data.numOptions; --i >= 0; )
XklDebug( 150, "%d: [%s]\n", i, data.options[i] );
#endif
if( !XklSetNamesProp( xklVTable->backupConfigAtom, rf, &data ) )
{
XklDebug( 150, "Could not backup the configuration" );
rv = False;
}
if( rf != NULL )
free( rf );
} else
{
XklDebug( 150, "Could not get the configuration for backup" );
rv = False;
}
XklConfigRecDestroy( &data );
return rv;
}
Bool XklRestoreNamesProp( )
{
Bool rv = True;
char *rf = NULL;
XklConfigRec data;
XklConfigRecInit( &data );
if( !XklGetNamesProp( xklVTable->backupConfigAtom, NULL, &data ) )
{
XklConfigRecDestroy( &data );
return False;
}
if( !XklSetNamesProp( xklVTable->baseConfigAtom, rf, &data ) )
{
XklDebug( 150, "Could not backup the configuration" );
rv = False;
}
XklConfigRecDestroy( &data );
return rv;
}
Bool XklGetNamesProp( Atom rulesAtom,
char **rulesFileOut, XklConfigRecPtr data )
{
Atom realPropType;
int fmt;
unsigned long nitems, extraBytes;
char *propData = NULL, *out;
Status rtrn;
/* no such atom! */
if( rulesAtom == None ) /* property cannot exist */
{
_xklLastErrorMsg = "Could not find the atom";
return False;
}
rtrn =
XGetWindowProperty( _xklDpy, _xklRootWindow, rulesAtom, 0L,
_XKB_RF_NAMES_PROP_MAXLEN, False, XA_STRING,
&realPropType, &fmt, &nitems, &extraBytes,
( unsigned char ** ) ( void * ) &propData );
/* property not found! */
if( rtrn != Success )
{
_xklLastErrorMsg = "Could not get the property";
return False;
}
/* set rules file to "" */
if( rulesFileOut )
*rulesFileOut = NULL;
/* has to be array of strings */
if( ( extraBytes > 0 ) || ( realPropType != XA_STRING ) || ( fmt != 8 ) )
{
if( propData )
XFree( propData );
_xklLastErrorMsg = "Wrong property format";
return False;
}
if( !propData )
{
_xklLastErrorMsg = "No properties returned";
return False;
}
/* rules file */
out = propData;
if( out && ( *out ) && rulesFileOut )
*rulesFileOut = strdup( out );
out += strlen( out ) + 1;
/* if user is interested in rules only - don't waste the time */
if( !data )
{
XFree( propData );
return True;
}
if( ( out - propData ) < nitems )
{
if( *out )
data->model = strdup( out );
out += strlen( out ) + 1;
}
if( ( out - propData ) < nitems )
{
_XklConfigRecSplitLayouts( data, out );
out += strlen( out ) + 1;
}
if( ( out - propData ) < nitems )
{
int i;
char **theLayout, **theVariant;
_XklConfigRecSplitVariants( data, out );
/*
Now have to ensure that number of variants matches the number of layouts
The 'remainder' is filled with NULLs (not ""s!)
*/
if( data->numVariants < data->numLayouts )
{
data->variants =
realloc( data->variants, data->numLayouts * sizeof( char * ) );
memset( data->variants + data->numVariants, 0,
( data->numLayouts - data->numVariants ) * sizeof( char * ) );
data->numVariants = data->numLayouts;
}
/* take variants from layouts like ru(winkeys) */
theLayout = data->layouts;
theVariant = data->variants;
for( i = data->numLayouts; --i >= 0; theLayout++, theVariant++ )
{
if( *theLayout != NULL )
{
char *varstart = strchr( *theLayout, '(' );
if( varstart != NULL )
{
char *varend = strchr( varstart, ')' );
if( varend != NULL )
{
int varlen = varend - varstart;
int laylen = varstart - *theLayout;
/* I am not sure - but I assume variants in layout have priority */
char *var = *theVariant = ( *theVariant != NULL ) ?
realloc( *theVariant, varlen ) : malloc( varlen );
memcpy( var, varstart + 1, --varlen );
var[varlen] = '\0';
realloc( *theLayout, laylen + 1 );
( *theLayout )[laylen] = '\0';
}
}
}
}
out += strlen( out ) + 1;
}
if( ( out - propData ) < nitems )
{
_XklConfigRecSplitOptions( data, out );
/* out += strlen( out ) + 1; */
}
XFree( propData );
return True;
}
/* taken from XFree86 maprules.c */
Bool XklSetNamesProp( Atom rulesAtom,
char *rulesFile, const XklConfigRecPtr data )
{
int len, rv;
char *pval;
char *next;
char *allLayouts = _XklConfigRecMergeLayouts( data );
char *allVariants = _XklConfigRecMergeVariants( data );
char *allOptions = _XklConfigRecMergeOptions( data );
len = ( rulesFile ? strlen( rulesFile ) : 0 );
len += ( data->model ? strlen( data->model ) : 0 );
len += ( allLayouts ? strlen( allLayouts ) : 0 );
len += ( allVariants ? strlen( allVariants ) : 0 );
len += ( allOptions ? strlen( allOptions ) : 0 );
if( len < 1 )
return True;
len += 5; /* trailing NULs */
pval = next = ( char * ) malloc( len + 1 );
if( !pval )
{
_xklLastErrorMsg = "Could not allocate buffer";
if ( allLayouts != NULL ) free( allLayouts );
if ( allVariants != NULL ) free( allVariants );
if ( allOptions != NULL ) free( allOptions );
return False;
}
if( rulesFile )
{
strcpy( next, rulesFile );
next += strlen( rulesFile );
}
*next++ = '\0';
if( data->model )
{
strcpy( next, data->model );
next += strlen( data->model );
}
*next++ = '\0';
if( data->layouts )
{
strcpy( next, allLayouts );
next += strlen( allLayouts );
}
*next++ = '\0';
if( data->variants )
{
strcpy( next, allVariants );
next += strlen( allVariants );
}
*next++ = '\0';
if( data->options )
{
strcpy( next, allOptions );
next += strlen( allOptions );
}
*next++ = '\0';
if( ( next - pval ) != len )
{
XklDebug( 150, "Illegal final position: %d/%d\n", ( next - pval ), len );
if ( allLayouts != NULL ) free( allLayouts );
if ( allVariants != NULL ) free( allVariants );
if ( allOptions != NULL ) free( allOptions );
free( pval );
_xklLastErrorMsg = "Internal property parsing error";
return False;
}
rv = XChangeProperty( _xklDpy, _xklRootWindow, rulesAtom, XA_STRING, 8,
PropModeReplace, ( unsigned char * ) pval, len );
XSync( _xklDpy, False );
#if 0
for( i = len - 1; --i >= 0; )
if( pval[i] == '\0' )
pval[i] = '?';
XklDebug( 150, "Stored [%s] of length %d to [%s] of %X: %d\n", pval, len,
propName, _xklRootWindow, rv );
#endif
if ( allLayouts != NULL ) free( allLayouts );
if ( allVariants != NULL ) free( allVariants );
if ( allOptions != NULL ) free( allOptions );
free( pval );
return True;
}
|