From b35038e70a5f5693cf404378a49ec43cb3a4b8bb Mon Sep 17 00:00:00 2001 From: Dana Jansens Date: Thu, 31 May 2007 22:34:01 +0000 Subject: [PATCH] add the keybindings code --- src/keybindings.c | 96 +++++++++++++++++++++++++++++++++++++++++++++++ src/keybindings.h | 25 ++++++++++++ 2 files changed, 121 insertions(+) create mode 100644 src/keybindings.c create mode 100644 src/keybindings.h diff --git a/src/keybindings.c b/src/keybindings.c new file mode 100644 index 0000000..c7c0971 --- /dev/null +++ b/src/keybindings.c @@ -0,0 +1,96 @@ +/* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*- + + keybindings.c for ObConf, the configuration tool for Openbox + Copyright (c) 2007 Dana Jansens + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program 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 General Public License for more details. + + See the COPYING file for a copy of the GNU General Public License. +*/ + +#include "main.h" +#include "tree.h" + +static gboolean mapping = FALSE; +static gchar *saved_text = NULL; + +static gboolean validate_key(const gchar *s); + +void keybindings_setup_tab() +{ + GtkWidget *w; + gchar *s; + + mapping = TRUE; + + w = get_widget("chain_quit_key"); + s = tree_get_string("keyboard/chainQuitKey", "C-g"); + gtk_entry_set_text(GTK_ENTRY(w), s); + g_free(s); + + mapping = FALSE; +} + +void on_chain_quit_key_focus_in(GtkEntry *w, gpointer data) +{ + g_assert(saved_text == NULL); + saved_text = g_strdup(gtk_entry_get_text(w)); +} + +void on_chain_quit_key_focus_out(GtkEntry *w, gpointer data) +{ + const gchar *s; + + if (mapping) return; + + s = gtk_entry_get_text(w); + if (!validate_key(s)) { + g_print("bad key binding: %s\n", s); + gtk_entry_set_text(w, saved_text); + } else + tree_set_string("keyboard/chainQuitKey", s); + + g_free(saved_text); + saved_text = NULL; +} + +static gboolean validate_key(const gchar *s) +{ + const gchar *next; + const gchar *valid[] = { "Mod1", "Mod2", "Mod3", "Mod4", "Mod5", + "Control", "C", "Alt", "A", "Meta", "M", + "Super", "W", "Shift", "S", "Hyper", "H", + NULL }; + + while ((next = strchr(s, '-'))) { + /* it's a modifier, validate it */ + const gchar **it; + gboolean found = FALSE; + + for (it = valid; *it && !found; ++it) + if (!g_ascii_strncasecmp(*it, s, strlen(*it))) + found = TRUE; + + if (!found) { + g_print("Invalid modifier\n"); + return FALSE; + } + + s = next + 1; /* skip past the '-' */ + } + /* we're at the real key part */ + if (!gdk_keyval_from_name(s)) { + g_print("Invalid key: %s\n", s); + return FALSE; + } + + return TRUE; +} diff --git a/src/keybindings.h b/src/keybindings.h new file mode 100644 index 0000000..4521270 --- /dev/null +++ b/src/keybindings.h @@ -0,0 +1,25 @@ +/* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*- + + keybindings.h for ObConf, the configuration tool for Openbox + Copyright (c) 2003-2007 Dana Jansens + Copyright (c) 2003 Tim Riley + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program 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 General Public License for more details. + + See the COPYING file for a copy of the GNU General Public License. +*/ + +#ifndef obconf__keybindings_h +#define obconf__keybindings_h + +void keybindings_setup_tab(); + +#endif -- 2.39.2