summaryrefslogtreecommitdiff
path: root/src/e_mod_main.c
blob: fd7d5d0eff0e3a040e8162a0951cb0c0b7d3d633 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#include "e.h"

/* module setup */
E_API E_Module_Api e_modapi =
{
   E_MODULE_API_VERSION,
     "Presentator helper tool"
};

static Evas_Object *notify, *bx;
static Eina_Hash *string_hash;
static int entry_counter = 0;

static Ecore_Exe *keylogger;

typedef struct {
   unsigned int ref;
   char *name;
   Evas_Object *vis;
   Ecore_Timer *timer;
} Entry;

static void
_del_key(const char *name)
{
   Evas_Object *o;
   Eina_List *list;

   Entry *e = eina_hash_find(string_hash, name);

   //something fishy is going on sometimes, then we dont even have a entry
   if (!e)
     {
        ERR("What the fuck?!");
        return;
     }

   //deref the entry
   e->ref --;
   //free if null
   if (e->ref == 0)
     {

        //if ref is 0 nuke out that entry
        eina_hash_del_by_key(string_hash, name);
        entry_counter --;
        //we need to update the notify visuals depending on the state of the box
        if (entry_counter <= 0)
          evas_object_hide(notify);
     }
}

static Eina_Bool
_timed_del(void *data)
{
   Entry *e = data;
   _del_key(e->name);

   return 0;
}

static void
_add_key(const char *name)
{
   Eina_Strbuf *buf;
   Evas_Object *o;

   Entry *e = eina_hash_find(string_hash, name);

   //first if we have already displayed that string do not display it again
   if (e)
     {
        e->ref ++;
        ecore_timer_reset(e->timer);
        return;
     }

   //find a considerable text to display
   buf = eina_strbuf_new();
   if (entry_counter == 0)
     eina_strbuf_append_printf(buf, "%s", name);
   else
     eina_strbuf_append_printf(buf, " + %s", name);

   //create a label and attach to the box
   o = elm_label_add(bx);
   elm_object_text_set(o, eina_strbuf_string_get(buf));
   evas_object_show(o);
   elm_box_pack_end(bx, o);
   evas_object_show(notify);

   //save as entry somewhere
   e = calloc(1, sizeof(Entry));
   e->ref = 2; //one for beeing used and one for the minimum time
   e->vis = o;
   e->name = strdup(name);
   entry_counter ++;
   eina_hash_add(string_hash, name, e);
   e->timer = ecore_timer_add(0.5, _timed_del, e);
}

static void
_entry_free(Entry *e)
{
   evas_object_del(e->vis);
   free(e->name);
   free(e);
}

static Eina_Bool
_msg_from_child_handler(void *data EINA_UNUSED, int type EINA_UNUSED, void *event)
{
   Ecore_Exe_Event_Data *dataFromProcess = (Ecore_Exe_Event_Data *)event;

   for (int i = 0; dataFromProcess->lines[i].line; ++i)
     {
        char first = dataFromProcess->lines[i].line[0];
        char *key = &dataFromProcess->lines[i].line[1];
        if (first == ',')
          {
             _del_key(key);
          }
        else if (first == '.')
          {
             _add_key(key);
          }
        else
        {
            printf("WHAT THE FUCK\n");
        }
     }
   return ECORE_CALLBACK_PASS_ON;
}

static Eina_Bool
_msg_from_child_handler_error(void *data EINA_UNUSED, int type EINA_UNUSED, void *event)
{

   printf("ERROR!!!!!!!\n");

   return ECORE_CALLBACK_PASS_ON;
}

E_API void *
e_modapi_init(E_Module *m)
{
   ecore_init();

   string_hash = eina_hash_string_small_new(_entry_free);

   notify = elm_notify_add(e_comp->elm);
   elm_notify_allow_events_set(notify, EINA_TRUE);
   evas_object_layer_set(notify, E_LAYER_POPUP);
   elm_notify_align_set(notify, 0.5, 1.0);

   bx = elm_box_add(notify);
   elm_box_horizontal_set(bx, EINA_TRUE);
   elm_object_content_set(notify, bx);
   evas_object_show(bx);

   {
      keylogger = ecore_exe_pipe_run("/usr/local/bin/keylogger", ECORE_EXE_PIPE_WRITE |
                                                                 ECORE_EXE_PIPE_READ_LINE_BUFFERED |
                                                                 ECORE_EXE_PIPE_READ, notify);
      EINA_SAFETY_ON_NULL_RETURN_VAL(keylogger, NULL);
      ecore_event_handler_add(ECORE_EXE_EVENT_DATA, _msg_from_child_handler, NULL);
      ecore_event_handler_add(ECORE_EXE_EVENT_ERROR, _msg_from_child_handler_error, NULL);
   }

   return m;
}

E_API int
e_modapi_shutdown(E_Module *m EINA_UNUSED)
{

   ecore_shutdown();
   return 1;
}

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