summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMike Blumenkrantz <michael.blumenkrantz@gmail.com>2011-06-29 19:54:39 +0000
committerMike Blumenkrantz <michael.blumenkrantz@gmail.com>2011-06-29 19:54:39 +0000
commit234b2000684ed87bb4d08b222d8f6a73b3618d17 (patch)
tree4b5f796947b031973ff9cf0f318316db1d08f378
parent9611111aba40c527c63ce35bf7a750226ab53d62 (diff)
whoops, forgot to actually add the scanner last time
SVN revision: 60838
-rw-r--r--legacy/eeze/src/bin/eeze_scanner.c438
-rw-r--r--legacy/eeze/src/bin/eeze_scanner.h27
2 files changed, 465 insertions, 0 deletions
diff --git a/legacy/eeze/src/bin/eeze_scanner.c b/legacy/eeze/src/bin/eeze_scanner.c
new file mode 100644
index 0000000000..de18edb534
--- /dev/null
+++ b/legacy/eeze/src/bin/eeze_scanner.c
@@ -0,0 +1,438 @@
1#ifdef HAVE_CONFIG_H
2# include "config.h"
3#endif
4
5#include <Eet.h>
6#include <Eeze.h>
7#include <Ecore_Con.h>
8#include <Eeze_Disk.h>
9#include <sys/stat.h>
10#include <sys/types.h>
11#include <errno.h>
12#include <unistd.h>
13#include <fcntl.h>
14
15#include "eeze_scanner.h"
16
17#define DBG(...) EINA_LOG_DOM_DBG(es_log_dom, __VA_ARGS__)
18#define INF(...) EINA_LOG_DOM_INFO(es_log_dom, __VA_ARGS__)
19#define WRN(...) EINA_LOG_DOM_WARN(es_log_dom, __VA_ARGS__)
20#define ERR(...) EINA_LOG_DOM_ERR(es_log_dom, __VA_ARGS__)
21#define CRI(...) EINA_LOG_DOM_CRIT(es_log_dom, __VA_ARGS__)
22
23static int es_log_dom = -1;
24static Ecore_Con_Server *svr = NULL;
25static Eet_Data_Descriptor *es_edd = NULL;
26static Eina_Hash *clients = NULL;
27
28static Eina_List *storage_devices = NULL;
29static Eina_List *storage_cdrom = NULL;
30
31static Eina_List *volume_cdrom = NULL;
32static Eina_List *volume_devices = NULL;
33
34static void
35event_send(const char *device, Eeze_Scanner_Event_Type type, Eina_Bool volume)
36{
37 Eeze_Scanner_Event ev;
38 Eina_List *l;
39 Ecore_Con_Client *cl;
40
41 ev.device = device;
42 ev.type = type;
43 ev.volume = volume;
44 EINA_LIST_FOREACH(ecore_con_server_clients_get(svr), l, cl)
45 {
46 Eet_Connection *ec;
47
48 ec = eina_hash_find(clients, cl);
49 if (!ec) continue;
50 INF("Serializing event...");
51 eet_connection_send(ec, es_edd, &ev, NULL);
52 }
53}
54
55static Eina_Bool
56event_write(const void *data, size_t size, Ecore_Con_Client *cl)
57{
58 INF("Event sent!");
59 ecore_con_client_send(cl, data, size);
60 return EINA_TRUE;
61}
62
63static Eina_Bool
64disk_mount(void *data __UNUSED__, int type __UNUSED__, Eeze_Disk *disk)
65{
66 Eina_List *l;
67 Eeze_Scanner_Device *d;
68 if (eeze_disk_type_get(disk) != EEZE_DISK_TYPE_CDROM) return ECORE_CALLBACK_RENEW;
69
70 EINA_LIST_FOREACH(storage_cdrom, l, d)
71 {
72 if (d->device == eeze_disk_syspath_get(disk))
73 {
74 d->mounted = !d->mounted;
75 break;
76 }
77 }
78 return ECORE_CALLBACK_RENEW;
79}
80
81static void
82cl_setup(Ecore_Con_Client *cl, Eet_Connection *ec)
83{
84 Eina_List *l;
85 Eeze_Scanner_Device *dev;
86 Eeze_Scanner_Event ev;
87 const char *sys;
88
89 INF("Sending initial events to new client");
90 EINA_LIST_FOREACH(storage_devices, l, sys)
91 {
92 ev.device = sys;
93 ev.type = EEZE_SCANNER_EVENT_TYPE_ADD;
94 ev.volume = EINA_FALSE;
95 eet_connection_send(ec, es_edd, &ev, NULL);
96 }
97 EINA_LIST_FOREACH(storage_cdrom, l, dev)
98 {
99 ev.device = dev->device;
100 ev.type = EEZE_SCANNER_EVENT_TYPE_ADD;
101 ev.volume = EINA_FALSE;
102 eet_connection_send(ec, es_edd, &ev, NULL);
103 }
104 EINA_LIST_FOREACH(volume_devices, l, sys)
105 {
106 ev.device = sys;
107 ev.type = EEZE_SCANNER_EVENT_TYPE_ADD;
108 ev.volume = EINA_TRUE;
109 eet_connection_send(ec, es_edd, &ev, NULL);
110 }
111 EINA_LIST_FOREACH(volume_cdrom, l, dev)
112 {
113 ev.device = dev->device;
114 ev.type = EEZE_SCANNER_EVENT_TYPE_ADD;
115 ev.volume = EINA_TRUE;
116 eet_connection_send(ec, es_edd, &ev, NULL);
117 }
118}
119
120static Eina_Bool
121es_read(const void *eet_data __UNUSED__, size_t size __UNUSED__, void *user_data __UNUSED__)
122{
123 return EINA_TRUE;
124}
125
126static Eina_Bool
127cl_add(void *data __UNUSED__, int type __UNUSED__, Ecore_Con_Event_Client_Add *ev)
128{
129 Eet_Connection *ec;
130 INF("Added client");
131
132 ec = eet_connection_new(es_read, (Eet_Write_Cb*)event_write, ev->client);
133 if (!ec)
134 {
135 ERR("Could not create eet serializer! Lost client!");
136 ecore_con_client_del(ev->client);
137 return ECORE_CALLBACK_RENEW;
138 }
139
140 eina_hash_direct_add(clients, ev->client, ec);
141 cl_setup(ev->client, ec);
142 return ECORE_CALLBACK_RENEW;
143}
144
145static Eina_Bool
146cl_del(void *data __UNUSED__, int type __UNUSED__, Ecore_Con_Event_Client_Del *ev)
147{
148 Eet_Connection *ec;
149 Eina_Bool d;
150 INF("Removed client");
151 ec = eina_hash_find(clients, ev->client);
152 if (ec)
153 {
154 eet_connection_close(ec, &d);
155 eina_hash_del_by_data(clients, ec);
156 }
157
158 return ECORE_CALLBACK_RENEW;
159}
160
161static void
162eet_setup(void)
163{
164 Eet_Data_Descriptor_Class eddc;
165
166 if (!eet_eina_stream_data_descriptor_class_set(&eddc, sizeof(eddc), "eeze_scanner_event", sizeof(Eeze_Scanner_Event)))
167 {
168 CRI("Could not create eet data descriptor!");
169 exit(1);
170 }
171
172 es_edd = eet_data_descriptor_stream_new(&eddc);
173#define DAT(MEMBER, TYPE) EET_DATA_DESCRIPTOR_ADD_BASIC(es_edd, Eeze_Scanner_Event, #MEMBER, MEMBER, EET_T_##TYPE)
174 DAT(device, INLINED_STRING);
175 DAT(type, UINT);
176 DAT(volume, UCHAR);
177#undef DAT
178}
179
180static Eina_Bool
181cdrom_timer(Eeze_Scanner_Device *dev)
182{
183 const char *devpath;
184 int fd;
185
186 /* cdrom already mounted, no need to poll */
187 if (dev->mounted) return EINA_TRUE;
188 devpath = eeze_udev_syspath_get_devpath(dev->device);
189 fd = open(devpath, O_RDONLY);
190 if (fd < 0)
191 {
192 Eina_List *l;
193
194 l = eina_list_data_find_list(volume_cdrom, dev);
195 if (l)
196 {
197 /* disc removed, delete volume */
198 INF("Removed cdrom '%s'", dev->device);
199 volume_cdrom = eina_list_remove_list(volume_cdrom, l);
200 event_send(dev->device, EEZE_SCANNER_EVENT_TYPE_CHANGE, EINA_TRUE);
201 }
202 /* just in case */
203 dev->mounted = EINA_FALSE;
204 }
205 else
206 {
207 if (!eina_list_data_find(volume_cdrom, dev))
208 {
209 INF("Added cdrom '%s'", dev->device);
210 volume_cdrom = eina_list_append(volume_cdrom, dev);
211 event_send(dev->device, EEZE_SCANNER_EVENT_TYPE_CHANGE, EINA_TRUE);
212 }
213 close(fd);
214 }
215 eina_stringshare_del(devpath);
216 return EINA_TRUE;
217}
218
219static void
220storage_setup(void)
221{
222 Eina_List *l, *ll;
223 const char *sys;
224
225 storage_devices = eeze_udev_find_by_type(EEZE_UDEV_TYPE_DRIVE_INTERNAL, NULL);
226 if (!storage_devices)
227 {
228 ERR("No storage devices found! This is not supposed to happen!");
229 exit(1);
230 }
231 EINA_LIST_FOREACH(storage_devices, l, sys)
232 event_send(sys, EEZE_SCANNER_EVENT_TYPE_ADD, EINA_FALSE);
233
234 ll = eeze_udev_find_by_type(EEZE_UDEV_TYPE_DRIVE_REMOVABLE, NULL);
235 EINA_LIST_FREE(ll, sys)
236 {
237 event_send(sys, EEZE_SCANNER_EVENT_TYPE_ADD, EINA_FALSE);
238 storage_devices = eina_list_append(storage_devices, sys);
239 }
240
241 l = eeze_udev_find_by_type(EEZE_UDEV_TYPE_DRIVE_CDROM, NULL);
242 EINA_LIST_FREE(l, sys)
243 {
244 Eeze_Scanner_Device *dev;
245 Eeze_Disk *disk;
246
247 dev = calloc(1, sizeof(Eeze_Scanner_Device));
248 if (!dev)
249 {
250 ERR("Lost cdrom device '%s'!", sys);
251 eina_stringshare_del(sys);
252 continue;
253 }
254 disk = eeze_disk_new(sys);
255 if (!disk)
256 {
257 ERR("Lost cdrom device '%s'!", sys);
258 eina_stringshare_del(sys);
259 free(dev);
260 continue;
261 }
262 dev->device = sys;
263 dev->mounted = eeze_disk_mounted_get(disk);
264 eeze_disk_free(disk);
265 event_send(sys, EEZE_SCANNER_EVENT_TYPE_ADD, EINA_FALSE);
266 ecore_poller_add(ECORE_POLLER_CORE, 32, (Ecore_Task_Cb)cdrom_timer, dev);
267 storage_cdrom = eina_list_append(storage_cdrom, dev);
268 }
269 volume_devices = eeze_udev_find_by_type(EEZE_UDEV_TYPE_DRIVE_MOUNTABLE, NULL);
270 EINA_LIST_FOREACH_SAFE(volume_devices, l, ll, sys)
271 {
272 Eina_List *c;
273 Eeze_Scanner_Device *dev;
274
275 EINA_LIST_FOREACH(storage_cdrom, c, dev)
276 if (sys == dev->device)
277 {
278 eina_stringshare_del(sys);
279 volume_devices = eina_list_remove_list(volume_devices, l);
280 volume_cdrom = eina_list_append(volume_cdrom, dev);
281 event_send(sys, EEZE_SCANNER_EVENT_TYPE_ADD, EINA_TRUE);
282 l = NULL;
283 break;
284 }
285 if (!l) continue;
286 event_send(sys, EEZE_SCANNER_EVENT_TYPE_ADD, EINA_TRUE);
287 }
288}
289
290static void
291cb_vol_chg(const char *device, Eeze_Udev_Event ev, void *data __UNUSED__, Eeze_Udev_Watch *watch __UNUSED__)
292{
293 Eina_List *l;
294 Eeze_Scanner_Device *dev;
295
296 DBG("device='%s'", device);
297
298 if (ev == EEZE_UDEV_EVENT_ONLINE) ev = EEZE_SCANNER_EVENT_TYPE_ADD;
299 else if (ev == EEZE_UDEV_EVENT_OFFLINE) ev = EEZE_SCANNER_EVENT_TYPE_REMOVE;
300
301 event_send(device, ev, EINA_TRUE);
302 switch (ev)
303 {
304 case EEZE_UDEV_EVENT_ADD:
305 case EEZE_UDEV_EVENT_ONLINE:
306 INF("Added volume '%s'", device);
307 EINA_LIST_FOREACH(storage_cdrom, l, dev)
308 if (device == dev->device)
309 {
310 volume_cdrom = eina_list_append(volume_cdrom, dev);
311 return;
312 }
313 volume_devices = eina_list_append(volume_devices, eina_stringshare_add(device));
314 break;
315 case EEZE_UDEV_EVENT_REMOVE:
316 case EEZE_UDEV_EVENT_OFFLINE:
317 INF("Removed volume '%s'", device);
318 EINA_LIST_FOREACH(volume_cdrom, l, dev)
319 if (device == dev->device)
320 {
321 volume_cdrom = eina_list_remove_list(volume_cdrom, l);
322 return;
323 }
324 volume_devices = eina_list_remove(volume_devices, device);
325 eina_stringshare_del(device);
326 default:
327 break;
328 }
329}
330
331static void
332cb_stor_chg(const char *device, Eeze_Udev_Event ev, void *data __UNUSED__, Eeze_Udev_Watch *watch __UNUSED__)
333{
334 Eina_List *l;
335 Eeze_Scanner_Device *dev = NULL;
336 const char *str;
337
338
339 DBG("device='%s'", device);
340 switch (ev)
341 {
342 case EEZE_UDEV_EVENT_ADD:
343 case EEZE_UDEV_EVENT_ONLINE:
344 INF("Added device '%s'", device);
345 event_send(device, ev, EINA_FALSE);
346 str = eeze_udev_syspath_get_property(device, "ID_CDROM");
347 if (!str)
348 {
349 storage_devices = eina_list_append(storage_devices, eina_stringshare_add(device));
350 return;
351 }
352 eina_stringshare_del(str);
353 dev = calloc(1, sizeof(Eeze_Scanner_Device));
354 dev->device = eina_stringshare_add(device);
355 storage_cdrom = eina_list_append(storage_cdrom, dev);
356 break;
357 case EEZE_UDEV_EVENT_REMOVE:
358 case EEZE_UDEV_EVENT_OFFLINE:
359 if (!eina_list_data_find(storage_devices, device))
360 {
361 EINA_LIST_FOREACH(storage_cdrom, l, dev)
362 if (dev->device == device) break;
363 if ((!dev) || (dev->device != device)) return;
364 }
365 INF("Removed device '%s'", device);
366 event_send(device, ev, EINA_FALSE);
367 EINA_LIST_FOREACH(storage_cdrom, l, dev)
368 if (device == dev->device)
369 {
370 storage_cdrom = eina_list_remove_list(storage_cdrom, l);
371 eina_stringshare_del(dev->device);
372 free(dev);
373 return;
374 }
375 storage_devices = eina_list_remove(storage_devices, device);
376 eina_stringshare_del(device);
377 default:
378 break;
379 }
380}
381
382int
383main(void)
384{
385 const char *tmp;
386 char buf[1024];
387 struct stat st;
388
389 eina_init();
390 ecore_init();
391 ecore_con_init();
392 eeze_init();
393 eeze_disk_function();
394 eeze_mount_tabs_watch();
395
396 es_log_dom = eina_log_domain_register("eeze_scanner", EINA_COLOR_CYAN);
397 eina_log_domain_level_set("eeze_scanner", EINA_LOG_LEVEL_DBG);
398 eina_log_domain_level_set("eeze", EINA_LOG_LEVEL_DBG);
399
400 tmp = getenv("TMPDIR");
401 if (!tmp) tmp = "/tmp";
402
403 snprintf(buf, sizeof(buf), "%s/.ecore_service|eeze_scanner", tmp);
404 if (!stat(buf, &st))
405 {
406 ERR("Socket file '%s' for scanner already exists! Refusing to start up!", buf);
407 exit(1);
408 }
409 eet_setup();
410 clients = eina_hash_pointer_new(NULL);
411 EINA_SAFETY_ON_NULL_GOTO(clients, error);
412
413 ecore_event_handler_add(ECORE_CON_EVENT_CLIENT_ADD, (Ecore_Event_Handler_Cb)cl_add, NULL);
414 ecore_event_handler_add(ECORE_CON_EVENT_CLIENT_DEL, (Ecore_Event_Handler_Cb)cl_del, NULL);
415 ecore_event_handler_add(EEZE_EVENT_DISK_UNMOUNT, (Ecore_Event_Handler_Cb)disk_mount, NULL);
416 ecore_event_handler_add(EEZE_EVENT_DISK_MOUNT, (Ecore_Event_Handler_Cb)disk_mount, NULL);
417
418 eeze_udev_watch_add(EEZE_UDEV_TYPE_DRIVE_INTERNAL, EEZE_UDEV_EVENT_NONE, cb_stor_chg, NULL);
419 eeze_udev_watch_add(EEZE_UDEV_TYPE_DRIVE_REMOVABLE, EEZE_UDEV_EVENT_NONE, cb_stor_chg, NULL);
420 eeze_udev_watch_add(EEZE_UDEV_TYPE_DRIVE_CDROM, EEZE_UDEV_EVENT_NONE, cb_stor_chg, NULL);
421 eeze_udev_watch_add(EEZE_UDEV_TYPE_DRIVE_MOUNTABLE, EEZE_UDEV_EVENT_NONE, cb_vol_chg, NULL);
422
423 svr = ecore_con_server_add(ECORE_CON_LOCAL_SYSTEM, buf, 0, NULL);
424 EINA_SAFETY_ON_NULL_GOTO(svr, error);
425 snprintf(buf, sizeof(buf), "%s/.ecore_service|eeze_scanner|0", tmp);
426 if (chmod(buf, S_IROTH))
427 {
428 ERR("Could not chmod socket (%s)! \"%s\"", buf, strerror(errno));
429 goto error;
430 }
431
432 storage_setup();
433 ecore_main_loop_begin();
434 return 0;
435error:
436 ERR("Could not start up!");
437 exit(1);
438}
diff --git a/legacy/eeze/src/bin/eeze_scanner.h b/legacy/eeze/src/bin/eeze_scanner.h
new file mode 100644
index 0000000000..4942d2d3ac
--- /dev/null
+++ b/legacy/eeze/src/bin/eeze_scanner.h
@@ -0,0 +1,27 @@
1#ifndef EEZE_SCANNER_H
2#define EEZE_SCANNER_H
3
4#include <Eeze.h>
5
6typedef enum
7{
8 EEZE_SCANNER_EVENT_TYPE_NONE,
9 EEZE_SCANNER_EVENT_TYPE_ADD = EEZE_UDEV_EVENT_ADD,
10 EEZE_SCANNER_EVENT_TYPE_REMOVE = EEZE_UDEV_EVENT_REMOVE,
11 EEZE_SCANNER_EVENT_TYPE_CHANGE = EEZE_UDEV_EVENT_CHANGE
12} Eeze_Scanner_Event_Type;
13
14typedef struct
15{
16 const char *device;
17 Eeze_Scanner_Event_Type type;
18 Eina_Bool volume;
19} Eeze_Scanner_Event;
20
21typedef struct
22{
23 const char *device;
24 Eina_Bool mounted : 1;
25} Eeze_Scanner_Device;
26
27#endif