2010-05-10 16:45:49 -07:00
|
|
|
#include "e.h"
|
|
|
|
#include "evry_api.h"
|
2010-04-07 08:03:27 -07:00
|
|
|
// TODO - show error when input not parseable
|
2009-07-04 04:10:28 -07:00
|
|
|
|
2011-02-20 23:30:55 -08:00
|
|
|
typedef struct _Plugin Plugin;
|
|
|
|
|
|
|
|
struct _Plugin
|
|
|
|
{
|
2011-11-29 19:32:54 -08:00
|
|
|
Evry_Plugin base;
|
2011-02-20 23:30:55 -08:00
|
|
|
};
|
|
|
|
|
2011-11-29 19:32:54 -08:00
|
|
|
static Eina_Bool _cb_data(void *data, int type, void *event);
|
|
|
|
static Eina_Bool _cb_error(void *data, int type, void *event);
|
|
|
|
static Eina_Bool _cb_del(void *data, int type, void *event);
|
2009-07-03 06:37:54 -07:00
|
|
|
|
2011-08-12 04:41:15 -07:00
|
|
|
static const Evry_API *evry = NULL;
|
2010-05-10 16:45:49 -07:00
|
|
|
static Evry_Module *evry_module = NULL;
|
2010-05-16 14:35:02 -07:00
|
|
|
static Evry_Plugin *_plug;
|
|
|
|
static Ecore_Event_Handler *action_handler = NULL;
|
2010-05-10 16:45:49 -07:00
|
|
|
|
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-08-10 04:36:37 -07:00
|
|
|
static int error = 0;
|
2010-06-04 12:40:14 -07:00
|
|
|
static Eina_Bool active = EINA_FALSE;
|
2010-04-30 05:55:01 -07:00
|
|
|
static char _module_icon[] = "accessories-calculator";
|
2011-01-23 14:34:28 -08:00
|
|
|
static Evry_Item *cur_item = NULL;
|
2009-07-03 06:37:54 -07:00
|
|
|
|
2009-08-21 15:42:06 -07:00
|
|
|
static Evry_Plugin *
|
2015-03-18 15:09:09 -07:00
|
|
|
_begin(Evry_Plugin *plugin, const Evry_Item *item EINA_UNUSED)
|
2009-07-03 06:37:54 -07:00
|
|
|
{
|
2011-02-20 23:30:55 -08:00
|
|
|
Plugin *p;
|
2011-08-12 10:18:25 -07:00
|
|
|
|
2010-06-04 12:40:14 -07:00
|
|
|
if (active)
|
|
|
|
return NULL;
|
|
|
|
|
2011-02-20 23:30:55 -08:00
|
|
|
EVRY_PLUGIN_INSTANCE(p, plugin)
|
|
|
|
|
2010-06-04 12:40:14 -07:00
|
|
|
active = EINA_TRUE;
|
2011-08-12 10:18:25 -07:00
|
|
|
|
2011-02-20 23:30:55 -08:00
|
|
|
return EVRY_PLUGIN(p);
|
2009-08-19 05:07:37 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2011-02-20 23:30:55 -08:00
|
|
|
_run_bc(Plugin *p)
|
2009-08-19 05:07:37 -07:00
|
|
|
{
|
|
|
|
handlers = eina_list_append
|
2011-11-29 19:32:54 -08:00
|
|
|
(handlers, ecore_event_handler_add
|
|
|
|
(ECORE_EXE_EVENT_DATA, _cb_data, p));
|
2009-08-19 05:07:37 -07:00
|
|
|
handlers = eina_list_append
|
2011-11-29 19:32:54 -08:00
|
|
|
(handlers, ecore_event_handler_add
|
|
|
|
(ECORE_EXE_EVENT_ERROR, _cb_error, p));
|
2009-08-19 05:07:37 -07:00
|
|
|
handlers = eina_list_append
|
2011-11-29 19:32:54 -08:00
|
|
|
(handlers, ecore_event_handler_add
|
|
|
|
(ECORE_EXE_EVENT_DEL, _cb_del, p));
|
2009-08-19 05:07:37 -07:00
|
|
|
|
|
|
|
exe = ecore_exe_pipe_run("bc -l",
|
2011-11-29 19:32:54 -08:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2011-02-20 23:30:55 -08:00
|
|
|
_finish(Evry_Plugin *plugin)
|
2009-07-03 06:37:54 -07:00
|
|
|
{
|
2011-08-12 10:18:25 -07:00
|
|
|
GET_PLUGIN(p, plugin);
|
2009-08-19 05:07:37 -07:00
|
|
|
Ecore_Event_Handler *h;
|
2009-07-24 22:53:01 -07:00
|
|
|
Evry_Item *it;
|
2011-02-20 23:30:55 -08:00
|
|
|
int items = 0;
|
2009-08-14 07:44:18 -07:00
|
|
|
|
2011-11-29 19:32:54 -08:00
|
|
|
EINA_LIST_FREE (p->base.items, it)
|
2009-08-14 02:35:51 -07:00
|
|
|
{
|
2011-11-29 19:32:54 -08:00
|
|
|
if ((items++ > 1) && (items < 10))
|
|
|
|
history = eina_list_prepend(history, eina_stringshare_add(it->label));
|
2009-08-14 02:35:51 -07:00
|
|
|
|
2012-12-14 06:25:48 -08:00
|
|
|
if (it == cur_item)
|
|
|
|
cur_item = NULL;
|
|
|
|
|
2011-11-29 19:32:54 -08:00
|
|
|
EVRY_ITEM_FREE(it);
|
2009-08-14 02:35:51 -07:00
|
|
|
}
|
2009-07-03 06:37:54 -07:00
|
|
|
|
2012-12-14 06:25:48 -08:00
|
|
|
if (cur_item)
|
|
|
|
{
|
|
|
|
EVRY_ITEM_FREE(cur_item);
|
|
|
|
cur_item = NULL;
|
|
|
|
}
|
|
|
|
|
2011-11-29 19:32:54 -08:00
|
|
|
EINA_LIST_FREE (handlers, h)
|
2009-08-19 05:07:37 -07:00
|
|
|
ecore_event_handler_del(h);
|
2009-07-04 04:10:28 -07:00
|
|
|
|
2009-08-19 05:07:37 -07:00
|
|
|
if (exe)
|
|
|
|
{
|
2014-12-06 09:04:58 -08:00
|
|
|
ecore_exe_terminate(exe);
|
2011-11-29 19:32:54 -08:00
|
|
|
ecore_exe_free(exe);
|
|
|
|
exe = NULL;
|
2009-08-19 05:07:37 -07:00
|
|
|
}
|
2010-06-04 12:40:14 -07:00
|
|
|
active = EINA_FALSE;
|
2011-02-20 23:30:55 -08:00
|
|
|
|
|
|
|
E_FREE(p);
|
2009-07-03 06:37:54 -07:00
|
|
|
}
|
|
|
|
|
2010-06-24 09:19:12 -07:00
|
|
|
static Eina_Bool
|
2015-03-18 15:09:09 -07:00
|
|
|
_cb_action_performed(EINA_UNUSED void *data, EINA_UNUSED int type, void *event)
|
2010-05-16 14:35:02 -07:00
|
|
|
{
|
|
|
|
Eina_List *l;
|
|
|
|
Evry_Item *it, *it2, *it_old;
|
|
|
|
Evry_Event_Action_Performed *ev = event;
|
|
|
|
Evry_Plugin *p = _plug;
|
|
|
|
|
|
|
|
if (!ev->it1 || !(ev->it1->plugin == p))
|
2010-06-24 09:19:12 -07:00
|
|
|
return ECORE_CALLBACK_PASS_ON;
|
2010-05-16 14:35:02 -07:00
|
|
|
|
2010-05-16 15:04:34 -07:00
|
|
|
if (!p->items)
|
2010-06-24 09:19:12 -07:00
|
|
|
return ECORE_CALLBACK_PASS_ON;
|
2010-05-29 10:18:17 -07:00
|
|
|
|
2010-05-16 14:35:02 -07:00
|
|
|
/* remove duplicates */
|
|
|
|
if (p->items->next)
|
|
|
|
{
|
2011-11-29 19:32:54 -08:00
|
|
|
it = p->items->data;
|
|
|
|
|
|
|
|
EINA_LIST_FOREACH (p->items->next, l, it2)
|
|
|
|
{
|
|
|
|
if (!strcmp(it->label, it2->label))
|
|
|
|
{
|
|
|
|
p->items = eina_list_promote_list(p->items, l);
|
|
|
|
evry->item_changed(it, 0, 1);
|
|
|
|
EVRY_PLUGIN_UPDATE(p, EVRY_UPDATE_ADD);
|
|
|
|
return ECORE_CALLBACK_PASS_ON;
|
|
|
|
}
|
|
|
|
}
|
2010-05-16 14:35:02 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
it_old = p->items->data;
|
|
|
|
it_old->selected = EINA_FALSE;
|
|
|
|
|
|
|
|
it2 = EVRY_ITEM_NEW(Evry_Item, p, it_old->label, NULL, NULL);
|
|
|
|
it2->context = eina_stringshare_ref(p->name);
|
|
|
|
p->items = eina_list_prepend(p->items, it2);
|
2010-05-21 23:35:46 -07:00
|
|
|
evry->item_changed(it2, 0, 1);
|
2010-05-16 14:35:02 -07:00
|
|
|
EVRY_PLUGIN_UPDATE(p, EVRY_UPDATE_ADD);
|
|
|
|
|
2010-06-24 09:19:12 -07:00
|
|
|
return ECORE_CALLBACK_PASS_ON;
|
2010-05-16 14:35:02 -07:00
|
|
|
}
|
2009-07-03 06:37:54 -07:00
|
|
|
|
|
|
|
static int
|
2011-02-20 23:30:55 -08:00
|
|
|
_fetch(Evry_Plugin *plugin, const char *input)
|
2009-07-03 06:37:54 -07:00
|
|
|
{
|
2011-02-20 23:30:55 -08:00
|
|
|
GET_PLUGIN(p, plugin);
|
2011-08-12 10:18:25 -07:00
|
|
|
|
2009-07-15 13:39:33 -07:00
|
|
|
char buf[1024];
|
2012-12-14 06:25:48 -08:00
|
|
|
Evry_Item *it;
|
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;
|
|
|
|
|
2012-12-14 06:25:48 -08:00
|
|
|
if (!cur_item)
|
|
|
|
{
|
|
|
|
it = EVRY_ITEM_NEW(Evry_Item, p, "0", NULL, NULL);
|
|
|
|
it->context = eina_stringshare_ref(p->base.name);
|
|
|
|
cur_item = it;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (history)
|
|
|
|
{
|
|
|
|
const char *result;
|
|
|
|
|
|
|
|
EINA_LIST_FREE (history, result)
|
|
|
|
{
|
|
|
|
it = EVRY_ITEM_NEW(Evry_Item, p, result, NULL, NULL);
|
|
|
|
it->context = eina_stringshare_ref(p->base.name);
|
|
|
|
p->base.items = eina_list_prepend(p->base.items, it);
|
|
|
|
eina_stringshare_del(result);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
{
|
2011-11-29 19:32:54 -08:00
|
|
|
ecore_exe_send(exe, buf, strlen(buf));
|
|
|
|
error = 0;
|
2009-07-15 13:39:33 -07:00
|
|
|
}
|
2009-08-12 12:00:25 -07:00
|
|
|
|
2011-08-21 08:41:37 -07:00
|
|
|
return EVRY_PLUGIN_HAS_ITEMS(p);
|
2009-07-03 06:37:54 -07:00
|
|
|
}
|
|
|
|
|
2010-06-24 09:19:12 -07:00
|
|
|
static Eina_Bool
|
2015-03-18 15:09:09 -07:00
|
|
|
_cb_data(void *data, int type EINA_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-27 20:22:40 -07:00
|
|
|
|
2010-06-24 09:19:12 -07:00
|
|
|
if (ev->exe != exe) return ECORE_CALLBACK_PASS_ON;
|
2009-08-12 12:00:25 -07:00
|
|
|
|
2009-08-14 02:35:51 -07:00
|
|
|
if (ev->lines)
|
2010-04-27 20:22:40 -07:00
|
|
|
{
|
2011-11-29 19:32:54 -08:00
|
|
|
it = cur_item;
|
|
|
|
eina_stringshare_del(it->label);
|
|
|
|
it->label = eina_stringshare_add(ev->lines->line);
|
|
|
|
|
|
|
|
if (!(it = eina_list_data_get(p->items)) || (it != cur_item))
|
|
|
|
{
|
|
|
|
p->items = eina_list_prepend(p->items, cur_item);
|
|
|
|
EVRY_PLUGIN_UPDATE(p, EVRY_UPDATE_ADD);
|
|
|
|
}
|
|
|
|
else if (it)
|
|
|
|
evry->item_changed(it, 0, 0);
|
2009-07-03 06:37:54 -07:00
|
|
|
}
|
|
|
|
|
2010-06-24 09:19:12 -07:00
|
|
|
return ECORE_CALLBACK_PASS_ON;
|
2009-07-03 06:37:54 -07:00
|
|
|
}
|
2009-07-15 13:39:33 -07:00
|
|
|
|
2010-06-24 09:19:12 -07:00
|
|
|
static Eina_Bool
|
2015-03-18 15:09:09 -07:00
|
|
|
_cb_error(void *data, int type EINA_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;
|
2011-01-23 14:34:28 -08:00
|
|
|
Evry_Plugin *p = data;
|
2011-08-12 10:18:25 -07:00
|
|
|
|
2009-07-25 16:53:05 -07:00
|
|
|
if (ev->exe != exe)
|
2010-06-24 09:19:12 -07:00
|
|
|
return ECORE_CALLBACK_PASS_ON;
|
2009-07-25 16:53:05 -07:00
|
|
|
|
2011-01-23 14:34:28 -08:00
|
|
|
p->items = eina_list_remove(p->items, cur_item);
|
|
|
|
EVRY_PLUGIN_UPDATE(p, EVRY_UPDATE_ADD);
|
2009-07-15 13:39:33 -07:00
|
|
|
error = 1;
|
|
|
|
|
2010-06-24 09:19:12 -07:00
|
|
|
return ECORE_CALLBACK_PASS_ON;
|
2009-07-15 13:39:33 -07:00
|
|
|
}
|
2009-07-25 16:53:05 -07:00
|
|
|
|
2010-06-24 09:19:12 -07:00
|
|
|
static Eina_Bool
|
2015-03-18 15:09:09 -07:00
|
|
|
_cb_del(void *data EINA_UNUSED, int type EINA_UNUSED, void *event)
|
2009-07-25 16:53:05 -07:00
|
|
|
{
|
|
|
|
Ecore_Exe_Event_Del *e = event;
|
|
|
|
|
|
|
|
if (e->exe != exe)
|
2010-06-24 09:19:12 -07:00
|
|
|
return ECORE_CALLBACK_PASS_ON;
|
2009-07-25 16:53:05 -07:00
|
|
|
|
|
|
|
exe = NULL;
|
2010-06-24 09:19:12 -07:00
|
|
|
return ECORE_CALLBACK_PASS_ON;
|
2009-07-25 16:53:05 -07:00
|
|
|
}
|
2009-07-29 13:43:09 -07:00
|
|
|
|
2010-05-10 16:45:49 -07:00
|
|
|
static int
|
2011-08-12 04:41:15 -07:00
|
|
|
_plugins_init(const Evry_API *_api)
|
2010-05-16 14:35:02 -07:00
|
|
|
{
|
2011-08-12 04:41:15 -07:00
|
|
|
evry = _api;
|
|
|
|
|
2010-05-10 16:45:49 -07:00
|
|
|
if (!evry->api_version_check(EVRY_API_VERSION))
|
2010-04-08 04:55:15 -07:00
|
|
|
return EINA_FALSE;
|
|
|
|
|
2010-05-23 00:35:48 -07:00
|
|
|
action_handler = evry->event_handler_add(EVRY_EVENT_ACTION_PERFORMED,
|
2011-11-29 19:32:54 -08:00
|
|
|
_cb_action_performed, NULL);
|
2010-05-16 14:35:02 -07:00
|
|
|
|
2013-01-05 14:53:46 -08:00
|
|
|
_plug = EVRY_PLUGIN_BASE(N_("Calculator"), _module_icon, EVRY_TYPE_TEXT,
|
2011-11-29 19:32:54 -08:00
|
|
|
_begin, _finish, _fetch);
|
2010-04-15 14:42:30 -07:00
|
|
|
|
2011-11-29 19:32:54 -08:00
|
|
|
_plug->history = EINA_FALSE;
|
2010-05-16 14:35:02 -07:00
|
|
|
_plug->async_fetch = EINA_TRUE;
|
2010-04-27 20:22:40 -07:00
|
|
|
|
2010-05-16 14:35:02 -07:00
|
|
|
if (evry->plugin_register(_plug, EVRY_PLUGIN_SUBJECT, 0))
|
2010-05-06 06:09:14 -07:00
|
|
|
{
|
2011-11-29 19:32:54 -08:00
|
|
|
Plugin_Config *pc = _plug->config;
|
|
|
|
pc->view_mode = VIEW_MODE_LIST;
|
|
|
|
pc->trigger = eina_stringshare_add("=");
|
|
|
|
pc->trigger_only = EINA_TRUE;
|
|
|
|
pc->aggregate = EINA_FALSE;
|
|
|
|
/* pc->top_level = EINA_FALSE; */
|
|
|
|
/* pc->min_query = 3; */
|
2010-05-06 06:09:14 -07:00
|
|
|
}
|
2009-08-10 04:36:37 -07:00
|
|
|
|
|
|
|
return EINA_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2010-04-24 16:07:13 -07:00
|
|
|
_plugins_shutdown(void)
|
2009-08-10 04:36:37 -07:00
|
|
|
{
|
2010-05-16 14:35:02 -07:00
|
|
|
ecore_event_handler_del(action_handler);
|
|
|
|
action_handler = NULL;
|
|
|
|
|
|
|
|
EVRY_PLUGIN_FREE(_plug);
|
2010-05-10 16:45:49 -07:00
|
|
|
}
|
2009-08-10 04:36:37 -07:00
|
|
|
|
2010-04-10 13:05:57 -07:00
|
|
|
/***************************************************************************/
|
|
|
|
|
2011-04-08 15:16:59 -07:00
|
|
|
Eina_Bool
|
2015-03-18 15:09:09 -07:00
|
|
|
evry_plug_calc_init(E_Module *m EINA_UNUSED)
|
2011-04-08 15:16:59 -07:00
|
|
|
{
|
2011-08-12 04:41:15 -07:00
|
|
|
EVRY_MODULE_NEW(evry_module, evry, _plugins_init, _plugins_shutdown);
|
2011-04-08 15:16:59 -07:00
|
|
|
|
|
|
|
return EINA_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
evry_plug_calc_shutdown(void)
|
|
|
|
{
|
|
|
|
EVRY_MODULE_FREE(evry_module);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
evry_plug_calc_save(void){}
|
2011-11-29 19:32:54 -08:00
|
|
|
|