Defining the complete layout in a single variable

This commit is contained in:
Pierre Pronchery 2011-10-27 23:29:34 +00:00
parent 59d70519a6
commit 2debe64195

View File

@ -45,14 +45,14 @@ struct _Keyboard
int y; int y;
}; };
typedef struct _KeyboardKeys typedef struct _KeyboardKeyDefinition
{ {
unsigned int row; unsigned int row;
unsigned int width; unsigned int width;
unsigned int modifier; unsigned int modifier;
unsigned int keysym; unsigned int keysym;
char const * label; char const * label;
} KeyboardKeys; } KeyboardKeyDefinition;
typedef enum _KeyboardLayoutSection typedef enum _KeyboardLayoutSection
{ {
@ -63,9 +63,15 @@ typedef enum _KeyboardLayoutSection
#define KLS_LAST KLS_SPECIAL #define KLS_LAST KLS_SPECIAL
#define KLS_COUNT (KLS_LAST + 1) #define KLS_COUNT (KLS_LAST + 1)
typedef struct _KeyboardLayoutDefinition
{
char const * label;
KeyboardKeyDefinition const * keys;
} KeyboardLayoutDefinition;
/* variables */ /* variables */
static const KeyboardKeys _keyboard_layout_letters[] = static KeyboardKeyDefinition const _keyboard_layout_letters[] =
{ {
{ 0, 2, 0, XK_q, "q" }, { 0, 2, 0, XK_q, "q" },
{ 0, 0, XK_Shift_L, XK_Q, "Q" }, { 0, 0, XK_Shift_L, XK_Q, "Q" },
@ -134,7 +140,7 @@ static const KeyboardKeys _keyboard_layout_letters[] =
{ 0, 0, 0, 0, NULL } { 0, 0, 0, 0, NULL }
}; };
static const KeyboardKeys _keyboard_layout_keypad[] = static KeyboardKeyDefinition const _keyboard_layout_keypad[] =
{ {
{ 0, 3, 0, XK_Num_Lock, "Num" }, { 0, 3, 0, XK_Num_Lock, "Num" },
{ 0, 3, 0, XK_KP_Home, "\xe2\x86\x96" }, { 0, 3, 0, XK_KP_Home, "\xe2\x86\x96" },
@ -168,7 +174,7 @@ static const KeyboardKeys _keyboard_layout_keypad[] =
{ 0, 0, 0, 0, NULL } { 0, 0, 0, 0, NULL }
}; };
static const KeyboardKeys _keyboard_layout_special[] = static KeyboardKeyDefinition const _keyboard_layout_special[] =
{ {
{ 0, 3, 0, XK_Escape, "Esc" }, { 0, 3, 0, XK_Escape, "Esc" },
{ 0, 2, 0, XK_F1, "F1" }, { 0, 2, 0, XK_F1, "F1" },
@ -231,17 +237,18 @@ static const KeyboardKeys _keyboard_layout_special[] =
{ 0, 0, 0, 0, NULL } { 0, 0, 0, 0, NULL }
}; };
static const KeyboardKeys * _keyboard_layout[KLS_COUNT] = static KeyboardLayoutDefinition _keyboard_layout[KLS_COUNT] =
{ {
_keyboard_layout_letters, { "Abc", _keyboard_layout_letters },
_keyboard_layout_keypad, { "123", _keyboard_layout_keypad },
_keyboard_layout_special { ",./", _keyboard_layout_special }
}; };
/* prototypes */ /* prototypes */
static GtkWidget * _keyboard_add_layout(Keyboard * keyboard, static GtkWidget * _keyboard_add_layout(Keyboard * keyboard,
KeyboardKeys const * keys, unsigned int n); KeyboardLayoutDefinition * definitions,
size_t definitions_cnt, KeyboardLayoutSection section);
/* public */ /* public */
@ -314,17 +321,14 @@ Keyboard * keyboard_new(KeyboardPrefs * prefs)
/* layouts */ /* layouts */
vbox = gtk_vbox_new(TRUE, 4); vbox = gtk_vbox_new(TRUE, 4);
gtk_widget_show(vbox); gtk_widget_show(vbox);
if((widget = _keyboard_add_layout(keyboard, if((widget = _keyboard_add_layout(keyboard, _keyboard_layout,
_keyboard_layout[KLS_LETTERS], KLS_COUNT, KLS_LETTERS)) != NULL)
KLS_LETTERS)) != NULL)
gtk_box_pack_start(GTK_BOX(vbox), widget, TRUE, TRUE, 0); gtk_box_pack_start(GTK_BOX(vbox), widget, TRUE, TRUE, 0);
if((widget = _keyboard_add_layout(keyboard, if((widget = _keyboard_add_layout(keyboard, _keyboard_layout,
_keyboard_layout[KLS_KEYPAD], KLS_COUNT, KLS_KEYPAD)) != NULL)
KLS_KEYPAD)) != NULL)
gtk_box_pack_start(GTK_BOX(vbox), widget, TRUE, TRUE, 0); gtk_box_pack_start(GTK_BOX(vbox), widget, TRUE, TRUE, 0);
if((widget = _keyboard_add_layout(keyboard, if((widget = _keyboard_add_layout(keyboard, _keyboard_layout,
_keyboard_layout[KLS_SPECIAL], KLS_COUNT, KLS_SPECIAL)) != NULL)
KLS_SPECIAL)) != NULL)
gtk_box_pack_start(GTK_BOX(vbox), widget, TRUE, TRUE, 0); gtk_box_pack_start(GTK_BOX(vbox), widget, TRUE, TRUE, 0);
gtk_container_add(GTK_CONTAINER(keyboard->window), vbox); gtk_container_add(GTK_CONTAINER(keyboard->window), vbox);
if(prefs->embedded == 0) if(prefs->embedded == 0)
@ -411,14 +415,15 @@ void keyboard_show(Keyboard * keyboard, gboolean show)
static void _layout_changed(GtkWidget * widget, gpointer data); static void _layout_changed(GtkWidget * widget, gpointer data);
static GtkWidget * _keyboard_add_layout(Keyboard * keyboard, static GtkWidget * _keyboard_add_layout(Keyboard * keyboard,
KeyboardKeys const * keys, unsigned int n) KeyboardLayoutDefinition * definitions,
size_t definitions_cnt, KeyboardLayoutSection section)
{ {
KeyboardLayout ** p; KeyboardLayout ** p;
KeyboardLayout * layout; KeyboardLayout * layout;
KeyboardKeyDefinition const * keys;
size_t i; size_t i;
KeyboardKey * key; KeyboardKey * key;
GtkWidget * widget; GtkWidget * widget;
char const * labels[] = { "Abc", "123", ",./", NULL };
unsigned long l; unsigned long l;
if((p = realloc(keyboard->layouts, sizeof(*p) * (keyboard->layouts_cnt if((p = realloc(keyboard->layouts, sizeof(*p) * (keyboard->layouts_cnt
@ -428,6 +433,7 @@ static GtkWidget * _keyboard_add_layout(Keyboard * keyboard,
if((layout = keyboard_layout_new()) == NULL) if((layout = keyboard_layout_new()) == NULL)
return NULL; return NULL;
keyboard->layouts[keyboard->layouts_cnt++] = layout; keyboard->layouts[keyboard->layouts_cnt++] = layout;
keys = definitions[section].keys;
for(i = 0; keys[i].width != 0; i++) for(i = 0; keys[i].width != 0; i++)
{ {
key = keyboard_layout_add(layout, keys[i].row, keys[i].width, key = keyboard_layout_add(layout, keys[i].row, keys[i].width,
@ -443,10 +449,11 @@ static GtkWidget * _keyboard_add_layout(Keyboard * keyboard,
widget = gtk_combo_box_new_text(); widget = gtk_combo_box_new_text();
gtk_widget_modify_font(gtk_bin_get_child(GTK_BIN(widget)), gtk_widget_modify_font(gtk_bin_get_child(GTK_BIN(widget)),
keyboard->font); keyboard->font);
for(l = 0; labels[l] != NULL; l++) for(l = 0; l < definitions_cnt; l++)
{ {
gtk_combo_box_append_text(GTK_COMBO_BOX(widget), labels[l]); gtk_combo_box_append_text(GTK_COMBO_BOX(widget),
if(l == n) definitions[l].label);
if(l == section)
{ {
g_object_set_data(G_OBJECT(widget), "layout", g_object_set_data(G_OBJECT(widget), "layout",
(void *)l); (void *)l);