/* * 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. * * */ /** * Read /etc/sysconfig/keyboard and convert the contents into an XKB * description. Save this configuration as an xorg.conf.d snippet. */ #include #include #include #include #include #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; 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 *model, *layout, *variant, *options; GHashTable* kbd_models; gchar *keytable; gchar *property; 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(); 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); 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_hash_table_destroy(kbd_models); return 0; }