2010-05-16 19:59:07 -07:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
2010-05-13 22:21:52 -07:00
|
|
|
#include <Eeze_Udev.h>
|
2010-05-16 19:59:07 -07:00
|
|
|
#include "eeze_udev_private.h"
|
2010-05-13 03:49:17 -07:00
|
|
|
|
|
|
|
/* opaque */
|
2010-05-14 00:02:50 -07:00
|
|
|
struct Eeze_Udev_Watch
|
2010-05-13 03:49:17 -07:00
|
|
|
{
|
|
|
|
struct udev_monitor *mon;
|
|
|
|
Ecore_Fd_Handler *handler;
|
2010-05-14 00:02:50 -07:00
|
|
|
Eeze_Udev_Type type;
|
2010-05-13 03:49:17 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
/* private */
|
|
|
|
struct _store_data
|
|
|
|
{
|
2010-05-14 00:02:50 -07:00
|
|
|
void(*func)(const char *, const char *, void *, Eeze_Udev_Watch *);
|
2010-05-13 03:49:17 -07:00
|
|
|
void *data;
|
2010-05-19 21:28:05 -07:00
|
|
|
int event;
|
2010-05-13 03:49:17 -07:00
|
|
|
struct udev_monitor *mon;
|
2010-05-14 00:02:50 -07:00
|
|
|
Eeze_Udev_Type type;
|
|
|
|
Eeze_Udev_Watch *watch;
|
2010-05-13 03:49:17 -07:00
|
|
|
};
|
|
|
|
|
2010-05-14 00:02:50 -07:00
|
|
|
|
|
|
|
/* private function to further filter watch results based on Eeze_Udev_Type
|
2010-05-16 19:59:07 -07:00
|
|
|
* specified; helpful for new udev versions, but absolutely required for
|
|
|
|
* old udev, which does not implement filtering in device monitors.
|
2010-05-13 03:49:17 -07:00
|
|
|
*/
|
|
|
|
static int
|
|
|
|
_get_syspath_from_watch(void *data, Ecore_Fd_Handler *fd_handler)
|
|
|
|
{
|
|
|
|
struct _store_data *store = data;
|
2010-05-19 21:28:05 -07:00
|
|
|
struct udev_device *device, *parent, *tmpdev;
|
2010-05-13 03:49:17 -07:00
|
|
|
const char *ret, *test;
|
2010-05-19 21:28:05 -07:00
|
|
|
void(*func)(const char *, const char *, void *, Eeze_Udev_Watch *) = store->func;
|
2010-05-13 03:49:17 -07:00
|
|
|
void *sdata = store->data;
|
2010-05-14 00:02:50 -07:00
|
|
|
Eeze_Udev_Watch *watch = store->watch;
|
2010-05-13 03:49:17 -07:00
|
|
|
int cap = 0;
|
|
|
|
|
|
|
|
if (!ecore_main_fd_handler_active_get(fd_handler, ECORE_FD_READ))
|
2010-05-13 13:52:27 -07:00
|
|
|
return 1;
|
2010-05-13 03:49:17 -07:00
|
|
|
device = udev_monitor_receive_device(store->mon);
|
2010-05-13 13:52:27 -07:00
|
|
|
if (!device) return 1;
|
2010-05-13 03:49:17 -07:00
|
|
|
|
2010-05-13 14:21:12 -07:00
|
|
|
switch (store->type)
|
|
|
|
{
|
2010-05-14 00:02:50 -07:00
|
|
|
case EEZE_UDEV_TYPE_KEYBOARD:
|
2010-05-13 22:45:09 -07:00
|
|
|
#ifdef OLD_UDEV_RRRRRRRRRRRRRR
|
|
|
|
if ((!(test = udev_device_get_subsystem(device))) || (strcmp(test, "input")))
|
|
|
|
goto error;
|
2010-05-14 00:23:18 -07:00
|
|
|
test = udev_device_get_property_value(device, "ID_CLASS");
|
2010-05-20 21:11:58 -07:00
|
|
|
if ((_walk_parents_test_attr(device, "bInterfaceProtocol", "01")) || ((test) && (!strcmp(test, "kbd"))))
|
2010-05-14 00:24:56 -07:00
|
|
|
break;
|
|
|
|
goto error;
|
2010-05-13 22:45:09 -07:00
|
|
|
#endif
|
2010-05-13 14:21:12 -07:00
|
|
|
if (!udev_device_get_property_value(device, "ID_INPUT_KEYBOARD"))
|
|
|
|
goto error;
|
|
|
|
break;
|
2010-05-14 00:02:50 -07:00
|
|
|
case EEZE_UDEV_TYPE_MOUSE:
|
2010-05-13 22:45:09 -07:00
|
|
|
#ifdef OLD_UDEV_RRRRRRRRRRRRRR
|
|
|
|
if ((!(test = udev_device_get_subsystem(device))) || (strcmp(test, "input")))
|
|
|
|
goto error;
|
2010-05-14 00:23:18 -07:00
|
|
|
test = udev_device_get_property_value(device, "ID_CLASS");
|
2010-05-20 21:11:58 -07:00
|
|
|
if ((_walk_parents_test_attr(device, "bInterfaceProtocol", "02")) || ((test) && (!strcmp(test, "mouse"))))
|
2010-05-14 00:24:56 -07:00
|
|
|
break;
|
|
|
|
goto error;
|
2010-05-13 22:45:09 -07:00
|
|
|
#endif
|
2010-05-13 14:21:12 -07:00
|
|
|
if (!udev_device_get_property_value(device, "ID_INPUT_MOUSE"))
|
|
|
|
goto error;
|
|
|
|
break;
|
2010-05-14 00:02:50 -07:00
|
|
|
case EEZE_UDEV_TYPE_TOUCHPAD:
|
2010-05-13 22:45:09 -07:00
|
|
|
#ifdef OLD_UDEV_RRRRRRRRRRRRRR
|
|
|
|
if ((!(test = udev_device_get_subsystem(device))) || (strcmp(test, "input")))
|
|
|
|
goto error;
|
2010-05-20 21:11:58 -07:00
|
|
|
if (_walk_parents_test_attr(device, "resolution", NULL))
|
2010-05-14 00:02:50 -07:00
|
|
|
break;
|
|
|
|
goto error;
|
2010-05-13 22:45:09 -07:00
|
|
|
#endif
|
2010-05-13 14:21:12 -07:00
|
|
|
if (!udev_device_get_property_value(device, "ID_INPUT_TOUCHPAD"))
|
|
|
|
goto error;
|
|
|
|
break;
|
2010-05-14 00:02:50 -07:00
|
|
|
case EEZE_UDEV_TYPE_DRIVE_MOUNTABLE:
|
2010-05-13 22:45:09 -07:00
|
|
|
#ifdef OLD_UDEV_RRRRRRRRRRRRRR
|
|
|
|
if ((!(test = udev_device_get_subsystem(device))) || (strcmp(test, "block")))
|
|
|
|
goto error;
|
|
|
|
#endif
|
2010-05-13 14:21:12 -07:00
|
|
|
test = udev_device_get_sysattr_value(device, "capability");
|
|
|
|
if (test) cap = atoi(test);
|
|
|
|
if (!(test = (udev_device_get_property_value(device, "ID_FS_USAGE"))) ||
|
|
|
|
(strcmp("filesystem", test)) || (cap == 52))
|
|
|
|
goto error;
|
|
|
|
break;
|
2010-05-14 00:02:50 -07:00
|
|
|
case EEZE_UDEV_TYPE_DRIVE_INTERNAL:
|
2010-05-13 22:45:09 -07:00
|
|
|
#ifdef OLD_UDEV_RRRRRRRRRRRRRR
|
|
|
|
if ((!(test = udev_device_get_subsystem(device))) || (strcmp(test, "block")))
|
|
|
|
goto error;
|
|
|
|
#endif
|
2010-05-13 14:21:12 -07:00
|
|
|
if (!(test = udev_device_get_property_value(device, "ID_BUS")) || (strcmp("ata", test)) ||
|
|
|
|
!(test = udev_device_get_sysattr_value(device, "removable")) || (atoi(test)))
|
|
|
|
goto error;
|
|
|
|
break;
|
2010-05-14 00:02:50 -07:00
|
|
|
case EEZE_UDEV_TYPE_DRIVE_REMOVABLE:
|
2010-05-13 22:45:09 -07:00
|
|
|
#ifdef OLD_UDEV_RRRRRRRRRRRRRR
|
|
|
|
if ((!(test = udev_device_get_subsystem(device))) || (strcmp(test, "block")))
|
|
|
|
goto error;
|
|
|
|
#endif
|
2010-05-13 14:21:12 -07:00
|
|
|
if ((!(test = udev_device_get_sysattr_value(device, "removable")) || (!atoi(test))) &&
|
|
|
|
(!(test = udev_device_get_sysattr_value(device, "capability")) || (atoi(test) != 10)))
|
|
|
|
goto error;
|
|
|
|
break;
|
2010-05-14 00:02:50 -07:00
|
|
|
case EEZE_UDEV_TYPE_DRIVE_CDROM:
|
2010-05-13 22:45:09 -07:00
|
|
|
#ifdef OLD_UDEV_RRRRRRRRRRRRRR
|
|
|
|
if ((!(test = udev_device_get_subsystem(device))) || (strcmp(test, "block")))
|
|
|
|
goto error;
|
|
|
|
#endif
|
2010-05-13 14:21:12 -07:00
|
|
|
if (!udev_device_get_property_value(device, "ID_CDROM"))
|
|
|
|
goto error;
|
|
|
|
break;
|
2010-05-14 00:02:50 -07:00
|
|
|
case EEZE_UDEV_TYPE_POWER_AC:
|
2010-05-13 22:45:09 -07:00
|
|
|
#ifdef OLD_UDEV_RRRRRRRRRRRRRR
|
|
|
|
if ((!(test = udev_device_get_subsystem(device))) || (strcmp(test, "power_supply")))
|
|
|
|
goto error;
|
|
|
|
#endif
|
2010-05-13 14:21:12 -07:00
|
|
|
if (!(test = (udev_device_get_property_value(device, "POWER_SUPPLY_TYPE"))) ||
|
|
|
|
(strcmp("Mains", test)))
|
2010-05-17 16:41:07 -07:00
|
|
|
goto error;
|
2010-05-13 14:21:12 -07:00
|
|
|
break;
|
2010-05-14 00:02:50 -07:00
|
|
|
case EEZE_UDEV_TYPE_POWER_BAT:
|
2010-05-13 22:45:09 -07:00
|
|
|
#ifdef OLD_UDEV_RRRRRRRRRRRRRR
|
|
|
|
if ((!(test = udev_device_get_subsystem(device))) || (strcmp(test, "power_supply")))
|
|
|
|
goto error;
|
|
|
|
#endif
|
2010-05-13 14:21:12 -07:00
|
|
|
if (!(test = (udev_device_get_property_value(device, "POWER_SUPPLY_TYPE"))) ||
|
|
|
|
(strcmp("Battery", test)))
|
2010-05-17 16:41:07 -07:00
|
|
|
goto error;
|
2010-05-13 14:21:12 -07:00
|
|
|
break;
|
2010-05-19 21:28:05 -07:00
|
|
|
case EEZE_UDEV_TYPE_IS_IT_HOT_OR_IS_IT_COLD_SENSOR:
|
|
|
|
#ifdef OLD_UDEV_RRRRRRRRRRRRRR
|
|
|
|
if ((!(test = udev_device_get_subsystem(device))) || (strcmp(test, "hwmon")))
|
|
|
|
goto error;
|
|
|
|
#endif /* have to do stuff up here since we need info from the parent */
|
2010-05-20 21:11:58 -07:00
|
|
|
if (!_walk_parents_test_attr(device, "temp1_input", NULL))
|
2010-05-20 11:56:30 -07:00
|
|
|
goto error;
|
|
|
|
/* if device is not the one which has the temp input, we must go up the chain */
|
|
|
|
if (!(test = udev_device_get_sysattr_value(device, "temp1_input")))
|
|
|
|
{
|
|
|
|
tmpdev = NULL;
|
|
|
|
for (parent = udev_device_get_parent(device); parent; parent = udev_device_get_parent(parent))/*check for parent*/
|
|
|
|
if (((test = udev_device_get_sysattr_value(parent, "temp1_input"))))
|
|
|
|
{
|
|
|
|
tmpdev = parent;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
tmpdev = device;
|
|
|
|
if ((!tmpdev) || !(test = udev_device_get_action(device)) || (!(ret = udev_device_get_syspath(tmpdev))))
|
2010-05-19 21:28:05 -07:00
|
|
|
goto error;
|
|
|
|
|
|
|
|
(*func)(eina_stringshare_add(ret), test, sdata, watch);
|
|
|
|
udev_device_unref(device);
|
|
|
|
return 1;
|
|
|
|
break;
|
2010-05-13 14:21:12 -07:00
|
|
|
/*
|
2010-05-14 00:02:50 -07:00
|
|
|
case EEZE_UDEV_TYPE_ANDROID:
|
2010-05-13 14:21:12 -07:00
|
|
|
udev_monitor_filter_add_match_subsystem_devtype(mon, "input", "usb_interface");
|
|
|
|
break;
|
2010-05-13 03:49:17 -07:00
|
|
|
*/
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2010-05-19 21:28:05 -07:00
|
|
|
if ((!(test = udev_device_get_action(device))) || (!(ret = udev_device_get_syspath(device))))
|
2010-05-14 10:55:03 -07:00
|
|
|
goto error;
|
2010-05-19 21:28:05 -07:00
|
|
|
if (store->event)
|
|
|
|
{
|
|
|
|
if (!strcmp(test, "add"))
|
|
|
|
{
|
|
|
|
if ((store->event & ~EEZE_UDEV_EVENT_ADD))
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
else if (!strcmp(test, "remove"))
|
|
|
|
{
|
|
|
|
if ((store->event & ~EEZE_UDEV_EVENT_REMOVE))
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
else if (!strcmp(test, "change"))
|
|
|
|
{
|
|
|
|
if ((store->event & ~EEZE_UDEV_EVENT_CHANGE))
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
else if (!strcmp(test, "online"))
|
|
|
|
{
|
|
|
|
if ((store->event & ~EEZE_UDEV_EVENT_ONLINE))
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if ((store->event & ~EEZE_UDEV_EVENT_OFFLINE))
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
(*func)(eina_stringshare_add(ret), eina_stringshare_add(test), sdata, watch);
|
2010-05-13 03:49:17 -07:00
|
|
|
error:
|
2010-05-13 14:21:12 -07:00
|
|
|
udev_device_unref(device);
|
|
|
|
return 1;
|
2010-05-13 03:49:17 -07:00
|
|
|
}
|
|
|
|
/**
|
2010-05-19 21:28:05 -07:00
|
|
|
* Add a watch for a device type
|
2010-05-13 03:49:17 -07:00
|
|
|
*
|
2010-05-19 21:28:05 -07:00
|
|
|
* @param type The Eeze_Udev_Type to watch
|
|
|
|
* @param event The events to watch; an OR list of EEZE_UDEV_EVENTs (ie (EEZE_UDEV_EVENT_ADD | EEZE_UDEV_EVENT_REMOVE)), or 0 for all events
|
2010-05-13 12:36:04 -07:00
|
|
|
* @param func The function to call when the watch receives data;
|
2010-05-14 00:02:50 -07:00
|
|
|
* must take (const char *device, const char *event_type, void *data, Eeze_Udev_Watch *watch)
|
2010-05-13 03:49:17 -07:00
|
|
|
* @param user_data Data to pass to the callback function
|
|
|
|
*
|
|
|
|
* @return A watch struct for the watch type specified, or NULL on failure
|
|
|
|
*
|
|
|
|
* @ingroup udev
|
|
|
|
*/
|
2010-05-14 00:02:50 -07:00
|
|
|
EAPI Eeze_Udev_Watch *
|
2010-05-19 21:28:05 -07:00
|
|
|
eeze_udev_watch_add(Eeze_Udev_Type type, int event, void(*func)(const char *syspath, const char *event, void *data, Eeze_Udev_Watch *watch), void *user_data)
|
2010-05-13 03:49:17 -07:00
|
|
|
{
|
|
|
|
struct udev *udev;
|
|
|
|
struct udev_monitor *mon;
|
|
|
|
int fd;
|
|
|
|
Ecore_Fd_Handler *handler;
|
2010-05-14 00:02:50 -07:00
|
|
|
Eeze_Udev_Watch *watch;
|
2010-05-13 03:49:17 -07:00
|
|
|
struct _store_data *store;
|
|
|
|
|
2010-05-19 21:28:05 -07:00
|
|
|
if (!(store = calloc(sizeof(struct _store_data), 1))) return NULL;
|
2010-05-14 00:02:50 -07:00
|
|
|
if (!(watch = malloc(sizeof(Eeze_Udev_Watch))))
|
2010-05-13 14:21:12 -07:00
|
|
|
goto error;
|
2010-05-13 03:49:17 -07:00
|
|
|
|
|
|
|
if (!(udev = udev_new()))
|
2010-05-13 14:21:12 -07:00
|
|
|
goto error;
|
2010-05-13 03:49:17 -07:00
|
|
|
if (!(mon = udev_monitor_new_from_netlink(udev, "udev")))
|
2010-05-13 14:21:12 -07:00
|
|
|
goto error;
|
2010-05-13 22:45:09 -07:00
|
|
|
#ifndef OLD_UDEV_RRRRRRRRRRRRRR
|
2010-05-13 03:49:17 -07:00
|
|
|
switch (type)
|
|
|
|
{
|
2010-05-14 00:02:50 -07:00
|
|
|
case EEZE_UDEV_TYPE_KEYBOARD:
|
2010-05-13 14:21:12 -07:00
|
|
|
udev_monitor_filter_add_match_subsystem_devtype(mon, "input", NULL);
|
|
|
|
break;
|
2010-05-14 00:02:50 -07:00
|
|
|
case EEZE_UDEV_TYPE_MOUSE:
|
2010-05-13 14:21:12 -07:00
|
|
|
udev_monitor_filter_add_match_subsystem_devtype(mon, "input", NULL);
|
|
|
|
break;
|
2010-05-14 00:02:50 -07:00
|
|
|
case EEZE_UDEV_TYPE_TOUCHPAD:
|
2010-05-13 14:21:12 -07:00
|
|
|
udev_monitor_filter_add_match_subsystem_devtype(mon, "input", NULL);
|
|
|
|
break;
|
2010-05-14 00:02:50 -07:00
|
|
|
case EEZE_UDEV_TYPE_DRIVE_MOUNTABLE:
|
2010-05-13 14:21:12 -07:00
|
|
|
udev_monitor_filter_add_match_subsystem_devtype(mon, "block", NULL);
|
|
|
|
break;
|
2010-05-14 00:02:50 -07:00
|
|
|
case EEZE_UDEV_TYPE_DRIVE_INTERNAL:
|
2010-05-13 14:21:12 -07:00
|
|
|
udev_monitor_filter_add_match_subsystem_devtype(mon, "block", NULL);
|
|
|
|
break;
|
2010-05-14 00:02:50 -07:00
|
|
|
case EEZE_UDEV_TYPE_DRIVE_REMOVABLE:
|
2010-05-13 14:21:12 -07:00
|
|
|
udev_monitor_filter_add_match_subsystem_devtype(mon, "block", NULL);
|
|
|
|
break;
|
2010-05-14 00:02:50 -07:00
|
|
|
case EEZE_UDEV_TYPE_DRIVE_CDROM:
|
2010-05-13 14:21:12 -07:00
|
|
|
udev_monitor_filter_add_match_subsystem_devtype(mon, "block", NULL);
|
|
|
|
break;
|
2010-05-14 00:02:50 -07:00
|
|
|
case EEZE_UDEV_TYPE_POWER_AC:
|
|
|
|
case EEZE_UDEV_TYPE_POWER_BAT:
|
2010-05-13 14:21:12 -07:00
|
|
|
udev_monitor_filter_add_match_subsystem_devtype(mon, "power_supply", NULL);
|
|
|
|
break;
|
2010-05-19 21:28:05 -07:00
|
|
|
case EEZE_UDEV_TYPE_IS_IT_HOT_OR_IS_IT_COLD_SENSOR:
|
|
|
|
udev_monitor_filter_add_match_subsystem_devtype(mon, "hwmon", NULL);
|
|
|
|
break;
|
2010-05-13 14:21:12 -07:00
|
|
|
/*
|
2010-05-14 00:02:50 -07:00
|
|
|
case EEZE_UDEV_TYPE_ANDROID:
|
2010-05-13 14:21:12 -07:00
|
|
|
udev_monitor_filter_add_match_subsystem_devtype(mon, "input", "usb_interface");
|
|
|
|
break;
|
2010-05-13 03:49:17 -07:00
|
|
|
*/
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2010-05-13 22:45:09 -07:00
|
|
|
#endif
|
2010-05-13 03:49:17 -07:00
|
|
|
if (udev_monitor_enable_receiving(mon))
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
fd = udev_monitor_get_fd(mon);
|
|
|
|
store->func = func;
|
|
|
|
store->data = user_data;
|
|
|
|
store->mon = mon;
|
|
|
|
store->type = type;
|
|
|
|
store->watch = watch;
|
2010-05-19 21:28:05 -07:00
|
|
|
store->event = event;
|
2010-05-13 03:49:17 -07:00
|
|
|
if (!(handler = ecore_main_fd_handler_add(fd, ECORE_FD_READ, _get_syspath_from_watch, store, NULL, NULL)))
|
|
|
|
goto error;
|
|
|
|
watch->mon = mon;
|
|
|
|
watch->handler = handler;
|
|
|
|
|
|
|
|
return watch;
|
|
|
|
|
|
|
|
error:
|
|
|
|
free(store);
|
|
|
|
free(watch);
|
|
|
|
udev_monitor_unref(mon);
|
|
|
|
udev_unref(udev);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Deletes a watch.
|
|
|
|
*
|
2010-05-14 00:02:50 -07:00
|
|
|
* @param watch An Eeze_Udev_Watch object
|
2010-05-13 03:49:17 -07:00
|
|
|
* @return The data originally associated with the watch, or NULL
|
|
|
|
*
|
|
|
|
* Deletes a watch, closing file descriptors and freeing related udev memory.
|
|
|
|
*
|
|
|
|
* @ingroup udev
|
|
|
|
*/
|
|
|
|
EAPI void *
|
2010-05-14 00:02:50 -07:00
|
|
|
eeze_udev_watch_del(Eeze_Udev_Watch *watch)
|
2010-05-13 03:49:17 -07:00
|
|
|
{
|
|
|
|
struct udev *udev;
|
2010-05-13 14:21:12 -07:00
|
|
|
struct _store_data *sdata;
|
2010-05-13 03:49:17 -07:00
|
|
|
void *ret;
|
|
|
|
|
2010-05-17 17:17:13 -07:00
|
|
|
if ((!watch) || (!watch->mon) || (!watch->handler)) return NULL;
|
2010-05-13 03:49:17 -07:00
|
|
|
|
2010-05-17 17:17:13 -07:00
|
|
|
udev = udev_monitor_get_udev(watch->mon);
|
|
|
|
udev_monitor_unref(watch->mon);
|
2010-05-17 17:18:45 -07:00
|
|
|
udev_unref(udev);
|
2010-05-17 17:17:13 -07:00
|
|
|
sdata = ecore_main_fd_handler_del(watch->handler);
|
2010-05-13 13:52:27 -07:00
|
|
|
if (sdata)
|
|
|
|
{
|
|
|
|
ret = sdata->data;
|
|
|
|
free(sdata);
|
|
|
|
}
|
2010-05-13 03:49:17 -07:00
|
|
|
free(watch);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|