summaryrefslogtreecommitdiff
path: root/tests/test_config.c
blob: 2ef07b75523c8cb8c18fba97851466dca23e5e82 (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
/*
 * Copyright (C) 2002-2006 Sergey V. Udaltsov <svu@gnome.org>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#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>

#ifdef HAVE_SETLOCALE
# include <locale.h>
#endif

extern void xkl_config_rec_dump(FILE * file, XklConfigRec * data);

enum { ACTION_NONE, ACTION_LIST, ACTION_GET, ACTION_SET,
	ACTION_WRITE
};

static void
print_usage(void)
{
	printf
	    ("Usage: test_config (-g)|(-s -m <model> -l <layouts> -o <options>)|(-h)|(-ws)|(-wb)(-d <debugLevel>)\n");
	printf("Options:\n");
	printf("         -al - list all available layouts and variants\n");
	printf("         -am - list all available models\n");
	printf
	    ("         -ao - list all available options groups and 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");
}

static void
print_option(XklConfigRegistry * config, const XklConfigItem * item,
	     gpointer data)
{
	printf("  [%s][%s][%s]\n", item->name,
	       item->description, item->short_description);
}

static void
print_option_group(XklConfigRegistry * config, const XklConfigItem * item,
		   gpointer data)
{
	printf("[%s][%s][%s] %s multiple selection\n", item->name,
	       item->description, item->short_description,
	       GPOINTER_TO_INT(g_object_get_data
			       (G_OBJECT(item),
				XCI_PROP_ALLOW_MULTIPLE_SELECTION)) ?
	       "Allows" : "Does not allow");
	xkl_config_registry_foreach_option(config, item->name,
					   print_option, data);
}

static void
print_model(XklConfigRegistry * config, const XklConfigItem * item,
	    gpointer data)
{
	gchar *vendor =
	    (gchar *) g_object_get_data(G_OBJECT(item), XCI_PROP_VENDOR);
	printf("[%s][%s][%s] by %s\n", item->name, item->description,
	       item->short_description,
	       vendor == NULL ? "unknown" : vendor);
}

static void
print_xci(XklConfigRegistry * config, const XklConfigItem * item,
	  gint indent)
{
	gchar **countries = (gchar **) g_object_get_data(G_OBJECT(item),
							 XCI_PROP_COUNTRY_LIST);
	gchar **languages = (gchar **) g_object_get_data(G_OBJECT(item),
							 XCI_PROP_LANGUAGE_LIST);
	gint i;
	printf("%*s[%s][%s][%s]\n", indent, "", item->name,
	       item->description, item->short_description);
	if (countries != NULL)
		for (i = 0; i < g_strv_length(countries); i++)
			printf("%*s  country: [%s]\n", indent, "",
			       countries[i]);
	if (languages != NULL)
		for (i = 0; i < g_strv_length(languages); i++)
			printf("%*s  language: [%s]\n", indent, "",
			       languages[i]);
}

static void
print_variant(XklConfigRegistry * config, const XklConfigItem * item,
	      gpointer data)
{
	print_xci(config, item, 2);
}

static void
print_layout(XklConfigRegistry * config, const XklConfigItem * item,
	     gpointer data)
{
	print_xci(config, item, 0);

	xkl_config_registry_foreach_layout_variant(config, item->name,
						   print_variant, data);
}


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

	g_type_init_with_debug_flags(G_TYPE_DEBUG_OBJECTS |
				     G_TYPE_DEBUG_SIGNALS);

	while (1) {
		c = getopt(argc, argv, "ha:sgm:l:o:d:w:c:");
		if (c == -1)
			break;
		switch (c) {
		case 'a':
			which_list = optarg[0];
			printf("List the registry\n");
			action = ACTION_LIST;
			break;
		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':
			print_usage();
			exit(0);
		case 'd':
			debug_level = atoi(optarg);
			break;
		case 'w':
			action = ACTION_WRITE;
			binary = ('b' == optarg[0]);
		default:
			fprintf(stderr,
				"?? getopt returned character code 0%o ??\n",
				c);
			print_usage();
		}
	}

	if (action == ACTION_NONE) {
		print_usage();
		exit(0);
	}
#ifdef HAVE_SETLOCALE
	setlocale(LC_ALL, "");
#endif

	dpy = XOpenDisplay(NULL);
	if (dpy == NULL) {
		fprintf(stderr, "Could not open display\n");
		exit(1);
	}
	if (debug_level != -1)
		xkl_set_debug_level(debug_level);
	engine = xkl_engine_get_instance(dpy);
	if (engine != NULL) {
		XklConfigRec *current_config, *r2;
		XklConfigRegistry *config;

		xkl_debug(0, "Xklavier initialized\n");
		config = xkl_config_registry_get_instance(engine);
		xkl_config_registry_load(config);

		xkl_debug(0, "Xklavier registry loaded\n");
		xkl_debug(0, "Backend: [%s]\n",
			  xkl_engine_get_backend_name(engine));
		xkl_debug(0, "Supported features: 0x0%X\n",
			  xkl_engine_get_features(engine));
		xkl_debug(0, "Max number of groups: %d\n",
			  xkl_engine_get_max_num_groups(engine));

		current_config = xkl_config_rec_new();
		xkl_config_rec_get_from_server(current_config, engine);

		switch (action) {
		case ACTION_LIST:
			switch (which_list) {
			case 'l':
				xkl_config_registry_foreach_layout(config,
								   print_layout,
								   NULL);
				break;
			case 'm':
				xkl_config_registry_foreach_model(config,
								  print_model,
								  NULL);
				break;
			case 'o':
				xkl_config_registry_foreach_option_group
				    (config, print_option_group, NULL);
				break;
			default:
				printf("Unknown list: %c\n", which_list);
				print_usage();
				break;
			}
			break;
		case ACTION_GET:
			xkl_debug(0, "Got config from the server\n");
			xkl_config_rec_dump(stdout, current_config);

			r2 = xkl_config_rec_new();

			if (xkl_config_rec_get_from_backup(r2, engine)) {
				xkl_debug(0,
					  "Got config from the backup\n");
				xkl_config_rec_dump(stdout, r2);
			}

			if (xkl_config_rec_activate(r2, engine)) {
				xkl_debug(0,
					  "The backup configuration restored\n");
				if (xkl_config_rec_activate
				    (current_config, engine)) {
					xkl_debug(0,
						  "Reverting the configuration change\n");
				} else {
					xkl_debug(0,
						  "The configuration could not be reverted: %s\n",
						  xkl_get_last_error());
				}
			} else {
				xkl_debug(0,
					  "The backup configuration could not be restored: %s\n",
					  xkl_get_last_error());
			}

			g_object_unref(G_OBJECT(r2));
			break;
		case ACTION_SET:
			if (model != NULL) {
				if (current_config->model != NULL)
					g_free(current_config->model);
				current_config->model = g_strdup(model);
			}

			if (layouts != NULL) {
				if (current_config->layouts != NULL)
					g_strfreev(current_config->
						   layouts);
				if (current_config->variants != NULL)
					g_strfreev(current_config->
						   variants);

				current_config->layouts =
				    g_new0(char *, 2);
				current_config->layouts[0] =
				    g_strdup(layouts);
				current_config->variants =
				    g_new0(char *, 2);
				current_config->variants[0] = g_strdup("");
			}

			if (options != NULL) {
				if (current_config->options != NULL)
					g_strfreev(current_config->
						   options);

				current_config->options =
				    g_new0(char *, 2);
				current_config->options[0] =
				    g_strdup(options);
			}

			xkl_debug(0, "New config:\n");
			xkl_config_rec_dump(stdout, current_config);
			if (xkl_config_rec_activate
			    (current_config, engine))
				xkl_debug(0, "Set the config\n");
			else
				xkl_debug(0,
					  "Could not set the config: %s\n",
					  xkl_get_last_error());
			break;
		case ACTION_WRITE:
			xkl_config_rec_write_to_file(engine,
						     binary ? (PACKAGE
							       ".xkm")
						     : (PACKAGE ".xkb"),
						     current_config,
						     binary);
			xkl_debug(0, "The file " PACKAGE "%s is written\n",
				  binary ? ".xkm" : ".xkb");
			break;
		}

		g_object_unref(G_OBJECT(current_config));

		g_object_unref(G_OBJECT(config));
		xkl_debug(0, "Xklavier registry freed\n");
		xkl_debug(0, "Xklavier terminating\n");
		g_object_unref(G_OBJECT(engine));
	} else {
		fprintf(stderr, "Could not init _xklavier\n");
		exit(2);
	}
	printf("closing display: %p\n", dpy);
	XCloseDisplay(dpy);
	return 0;
}