From 3f0c6ac96b206a18094241dd3eb7f8f3c25863fd Mon Sep 17 00:00:00 2001 From: Simmo Saan Date: Sat, 1 Sep 2018 15:18:12 +0300 Subject: [PATCH] gui: add gui_focus_info hashtable info (#1245) --- src/core/weechat.c | 2 ++ src/gui/gui-focus.c | 78 +++++++++++++++++++++++++++++++++++++++++++++ src/gui/gui-focus.h | 1 + 3 files changed, 81 insertions(+) diff --git a/src/core/weechat.c b/src/core/weechat.c index 82178ce32..9a61c6894 100644 --- a/src/core/weechat.c +++ b/src/core/weechat.c @@ -76,6 +76,7 @@ #include "../gui/gui-chat.h" #include "../gui/gui-color.h" #include "../gui/gui-completion.h" +#include "../gui/gui-focus.h" #include "../gui/gui-key.h" #include "../gui/gui-layout.h" #include "../gui/gui-main.h" @@ -801,6 +802,7 @@ weechat_init (int argc, char *argv[], void (*gui_init_cb)()) command_init (); /* initialize WeeChat commands */ completion_init (); /* add core completion hooks */ gui_key_init (); /* init keys */ + gui_focus_init (); /* initialize focus */ network_init_gcrypt (); /* init gcrypt */ if (!secure_init ()) /* init secured data */ weechat_shutdown (EXIT_FAILURE, 0); diff --git a/src/gui/gui-focus.c b/src/gui/gui-focus.c index 4d04789b7..0f7dd3495 100644 --- a/src/gui/gui-focus.c +++ b/src/gui/gui-focus.c @@ -28,6 +28,7 @@ #include "../core/weechat.h" #include "../core/wee-hashtable.h" +#include "../core/wee-hook.h" #include "../core/wee-string.h" #include "../plugins/plugin.h" #include "gui-bar.h" @@ -255,3 +256,80 @@ gui_focus_to_hashtable (struct t_gui_focus_info *focus_info, const char *key) return hashtable; } + +/* + * Returns GUI focus info with hashtable "gui_focus_info". + */ + +struct t_hashtable * +gui_focus_info_hashtable_gui_focus_info_cb (const void *pointer, void *data, + const char *info_name, + struct t_hashtable *hashtable) +{ + char *error; + const char *ptr_value; + int x, y; + struct t_gui_focus_info *focus_info; + struct t_hashtable *focus_hashtable, *ret_hashtable; + + /* make C compiler happy */ + (void) pointer; + (void) data; + (void) info_name; + + if (!hashtable) + return NULL; + + /* parse coordinates */ + ptr_value = hashtable_get (hashtable, "x"); + if (!ptr_value) + return NULL; + error = NULL; + x = (int)strtol (ptr_value, &error, 10); + if (!error || error[0]) + return NULL; + + ptr_value = hashtable_get (hashtable, "y"); + if (!ptr_value) + return NULL; + error = NULL; + y = (int)strtol (ptr_value, &error, 10); + if (!error || error[0]) + return NULL; + + /* get focus info */ + focus_info = gui_focus_get_info (x, y); + if (!focus_info) + return NULL; + + /* convert to hashtable */ + focus_hashtable = gui_focus_to_hashtable (focus_info, NULL); /* no key */ + gui_focus_free_info (focus_info); + if (!focus_hashtable) + return NULL; + + hashtable_remove (focus_hashtable, "_key"); /* remove useless key */ + + /* run hook_focus callbacks that add extra data */ + ret_hashtable = hook_focus_get_data (focus_hashtable, NULL); /* no gesture */ + + free (focus_hashtable); + + return ret_hashtable; +} + +/* + * Initializes focus hooks. + */ + +void +gui_focus_init () +{ + hook_info_hashtable (NULL, + "gui_focus_info", + N_("get focus info"), + /* TRANSLATORS: please do not translate key names (enclosed by quotes) */ + N_("\"x\": x coordinate, \"y\": y coordinate"), + N_("see hook_focus"), + &gui_focus_info_hashtable_gui_focus_info_cb, NULL, NULL); +} \ No newline at end of file diff --git a/src/gui/gui-focus.h b/src/gui/gui-focus.h index 4126b7efc..c684556fc 100644 --- a/src/gui/gui-focus.h +++ b/src/gui/gui-focus.h @@ -45,5 +45,6 @@ extern struct t_gui_focus_info *gui_focus_get_info (int x, int y); extern void gui_focus_free_info (struct t_gui_focus_info *focus_info); extern struct t_hashtable *gui_focus_to_hashtable (struct t_gui_focus_info *focus_info, const char *key); +extern void gui_focus_init (); #endif /* WEECHAT_GUI_FOCUS_H */