summaryrefslogtreecommitdiff
path: root/legacy/ecore/src/lib/ecore_config/ecore_config_ipc_ecore.c
blob: 918c2be7079ebd1f1eb16d58d3f5c6be326479c2 (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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
/* by Azundris, with thanks to Corey Donohoe <atmos@atmos.org> */
#include "ecore_private.h"
#include "ecore_config_ipc.h"
#include "ecore_config_util.h"
#include "ecore_config_private.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <limits.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <ctype.h>

#include <Ecore.h>
#include <Ecore_Ipc.h>

#include "Ecore_Config.h"
#include "config.h"


/*****************************************************************************/

static int
_ecore_config_ipc_ecore_string_get(char **m, char **r)
{
   char               *q;
   int                 l = 0;

   if (!m || !*m)
      return ECORE_CONFIG_ERR_NODATA;
   if (!r)
      return ECORE_CONFIG_ERR_FAIL;
   q = *m;
   if (*q != 's')
      return ECORE_CONFIG_ERR_TYPEMISMATCH;
   q++;
   l = (*(q++)) << 8;
   l += *(q++);
   *r = q;
   q += l;
   *m = q;
   E(1, "IPC/eCore: got string-%d \"%s\"\n", l, *r);
   return ECORE_CONFIG_ERR_SUCC;
}

static char               *
_ecore_config_ipc_global_prop_list(Ecore_Config_Server * srv __UNUSED__, long serial __UNUSED__)
{
   Ecore_Config_DB_File  *db;
   char                 **keys;
   int                    key_count, x;
   estring               *s;
   int                    f;
   char                   buf[PATH_MAX], *p, *type, *data;

   db = NULL;
   s = estring_new(8192);
   f = 0;
   if ((p = getenv("HOME")))
     {
	snprintf(buf, sizeof(buf), "%s/.e/config.eet", p);
	if (!(db = _ecore_config_db_open_read(buf)))
	  {
	     strcpy(buf, PACKAGE_DATA_DIR"/system.eet");
	     if (!(db = _ecore_config_db_open_read(buf)))
	       return NULL;
	  }
     }
   if (!db) return NULL;
   key_count = 0;
   keys = _ecore_config_db_keys_get(db, &key_count);
   if (keys)
     {
	for (x = 0; x < key_count; x++)
	  {
	     type = _ecore_config_db_key_type_get(db, keys[x]);
	     if (!type) type = strdup("?");
	     if (!strcmp(type, "int"))
	       estring_appendf(s, "%s%s: integer", f ? "\n" : "", keys[x]);
	     else if (!strcmp(type, "float"))
	       estring_appendf(s, "%s%s: float", f ? "\n" : "", keys[x]);
	     else if (!strcmp(type, "str"))
	       {
		  data = _ecore_config_db_key_str_get(db, keys[x]);
		  if (data)
		    {
		       if (ecore_config_type_guess(keys[x], data) == PT_RGB)
			 estring_appendf(s, "%s%s: colour", f ? "\n" : "", keys[x]);
		       else
			 estring_appendf(s, "%s%s: string", f ? "\n" : "", keys[x]);
		       free(data);
		    }
		  else
		    {
		       estring_appendf(s, "%s%s: string", f ? "\n" : "", keys[x]);
		    }
	       }
	     else
	       {
		  estring_appendf(s, "%s%s: unknown", f ? "\n" : "", keys[x]);
		  continue;
	       }
	     if (type) free(type);
	     f = 1;
	  }
     }
   _ecore_config_db_close(db);
   if (keys)
     {
	for (x = 0; x < key_count; x++)
	  {
	     free(keys[x]);
	  }
	free(keys);
     }
   
   return estring_disown(s);
}

/*****************************************************************************/

static int
_ecore_config_ipc_ecore_send(Ecore_Ipc_Event_Client_Data * e, int code,
			     char *reply)
{
   static int          our_ref = 0;
   int                 len = reply ? strlen(reply) + 1 : 0;

   our_ref++;
   E(1, "IPC/eCore: replying [0,0] %d IRT %d => %d {\"%s\":%d}\n", our_ref,
     e->ref, code, reply ? reply : "", len);
   return ecore_ipc_client_send(e->client, 0, 0, our_ref, e->ref, code, reply,
				len);
}

/*****************************************************************************/

static int
_ecore_config_ipc_ecore_handle_request(Ecore_Ipc_Server * server,
				       Ecore_Ipc_Event_Client_Data * e)
{
   Ecore_Config_Server *srv;
   long                serial;
   int                 ret;
   char               *r, *k, *v, *m;

   srv = _ecore_config_server_convert(server);
   serial = e->minor;
   ret = ECORE_CONFIG_ERR_FAIL;
   r = NULL;
   m = (char *)e->data;
   E(1, "IPC/eCore: client sent: [%d,%d] #%d (%d) @ %p\n", e->major, e->minor,
     e->ref, e->size, server);

   switch (e->major)
     {
     case IPC_PROP_LIST:
	if (srv == __ecore_config_server_global)
	   r = _ecore_config_ipc_global_prop_list(srv, serial);
	else
	   r = _ecore_config_ipc_prop_list(srv, serial);
	break;
     case IPC_PROP_DESC:
	if (_ecore_config_ipc_ecore_string_get(&m, &k) == ECORE_CONFIG_ERR_SUCC)
	   r = _ecore_config_ipc_prop_desc(srv, serial, k);
	break;
     case IPC_PROP_GET:
	if (_ecore_config_ipc_ecore_string_get(&m, &k) == ECORE_CONFIG_ERR_SUCC)
	   r = _ecore_config_ipc_prop_get(srv, serial, k);
	break;
     case IPC_PROP_SET:
	if (_ecore_config_ipc_ecore_string_get(&m, &k) == ECORE_CONFIG_ERR_SUCC)
	  {
	     if (_ecore_config_ipc_ecore_string_get(&m, &v) ==
		 ECORE_CONFIG_ERR_SUCC)
		return _ecore_config_ipc_ecore_send(e,
						    _ecore_config_ipc_prop_set
						    (srv, serial, k, v), NULL);
	  }
	break;

     case IPC_BUNDLE_LIST:
	r = _ecore_config_ipc_bundle_list(srv);
	break;
     case IPC_BUNDLE_NEW:
	if (_ecore_config_ipc_ecore_string_get(&m, &k) == ECORE_CONFIG_ERR_SUCC)
	   return _ecore_config_ipc_ecore_send(e,
					       k ?
					       _ecore_config_ipc_bundle_new(srv,
									    k) :
					       ECORE_CONFIG_ERR_FAIL, NULL);
	break;
     case IPC_BUNDLE_LABEL_SET:
	if (_ecore_config_ipc_ecore_string_get(&m, &k) == ECORE_CONFIG_ERR_SUCC)
	   return _ecore_config_ipc_ecore_send(e,
					       k ?
					       _ecore_config_ipc_bundle_label_set
					       (srv, serial,
						k) : ECORE_CONFIG_ERR_FAIL,
					       NULL);
	break;
     case IPC_BUNDLE_LABEL_FIND:
	if (_ecore_config_ipc_ecore_string_get(&m, &k) == ECORE_CONFIG_ERR_SUCC)
	   return _ecore_config_ipc_ecore_send(e,
					       _ecore_config_ipc_bundle_label_find
					       (srv, k), NULL);
	break;
     case IPC_BUNDLE_LABEL_GET:
	r = _ecore_config_ipc_bundle_label_get(srv, serial);
	break;
     }

   ret =
      _ecore_config_ipc_ecore_send(e,
				   r ? ECORE_CONFIG_ERR_SUCC :
				   ECORE_CONFIG_ERR_FAIL, r);
   if (r)
     {
	free(r);
	return ret;
     }
   return ECORE_CONFIG_ERR_NOTFOUND;
}

/*****************************************************************************/

static int
_ecore_config_ipc_client_add(void *data, int type __UNUSED__, void *event)
{
   Ecore_Ipc_Server  **server;
   Ecore_Ipc_Event_Client_Data *e;

   server = (Ecore_Ipc_Server **) data;
   e = (Ecore_Ipc_Event_Client_Data *) event;

   if (*server != ecore_ipc_client_server_get(e->client))
      return 1;

   E(1, "IPC/eCore: Client connected. @ %p\n", server);
   return 1;
}

static int
_ecore_config_ipc_client_del(void *data, int type __UNUSED__, void *event)
{
   Ecore_Ipc_Server  **server;
   Ecore_Ipc_Event_Client_Data *e;

   server = (Ecore_Ipc_Server **) data;
   e = (Ecore_Ipc_Event_Client_Data *) event;

   if (*server != ecore_ipc_client_server_get(e->client))
      return 1;

   E(1, "IPC/eCore: Client disconnected. @ %p\n", server);
   return 1;
}

static int
_ecore_config_ipc_client_sent(void *data, int type __UNUSED__, void *event)
{
   Ecore_Ipc_Server  **server;
   Ecore_Ipc_Event_Client_Data *e;

   server = (Ecore_Ipc_Server **) data;
   e = (Ecore_Ipc_Event_Client_Data *) event;

   if (*server != ecore_ipc_client_server_get(e->client))
      return 1;

   _ecore_config_ipc_ecore_handle_request(*server, e);
   return 1;
}

/*****************************************************************************/

int
_ecore_config_mod_init(const char *pipe_name, void **data)
{
   Ecore_Ipc_Server  **server;
   struct stat         st;
   char               *p;
   int                 port;
   char                socket[PATH_MAX];

   server = (Ecore_Ipc_Server **) data;
   port = 0;
   if (!server)
      return ECORE_CONFIG_ERR_FAIL;

/*  if(*server)
      return ECORE_CONFIG_ERR_IGNORED; */

   ecore_init();
   if (ecore_ipc_init() < 1)
      return ECORE_CONFIG_ERR_FAIL;

   if ((p = getenv("HOME")))
     {				/* debug-only ### FIXME */
	int                 stale;

	stale = 1;
	while (stale)
	  {
	     snprintf(socket, PATH_MAX, "%s/.ecore/%s/%d", p, pipe_name, port);

	     if (!stat(socket, &st))
	       {
		  E(0, "IPC/eCore: pipe \"%s\" already exists!?\n", socket);
/*      if(unlink(buf))
  	E(0,"IPC/eCore: could not remove pipe \"%s\": %d\n",buf,errno); }}*/
		  port++;
	       }
	     else
	       {
		  stale = 0;
	       }
	  }
     }
   *server = ecore_ipc_server_add(ECORE_IPC_LOCAL_USER, pipe_name, port, NULL);
   ecore_event_handler_add(ECORE_IPC_EVENT_CLIENT_ADD,
			   _ecore_config_ipc_client_add, server);
   ecore_event_handler_add(ECORE_IPC_EVENT_CLIENT_DEL,
			   _ecore_config_ipc_client_del, server);
   ecore_event_handler_add(ECORE_IPC_EVENT_CLIENT_DATA,
			   _ecore_config_ipc_client_sent, server);

   if (server)
     {
	E(1, "IPC/eCore: Server is listening on %s.\n", pipe_name);
     }

   return ECORE_CONFIG_ERR_SUCC;
}

int
_ecore_config_mod_exit(void **data)
{
   int                 ret;
   Ecore_Ipc_Server  **server;

   ret = ECORE_CONFIG_ERR_SUCC;
   server = (Ecore_Ipc_Server **) data;

   if (!server)
      return ECORE_CONFIG_ERR_FAIL;

   if (*server)
     {
	ecore_ipc_server_del(*server);
	*server = NULL;
     }

   ecore_ipc_shutdown();

   return ret;
}

/*****************************************************************************/

int
_ecore_config_mod_poll(void **data)
{
   Ecore_Ipc_Server  **server;

   server = (Ecore_Ipc_Server **) data;

   if (!server)
      return ECORE_CONFIG_ERR_FAIL;

   ecore_main_loop_iterate();

   return ECORE_CONFIG_ERR_SUCC;
}

/*****************************************************************************/