<commit_before>#include "e.h"
#include "e_mod_main.h"

/* local structures */
typedef struct _Instance Instance;
struct _Instance 
{
   E_Gadcon_Client *gcc;
   Evas_Object *o_btn;
   Eina_List *handlers;
};

/* local function prototypes */
static E_Gadcon_Client *_gc_init(E_Gadcon *gc, const char *name, const char *id, const char *style);
static void _gc_shutdown(E_Gadcon_Client *gcc);
static void _gc_orient(E_Gadcon_Client *gcc, E_Gadcon_Orient orient);
static char *_gc_label(E_Gadcon_Client_Class *cc);
static Evas_Object *_gc_icon(E_Gadcon_Client_Class *cc, Evas *evas);
static const char *_gc_id_new(E_Gadcon_Client_Class *cc);
static void _cb_btn_click(void *data, void *data2);
static int _cb_border_focus_in(void *data, int type, void *event);
static int _cb_border_remove(void *data, int type, void *event);

/* local variables */
static Eina_List *instances = NULL;
static const char *mod_dir = NULL;

static const E_Gadcon_Client_Class _gc_class = 
{
   GADCON_CLIENT_CLASS_VERSION, "illume-kbd-toggle", 
     { _gc_init, _gc_shutdown, _gc_orient, _gc_label, _gc_icon, _gc_id_new, NULL, 
          e_gadcon_site_is_not_toolbar
     }, E_GADCON_CLIENT_STYLE_PLAIN
};

/* public functions */
EAPI E_Module_Api e_modapi = { E_MODULE_API_VERSION, "Illume Keyboard Toggle" };

EAPI void *
e_modapi_init(E_Module *m) 
{
   mod_dir = eina_stringshare_add(m->dir);
   e_gadcon_provider_register(&_gc_class);
   return m;
}

EAPI int 
e_modapi_shutdown(E_Module *m) 
{
   e_gadcon_provider_unregister(&_gc_class);
   if (mod_dir) eina_stringshare_del(mod_dir);
   mod_dir = NULL;
   return 1;
}

EAPI int 
e_modapi_save(E_Module *m) 
{
   return 1;
}

/* local function prototypes */
static E_Gadcon_Client *
_gc_init(E_Gadcon *gc, const char *name, const char *id, const char *style) 
{
   Instance *inst;
   Evas_Object *icon;
   char buff[PATH_MAX];

   snprintf(buff, sizeof(buff), "%s/e-module-illume-kbd-toggle.edj", mod_dir);

   inst = E_NEW(Instance, 1);
   inst->o_btn = e_widget_button_add(gc->evas, NULL, NULL, 
                                     _cb_btn_click, inst, NULL);
   icon = e_icon_add(evas_object_evas_get(inst->o_btn));
   e_icon_file_edje_set(icon, buff, "icon");
   e_widget_button_icon_set(inst->o_btn, icon);

   inst->gcc = e_gadcon_client_new(gc, name, id, style, inst->o_btn);
   inst->gcc->data = inst;

   inst->handlers = 
     eina_list_append(inst->handlers, 
                      ecore_event_handler_add(E_EVENT_BORDER_FOCUS_IN, 
                                              _cb_border_focus_in, inst));
   inst->handlers = 
     eina_list_append(inst->handlers, 
                      ecore_event_handler_add(E_EVENT_BORDER_REMOVE, 
                                              _cb_border_remove, inst));

   instances = eina_list_append(instances, inst);
   return inst->gcc;
}

static void 
_gc_shutdown(E_Gadcon_Client *gcc) 
{
   Instance *inst;
   Ecore_Event_Handler *handler;

   if (!(inst = gcc->data)) return;
   instances = eina_list_remove(instances, inst);
   if (inst->o_btn) evas_object_del(inst->o_btn);
   EINA_LIST_FREE(inst->handlers, handler)
     ecore_event_handler_del(handler);
   E_FREE(inst);
}

static void 
_gc_orient(E_Gadcon_Client *gcc, E_Gadcon_Orient orient) 
{
   e_gadcon_client_aspect_set(gcc, 16, 16);
   e_gadcon_client_min_size_set(gcc, 16, 16);
}

static char *
_gc_label(E_Gadcon_Client_Class *cc) 
{
   return _("Illume-Keyboard-Toggle");
}

static Evas_Object *
_gc_icon(E_Gadcon_Client_Class *cc, Evas *evas) 
{
   Evas_Object *o;
   char buff[PATH_MAX];

   snprintf(buff, sizeof(buff), "%s/e-module-illume-kbd-toggle.edj", mod_dir);
   o = edje_object_add(evas);
   edje_object_file_set(o, buff, "icon");
   return o;
}

static const char *
_gc_id_new(E_Gadcon_Client_Class *cc) 
{
   char buff[PATH_MAX];

   snprintf(buff, sizeof(buff), "%s.%d", _gc_class.name, 
            eina_list_count(instances));
   return strdup(buff);
}

static void 
_cb_btn_click(void *data, void *data2) 
{
   Ecore_X_Virtual_Keyboard_State state;
   Instance *inst;
   E_Border *bd;
   Evas_Object *icon;
   char buff[PATH_MAX];

   if (!(inst = data)) return;
   if (!(bd = e_border_focused_get())) return;

   snprintf(buff, sizeof(buff), "%s/e-module-illume-kbd-toggle.edj", mod_dir);

   icon = e_icon_add(evas_object_evas_get(inst->o_btn));
   state = bd->client.vkbd.state;
   if ((state == ECORE_X_VIRTUAL_KEYBOARD_STATE_OFF) ||
       (state == ECORE_X_VIRTUAL_KEYBOARD_STATE_UNKNOWN)) 
     {
        ecore_x_e_virtual_keyboard_state_set(bd->client.win, 
                                             ECORE_X_VIRTUAL_KEYBOARD_STATE_ON);
        e_icon_file_edje_set(icon, buff, "btn_icon");
     }
   else 
     {
        ecore_x_e_virtual_keyboard_state_set(bd->client.win, 
                                             ECORE_X_VIRTUAL_KEYBOARD_STATE_OFF);
        e_icon_file_edje_set(icon, buff, "icon");
     }
   e_widget_button_icon_set(inst->o_btn, icon);
}

static int 
_cb_border_focus_in(void *data, int type, void *event) 
{
   Instance *inst;
   E_Event_Border_Focus_In *ev;
   E_Border *bd;
   Evas_Object *icon;
   Ecore_X_Virtual_Keyboard_State state;
   char buff[PATH_MAX];

   if (!(inst = data)) return 1;
   ev = event;
   if (ev->border->stolen) return 1;
   if (!(bd = ev->border)) return 1;

   snprintf(buff, sizeof(buff), "%s/e-module-illume-kbd-toggle.edj", mod_dir);

   icon = e_icon_add(evas_object_evas_get(inst->o_btn));
   state = bd->client.vkbd.state;
   if ((state == ECORE_X_VIRTUAL_KEYBOARD_STATE_OFF) ||
       (state == ECORE_X_VIRTUAL_KEYBOARD_STATE_UNKNOWN)) 
     e_icon_file_edje_set(icon, buff, "icon");
   else 
     e_icon_file_edje_set(icon, buff, "btn_icon");
   e_widget_button_icon_set(inst->o_btn, icon);

   return 1;
}

static int 
_cb_border_remove(void *data, int type, void *event) 
{
   Instance *inst;
   Evas_Object *icon;
   char buff[PATH_MAX];

   if (!(inst = data)) return 1;
   snprintf(buff, sizeof(buff), "%s/e-module-illume-kbd-toggle.edj", mod_dir);
   icon = e_icon_add(evas_object_evas_get(inst->o_btn));
   e_icon_file_edje_set(icon, buff, "icon");
   e_widget_button_icon_set(inst->o_btn, icon);
   return 1;
}<commit_msg>Easier checks for kbd state.<commit_after>#include "e.h"
#include "e_mod_main.h"

/* local structures */
typedef struct _Instance Instance;
struct _Instance 
{
   E_Gadcon_Client *gcc;
   Evas_Object *o_btn;
   Eina_List *handlers;
};

/* local function prototypes */
static E_Gadcon_Client *_gc_init(E_Gadcon *gc, const char *name, const char *id, const char *style);
static void _gc_shutdown(E_Gadcon_Client *gcc);
static void _gc_orient(E_Gadcon_Client *gcc, E_Gadcon_Orient orient);
static char *_gc_label(E_Gadcon_Client_Class *cc);
static Evas_Object *_gc_icon(E_Gadcon_Client_Class *cc, Evas *evas);
static const char *_gc_id_new(E_Gadcon_Client_Class *cc);
static void _cb_btn_click(void *data, void *data2);
static int _cb_border_focus_in(void *data, int type, void *event);
static int _cb_border_remove(void *data, int type, void *event);

/* local variables */
static Eina_List *instances = NULL;
static const char *mod_dir = NULL;

static const E_Gadcon_Client_Class _gc_class = 
{
   GADCON_CLIENT_CLASS_VERSION, "illume-kbd-toggle", 
     { _gc_init, _gc_shutdown, _gc_orient, _gc_label, _gc_icon, _gc_id_new, NULL, 
          e_gadcon_site_is_not_toolbar
     }, E_GADCON_CLIENT_STYLE_PLAIN
};

/* public functions */
EAPI E_Module_Api e_modapi = { E_MODULE_API_VERSION, "Illume Keyboard Toggle" };

EAPI void *
e_modapi_init(E_Module *m) 
{
   mod_dir = eina_stringshare_add(m->dir);
   e_gadcon_provider_register(&_gc_class);
   return m;
}

EAPI int 
e_modapi_shutdown(E_Module *m) 
{
   e_gadcon_provider_unregister(&_gc_class);
   if (mod_dir) eina_stringshare_del(mod_dir);
   mod_dir = NULL;
   return 1;
}

EAPI int 
e_modapi_save(E_Module *m) 
{
   return 1;
}

/* local function prototypes */
static E_Gadcon_Client *
_gc_init(E_Gadcon *gc, const char *name, const char *id, const char *style) 
{
   Instance *inst;
   Evas_Object *icon;
   char buff[PATH_MAX];

   snprintf(buff, sizeof(buff), "%s/e-module-illume-kbd-toggle.edj", mod_dir);

   inst = E_NEW(Instance, 1);
   inst->o_btn = e_widget_button_add(gc->evas, NULL, NULL, 
                                     _cb_btn_click, inst, NULL);
   icon = e_icon_add(evas_object_evas_get(inst->o_btn));
   e_icon_file_edje_set(icon, buff, "icon");
   e_widget_button_icon_set(inst->o_btn, icon);

   inst->gcc = e_gadcon_client_new(gc, name, id, style, inst->o_btn);
   inst->gcc->data = inst;

   inst->handlers = 
     eina_list_append(inst->handlers, 
                      ecore_event_handler_add(E_EVENT_BORDER_FOCUS_IN, 
                                              _cb_border_focus_in, inst));
   inst->handlers = 
     eina_list_append(inst->handlers, 
                      ecore_event_handler_add(E_EVENT_BORDER_REMOVE, 
                                              _cb_border_remove, inst));

   instances = eina_list_append(instances, inst);
   return inst->gcc;
}

static void 
_gc_shutdown(E_Gadcon_Client *gcc) 
{
   Instance *inst;
   Ecore_Event_Handler *handler;

   if (!(inst = gcc->data)) return;
   instances = eina_list_remove(instances, inst);
   if (inst->o_btn) evas_object_del(inst->o_btn);
   EINA_LIST_FREE(inst->handlers, handler)
     ecore_event_handler_del(handler);
   E_FREE(inst);
}

static void 
_gc_orient(E_Gadcon_Client *gcc, E_Gadcon_Orient orient) 
{
   e_gadcon_client_aspect_set(gcc, 16, 16);
   e_gadcon_client_min_size_set(gcc, 16, 16);
}

static char *
_gc_label(E_Gadcon_Client_Class *cc) 
{
   return _("Illume-Keyboard-Toggle");
}

static Evas_Object *
_gc_icon(E_Gadcon_Client_Class *cc, Evas *evas) 
{
   Evas_Object *o;
   char buff[PATH_MAX];

   snprintf(buff, sizeof(buff), "%s/e-module-illume-kbd-toggle.edj", mod_dir);
   o = edje_object_add(evas);
   edje_object_file_set(o, buff, "icon");
   return o;
}

static const char *
_gc_id_new(E_Gadcon_Client_Class *cc) 
{
   char buff[PATH_MAX];

   snprintf(buff, sizeof(buff), "%s.%d", _gc_class.name, 
            eina_list_count(instances));
   return strdup(buff);
}

static void 
_cb_btn_click(void *data, void *data2) 
{
   Ecore_X_Virtual_Keyboard_State state;
   Instance *inst;
   E_Border *bd;
   Evas_Object *icon;
   char buff[PATH_MAX];

   if (!(inst = data)) return;
   if (!(bd = e_border_focused_get())) return;

   snprintf(buff, sizeof(buff), "%s/e-module-illume-kbd-toggle.edj", mod_dir);

   icon = e_icon_add(evas_object_evas_get(inst->o_btn));
   state = bd->client.vkbd.state;
   if (state <= ECORE_X_VIRTUAL_KEYBOARD_STATE_OFF) 
     {
        ecore_x_e_virtual_keyboard_state_set(bd->client.win, 
                                             ECORE_X_VIRTUAL_KEYBOARD_STATE_ON);
        e_icon_file_edje_set(icon, buff, "btn_icon");
     }
   else 
     {
        ecore_x_e_virtual_keyboard_state_set(bd->client.win, 
                                             ECORE_X_VIRTUAL_KEYBOARD_STATE_OFF);
        e_icon_file_edje_set(icon, buff, "icon");
     }
   e_widget_button_icon_set(inst->o_btn, icon);
}

static int 
_cb_border_focus_in(void *data, int type, void *event) 
{
   Instance *inst;
   E_Event_Border_Focus_In *ev;
   E_Border *bd;
   Evas_Object *icon;
   Ecore_X_Virtual_Keyboard_State state;
   char buff[PATH_MAX];

   if (!(inst = data)) return 1;
   ev = event;
   if (ev->border->stolen) return 1;
   if (!(bd = ev->border)) return 1;

   snprintf(buff, sizeof(buff), "%s/e-module-illume-kbd-toggle.edj", mod_dir);

   icon = e_icon_add(evas_object_evas_get(inst->o_btn));
   state = bd->client.vkbd.state;
   if (state <= ECORE_X_VIRTUAL_KEYBOARD_STATE_OFF)
     e_icon_file_edje_set(icon, buff, "icon");
   else 
     e_icon_file_edje_set(icon, buff, "btn_icon");
   e_widget_button_icon_set(inst->o_btn, icon);

   return 1;
}

static int 
_cb_border_remove(void *data, int type, void *event) 
{
   Instance *inst;
   Evas_Object *icon;
   char buff[PATH_MAX];

   if (!(inst = data)) return 1;
   snprintf(buff, sizeof(buff), "%s/e-module-illume-kbd-toggle.edj", mod_dir);
   icon = e_icon_add(evas_object_evas_get(inst->o_btn));
   e_icon_file_edje_set(icon, buff, "icon");
   e_widget_button_icon_set(inst->o_btn, icon);
   return 1;
}
{'fraction_non_alphanumeric': 0.11712373490042442, 'fraction_numerical': 0.003101534443356187, 'mean_word_length': 3.721772639691715, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 65, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 18, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3422428', 'n_tokens_mistral': 4895, 'n_tokens_neox': 4758, 'n_words': 852}