summaryrefslogtreecommitdiff
path: root/fedora-setup-keyboard.c
blob: 212c12ac1662b126e3f74f46fa901e4eaf6f1e37 (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
/*
 * Copyright © 2009 Adel Gadllah
 * Copyright © 2009 Red Hat, Inc.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 *
 *
 */

#include <glib.h>
#include <string.h>
#include <stdlib.h>

#define KBDCONFIG "/etc/sysconfig/keyboard"
#define KEY_OPTIONS "input.xkb.options"

gchar* remove_quotes(gchar *str) {
	gchar *tmp;
	gboolean s_offset = FALSE;

	if(str == NULL)
		return NULL;

	if(str[0] == '"') {
		str++;
		s_offset = TRUE;
	}
	if(str[strlen(str) - 1] == '"') 
		str[strlen(str) - 1] = 0;

	tmp = g_strdup(str);

	if(s_offset)
		str--;
	g_free(str);
	return tmp;	
}

int main() {
	GKeyFile *cfg_file;
	gchar *buffer, *conf;

	gchar *map[] = { "layout", "model", "variant", "options" };
	GHashTable* kbd_models;
	gchar *keytable;
	gchar *property;
	guint n, i;
	gchar **list;
	gchar *key, *udi, *tmp;

	/* Parse the config file */
	cfg_file = g_key_file_new();
	g_file_get_contents(KBDCONFIG, &buffer, NULL, NULL);
	conf = g_strdup_printf("[kbd]\n%s", buffer);
	g_free(buffer);
	g_key_file_load_from_data(cfg_file, conf, strlen(conf), G_KEY_FILE_NONE, NULL);
	property = remove_quotes(g_key_file_get_value(cfg_file, "kbd", "KEYTABLE", NULL));
	if(property == NULL) 
		return 1;

	/* Generate model database */
	kbd_models = g_hash_table_new(g_str_hash, g_str_equal);
#include "keyboards.h"

	/* Read and apply user config */
	keytable = g_hash_table_lookup(kbd_models, (gpointer) property);
	g_free(property);

	list = g_strsplit(keytable, " ", 4);
	n = g_strv_length(list);
	for ( i = 0; i < n; i++) {
		gchar *value;
		/* honor user setting */
		tmp = g_ascii_strup(map[i], -1);
		property = remove_quotes(g_key_file_get_value(cfg_file, "kbd", tmp, NULL));
		g_free(tmp);

		key = g_strdup_printf("input.xkb.%s", map[i]);

		if(property != NULL) 
			g_free(property);
		g_free(value);
		g_free(key);
		
	}

	/* cleanup */
	g_free(conf);
	g_free(list);
	g_hash_table_destroy(kbd_models);

	return 0;
}