Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for a-z keys #200

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,4 @@ Makefile.in
/flatpak/.flatpak-builder/
/flatpak/build-dir/
/.cache/
/.vscode/
78 changes: 71 additions & 7 deletions src/callbacks.c
Original file line number Diff line number Diff line change
Expand Up @@ -270,13 +270,15 @@ gboolean on_buttonpress (GtkWidget *win,
gdk_window_set_event_compression(gtk_widget_get_window(data->win), TRUE);
}

/* See GdkModifierType. Am I fixing a Gtk misbehaviour??? */
ev->state |= 1 << (ev->button + 7);
// add new buttons to GromitState
GromitState newState = data->state;
newState.buttons |= (ev->button <= 10) ? 1 << (ev->button - 1) : 0;
newState.modifiers = ev->state & 0xF;


if (ev->state != devdata->state ||
if (!compare_state(data->state, newState) ||
devdata->lastslave != gdk_event_get_source_device ((GdkEvent *) ev))
select_tool (data, ev->device, gdk_event_get_source_device ((GdkEvent *) ev), ev->state);
select_tool (data, ev->device, gdk_event_get_source_device ((GdkEvent *) ev), newState);

GromitPaintType type = devdata->cur_context->type;

Expand All @@ -301,7 +303,7 @@ gboolean on_buttonpress (GtkWidget *win,
if(data->maxwidth > devdata->cur_context->maxwidth)
data->maxwidth = devdata->cur_context->maxwidth;

if (ev->button <= 5)
if (ev->button <= 10)
draw_line (data, ev->device, ev->x, ev->y, ev->x, ev->y);

coord_list_prepend (data, ev->device, ev->x, ev->y, data->maxwidth);
Expand All @@ -325,12 +327,22 @@ gboolean on_motion (GtkWidget *win,
if (!devdata->is_grabbed)
return FALSE;

// GdkEventMotion->state has only buttons 1-5, keep 6-10
GromitState newState = data->state;
newState.buttons &= 0x3E0; // remove old 1-5, keep 6-10
newState.buttons |= (ev->state >> 8) & 0x1F; // update new 1-5
newState.modifiers = ev->state & 0xF;

// return if there is no button pressed
if(!newState.buttons)
return TRUE;

if(data->debug)
g_printerr("DEBUG: Device '%s': motion to (x,y)=(%.2f : %.2f)\n", gdk_device_get_name(ev->device), ev->x, ev->y);

if (ev->state != devdata->state ||
if(!compare_state(data->state, newState) ||
devdata->lastslave != gdk_event_get_source_device ((GdkEvent *) ev))
select_tool (data, ev->device, gdk_event_get_source_device ((GdkEvent *) ev), ev->state);
select_tool (data, ev->device, gdk_event_get_source_device ((GdkEvent *) ev), newState);

GromitPaintType type = devdata->cur_context->type;

Expand Down Expand Up @@ -455,6 +467,11 @@ gboolean on_buttonrelease (GtkWidget *win,
(ev->y != devdata->lasty))
on_motion(win, (GdkEventMotion *) ev, user_data);

// remove released button bit from GromitState
guint button = 1 << (ev->button - 1);
data->state.buttons &= ~button;
data->state.modifiers = ev->state & 0xF;

if (!devdata->is_grabbed)
return FALSE;

Expand Down Expand Up @@ -491,6 +508,53 @@ gboolean on_buttonrelease (GtkWidget *win,
return TRUE;
}

gboolean on_keypress(GtkWidget *win, GdkEventKey *ev, gpointer user_data)
{
GromitData *data = (GromitData *)user_data;

guint32 keyunicode = gdk_keyval_to_unicode(ev->keyval);
gchar *keyname = gdk_keyval_name(ev->keyval);
GdkDevice *dev = gdk_event_get_device((GdkEvent *)ev);

if (data->debug)
g_printerr("DEBUG: Received key %s %d press from device '%s'\n", keyname, keyunicode, gdk_device_get_name(dev));

// add key to GromitState
if (keyunicode >= GDK_KEY_a && keyunicode <= GDK_KEY_z)
{
data->state.keys |= 1 << (keyunicode - GDK_KEY_a);
return TRUE;
}
if (keyunicode >= GDK_KEY_A && keyunicode <= GDK_KEY_Z)
{
data->state.keys |= 1 << (keyunicode - GDK_KEY_A);
return TRUE;
}

return FALSE;
}

gboolean on_keyrelease(GtkWidget *win, GdkEventKey *ev, gpointer user_data)
{
GromitData *data = (GromitData *)user_data;

guint32 keyunicode = gdk_keyval_to_unicode(ev->keyval);

// remove key from GromitState
if (keyunicode >= GDK_KEY_a && keyunicode <= GDK_KEY_z)
{
data->state.keys &= ~(1 << (keyunicode - GDK_KEY_a));
return TRUE;
}
if (keyunicode >= GDK_KEY_A && keyunicode <= GDK_KEY_Z)
{
data->state.keys &= ~(1 << (keyunicode - GDK_KEY_A));
return TRUE;
}

return FALSE;
}

/* Remote control */
void on_mainapp_selection_get (GtkWidget *widget,
GtkSelectionData *selection_data,
Expand Down
3 changes: 3 additions & 0 deletions src/callbacks.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ gboolean on_motion (GtkWidget *win, GdkEventMotion *ev, gpointer user_data);

gboolean on_buttonrelease (GtkWidget *win, GdkEventButton *ev, gpointer user_data);

gboolean on_keypress(GtkWidget *win, GdkEventKey *ev, gpointer user_data);
gboolean on_keyrelease(GtkWidget *win, GdkEventKey *ev, gpointer user_data);

void on_mainapp_selection_get (GtkWidget *widget,
GtkSelectionData *selection_data,
guint info,
Expand Down
63 changes: 37 additions & 26 deletions src/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,33 +36,33 @@

#define KEYFILE_FLAGS G_KEY_FILE_KEEP_COMMENTS|G_KEY_FILE_KEEP_TRANSLATIONS

static gpointer HOTKEY_SYMBOL_VALUE = (gpointer) 3;
static gpointer UNDOKEY_SYMBOL_VALUE = (gpointer) 4;
static gpointer HOTKEY_SYMBOL_VALUE = (gpointer) 5;
static gpointer UNDOKEY_SYMBOL_VALUE = (gpointer) 6;

/*
* Functions for parsing the Configuration-file
*/

static gchar* parse_name (GScanner *scanner)
static gboolean parse_name (GScanner *scanner, GromitLookupKey *key)
{
GTokenType token;

guint buttons = 0;
guint modifier = 0;
gulong keys = 0;
guint len = 0;
gchar *name;

token = g_scanner_cur_token(scanner);

if (token != G_TOKEN_STRING)
{
g_scanner_unexp_token (scanner, G_TOKEN_STRING, NULL,
NULL, NULL, "aborting", TRUE);
return NULL;
return 0;
}

len = strlen (scanner->value.v_string);
name = g_strndup (scanner->value.v_string, len + 3);
key->name = g_strndup (scanner->value.v_string, len);

token = g_scanner_get_next_token (scanner);

Expand All @@ -74,24 +74,29 @@ static gchar* parse_name (GScanner *scanner)
{
g_scanner_set_scope (scanner, 1);
scanner->config->int_2_float = 0;
modifier = buttons = 0;
modifier = buttons = keys = 0;
while ((token = g_scanner_get_next_token (scanner))
!= G_TOKEN_RIGHT_BRACE)
{
if (token == G_TOKEN_SYMBOL)
{
if ((intptr_t) scanner->value.v_symbol < 11)
if ((intptr_t) scanner->value.v_symbol <= 10)
buttons |= 1 << ((intptr_t) scanner->value.v_symbol - 1);
else
modifier |= 1 << ((intptr_t) scanner->value.v_symbol - 11);
}
else if (token == G_TOKEN_INT)
{
if (scanner->value.v_int <= 5 && scanner->value.v_int > 0)
if (scanner->value.v_int <= 10 && scanner->value.v_int > 0)
buttons |= 1 << (scanner->value.v_int - 1);
else
g_printerr ("Only Buttons 1-5 are supported!\n");
g_printerr ("Only Buttons 1-10 are supported!\n");
}
// lower case and upper case write the same bit
else if (token >= GDK_KEY_a && token <= GDK_KEY_z)
keys |= 1 << (token - GDK_KEY_a);
else if (token >= GDK_KEY_A && token <= GDK_KEY_Z)
keys |= 1 << (token - GDK_KEY_A);
else
{
g_printerr ("skipped token\n");
Expand All @@ -102,12 +107,11 @@ static gchar* parse_name (GScanner *scanner)
token = g_scanner_get_next_token (scanner);
}

name [len] = 124;
name [len+1] = buttons + 64;
name [len+2] = modifier + 48;
name [len+3] = 0;
key->state.buttons = buttons;
key->state.modifiers = modifier;
key->state.keys = keys;

return name;
return 1;
}

gboolean parse_config (GromitData *data)
Expand All @@ -120,7 +124,7 @@ gboolean parse_config (GromitData *data)
gchar *filename;
int file;

gchar *name, *copy;
gboolean parsed;

GromitPaintType type;
GdkRGBA *fg_color=NULL;
Expand Down Expand Up @@ -181,10 +185,15 @@ gboolean parse_config (GromitData *data)
g_scanner_scope_add_symbol (scanner, 1, "BUTTON3", (gpointer) 3);
g_scanner_scope_add_symbol (scanner, 1, "BUTTON4", (gpointer) 4);
g_scanner_scope_add_symbol (scanner, 1, "BUTTON5", (gpointer) 5);
g_scanner_scope_add_symbol (scanner, 1, "BUTTON6", (gpointer) 6);
g_scanner_scope_add_symbol (scanner, 1, "BUTTON7", (gpointer) 7);
g_scanner_scope_add_symbol (scanner, 1, "BUTTON8", (gpointer) 8);
g_scanner_scope_add_symbol (scanner, 1, "BUTTON9", (gpointer) 9);
g_scanner_scope_add_symbol (scanner, 1, "BUTTON10", (gpointer) 10);
g_scanner_scope_add_symbol (scanner, 1, "SHIFT", (gpointer) 11);
g_scanner_scope_add_symbol (scanner, 1, "CONTROL", (gpointer) 12);
g_scanner_scope_add_symbol (scanner, 1, "META", (gpointer) 13);
g_scanner_scope_add_symbol (scanner, 1, "ALT", (gpointer) 13);
g_scanner_scope_add_symbol (scanner, 1, "CONTROL", (gpointer) 13);
g_scanner_scope_add_symbol (scanner, 1, "META", (gpointer) 14);
g_scanner_scope_add_symbol (scanner, 1, "ALT", (gpointer) 14);

g_scanner_scope_add_symbol (scanner, 2, "size", (gpointer) 1);
g_scanner_scope_add_symbol (scanner, 2, "color", (gpointer) 2);
Expand All @@ -206,10 +215,11 @@ gboolean parse_config (GromitData *data)
/*
* New tool definition
*/
GromitLookupKey keyName = {};

name = parse_name (scanner);
parsed = parse_name (scanner, &keyName);

if(!name)
if(!parsed)
goto cleanup;

token = g_scanner_cur_token(scanner);
Expand Down Expand Up @@ -239,11 +249,12 @@ gboolean parse_config (GromitData *data)
}
else if (token == G_TOKEN_STRING)
{
copy = parse_name (scanner);
if(!copy)
GromitLookupKey keyCopy = {};
parsed = parse_name (scanner, &keyCopy);
if(!parsed)
goto cleanup;
token = g_scanner_cur_token(scanner);
context_template = g_hash_table_lookup (data->tool_config, copy);
context_template = g_hash_table_lookup (data->tool_config, key2string(keyCopy));
if (context_template)
{
type = context_template->type;
Expand All @@ -257,7 +268,7 @@ gboolean parse_config (GromitData *data)
else
{
g_printerr ("WARNING: Unable to copy \"%s\": "
"not yet defined!\n", copy);
"not yet defined!\n", key2string(keyCopy));
}
}
else
Expand Down Expand Up @@ -430,7 +441,7 @@ gboolean parse_config (GromitData *data)

context = paint_context_new (data, type, fg_color, width,
arrowsize, arrowtype, minwidth, maxwidth);
g_hash_table_insert (data->tool_config, name, context);
g_hash_table_insert (data->tool_config, key2string(keyName), context);
}
else if (token == G_TOKEN_SYMBOL &&
(scanner->value.v_symbol == HOTKEY_SYMBOL_VALUE ||
Expand Down
Loading