diff options
-rw-r--r-- | fedora-setup-keyboard.c | 69 |
1 files changed, 47 insertions, 22 deletions
diff --git a/fedora-setup-keyboard.c b/fedora-setup-keyboard.c index 212c12a..0cf98b1 100644 --- a/fedora-setup-keyboard.c +++ b/fedora-setup-keyboard.c @@ -24,12 +24,34 @@ * */ +/** + * Read /etc/sysconfig/keyboard and convert the contents into an XKB + * description. Save this configuration as an xorg.conf.d snippet. + */ + + #include <glib.h> +#include <glib/gstdio.h> #include <string.h> #include <stdlib.h> +#include <unistd.h> #define KBDCONFIG "/etc/sysconfig/keyboard" #define KEY_OPTIONS "input.xkb.options" +#define CONFIG_DIR "/etc/xorg.conf.d" +#define CONFIG_FILE "99-fedora-setup-keyboard.conf" + +const char *config_snippet = +"# This file is autogenerated by fedora-setup-keyboard. Any \n" \ +"# modifications will be lost.\n\n" \ +"Section \"InputClass\"\n" \ +" Identifier \"fedora-setup-keyboard\"\n" \ +" MatchIsKeyboard \"on\"\n" \ +"%s Option \"XkbLayout\" \"%s\"\n" \ +"%s Option \"XkbVariant\" \"%s\"\n" \ +"%s Option \"XkbModel\" \"%s\"\n" \ +"%s Option \"XkbOptions\" \"%s\"\n" \ +"EndSection\n"; gchar* remove_quotes(gchar *str) { gchar *tmp; @@ -57,13 +79,19 @@ int main() { GKeyFile *cfg_file; gchar *buffer, *conf; - gchar *map[] = { "layout", "model", "variant", "options" }; + gchar *model, *layout, *variant, *options; GHashTable* kbd_models; gchar *keytable; gchar *property; - guint n, i; - gchar **list; - gchar *key, *udi, *tmp; + FILE *file; + + /* Check if the directory even exists */ + if (g_access(CONFIG_DIR, W_OK | X_OK)) + return 1; + + file = g_fopen(CONFIG_DIR "/" CONFIG_FILE, "w"); + if (!file) + return 1; /* Parse the config file */ cfg_file = g_key_file_new(); @@ -83,27 +111,24 @@ int main() { 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); - - } + model = remove_quotes(g_key_file_get_value(cfg_file, "kbd", "MODEL", NULL)); + layout = remove_quotes(g_key_file_get_value(cfg_file, "kbd", "LAYOUT", NULL)); + variant = remove_quotes(g_key_file_get_value(cfg_file, "kbd", "VARIANT", NULL)); + options = remove_quotes(g_key_file_get_value(cfg_file, "kbd", "OPTIONS", NULL)); + + /* if a value is not present, comment out the line.*/ + g_fprintf(file, config_snippet, model ? "" : "#", model, + layout ? "" : "#", layout, + variant ? "" : "#", variant, + options ? "" : "#", options); + fclose(file); /* cleanup */ + g_free(model); + g_free(layout); + g_free(variant); + g_free(options); g_free(conf); - g_free(list); g_hash_table_destroy(kbd_models); return 0; |