summaryrefslogtreecommitdiff
path: root/src/e_mod_main.c
blob: 7b8eeee0200e3bcae371443c00826df913161039 (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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
#include <e.h>
#include "e_mod_main.h"
#if TIME_WITH_SYS_TIME
# include <sys/time.h>
# include <time.h>
#else
# if HAVE_SYS_TIME_H
#  include <sys/time.h>
# else
#  include <time.h>
# endif
#endif
#if HAVE_LOCALE_H
#  include <locale.h>
#endif

// default timeout in minutes
#define DEFAULT_TIMEOUT 20
#define CONFIG_UPDATE_TIMEOUT 0.5

/* Func Proto Requirements for Gadcon */
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 __UNUSED__);
static const char *_gc_label(const E_Gadcon_Client_Class *client_class __UNUSED__);
static Evas_Object *_gc_icon(const E_Gadcon_Client_Class *client_class __UNUSED__, Evas *evas);
static const char *_gc_id_new(const E_Gadcon_Client_Class *client_class EINA_UNUSED);

/* Module Protos */
static void _espresso_cb_mouse_down(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info);
static void _espresso_menu_cb_timeout_set(void *data, E_Menu *m __UNUSED__, E_Menu_Item *mi __UNUSED__);
static void _espresso_menu_cb_post(void *data __UNUSED__, E_Menu *m __UNUSED__);
static Eina_Bool _espresso_cb_timeout(void *data __UNUSED__);

static E_Config_DD *conf_edd = NULL;
static Ecore_Timer *espresso_timer, *config_timer;

Config *espresso_config = NULL;
static char id_buffer[64];

/* Define the class and gadcon functions this module provides */
static const E_Gadcon_Client_Class _gc_class = 
{
   GADCON_CLIENT_CLASS_VERSION, "Espresso", 
     { _gc_init, _gc_shutdown, _gc_orient, _gc_label, _gc_icon, _gc_id_new, 
          NULL, NULL },
   E_GADCON_CLIENT_STYLE_PLAIN
};

typedef struct _Instance Instance;
struct _Instance
{
   E_Gadcon_Client *gcc;
   Evas_Object *espresso, *o_tip;
   E_Gadcon_Popup *tip;
};

typedef struct _Timer_Data Timer_Data;
struct _Timer_Data
{
	int minutes;
	Instance *inst;
};

static void _presentation_enable(Instance *inst, Eina_Bool enable);
static void _espresso_config_updated(Instance *inst);

static E_Gadcon_Client *
_gc_init(E_Gadcon *gc, const char *name, const char *id, const char *style)
{
   Evas_Object *o;
   E_Gadcon_Client *gcc;
   Instance *inst;
   char buf[PATH_MAX];

   inst = E_NEW(Instance, 1);

   o = edje_object_add(gc->evas);
   snprintf(buf, sizeof(buf), "%s/espresso.edj", espresso_config->mod_dir);
   if (!e_theme_edje_object_set(o, "base/theme/modules/espresso", 
                                "modules/espresso/main"))
     edje_object_file_set(o, buf, "modules/espresso/main");
   evas_object_show(o);

   gcc = e_gadcon_client_new(gc, name, id, style, o);
   gcc->data = inst;
   inst->gcc = gcc;
   inst->espresso = o;
   espresso_config->cups = eina_list_append(espresso_config->cups, inst);

   evas_object_event_callback_add(o, EVAS_CALLBACK_MOUSE_DOWN,
                                  _espresso_cb_mouse_down, inst);

   _presentation_enable(inst, e_config->mode.presentation);

   return gcc;
}

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

   if (!(inst = gcc->data)) return;

   evas_object_event_callback_del(inst->espresso, EVAS_CALLBACK_MOUSE_DOWN,
                                  _espresso_cb_mouse_down);

   if (inst->espresso) evas_object_del(inst->espresso);

   E_FREE(inst);
}

static void
_gc_orient(E_Gadcon_Client *gcc, E_Gadcon_Orient orient __UNUSED__)
{
   Instance *inst;
   Evas_Coord mw;

   if (!(inst = gcc->data)) return;
   edje_object_size_min_calc(inst->espresso, &mw, NULL);
   e_gadcon_client_min_size_set(gcc, mw, 16);
}

static const char *
_gc_label(const E_Gadcon_Client_Class *client_class __UNUSED__)
{
   return D_("Espresso");
}

static Evas_Object *
_gc_icon(const E_Gadcon_Client_Class *client_class __UNUSED__, Evas *evas)
{
   Evas_Object *o;
   char buf[PATH_MAX];

   o = edje_object_add (evas);
   snprintf(buf, sizeof(buf), "%s/e-module-espresso.edj", espresso_config->mod_dir);
   edje_object_file_set(o, buf, "icon");
   return o;
}

static const char *
_gc_id_new(const E_Gadcon_Client_Class *client_class EINA_UNUSED)
{
   id_buffer[63] = '\0';
   snprintf(id_buffer, sizeof(id_buffer), "espresso.%i", (eina_list_count(espresso_config->cups) - 1));
   return id_buffer;
}

static void
_espresso_cb_mouse_down(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info)
{
   Instance *inst;
   Evas_Event_Mouse_Down *ev;

   inst = data;
   ev = event_info;
   if ((ev->button == 3) && (!espresso_config->menu))
     {
        E_Menu *m;
        E_Menu_Item *mi;
        Timer_Data *td = NULL;
        int x = 0, y = 0;
        m = e_menu_new();

#define ADD_MENU_ITEM(label, timeout) \
        td = E_NEW(Timer_Data, 1); \
        td->minutes = timeout; \
        td->inst = inst; \
        mi = e_menu_item_new(m); \
        e_menu_item_label_set(mi, label); \
        e_menu_item_callback_set(mi, _espresso_menu_cb_timeout_set, (void*)td)
		ADD_MENU_ITEM( D_("20 Minutes"), 20);
		ADD_MENU_ITEM( D_("30 Minutes"), 30);
		ADD_MENU_ITEM( D_("1 hour"), 60);
		ADD_MENU_ITEM( D_("2 hour"), 120);
#undef ADD_MENU_ITEM
        m = e_gadcon_client_util_menu_items_append(inst->gcc, m, 0);
        e_menu_post_deactivate_callback_set(m, _espresso_menu_cb_post, inst);
        espresso_config->menu = m;
	
        e_gadcon_canvas_zone_geometry_get(inst->gcc->gadcon, &x, &y, NULL, NULL);
        e_menu_activate_mouse (m,
                               e_util_zone_current_get(e_manager_current_get()), 
                               x + ev->output.x, y + ev->output.y, 1, 1,
                               E_MENU_POP_DIRECTION_DOWN, ev->timestamp);
     }
     if (ev->button == 1)
     {
     	_presentation_enable(inst, !e_config->mode.presentation);
     }
}

static void
_espresso_menu_cb_post(void *data __UNUSED__, E_Menu *m __UNUSED__)
{
   if (!espresso_config->menu) return;
   e_object_del(E_OBJECT(espresso_config->menu));
   espresso_config->menu = NULL;
}

static void _indicator_set(Instance *inst, Eina_Bool enable)
{
  Evas_Object *icon = NULL;
  const char *signal = "on";

  if (!inst)
  {
    return;
  }

  icon = inst->espresso;
  e_config->mode.presentation = enable;
  e_config_mode_changed();
  if (!e_config->mode.presentation)
  {
    signal = "off";
  }
  edje_object_signal_emit(icon, signal, "e");
}

static void _presentation_enable(Instance *inst, Eina_Bool enable)
{
	if (!inst || (enable == e_config->mode.presentation))
		return;
  _indicator_set(inst, enable);
	_espresso_config_updated(inst);
}

static void
_espresso_menu_cb_timeout_set(void *data, E_Menu *m __UNUSED__, E_Menu_Item *mi __UNUSED__)
{
	Timer_Data *td = (Timer_Data*)data;
	espresso_config->timeout = td->minutes;
	_espresso_config_updated(td->inst);
	_espresso_menu_cb_post(NULL, NULL);
	E_FREE(td);
}

void
_espresso_config_updated(Instance *inst)
{
   if (!espresso_config)
   	return;
   if (espresso_timer)
   {
   	ecore_timer_del(espresso_timer);
   	espresso_timer = NULL;
   }
   if (e_config->mode.presentation)
   {
	   	espresso_timer = ecore_timer_add((espresso_config->timeout * 60), _espresso_cb_timeout, inst);
   }
}

static Eina_Bool
_espresso_cb_timeout(void *data)
{
	Instance *inst = NULL;
	Eina_List *iter = NULL;

	EINA_LIST_FOREACH(espresso_config->cups, iter,inst)
  {
    _presentation_enable(inst, EINA_FALSE);
  }
	return EINA_TRUE;
}


static Eina_Bool
_espresso_cb_config_update(void *data)
{
  Instance *inst = NULL;
  Eina_List *iter = NULL;

  EINA_LIST_FOREACH(espresso_config->cups, iter,inst)
  {
    if (e_config->mode.presentation)
    {
      _presentation_enable(inst, EINA_FALSE);
    }
    else
    {
      _indicator_set(inst, EINA_TRUE);
    }
  }
  return EINA_TRUE;
}

EAPI E_Module_Api e_modapi = { E_MODULE_API_VERSION, "espresso" };

EAPI void *
e_modapi_init(E_Module *m)
{
   char buf[PATH_MAX];

#if HAVE_LOCALE_H
   setlocale(LC_ALL, "");
#endif
   snprintf(buf, sizeof(buf), "%s/locale", m->dir);
   bindtextdomain(PACKAGE, buf);
   bind_textdomain_codeset(PACKAGE, "UTF-8");


   conf_edd = E_CONFIG_DD_NEW("Espresso_Config", Config);
#undef T
#undef D
#define T Config
#define D conf_edd
   E_CONFIG_VAL(D, T, timeout, INT);

   espresso_config = e_config_domain_load("module.espresso", conf_edd);
   if (!espresso_config)
     {
        espresso_config = E_NEW(Config, 1);
        espresso_config->timeout = DEFAULT_TIMEOUT;
        espresso_config->cups = NULL;
     }
   espresso_config->mod_dir = eina_stringshare_add(m->dir);

   e_gadcon_provider_register(&_gc_class);
   espresso_timer = ecore_timer_add(CONFIG_UPDATE_TIMEOUT, _espresso_cb_config_update, NULL);
   return m;
}

EAPI int
e_modapi_shutdown(E_Module *m __UNUSED__)
{
   e_gadcon_provider_unregister(&_gc_class);
   Instance *inst = NULL;

   if (espresso_config->menu)
     {
        e_menu_post_deactivate_callback_set(espresso_config->menu, NULL, NULL);
        e_object_del(E_OBJECT(espresso_config->menu));
        espresso_config->menu = NULL;
     }
   if (espresso_config->cups)
   {
     eina_list_free(espresso_config->cups);
     espresso_config->cups = NULL;
   }
   if (config_timer)
   {
    ecore_timer_del(config_timer);
    config_timer = NULL;
   }
   if (espresso_config->mod_dir) eina_stringshare_del(espresso_config->mod_dir);
   E_FREE(espresso_config);
   E_CONFIG_DD_FREE(conf_edd);
   return 1;
}

EAPI int
e_modapi_save(E_Module *m __UNUSED__)
{
   e_config_domain_save("module.espresso", conf_edd, espresso_config);
   return 1;
}