summaryrefslogtreecommitdiff
path: root/tests/test_config.c
blob: 6d062d0219f58d1cf0cfa2515d8a4d3c65256687 (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
#include <config.h>
#include <stdio.h>
#include <unistd.h>
#include <getopt.h>
#include <stdlib.h>
#include <string.h>
#include <X11/Xlib.h>
#include <libxklavier/xklavier.h>
#include <libxklavier/xklavier_config.h>

#ifdef __STRICT_ANSI__
/* these are functions which are NOT in ANSI C. 
   Probably we should provide the implementation */
extern char *strdup( const char *s );
#endif

extern void XklConfigDump( FILE* file,
                           XklConfigRecPtr data );

enum { ACTION_NONE, ACTION_GET, ACTION_SET, ACTION_WRITE };

static void printUsage(void)
{
  printf( "Usage: test_config (-g)|(-s -m <model> -l <layouts> -o <options>)|(-h)|(-ws)|(-wb)(-d <debugLevel>)\n" );
  printf( "Options:\n" );
  printf( "         -g - Dump the current config, load original system settings and revert back\n" ); 
  printf( "         -s - Set the configuration given my -m -l -o options. Similar to setxkbmap\n" ); 
  printf( "         -ws - Write the binary XKB config file (" PACKAGE ".xkm)\n" );
  printf( "         -wb - Write the source XKB config file (" PACKAGE ".xkb)\n" );
  printf( "         -d - Set the debug level (by default, 0)\n" );
  printf( "         -h - Show this help\n" );
}

int main( int argc, char * const argv[] )
{
  int c, i;
  int action = ACTION_NONE;
  const char* model = NULL;
  const char* layouts = NULL;
  const char* options = NULL;
  int debugLevel = -1;
  int binary = 0;
  Display *dpy;

  while (1)
  {
    c = getopt( argc, argv, "hsgm:l:o:d:w:" );
    if ( c == -1 )
      break;
    switch (c)
    {
      case 's':
        printf( "Set the config\n" );
        action = ACTION_SET;
        break;
      case 'g':
        printf( "Get the config\n" );
        action = ACTION_GET;
        break;
      case 'm':
        printf( "Model: [%s]\n", model = optarg );
        break;
      case 'l':
        printf( "Layouts: [%s]\n", layouts = optarg );
        break;
      case 'o':
        printf( "Options: [%s]\n", options = optarg );
        break;
      case 'h':
        printUsage();
        exit(0);
      case 'd':
        debugLevel = atoi( optarg );
        break;
      case 'w':
        action = ACTION_WRITE;
        binary = ( 'b' == optarg[0] );
      default:
        fprintf( stderr, "?? getopt returned character code 0%o ??\n", c );
        printUsage();
    }
  }

  if ( action == ACTION_NONE )
  {
    printUsage();
    exit( 0 );
  }

  dpy = XOpenDisplay( NULL );
  if ( dpy == NULL )
  {
    fprintf( stderr, "Could not open display\n" );
    exit(1);
  }
  printf( "opened display: %p\n", dpy );
  if ( !XklInit( dpy ) )
  {
    XklConfigRec currentConfig, r2;
    if( debugLevel != -1 )
      XklSetDebugLevel( debugLevel );
    XklDebug( 0, "Xklavier initialized\n" );
    XklConfigInit();
    XklConfigLoadRegistry();
    XklDebug( 0, "Xklavier registry loaded\n" );
    XklDebug( 0, "Backend: [%s]\n", XklGetBackendName() );
    XklDebug( 0, "Supported features: 0x0%X\n", XklGetBackendFeatures() );
    XklDebug( 0, "Max number of groups: %d\n", XklGetMaxNumGroups() );

    XklConfigRecInit( &currentConfig );
    XklConfigGetFromServer( &currentConfig );

    switch ( action )
    {
      case ACTION_GET:
        XklDebug( 0, "Got config from the server\n" );
        XklConfigDump( stdout, &currentConfig );

        XklConfigRecInit( &r2 );

        if ( XklConfigGetFromBackup( &r2 ) )
        {
          XklDebug( 0, "Got config from the backup\n" );
          XklConfigDump( stdout, &r2 );
        }

        if ( XklConfigActivate( &r2 ) )
        {
          XklDebug( 0, "The backup configuration restored\n" );
          if ( XklConfigActivate( &currentConfig ) )
          {
            XklDebug( 0, "Reverting the configuration change\n" );
          } else
          {
            XklDebug( 0, "The configuration could not be reverted: %s\n", XklGetLastError() );
          }
        } else
        {
          XklDebug( 0, "The backup configuration could not be restored: %s\n", XklGetLastError() );
        }

        XklConfigRecDestroy( &r2 );
        break;
      case ACTION_SET:
        if ( model != NULL )
        {
          if ( currentConfig.model != NULL ) free ( currentConfig.model );
          currentConfig.model = strdup( model );
        }

        if ( layouts != NULL )
        {
          if ( currentConfig.layouts != NULL ) 
          {
            for ( i = currentConfig.numLayouts; --i >=0; )
              free ( currentConfig.layouts[i] );
            free ( currentConfig.layouts );
            for ( i = currentConfig.numVariants; --i >=0; )
              free ( currentConfig.variants[i] );
            free ( currentConfig.variants );
          }
          currentConfig.numLayouts = 
          currentConfig.numVariants = 1;
          currentConfig.layouts = malloc( sizeof ( char* ) );
          currentConfig.layouts[0] = strdup( layouts );
          currentConfig.variants = malloc( sizeof ( char* ) );
          currentConfig.variants[0] = strdup( "" );
        }

        if ( options != NULL )
        {
          if ( currentConfig.options != NULL ) 
          {
            for ( i = currentConfig.numOptions; --i >=0; )
              free ( currentConfig.options[i] );
            free ( currentConfig.options );
          }
          currentConfig.numOptions = 1;
          currentConfig.options = malloc( sizeof ( char* ) );
          currentConfig.options[0] = strdup( options );
        }

        XklDebug( 0, "New config:\n" );
        XklConfigDump( stdout, &currentConfig );
        if ( XklConfigActivate( &currentConfig ) )
            XklDebug( 0, "Set the config\n" );
        else
            XklDebug( 0, "Could not set the config: %s\n", XklGetLastError() );
        break;
      case ACTION_WRITE:
        XklConfigWriteFile( binary ? ( PACKAGE ".xkm" ) : ( PACKAGE ".xkb" ),
                            &currentConfig, 
                            binary );     
        XklDebug( 0, "The file " PACKAGE "%s is written\n", 
                  binary ? ".xkm" : ".xkb"  );
        break;
    }

    XklConfigRecDestroy( &currentConfig );

    XklConfigFreeRegistry();
    XklConfigTerm();
    XklDebug( 0, "Xklavier registry freed\n" );
    XklDebug( 0, "Xklavier terminating\n" );
    XklTerm();
  } else
  {
    fprintf( stderr, "Could not init Xklavier: %s\n", XklGetLastError() );
    exit(2);
  }
  printf( "closing display: %p\n", dpy );
  XCloseDisplay(dpy);
  return 0;
}