From 766178a252ca0171431ce6fe7067411579b352c4 Mon Sep 17 00:00:00 2001 From: Mikael Magnusson Date: Tue, 5 Feb 2008 11:19:10 +0100 Subject: [PATCH] New SendKeyEvent and SetKeyTarget actions. These let you define SendKeyEvent actions that send a specified key to a target window. The target window can be changed at runtime with the SetKeyTarget action. --- Makefile.am | 1 + openbox/actions/all.c | 1 + openbox/actions/all.h | 1 + openbox/actions/sendkeyevent.c | 89 ++++++++++++++++++++++++++++++++++ po/POTFILES.in | 1 + 5 files changed, 93 insertions(+) create mode 100644 openbox/actions/sendkeyevent.c diff --git a/Makefile.am b/Makefile.am index 83bdcf0a..24ed0a9a 100644 --- a/Makefile.am +++ b/Makefile.am @@ -204,6 +204,7 @@ openbox_openbox_SOURCES = \ openbox/actions/resize.c \ openbox/actions/resizerelative.c \ openbox/actions/restart.c \ + openbox/actions/sendkeyevent.c \ openbox/actions/shade.c \ openbox/actions/showdesktop.c \ openbox/actions/showmenu.c \ diff --git a/openbox/actions/all.c b/openbox/actions/all.c index 0ac0dae4..f5d5d062 100644 --- a/openbox/actions/all.c +++ b/openbox/actions/all.c @@ -39,4 +39,5 @@ void action_all_startup(void) action_growtoedge_startup(); action_if_startup(); action_focustobottom_startup(); + action_sendkeyevent_startup(); } diff --git a/openbox/actions/all.h b/openbox/actions/all.h index ac58260c..332e8d11 100644 --- a/openbox/actions/all.h +++ b/openbox/actions/all.h @@ -40,5 +40,6 @@ void action_movetoedge_startup(void); void action_growtoedge_startup(void); void action_if_startup(void); void action_focustobottom_startup(void); +void action_sendkeyevent_startup(void); #endif diff --git a/openbox/actions/sendkeyevent.c b/openbox/actions/sendkeyevent.c new file mode 100644 index 00000000..2817882f --- /dev/null +++ b/openbox/actions/sendkeyevent.c @@ -0,0 +1,89 @@ +#include "openbox/actions.h" +#include "openbox/client.h" +#include "openbox/window.h" +#include "obt/display.h" +#include "gettext.h" + +typedef struct { + KeyCode key; +} Options; + +static gpointer setup_sendkey_func(xmlNodePtr node); +static void free_sendkey_func(gpointer options); +static gboolean sendkey(ObActionsData *data, gpointer options); +static gboolean settarget(ObActionsData *data, gpointer options); + +static Window target; + +void action_sendkeyevent_startup(void) +{ + actions_register("SendKeyEvent", + setup_sendkey_func, g_free, + sendkey, + NULL, NULL); + actions_register("SetKeyTarget", + NULL, NULL, + settarget, + NULL, NULL); +} + +static KeyCode parse_key(gchar *s) +{ + KeySym sym; + + sym = XStringToKeysym(s); + if (sym == NoSymbol) { + g_warning(_("Invalid key name '%s' in SendKeyEvent action."), s); + return 0; + } + + return XKeysymToKeycode(obt_display, sym); +} + +static gpointer setup_sendkey_func(xmlNodePtr node) +{ + xmlNodePtr n; + Options *o; + + o = g_new0(Options, 1); + + if ((n = obt_parse_find_node(node, "key"))) { + gchar *s = obt_parse_node_string(n); + o->key = parse_key(s); + g_free(s); + } else + o->key = parse_key("space"); + + return o; +} + +/* Always return FALSE because its not interactive */ +static gboolean sendkey(ObActionsData *data, gpointer options) +{ + Options *o = options; + XEvent ev; + + if (!o->key) /* the key couldn't be parsed */ + return FALSE; + + ev.xkey.window = target; + ev.xkey.state = 0; + ev.xkey.keycode = o->key; + obt_display_ignore_errors(TRUE); + ev.type = KeyPress; + XSendEvent(obt_display, target, False, 0, &ev); + ev.type = KeyRelease; + XSendEvent(obt_display, target, False, 0, &ev); + obt_display_ignore_errors(FALSE); + + return FALSE; +} + +/* Always return FALSE because its not interactive */ +static gboolean settarget(ObActionsData *data, gpointer options) +{ + if (data->client) + target = data->client->window; + + return FALSE; +} diff --git a/po/POTFILES.in b/po/POTFILES.in index 200e9ca3..f61e4435 100644 --- a/po/POTFILES.in +++ b/po/POTFILES.in @@ -2,6 +2,7 @@ openbox/actions.c openbox/actions/execute.c openbox/actions/exit.c +openbox/actions/sendkeyevent.c openbox/client.c openbox/client_list_combined_menu.c openbox/client_list_menu.c -- 2.34.1