2010-04-10 13:05:57 -07:00
|
|
|
/*
|
|
|
|
* vim:ts=8:sw=3:sts=8:noexpandtab:cino=>5n-3f0^-2{2
|
|
|
|
*/
|
|
|
|
|
2009-08-13 08:10:54 -07:00
|
|
|
#include "Evry.h"
|
2010-04-10 13:05:57 -07:00
|
|
|
#include "e_mod_main.h"
|
2010-04-07 08:03:27 -07:00
|
|
|
// TODO - show error when input not parseable
|
2009-07-04 04:10:28 -07:00
|
|
|
|
2009-07-03 06:37:54 -07:00
|
|
|
static int _cb_data(void *data, int type, void *event);
|
2009-07-15 13:39:33 -07:00
|
|
|
static int _cb_error(void *data, int type, void *event);
|
2009-07-25 16:53:05 -07:00
|
|
|
static int _cb_del(void *data, int type, void *event);
|
2009-07-03 06:37:54 -07:00
|
|
|
|
2009-08-14 02:35:51 -07:00
|
|
|
static Evry_Plugin *p1;
|
2009-07-03 06:37:54 -07:00
|
|
|
static Ecore_Exe *exe = NULL;
|
|
|
|
static Eina_List *history = NULL;
|
2009-08-19 05:07:37 -07:00
|
|
|
static Eina_List *handlers = NULL;
|
2009-07-04 04:10:28 -07:00
|
|
|
|
2009-08-10 04:36:37 -07:00
|
|
|
static int error = 0;
|
2009-07-03 06:37:54 -07:00
|
|
|
|
|
|
|
|
2009-08-14 02:35:51 -07:00
|
|
|
|
2009-08-21 15:42:06 -07:00
|
|
|
static Evry_Plugin *
|
2009-08-14 02:35:51 -07:00
|
|
|
_begin(Evry_Plugin *p, const Evry_Item *item __UNUSED__)
|
2009-07-03 06:37:54 -07:00
|
|
|
{
|
2009-08-14 02:35:51 -07:00
|
|
|
Evry_Item *it;
|
2009-07-03 06:37:54 -07:00
|
|
|
|
2009-08-14 02:35:51 -07:00
|
|
|
if (history)
|
|
|
|
{
|
|
|
|
const char *result;
|
|
|
|
|
|
|
|
EINA_LIST_FREE(history, result)
|
|
|
|
{
|
2009-08-22 20:12:05 -07:00
|
|
|
it = evry_item_new(NULL, p, result, NULL);
|
2009-08-14 02:35:51 -07:00
|
|
|
p->items = eina_list_prepend(p->items, it);
|
|
|
|
eina_stringshare_del(result);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-08-22 20:12:05 -07:00
|
|
|
it = evry_item_new(NULL, p, "0", NULL);
|
2009-08-14 02:35:51 -07:00
|
|
|
p->items = eina_list_prepend(p->items, it);
|
|
|
|
|
2009-08-21 15:42:06 -07:00
|
|
|
return p;
|
2009-08-19 05:07:37 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
_run_bc(Evry_Plugin *p)
|
|
|
|
{
|
|
|
|
handlers = eina_list_append
|
|
|
|
(handlers, ecore_event_handler_add
|
|
|
|
(ECORE_EXE_EVENT_DATA, _cb_data, p));
|
|
|
|
handlers = eina_list_append
|
|
|
|
(handlers, ecore_event_handler_add
|
|
|
|
(ECORE_EXE_EVENT_ERROR, _cb_error, p));
|
|
|
|
handlers = eina_list_append
|
|
|
|
(handlers, ecore_event_handler_add
|
|
|
|
(ECORE_EXE_EVENT_DEL, _cb_del, p));
|
|
|
|
|
|
|
|
exe = ecore_exe_pipe_run("bc -l",
|
|
|
|
ECORE_EXE_PIPE_READ |
|
|
|
|
ECORE_EXE_PIPE_READ_LINE_BUFFERED |
|
|
|
|
ECORE_EXE_PIPE_WRITE |
|
|
|
|
ECORE_EXE_PIPE_ERROR |
|
|
|
|
ECORE_EXE_PIPE_ERROR_LINE_BUFFERED,
|
|
|
|
NULL);
|
2009-07-24 22:53:01 -07:00
|
|
|
return !!exe;
|
2009-07-03 06:37:54 -07:00
|
|
|
}
|
|
|
|
|
2009-08-19 05:07:37 -07:00
|
|
|
|
2009-07-03 06:37:54 -07:00
|
|
|
static void
|
2009-07-07 09:30:11 -07:00
|
|
|
_cleanup(Evry_Plugin *p)
|
2009-07-03 06:37:54 -07:00
|
|
|
{
|
2009-08-19 05:07:37 -07:00
|
|
|
Ecore_Event_Handler *h;
|
2009-07-24 22:53:01 -07:00
|
|
|
Evry_Item *it;
|
2009-08-10 04:36:37 -07:00
|
|
|
int items = 10;
|
2009-07-04 04:10:28 -07:00
|
|
|
|
2009-08-14 07:44:18 -07:00
|
|
|
if (p->items)
|
|
|
|
{
|
|
|
|
evry_item_free(p->items->data);
|
|
|
|
p->items = eina_list_remove_list(p->items, p->items);
|
|
|
|
}
|
|
|
|
|
2009-07-03 06:37:54 -07:00
|
|
|
EINA_LIST_FREE(p->items, it)
|
2009-08-14 02:35:51 -07:00
|
|
|
{
|
|
|
|
if (items-- > 0)
|
|
|
|
history = eina_list_prepend(history, eina_stringshare_add(it->label));
|
|
|
|
|
|
|
|
evry_item_free(it);
|
|
|
|
}
|
2009-07-03 06:37:54 -07:00
|
|
|
|
2009-08-19 05:07:37 -07:00
|
|
|
EINA_LIST_FREE(handlers, h)
|
|
|
|
ecore_event_handler_del(h);
|
2009-07-04 04:10:28 -07:00
|
|
|
|
2009-08-19 05:07:37 -07:00
|
|
|
if (exe)
|
|
|
|
{
|
|
|
|
ecore_exe_quit(exe);
|
|
|
|
ecore_exe_free(exe);
|
|
|
|
exe = NULL;
|
|
|
|
}
|
2009-07-03 06:37:54 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2009-08-21 18:15:21 -07:00
|
|
|
_action(Evry_Plugin *p, const Evry_Item *it)
|
2009-07-03 06:37:54 -07:00
|
|
|
{
|
2009-08-14 02:35:51 -07:00
|
|
|
Eina_List *l;
|
2010-04-07 08:03:27 -07:00
|
|
|
Evry_Item *it2, *it_old;
|
2009-08-14 02:35:51 -07:00
|
|
|
|
|
|
|
/* remove duplicates */
|
|
|
|
if (p->items->next)
|
2009-07-03 06:37:54 -07:00
|
|
|
{
|
2009-08-14 02:35:51 -07:00
|
|
|
it = p->items->data;
|
2009-07-04 04:10:28 -07:00
|
|
|
|
2009-08-14 02:35:51 -07:00
|
|
|
EINA_LIST_FOREACH(p->items->next, l, it2)
|
|
|
|
{
|
|
|
|
if (!strcmp(it->label, it2->label))
|
|
|
|
break;
|
|
|
|
it2 = NULL;
|
|
|
|
}
|
2009-07-04 04:10:28 -07:00
|
|
|
|
2009-08-14 02:35:51 -07:00
|
|
|
if (it2)
|
2009-07-15 07:37:01 -07:00
|
|
|
{
|
2009-08-14 02:35:51 -07:00
|
|
|
p->items = eina_list_remove(p->items, it2);
|
|
|
|
evry_item_free(it2);
|
2009-07-03 06:37:54 -07:00
|
|
|
}
|
2009-08-14 02:35:51 -07:00
|
|
|
}
|
2009-07-04 04:10:28 -07:00
|
|
|
|
2010-04-07 08:03:27 -07:00
|
|
|
it_old = p->items->data;
|
|
|
|
it_old->selected = EINA_FALSE;
|
|
|
|
|
|
|
|
it2 = evry_item_new(NULL, p, it_old->label, NULL);
|
2009-08-14 02:35:51 -07:00
|
|
|
p->items = eina_list_prepend(p->items, it2);
|
2009-07-03 06:37:54 -07:00
|
|
|
|
2009-08-10 04:36:37 -07:00
|
|
|
evry_plugin_async_update(p, EVRY_ASYNC_UPDATE_ADD);
|
2009-07-15 07:37:01 -07:00
|
|
|
|
2009-07-03 06:37:54 -07:00
|
|
|
return EVRY_ACTION_FINISHED;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2009-07-07 09:30:11 -07:00
|
|
|
_fetch(Evry_Plugin *p, const char *input)
|
2009-07-03 06:37:54 -07:00
|
|
|
{
|
2009-07-15 13:39:33 -07:00
|
|
|
char buf[1024];
|
2009-08-12 12:00:25 -07:00
|
|
|
|
2009-08-18 17:27:41 -07:00
|
|
|
if (!input) return 0;
|
|
|
|
|
2009-08-19 05:07:37 -07:00
|
|
|
if (!exe && !_run_bc(p)) return 0;
|
|
|
|
|
2009-08-18 17:27:41 -07:00
|
|
|
if (!strncmp(input, "scale=", 6))
|
|
|
|
snprintf(buf, 1024, "%s\n", input);
|
2009-07-15 14:08:01 -07:00
|
|
|
else
|
2009-08-18 17:27:41 -07:00
|
|
|
snprintf(buf, 1024, "scale=3;%s\n", input);
|
2009-07-15 13:39:33 -07:00
|
|
|
|
|
|
|
ecore_exe_send(exe, buf, strlen(buf));
|
2009-07-04 04:10:28 -07:00
|
|
|
|
2009-07-15 13:39:33 -07:00
|
|
|
/* XXX after error we get no response for first input ?! - send a
|
|
|
|
second time...*/
|
|
|
|
if (error)
|
|
|
|
{
|
|
|
|
ecore_exe_send(exe, buf, strlen(buf));
|
|
|
|
error = 0;
|
|
|
|
}
|
2009-08-12 12:00:25 -07:00
|
|
|
|
2009-07-03 06:37:54 -07:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2010-04-07 08:03:27 -07:00
|
|
|
|
|
|
|
static void
|
|
|
|
_cb_free_item_changed(void *data, void *event)
|
|
|
|
{
|
|
|
|
Evry_Event_Item_Changed *ev = event;
|
|
|
|
|
|
|
|
evry_item_free(ev->item);
|
|
|
|
E_FREE(ev);
|
|
|
|
}
|
|
|
|
|
2009-07-03 06:37:54 -07:00
|
|
|
static int
|
2009-08-14 02:35:51 -07:00
|
|
|
_cb_data(void *data, int type __UNUSED__, void *event)
|
2009-07-03 06:37:54 -07:00
|
|
|
{
|
|
|
|
Ecore_Exe_Event_Data *ev = event;
|
2009-08-14 02:35:51 -07:00
|
|
|
Evry_Plugin *p = data;
|
|
|
|
Evry_Item *it;
|
2010-04-07 08:03:27 -07:00
|
|
|
|
2009-07-25 16:53:05 -07:00
|
|
|
if (ev->exe != exe) return 1;
|
2009-08-12 12:00:25 -07:00
|
|
|
|
2009-08-14 02:35:51 -07:00
|
|
|
if (ev->lines)
|
2010-04-07 08:03:27 -07:00
|
|
|
{
|
2009-08-14 02:35:51 -07:00
|
|
|
it = p->items->data;
|
2010-04-07 08:03:27 -07:00
|
|
|
eina_stringshare_del(it->label);
|
|
|
|
it->label = eina_stringshare_add(ev->lines->line);
|
|
|
|
|
|
|
|
if (it)
|
|
|
|
{
|
|
|
|
Evry_Event_Item_Changed *ev = E_NEW(Evry_Event_Item_Changed, 1);
|
|
|
|
ev->item = it;
|
|
|
|
evry_item_ref(it);
|
|
|
|
ecore_event_add(EVRY_EVENT_ITEM_CHANGED, ev, _cb_free_item_changed, NULL);
|
|
|
|
}
|
2009-07-03 06:37:54 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
2009-07-15 13:39:33 -07:00
|
|
|
|
|
|
|
static int
|
2009-07-25 16:53:05 -07:00
|
|
|
_cb_error(void *data __UNUSED__, int type __UNUSED__, void *event)
|
2009-07-15 13:39:33 -07:00
|
|
|
{
|
2009-07-25 16:53:05 -07:00
|
|
|
Ecore_Exe_Event_Data *ev = event;
|
|
|
|
|
|
|
|
if (ev->exe != exe)
|
|
|
|
return 1;
|
|
|
|
|
2009-07-15 13:39:33 -07:00
|
|
|
error = 1;
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
2009-07-25 16:53:05 -07:00
|
|
|
|
|
|
|
static int
|
|
|
|
_cb_del(void *data __UNUSED__, int type __UNUSED__, void *event)
|
|
|
|
{
|
|
|
|
Ecore_Exe_Event_Del *e = event;
|
|
|
|
|
|
|
|
if (e->exe != exe)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
exe = NULL;
|
|
|
|
return 1;
|
|
|
|
}
|
2009-07-29 13:43:09 -07:00
|
|
|
|
2009-08-10 04:36:37 -07:00
|
|
|
static Eina_Bool
|
2010-04-10 13:05:57 -07:00
|
|
|
module_init(void)
|
2009-08-10 04:36:37 -07:00
|
|
|
{
|
2010-04-08 04:55:15 -07:00
|
|
|
if (!evry_api_version_check(EVRY_API_VERSION))
|
|
|
|
return EINA_FALSE;
|
|
|
|
|
2010-04-15 14:42:30 -07:00
|
|
|
p1 = EVRY_PLUGIN_NEW(NULL, "Calculator", type_subject, NULL, "TEXT",
|
|
|
|
_begin, _cleanup, _fetch, NULL, NULL);
|
|
|
|
|
|
|
|
p1->view_mode = VIEW_MODE_LIST;
|
|
|
|
p1->aggregate = EINA_FALSE;
|
|
|
|
p1->history = EINA_FALSE;
|
|
|
|
p1->async_fetch = EINA_TRUE;
|
|
|
|
p1->icon = "accessories-calculator";
|
|
|
|
p1->trigger = "=";
|
|
|
|
p1->action = &_action;
|
2010-04-01 21:19:38 -07:00
|
|
|
|
2009-08-16 09:26:49 -07:00
|
|
|
evry_plugin_register(p1, 0);
|
2009-08-10 04:36:37 -07:00
|
|
|
|
|
|
|
return EINA_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2010-04-10 13:05:57 -07:00
|
|
|
module_shutdown(void)
|
2009-08-10 04:36:37 -07:00
|
|
|
{
|
2009-08-22 20:12:05 -07:00
|
|
|
EVRY_PLUGIN_FREE(p1);
|
2009-08-10 04:36:37 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-04-10 13:05:57 -07:00
|
|
|
/***************************************************************************/
|
|
|
|
/**/
|
|
|
|
/* actual module specifics */
|
|
|
|
|
|
|
|
static E_Module *module = NULL;
|
|
|
|
static Eina_Bool active = EINA_FALSE;
|
|
|
|
|
|
|
|
/***************************************************************************/
|
|
|
|
/**/
|
|
|
|
/* module setup */
|
|
|
|
EAPI E_Module_Api e_modapi =
|
|
|
|
{
|
|
|
|
E_MODULE_API_VERSION,
|
|
|
|
"everything-calc"
|
|
|
|
};
|
|
|
|
|
|
|
|
EAPI void *
|
|
|
|
e_modapi_init(E_Module *m)
|
|
|
|
{
|
|
|
|
module = m;
|
|
|
|
|
|
|
|
if (e_datastore_get("everything_loaded"))
|
|
|
|
active = module_init();
|
|
|
|
|
|
|
|
e_module_delayed_set(m, 1);
|
|
|
|
|
|
|
|
return m;
|
|
|
|
}
|
|
|
|
|
|
|
|
EAPI int
|
|
|
|
e_modapi_shutdown(E_Module *m)
|
|
|
|
{
|
2010-04-10 14:38:36 -07:00
|
|
|
char *result;
|
|
|
|
|
2010-04-10 13:05:57 -07:00
|
|
|
if (active && e_datastore_get("everything_loaded"))
|
|
|
|
module_shutdown();
|
|
|
|
|
2010-04-10 14:38:36 -07:00
|
|
|
EINA_LIST_FREE(history, result)
|
|
|
|
eina_stringshare_del(result);
|
|
|
|
|
2010-04-10 13:05:57 -07:00
|
|
|
module = NULL;
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
EAPI int
|
|
|
|
e_modapi_save(E_Module *m)
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**/
|
|
|
|
/***************************************************************************/
|
|
|
|
|