2009-07-04 04:10:28 -07:00
|
|
|
#include "e.h"
|
|
|
|
#include "e_mod_main.h"
|
|
|
|
|
2009-07-03 06:37:54 -07:00
|
|
|
/* TODO
|
|
|
|
* - dc support?
|
|
|
|
*/
|
|
|
|
|
2009-07-07 09:30:11 -07:00
|
|
|
static int _begin(Evry_Plugin *p, Evry_Item *it);
|
|
|
|
static int _fetch(Evry_Plugin *p, const char *input);
|
|
|
|
static int _action(Evry_Plugin *p, Evry_Item *item, const char *input);
|
|
|
|
static void _cleanup(Evry_Plugin *p);
|
|
|
|
static void _item_add(Evry_Plugin *p, char *output, int prio);
|
2009-07-03 06:37:54 -07:00
|
|
|
static int _cb_sort(const void *data1, const void *data2);
|
2009-07-07 09:30:11 -07:00
|
|
|
static void _item_icon_get(Evry_Plugin *p, Evry_Item *it, Evas *e);
|
2009-07-03 06:37:54 -07:00
|
|
|
static int _cb_data(void *data, int type, void *event);
|
|
|
|
|
|
|
|
static Evry_Plugin *p;
|
|
|
|
static Ecore_Exe *exe = NULL;
|
|
|
|
static Eina_List *history = NULL;
|
|
|
|
static Ecore_Event_Handler *data_handler = NULL;
|
|
|
|
static Ecore_Event_Handler *error_handler = NULL;
|
|
|
|
|
|
|
|
|
|
|
|
EAPI int
|
|
|
|
evry_plug_calc_init(void)
|
|
|
|
{
|
|
|
|
p = E_NEW(Evry_Plugin, 1);
|
|
|
|
p->name = "Calculator";
|
|
|
|
p->type_in = "NONE";
|
|
|
|
p->type_out = "NONE";
|
2009-07-09 15:58:40 -07:00
|
|
|
p->trigger = "=";
|
2009-07-03 06:37:54 -07:00
|
|
|
p->need_query = 0;
|
|
|
|
p->async_query = 1;
|
|
|
|
p->begin = &_begin;
|
|
|
|
p->fetch = &_fetch;
|
|
|
|
p->action = &_action;
|
|
|
|
p->cleanup = &_cleanup;
|
|
|
|
p->icon_get = &_item_icon_get;
|
|
|
|
evry_plugin_register(p);
|
2009-07-04 04:10:28 -07:00
|
|
|
|
2009-07-03 06:37:54 -07:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
EAPI int
|
|
|
|
evry_plug_calc_shutdown(void)
|
|
|
|
{
|
|
|
|
Evry_Item *it;
|
2009-07-04 04:10:28 -07:00
|
|
|
|
2009-07-03 06:37:54 -07:00
|
|
|
EINA_LIST_FREE(p->items, it)
|
|
|
|
{
|
|
|
|
if (it->label) eina_stringshare_del(it->label);
|
|
|
|
free(it);
|
|
|
|
}
|
2009-07-04 04:10:28 -07:00
|
|
|
|
2009-07-03 06:37:54 -07:00
|
|
|
evry_plugin_unregister(p);
|
2009-07-04 04:10:28 -07:00
|
|
|
E_FREE(p);
|
2009-07-03 06:37:54 -07:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2009-07-07 09:30:11 -07:00
|
|
|
_begin(Evry_Plugin *p, Evry_Item *it)
|
2009-07-03 06:37:54 -07:00
|
|
|
{
|
|
|
|
|
|
|
|
data_handler = ecore_event_handler_add(ECORE_EXE_EVENT_DATA, _cb_data, p);
|
2009-07-04 04:10:28 -07:00
|
|
|
|
2009-07-03 06:37:54 -07:00
|
|
|
exe = ecore_exe_pipe_run("bc",
|
|
|
|
ECORE_EXE_PIPE_READ |
|
|
|
|
ECORE_EXE_PIPE_READ_LINE_BUFFERED |
|
|
|
|
ECORE_EXE_PIPE_WRITE,
|
|
|
|
NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2009-07-07 09:30:11 -07:00
|
|
|
_cleanup(Evry_Plugin *p)
|
2009-07-03 06:37:54 -07:00
|
|
|
{
|
2009-07-04 04:10:28 -07:00
|
|
|
Evry_Item *it, *it2;
|
2009-07-03 06:37:54 -07:00
|
|
|
int i = 0;
|
2009-07-04 04:10:28 -07:00
|
|
|
Eina_List *l, *ll;
|
|
|
|
|
2009-07-03 06:37:54 -07:00
|
|
|
EINA_LIST_FREE(p->items, it)
|
|
|
|
{
|
|
|
|
if (i < 10)
|
|
|
|
{
|
|
|
|
history = eina_list_append(history, it);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (it->label) eina_stringshare_del(it->label);
|
|
|
|
free(it);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ecore_event_handler_del(data_handler);
|
|
|
|
data_handler = NULL;
|
2009-07-04 04:10:28 -07:00
|
|
|
|
2009-07-03 06:37:54 -07:00
|
|
|
ecore_exe_quit(exe);
|
|
|
|
exe = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
_send_input(const char *input)
|
|
|
|
{
|
|
|
|
char buf[1024];
|
2009-07-09 15:58:40 -07:00
|
|
|
snprintf(buf, 1024, "%s\n", input + (strlen(p->trigger)));
|
2009-07-03 06:37:54 -07:00
|
|
|
|
|
|
|
return ecore_exe_send(exe, buf, strlen(buf));
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2009-07-07 09:30:11 -07:00
|
|
|
_action(Evry_Plugin *p, Evry_Item *it, const char *input)
|
2009-07-03 06:37:54 -07:00
|
|
|
{
|
|
|
|
if (!it)
|
|
|
|
{
|
|
|
|
if (p->items)
|
|
|
|
{
|
2009-07-04 04:10:28 -07:00
|
|
|
Eina_List *l;
|
|
|
|
Evry_Item *it2;
|
|
|
|
|
|
|
|
evry_plugin_async_update(p, EVRY_ASYNC_UPDATE_CLEAR);
|
|
|
|
|
|
|
|
/* remove duplicates */
|
|
|
|
if (p->items->next)
|
|
|
|
{
|
2009-07-09 15:58:40 -07:00
|
|
|
it = p->items->data;
|
|
|
|
|
2009-07-04 04:10:28 -07:00
|
|
|
EINA_LIST_FOREACH(p->items->next, l, it2)
|
|
|
|
{
|
|
|
|
if (!strcmp(it->label, it2->label))
|
|
|
|
break;
|
|
|
|
it2 = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (it2)
|
|
|
|
{
|
|
|
|
p->items = eina_list_remove(p->items, it2);
|
|
|
|
eina_stringshare_del(it2->label);
|
|
|
|
E_FREE(it2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
it = p->items->data;
|
|
|
|
|
2009-07-07 09:30:11 -07:00
|
|
|
_item_add(p, (char *) it->label, 1);
|
2009-07-03 06:37:54 -07:00
|
|
|
}
|
2009-07-04 04:10:28 -07:00
|
|
|
|
|
|
|
evry_plugin_async_update(p, EVRY_ASYNC_UPDATE_ADD);
|
|
|
|
|
2009-07-09 15:58:40 -07:00
|
|
|
/* return EVRY_ACTION_CONTINUE; */
|
2009-07-03 06:37:54 -07:00
|
|
|
}
|
2009-07-09 15:58:40 -07:00
|
|
|
/* else */
|
2009-07-03 06:37:54 -07:00
|
|
|
{
|
|
|
|
/* XXX on which windows must the selection be set? */
|
|
|
|
ecore_x_selection_primary_set(e_manager_current_get()->win,
|
|
|
|
it->label, strlen(it->label));
|
2009-07-03 09:34:33 -07:00
|
|
|
ecore_x_selection_clipboard_set(e_manager_current_get()->win,
|
|
|
|
it->label, strlen(it->label));
|
|
|
|
|
2009-07-09 15:58:40 -07:00
|
|
|
/* if (p->items->data == it)
|
|
|
|
* {
|
|
|
|
* Evry_Item *it2 = p->items->data;
|
|
|
|
* _item_add(p, (char *) it2->label, 1);
|
|
|
|
* } */
|
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
|
|
|
{
|
|
|
|
if (history)
|
|
|
|
{
|
|
|
|
p->items = history;
|
|
|
|
history = NULL;
|
|
|
|
}
|
2009-07-04 04:10:28 -07:00
|
|
|
|
2009-07-03 06:37:54 -07:00
|
|
|
_send_input(input);
|
2009-07-04 04:10:28 -07:00
|
|
|
|
2009-07-03 06:37:54 -07:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2009-07-07 09:30:11 -07:00
|
|
|
_item_icon_get(Evry_Plugin *p, Evry_Item *it, Evas *e)
|
2009-07-04 04:10:28 -07:00
|
|
|
{
|
2009-07-03 06:37:54 -07:00
|
|
|
it->o_icon = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2009-07-07 09:30:11 -07:00
|
|
|
_item_add(Evry_Plugin *p, char *output, int prio)
|
2009-07-03 06:37:54 -07:00
|
|
|
{
|
2009-07-04 04:10:28 -07:00
|
|
|
Evry_Item *it;
|
2009-07-03 06:37:54 -07:00
|
|
|
|
|
|
|
it = E_NEW(Evry_Item, 1);
|
|
|
|
|
|
|
|
it->priority = prio;
|
|
|
|
it->label = eina_stringshare_add(output);
|
2009-07-04 04:10:28 -07:00
|
|
|
|
2009-07-03 06:37:54 -07:00
|
|
|
p->items = eina_list_prepend(p->items, it);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
_cb_data(void *data, int type, void *event)
|
|
|
|
{
|
|
|
|
Ecore_Exe_Event_Data *ev = event;
|
|
|
|
Ecore_Exe_Event_Data_Line *l;
|
2009-07-04 04:10:28 -07:00
|
|
|
|
2009-07-03 06:37:54 -07:00
|
|
|
if (data != p) return 1;
|
|
|
|
|
2009-07-03 09:34:33 -07:00
|
|
|
evry_plugin_async_update(p, EVRY_ASYNC_UPDATE_CLEAR);
|
2009-07-03 06:37:54 -07:00
|
|
|
|
|
|
|
for (l = ev->lines; l && l->line; l++)
|
|
|
|
{
|
|
|
|
if (p->items)
|
|
|
|
{
|
|
|
|
Evry_Item *it = p->items->data;
|
|
|
|
if (it->label)
|
|
|
|
eina_stringshare_del(it->label);
|
|
|
|
it->label = eina_stringshare_add(l->line);
|
|
|
|
}
|
|
|
|
else
|
2009-07-07 09:30:11 -07:00
|
|
|
_item_add(p, l->line, 1);
|
2009-07-03 06:37:54 -07:00
|
|
|
}
|
|
|
|
|
2009-07-03 09:34:33 -07:00
|
|
|
evry_plugin_async_update(p, EVRY_ASYNC_UPDATE_ADD);
|
2009-07-04 04:10:28 -07:00
|
|
|
|
2009-07-03 06:37:54 -07:00
|
|
|
return 1;
|
|
|
|
}
|