aboutsummaryrefslogtreecommitdiffstats
path: root/src/bin/e_msg.c
blob: e968dc3e56789ff55bc363f2db4508b5967eb06e (plain) (blame)
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
/*
 * vim:ts=8:sw=3:sts=8:noexpandtab:cino=>5n-3f0^-2{2
 */
#include "e.h"

typedef struct _E_Msg_Event E_Msg_Event;

struct _E_Msg_Handler
{
   void (*func) (void *data, char *name, char *info, int val, E_Object *obj);
   void *data;
   unsigned char delete_me : 1;
};

struct _E_Msg_Event
{
   char *name;
   char *info;
   int   val;
   E_Object *obj;
};

/* local subsystem functions */
static int _e_msg_event_cb(void *data, int ev_type, void *ev);
static void _e_msg_event_free(void *data, void *ev);

/* local subsystem globals */
static Evas_List *handlers = NULL;
static Evas_List *del_handlers = NULL;
static int processing_handlers = 0;
static int E_EVENT_MSG = 0;
static Ecore_Event_Handler *hand = NULL;

/* externally accessible functions */
EAPI int
e_msg_init(void)
{
   E_EVENT_MSG = ecore_event_type_new();
   hand = ecore_event_handler_add(E_EVENT_MSG, _e_msg_event_cb, NULL);
   return 1;
}

EAPI int
e_msg_shutdown(void)
{
   while (handlers) e_msg_handler_del(handlers->data);
   E_EVENT_MSG = 0;
   if (hand) ecore_event_handler_del(hand);
   hand = NULL;
   return 1;
}

EAPI void
e_msg_send(char *name, char *info, int val, E_Object *obj)
{
   E_Msg_Event *ev;
   
   ev = calloc(1, sizeof(E_Msg_Event));
   /* FIXME: probably better todup the strings but merge with a single
    * malloc for the event struct */
   if (name) ev->name = evas_stringshare_add(name);
   if (info) ev->info = evas_stringshare_add(info);
   ev->val = val;
   ev->obj = obj;
   if (ev->obj) e_object_ref(ev->obj);
   ecore_event_add(E_EVENT_MSG, ev, _e_msg_event_free, NULL);
}

EAPI E_Msg_Handler *
e_msg_handler_add(void (*func) (void *data, char *name, char *info, int val, E_Object *obj), void *data)
{
   E_Msg_Handler *emsgh;
   
   emsgh = calloc(1, sizeof(E_Msg_Handler));
   if (!emsgh) return NULL;
   emsgh->func = func;
   emsgh->data = data;
   handlers = evas_list_append(handlers, emsgh);
   return emsgh;
}

EAPI void
e_msg_handler_del(E_Msg_Handler *emsgh)
{
   if (processing_handlers > 0)
     {
	emsgh->delete_me = 1;
	del_handlers = evas_list_append(del_handlers, emsgh);
     }
   else
     {
	handlers = evas_list_remove(handlers, emsgh);
	free(emsgh);
     }
}

/* local subsystem functions */

static int
_e_msg_event_cb(void *data, int ev_type, void *ev)
{
   E_Msg_Event *e;
   Evas_List *l;

   processing_handlers++;
   e = ev;
   for (l = handlers; l; l = l->next)
     {
	E_Msg_Handler *emsgh;
	
	emsgh = l->data;
	if (!emsgh->delete_me)
	  emsgh->func(emsgh->data, e->name, e->info, e->val, e->obj);
     }
   processing_handlers--;
   if ((processing_handlers == 0) && (del_handlers))
     {
	while (del_handlers)
	  {
	     e_msg_handler_del(del_handlers->data);
	     del_handlers = evas_list_remove_list(del_handlers, del_handlers);
	  }
     }
   return 1;
}

static void
_e_msg_event_free(void *data, void *ev)
{
   E_Msg_Event *e;

   e = ev;
   if (e->name) evas_stringshare_del(e->name);
   if (e->info) evas_stringshare_del(e->info);
   if (e->obj) e_object_unref(e->obj);
}