BIG change, api break (again).

watches now return an int value that can be can be bitwise checked with & to reduce usage of strcmp
overall memory allocation should be less (I think), some related bugs fixed along the way
small memory leak in test app that I'm too lazy/blind to figure out
**this should be the last commit that causes an api break


SVN revision: 49118
This commit is contained in:
Mike Blumenkrantz 2010-05-22 01:55:14 +00:00
parent 9317ce25ac
commit 2e71c06192
9 changed files with 694 additions and 594 deletions

View File

@ -2,6 +2,13 @@
#include <Ecore.h>
#include <stdio.h>
/**
* This demo program shows how to use some eeze functions. It roughly
* 1kb as of now, TODO is to fix this but I'm too lazy now and it's only
* a demo.
*/
typedef struct kbdmouse
{
Eina_List *kbds;
@ -11,7 +18,7 @@ typedef struct kbdmouse
static void
/* event will always be a syspath starting with /sys */
catch_events(const char *device, const char *event, void *data, Eeze_Udev_Watch *watch)
catch_events(const char *device, int event, void *data, Eeze_Udev_Watch *watch)
{
kbdmouse *akbdmouse = data;
Eina_List *l;
@ -40,7 +47,7 @@ end:
* we can retrieve them now even though the device has been removed and
* is inaccessible to udev
*/
if (!strcmp(event, "add"))
if ((event & EEZE_UDEV_EVENT_ADD) == EEZE_UDEV_EVENT_ADD)
{
dev = eeze_udev_syspath_get_devpath(device);
type = "plugged in";
@ -52,19 +59,19 @@ end:
}
printf("You %s %s!\n", type, dev);
printf("All tests completed, exiting successfully!\n");
/* and the hash */
eina_hash_free(akbdmouse->hash);
/* now we free the lists */
eina_list_free(akbdmouse->kbds);
eina_list_free(akbdmouse->mice);
/* and the hash */
eina_hash_free(akbdmouse->hash);
/* and the random storage struct */
free(akbdmouse);
/* and quit the main loop */
ecore_main_loop_quit();
/* and delete the watch */
eeze_udev_watch_del(watch);
/* and shut down eudev */
eeze_udev_shutdown();
/* and quit the main loop */
ecore_main_loop_quit();
}
static void

View File

@ -88,9 +88,9 @@
/*bitmasks for watch events*/
#define EEZE_UDEV_EVENT_NONE 0x0000
#define EEZE_UDEV_EVENT_ADD 0x0001
#define EEZE_UDEV_EVENT_REMOVE 0x0001
#define EEZE_UDEV_EVENT_CHANGE 0x0002
#define EEZE_UDEV_EVENT_ONLINE 0x0004
#define EEZE_UDEV_EVENT_REMOVE 0x0002
#define EEZE_UDEV_EVENT_CHANGE 0x0004
#define EEZE_UDEV_EVENT_ONLINE 0x0008
#define EEZE_UDEV_EVENT_OFFLINE 0x0010
/*FIXME: these probably need to be bitmasks with categories*/
@ -143,7 +143,7 @@ extern "C" {
EAPI Eina_Bool eeze_udev_walk_check_sysattr(const char *syspath, const char *sysattr, const char *value);
EAPI const char *eeze_udev_walk_get_sysattr(const char *syspath, const char *sysattr);
EAPI Eeze_Udev_Watch *eeze_udev_watch_add(Eeze_Udev_Type type, int event, void(*func)(const char *, const char *, void *, Eeze_Udev_Watch *), void *user_data);
EAPI Eeze_Udev_Watch *eeze_udev_watch_add(Eeze_Udev_Type type, int event, void(*func)(const char *, int, void *, Eeze_Udev_Watch *), void *user_data);
EAPI void *eeze_udev_watch_del(Eeze_Udev_Watch *watch);
#ifdef __cplusplus

View File

@ -5,6 +5,8 @@
#include <Eeze_Udev.h>
#include "eeze_udev_private.h"
extern _udev *udev;
/**
* @defgroup udev udev
*
@ -16,7 +18,7 @@
*
* @param syspath The syspath of a device, with or without "/sys/"
* @return The syspath of the parent device
*
*
* Return a syspath (/sys/$syspath) for the parent device.
*
* @ingroup udev
@ -24,28 +26,24 @@
EAPI const char *
eeze_udev_syspath_get_parent(const char *syspath)
{
struct udev *udev;
struct udev_device *device, *parent;
_udev_device *device, *parent;
const char *ret;
Eina_Strbuf *sbuf;
if (!syspath) return NULL;
udev = udev_new();
if (!udev) return NULL;
if (!syspath)
return NULL;
sbuf = eina_strbuf_new();
if (!strstr(syspath, "/sys/"))
eina_strbuf_append(sbuf, "/sys/");
eina_strbuf_append(sbuf, syspath);
eina_strbuf_append(sbuf, syspath);
device = udev_device_new_from_syspath(udev, eina_strbuf_string_get(sbuf));
parent = udev_device_get_parent(device);
ret = eina_stringshare_add(udev_device_get_property_value(parent, "DEVPATH"));
udev_device_unref(device);
udev_unref(udev);
eina_strbuf_free(sbuf);
return ret;
}
@ -60,34 +58,33 @@ eeze_udev_syspath_get_parent(const char *syspath)
EAPI Eina_List *
eeze_udev_syspath_get_parents(const char *syspath)
{
struct udev *udev;
struct udev_device *child, *parent, *device;
_udev_device *child, *parent, *device;
const char *path;
Eina_Strbuf *sbuf;
Eina_List *devlist = NULL;
if (!syspath) return NULL;
udev = udev_new();
if (!udev) return NULL;
if (!syspath)
return NULL;
sbuf = eina_strbuf_new();
if (!strstr(syspath, "/sys/"))
eina_strbuf_append(sbuf, "/sys/");
eina_strbuf_append(sbuf, syspath);
eina_strbuf_append(sbuf, syspath);
device = udev_device_new_from_syspath(udev, eina_strbuf_string_get(sbuf));
if (!(parent = udev_device_get_parent(device)))
return NULL;
for (; parent; child = parent, parent = udev_device_get_parent(child))
{
path = udev_device_get_syspath(parent);
devlist = eina_list_append(devlist, eina_stringshare_add(path));
}
udev_device_unref(device);
udev_unref(udev);
return devlist;
for (; parent; child = parent, parent = udev_device_get_parent(child))
{
path = udev_device_get_syspath(parent);
devlist = eina_list_append(devlist, eina_stringshare_add(path));
}
udev_device_unref(device);
return devlist;
}
/**
@ -95,38 +92,36 @@ eeze_udev_syspath_get_parents(const char *syspath)
*
* @param syspath The /sys/ path with or without the /sys/
* @return A const char* with the /dev/ path or NULL on failure
*
*
* Takes /sys/$PATH and turns it into the corresponding "/dev/x/y".
*
*
* @ingroup udev
*/
EAPI const char *
eeze_udev_syspath_get_devpath(const char *syspath)
{
struct udev *udev;
struct udev_device *device;
const char *name = NULL;
Eina_Strbuf *sbuf;
_udev_device *device;
const char *name = NULL;
Eina_Strbuf *sbuf;
if (!syspath) return NULL;
udev = udev_new();
if (!udev) return NULL;
sbuf = eina_strbuf_new();
if (!strstr(syspath, "/sys/"))
eina_strbuf_append(sbuf, "/sys/");
eina_strbuf_append(sbuf, syspath);
if (!syspath)
return NULL;
device = udev_device_new_from_syspath(udev, eina_strbuf_string_get(sbuf));
if (!(name = udev_device_get_property_value(device, "DEVNAME")))
return NULL;
sbuf = eina_strbuf_new();
name = eina_stringshare_add(name);
udev_device_unref(device);
udev_unref(udev);
eina_strbuf_free(sbuf);
if (!strstr(syspath, "/sys/"))
eina_strbuf_append(sbuf, "/sys/");
return name;
eina_strbuf_append(sbuf, syspath);
device = udev_device_new_from_syspath(udev, eina_strbuf_string_get(sbuf));
if (!(name = udev_device_get_property_value(device, "DEVNAME")))
return NULL;
name = eina_stringshare_add(name);
udev_device_unref(device);
eina_strbuf_free(sbuf);
return name;
}
/**
@ -134,37 +129,34 @@ eeze_udev_syspath_get_devpath(const char *syspath)
*
* @param syspath The /sys/ path with or without the /sys/
* @return A const char* with the subsystem of the device or NULL on failure
*
*
* Takes /sys/$PATH and returns the corresponding device subsystem,
* such as "input" for keyboards/mice.
*
*
* @ingroup udev
*/
EAPI const char *
eeze_udev_syspath_get_subsystem(const char *syspath)
{
struct udev *udev;
struct udev_device *device;
const char *subsystem;
Eina_Strbuf *sbuf;
_udev_device *device;
const char *subsystem;
Eina_Strbuf *sbuf;
if (!syspath) return NULL;
udev = udev_new();
if (!udev) return NULL;
sbuf = eina_strbuf_new();
if (!strstr(syspath, "/sys/"))
eina_strbuf_append(sbuf, "/sys/");
eina_strbuf_append(sbuf, syspath);
if (!syspath)
return NULL;
device = udev_device_new_from_syspath(udev, eina_strbuf_string_get(sbuf));
subsystem = eina_stringshare_add(udev_device_get_property_value(device, "SUBSYSTEM"));
sbuf = eina_strbuf_new();
udev_device_unref(device);
udev_unref(udev);
eina_strbuf_free(sbuf);
if (!strstr(syspath, "/sys/"))
eina_strbuf_append(sbuf, "/sys/");
return subsystem;
eina_strbuf_append(sbuf, syspath);
device = udev_device_new_from_syspath(udev, eina_strbuf_string_get(sbuf));
subsystem =
eina_stringshare_add(udev_device_get_property_value(device, "SUBSYSTEM"));
udev_device_unref(device);
eina_strbuf_free(sbuf);
return subsystem;
}
/**
@ -173,35 +165,33 @@ eeze_udev_syspath_get_subsystem(const char *syspath)
* @param syspath The /sys/ path with or without the /sys/
* @param property The property to get; full list of these is a FIXME
* @return A const char* with the property or NULL on failure
*
*
* @ingroup udev
*/
EAPI const char *
eeze_udev_syspath_get_property(const char *syspath, const char *property)
{
struct udev *udev;
struct udev_device *device;
const char *value = NULL, *test;
Eina_Strbuf *sbuf;
_udev_device *device;
const char *value = NULL, *test;
Eina_Strbuf *sbuf;
if (!syspath || !property) return NULL;
udev = udev_new();
if (!udev) return NULL;
if (!syspath || !property)
return NULL;
sbuf = eina_strbuf_new();
if (!strstr(syspath, "/sys/"))
eina_strbuf_append(sbuf, "/sys/");
eina_strbuf_append(sbuf, syspath);
sbuf = eina_strbuf_new();
device = udev_device_new_from_syspath(udev, eina_strbuf_string_get(sbuf));
if ((test = udev_device_get_property_value(device, property)));
value = eina_stringshare_add(test);
if (!strstr(syspath, "/sys/"))
eina_strbuf_append(sbuf, "/sys/");
udev_device_unref(device);
udev_unref(udev);
eina_strbuf_free(sbuf);
eina_strbuf_append(sbuf, syspath);
device = udev_device_new_from_syspath(udev, eina_strbuf_string_get(sbuf));
return value;
if ((test = udev_device_get_property_value(device, property)))
value = eina_stringshare_add(test);
udev_device_unref(device);
eina_strbuf_free(sbuf);
return value;
}
/**
@ -210,35 +200,33 @@ eeze_udev_syspath_get_property(const char *syspath, const char *property)
* @param syspath The /sys/ path with or without the /sys/
* @param sysattr The sysattr to get; full list of these is a FIXME
* @return A const char* with the sysattr or NULL on failure
*
*
* @ingroup udev
*/
EAPI const char *
eeze_udev_syspath_get_sysattr(const char *syspath, const char *sysattr)
{
struct udev *udev;
struct udev_device *device;
const char *value = NULL, *test;
Eina_Strbuf *sbuf;
_udev_device *device;
const char *value = NULL, *test;
Eina_Strbuf *sbuf;
if (!syspath || !sysattr) return NULL;
udev = udev_new();
if (!udev) return NULL;
if (!syspath || !sysattr)
return NULL;
sbuf = eina_strbuf_new();
if (!strstr(syspath, "/sys/"))
eina_strbuf_append(sbuf, "/sys/");
eina_strbuf_append(sbuf, syspath);
sbuf = eina_strbuf_new();
device = udev_device_new_from_syspath(udev, eina_strbuf_string_get(sbuf));
if ((test = udev_device_get_sysattr_value(device, sysattr)));
value = eina_stringshare_add(test);
if (!strstr(syspath, "/sys/"))
eina_strbuf_append(sbuf, "/sys/");
udev_device_unref(device);
udev_unref(udev);
eina_strbuf_free(sbuf);
eina_strbuf_append(sbuf, syspath);
device = udev_device_new_from_syspath(udev, eina_strbuf_string_get(sbuf));
return value;
if ((test = udev_device_get_sysattr_value(device, sysattr)))
value = eina_stringshare_add(test);
udev_device_unref(device);
eina_strbuf_free(sbuf);
return value;
}
/**
@ -246,43 +234,40 @@ eeze_udev_syspath_get_sysattr(const char *syspath, const char *sysattr)
*
* @param devpath The /dev/ path of the device
* @return A const char* which corresponds to the /sys/ path of the device or NULL on failure
*
*
* Takes "/dev/path" and returns the corresponding /sys/ path (without the "/sys/")
*
*
* @ingroup udev
*/
EAPI const char *
eeze_udev_devpath_get_syspath(const char *devpath)
{
struct udev *udev;
struct udev_enumerate *en;
struct udev_list_entry *devs, *cur;
struct udev_device *device;
_udev_enumerate *en;
_udev_list_entry *devs, *cur;
_udev_device *device;
const char *name, *ret;
if (!devpath) return NULL;
udev = udev_new();
if (!udev) return NULL;
en = udev_enumerate_new(udev);
if (!en) return NULL;
if (!devpath)
return NULL;
en = udev_enumerate_new((udev));
if (!en)
return NULL;
udev_enumerate_add_match_property(en, "DEVNAME", devpath);
udev_enumerate_scan_devices(en);
devs = udev_enumerate_get_list_entry(en);
udev_list_entry_foreach(cur, devs)
{
name = udev_list_entry_get_name(cur);
device = udev_device_new_from_syspath(udev, name);
ret = eina_stringshare_add(udev_device_get_sysname(device));
udev_device_unref(device);
break; /*just in case there's more than one somehow*/
break; /*just in case there's more than one somehow */
}
udev_enumerate_unref(en);
udev_unref(udev);
return ret;
udev_enumerate_unref(en);
return ret;
}
/**
@ -290,43 +275,40 @@ eeze_udev_devpath_get_syspath(const char *devpath)
*
* @param devpath The /dev/ path of the device
* @return A const char* with the subsystem of the device or NULL on failure
*
*
* Takes "/dev/path" and returns the subsystem of the device.
*
*
* @ingroup udev
*/
EAPI const char *
eeze_udev_devpath_get_subsystem(const char *devpath)
{
struct udev *udev;
struct udev_enumerate *en;
struct udev_list_entry *devs, *cur;
struct udev_device *device;
_udev_enumerate *en;
_udev_list_entry *devs, *cur;
_udev_device *device;
const char *name, *ret;
if (!devpath) return NULL;
udev = udev_new();
if (!udev) return NULL;
en = udev_enumerate_new(udev);
if (!en) return NULL;
if (!devpath)
return NULL;
en = udev_enumerate_new((udev));
if (!en)
return NULL;
udev_enumerate_add_match_property(en, "DEVNAME", devpath);
udev_enumerate_scan_devices(en);
devs = udev_enumerate_get_list_entry(en);
udev_list_entry_foreach(cur, devs)
{
name = udev_list_entry_get_name(cur);
device = udev_device_new_from_syspath(udev, name);
ret = eina_stringshare_add(udev_device_get_subsystem(device));
udev_device_unref(device);
break; /*just in case there's more than one somehow*/
break; /*just in case there's more than one somehow */
}
udev_enumerate_unref(en);
udev_unref(udev);
return ret;
udev_enumerate_unref(en);
return ret;
}
/**
@ -334,46 +316,48 @@ eeze_udev_devpath_get_subsystem(const char *devpath)
*
* @param syspath The /sys/ path with or without the /sys/
* @return If true, the device is a mouse
*
*
* @ingroup udev
*/
EAPI Eina_Bool
eeze_udev_syspath_is_mouse(const char *syspath)
{
struct udev *udev;
struct udev_device *device;
Eina_Bool mouse = 0;
Eina_Strbuf *sbuf;
const char *test = NULL;
_udev_device *device;
Eina_Bool mouse = 0;
Eina_Strbuf *sbuf;
const char *test = NULL;
if (!syspath) return 0;
udev = udev_new();
if (!udev) return 0;
sbuf = eina_strbuf_new();
if (!strstr(syspath, "/sys/"))
eina_strbuf_append(sbuf, "/sys/");
eina_strbuf_append(sbuf, syspath);
if (!syspath)
return 0;
device = udev_device_new_from_syspath(udev, eina_strbuf_string_get(sbuf));
sbuf = eina_strbuf_new();
if (!strstr(syspath, "/sys/"))
eina_strbuf_append(sbuf, "/sys/");
eina_strbuf_append(sbuf, syspath);
device = udev_device_new_from_syspath(udev, eina_strbuf_string_get(sbuf));
#ifdef OLD_UDEV_RRRRRRRRRRRRRR
mouse = _walk_parents_test_attr(device, "bInterfaceProtocol", "02");
if (!mouse)
{
test = udev_device_get_property_value(device, "ID_CLASS");
if ((test) && (!strcmp(test, "mouse")))
mouse = 1;
}
mouse = _walk_parents_test_attr(device, "bInterfaceProtocol", "02");
if (!mouse)
{
test = udev_device_get_property_value(device, "ID_CLASS");
if ((test) && (!strcmp(test, "mouse")))
mouse = 1;
}
#else
test = udev_device_get_property_value(device, "ID_INPUT_MOUSE");
if (test) mouse = atoi(test);
test = udev_device_get_property_value(device, "ID_INPUT_MOUSE");
if (test)
mouse = atoi(test);
#endif
udev_device_unref(device);
udev_unref(udev);
eina_strbuf_free(sbuf);
return mouse;
udev_device_unref(device);
eina_strbuf_free(sbuf);
return mouse;
}
/**
@ -381,46 +365,48 @@ eeze_udev_syspath_is_mouse(const char *syspath)
*
* @param syspath The /sys/ path with or without the /sys/
* @return If true, the device is a keyboard
*
*
* @ingroup udev
*/
EAPI Eina_Bool
eeze_udev_syspath_is_kbd(const char *syspath)
{
struct udev *udev;
struct udev_device *device;
Eina_Bool kbd = 0;
Eina_Strbuf *sbuf;
const char *test = NULL;
_udev_device *device;
Eina_Bool kbd = 0;
Eina_Strbuf *sbuf;
const char *test = NULL;
if (!syspath) return 0;
udev = udev_new();
if (!udev) return 0;
sbuf = eina_strbuf_new();
if (!strstr(syspath, "/sys/"))
eina_strbuf_append(sbuf, "/sys/");
eina_strbuf_append(sbuf, syspath);
if (!syspath)
return 0;
device = udev_device_new_from_syspath(udev, eina_strbuf_string_get(sbuf));
sbuf = eina_strbuf_new();
if (!strstr(syspath, "/sys/"))
eina_strbuf_append(sbuf, "/sys/");
eina_strbuf_append(sbuf, syspath);
device = udev_device_new_from_syspath(udev, eina_strbuf_string_get(sbuf));
#ifdef OLD_UDEV_RRRRRRRRRRRRRR
kbd = _walk_parents_test_attr(device, "bInterfaceProtocol", "01");
if (!kbd)
{
test = udev_device_get_property_value(device, "ID_CLASS");
if ((test) && (!strcmp(test, "kbd")))
kbd = 1;
}
#else
test = udev_device_get_property_value(device, "ID_INPUT_KEYBOARD");
if (test) kbd = atoi(test);
kbd = _walk_parents_test_attr(device, "bInterfaceProtocol", "01");
if (!kbd)
{
test = udev_device_get_property_value(device, "ID_CLASS");
if ((test) && (!strcmp(test, "kbd")))
kbd = 1;
}
#else
test = udev_device_get_property_value(device, "ID_INPUT_KEYBOARD");
if (test)
kbd = atoi(test);
#endif
udev_device_unref(device);
udev_unref(udev);
eina_strbuf_free(sbuf);
return kbd;
udev_device_unref(device);
eina_strbuf_free(sbuf);
return kbd;
}
/**
@ -428,37 +414,37 @@ eeze_udev_syspath_is_kbd(const char *syspath)
*
* @param syspath The /sys/ path with or without the /sys/
* @return If true, the device is a touchpad
*
*
* @ingroup udev
*/
EAPI Eina_Bool
eeze_udev_syspath_is_touchpad(const char *syspath)
{
struct udev *udev;
struct udev_device *device;
Eina_Bool touchpad = 0;
Eina_Strbuf *sbuf;
_udev_device *device;
Eina_Bool touchpad = 0;
Eina_Strbuf *sbuf;
if (!syspath) return 0;
udev = udev_new();
if (!udev) return 0;
sbuf = eina_strbuf_new();
if (!strstr(syspath, "/sys/"))
eina_strbuf_append(sbuf, "/sys/");
eina_strbuf_append(sbuf, syspath);
if (!syspath)
return 0;
device = udev_device_new_from_syspath(udev, eina_strbuf_string_get(sbuf));
sbuf = eina_strbuf_new();
if (!strstr(syspath, "/sys/"))
eina_strbuf_append(sbuf, "/sys/");
eina_strbuf_append(sbuf, syspath);
device = udev_device_new_from_syspath(udev, eina_strbuf_string_get(sbuf));
#ifdef OLD_UDEV_RRRRRRRRRRRRRR
touchpad = _walk_parents_test_attr(device, "resolution", NULL);
touchpad = _walk_parents_test_attr(device, "resolution", NULL);
#else
const char *test;
test = udev_device_get_property_value(device, "ID_INPUT_TOUCHPAD");
if (test) touchpad = atoi(test);
#endif
udev_device_unref(device);
udev_unref(udev);
eina_strbuf_free(sbuf);
const char *test;
test = udev_device_get_property_value(device, "ID_INPUT_TOUCHPAD");
return touchpad;
if (test)
touchpad = atoi(test);
#endif
udev_device_unref(device);
eina_strbuf_free(sbuf);
return touchpad;
}

View File

@ -5,6 +5,7 @@
#include "eeze_udev_private.h"
#include <Eeze_Udev.h>
extern _udev *udev;
/**
* Returns a list of all syspaths that are (or should be) the same
@ -18,68 +19,67 @@
EAPI Eina_List *
eeze_udev_find_similar_from_syspath(const char *syspath)
{
struct udev *udev;
struct udev_device *device;
struct udev_list_entry *devs, *cur;
struct udev_enumerate *en;
_udev_device *device;
_udev_list_entry *devs, *cur;
_udev_enumerate *en;
Eina_List *l, *ret = NULL;
Eina_Strbuf *sbuf;
const char *vendor, *model, *revision, *devname, *dev;
if (!syspath) return NULL;
udev = udev_new();
if (!udev) return NULL;
en = udev_enumerate_new(udev);
if (!syspath)
return NULL;
en = udev_enumerate_new((udev));
if (!en)
{
udev_unref(udev);
return NULL;
}
return NULL;
sbuf = eina_strbuf_new();
if (!strstr(syspath, "/sys/"))
eina_strbuf_append(sbuf, "/sys/");
eina_strbuf_append(sbuf, syspath);
eina_strbuf_append(sbuf, syspath);
device = udev_device_new_from_syspath(udev, syspath);
vendor = udev_device_get_property_value(device, "ID_VENDOR_ID");
if (vendor)
udev_enumerate_add_match_property(en, "ID_VENDOR_ID", vendor);
model = udev_device_get_property_value(device, "ID_MODEL_ID");
if (model)
udev_enumerate_add_match_property(en, "ID_MODEL_ID", model);
revision = udev_device_get_property_value(device, "ID_REVISION");
if (revision)
udev_enumerate_add_match_property(en, "ID_REVISION", revision);
udev_enumerate_scan_devices(en);
udev_device_unref(device);
devs = udev_enumerate_get_list_entry(en);
udev_list_entry_foreach(cur, devs)
{
devname = udev_list_entry_get_name(cur);
/* verify unlisted device */
EINA_LIST_FOREACH(ret, l, dev)
if (!strcmp(dev, devname))
continue;
/* verify unlisted device */
EINA_LIST_FOREACH(ret, l, dev) if (!strcmp(dev, devname))
continue;
ret = eina_list_prepend(ret, eina_stringshare_add(devname));
device = udev_device_new_from_syspath(udev, devname);
/* only device roots have this sysattr,
* and we only need to check parents of the roots
*/
/* only device roots have this sysattr,
* and we only need to check parents of the roots
*/
if (udev_device_get_sysattr_value(device, "idVendor"))
_get_unlisted_parents(ret, device);
_get_unlisted_parents(ret, device);
udev_device_unref(device);
}
}
udev_enumerate_unref(en);
udev_unref(udev);
eina_strbuf_free(sbuf);
return ret;
}
@ -95,42 +95,43 @@ eeze_udev_find_similar_from_syspath(const char *syspath)
* @ingroup udev
*/
EAPI void
eeze_udev_find_unlisted_similar(Eina_List *list)
eeze_udev_find_unlisted_similar(Eina_List * list)
{
struct udev *udev;
struct udev_device *device;
struct udev_list_entry *devs, *cur;
struct udev_enumerate *en;
_udev_device *device;
_udev_list_entry *devs, *cur;
_udev_enumerate *en;
Eina_List *l;
const char *vendor, *model, *revision, *devname, *dev;
if (!list) return;
udev = udev_new();
if (!udev) return;
if (!list)
return;
EINA_LIST_FOREACH(list, l, dev)
{
en = udev_enumerate_new(udev);
if (!en)
{
udev_unref(udev);
return;
}
en = udev_enumerate_new((udev));
if (!en)
return;
device = udev_device_new_from_syspath(udev, dev);
if ((vendor = udev_device_get_property_value(device, "ID_VENDOR_ID")))
udev_enumerate_add_match_property(en, "ID_VENDOR_ID", vendor);
else if ((vendor = udev_device_get_property_value(device, "ID_VENDOR")))
udev_enumerate_add_match_property(en, "ID_VENDOR", vendor);
else
if ((vendor = udev_device_get_property_value(device, "ID_VENDOR")))
udev_enumerate_add_match_property(en, "ID_VENDOR", vendor);
if ((model = udev_device_get_property_value(device, "ID_MODEL_ID")))
udev_enumerate_add_match_property(en, "ID_MODEL_ID", model);
else if ((model = udev_device_get_property_value(device, "ID_MODEL")))
udev_enumerate_add_match_property(en, "ID_MODEL", model);
else
if ((model = udev_device_get_property_value(device, "ID_MODEL")))
udev_enumerate_add_match_property(en, "ID_MODEL", model);
if ((revision = udev_device_get_property_value(device, "ID_REVISION")))
udev_enumerate_add_match_property(en, "ID_REVISION", revision);
udev_enumerate_scan_devices(en);
udev_device_unref(device);
devs = udev_enumerate_get_list_entry(en);
udev_list_entry_foreach(cur, devs)
{
@ -144,11 +145,9 @@ eeze_udev_find_unlisted_similar(Eina_List *list)
_get_unlisted_parents(list, device);
udev_device_unref(device);
}
}
udev_enumerate_unref(en);
}
udev_unref(udev);
return;
}
@ -158,7 +157,7 @@ eeze_udev_find_unlisted_similar(Eina_List *list)
* @param type A Eeze_Udev_Type or 0
* @param name A filter for the device name or NULL
* @return A Eina_List* of matched devices or NULL on failure
*
*
* Return a list of syspaths (/sys/$syspath) for matching udev devices.
*
* @ingroup udev
@ -166,19 +165,19 @@ eeze_udev_find_unlisted_similar(Eina_List *list)
EAPI Eina_List *
eeze_udev_find_by_type(const Eeze_Udev_Type etype, const char *name)
{
struct udev *udev;
struct udev_enumerate *en;
struct udev_list_entry *devs, *cur;
struct udev_device *device, *parent;
_udev_enumerate *en;
_udev_list_entry *devs, *cur;
_udev_device *device, *parent;
const char *devname, *test;
Eina_List *ret = NULL;
if ((!etype) && (!name)) return NULL;
if ((!etype) && (!name))
return NULL;
udev = udev_new();
if (!udev) return NULL;
en = udev_enumerate_new(udev);
if (!en) return NULL;
en = udev_enumerate_new((udev));
if (!en)
return NULL;
switch (etype)
{
@ -236,67 +235,68 @@ eeze_udev_find_by_type(const Eeze_Udev_Type etype, const char *name)
case EEZE_UDEV_TYPE_IS_IT_HOT_OR_IS_IT_COLD_SENSOR:
udev_enumerate_add_match_subsystem(en, "hwmon");
break;
/*
case EEZE_UDEV_TYPE_ANDROID:
udev_enumerate_add_match_subsystem(en, "block");
udev_enumerate_add_match_property(en, "ID_MODEL", "Android_*");
break;
*/
/*
case EEZE_UDEV_TYPE_ANDROID:
udev_enumerate_add_match_subsystem(en, "block");
udev_enumerate_add_match_property(en, "ID_MODEL", "Android_*");
break;
*/
default:
break;
}
udev_enumerate_scan_devices(en);
devs = udev_enumerate_get_list_entry(en);
udev_list_entry_foreach(cur, devs)
{
devname = udev_list_entry_get_name(cur);
device = udev_device_new_from_syspath(udev, devname);
if (etype == EEZE_UDEV_TYPE_IS_IT_HOT_OR_IS_IT_COLD_SENSOR)
{/* ensure that temp input exists somewhere in this device chain */
if (!_walk_parents_test_attr(device, "temp1_input", NULL))
goto out;
/* 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")))
{
devname = 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"))))
{
devname = udev_device_get_syspath(parent);
break;
}
{ /* ensure that temp input exists somewhere in this device chain */
if (!_walk_parents_test_attr(device, "temp1_input", NULL))
goto out;
if (!devname)
goto out;
}
/* 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")))
{
devname = 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"))))
{
devname = udev_device_get_syspath(parent);
break;
}
if (!devname)
goto out;
}
}
else if (etype == EEZE_UDEV_TYPE_DRIVE_INTERNAL)
{
if (udev_device_get_property_value(device, "ID_USB_DRIVER"))
goto out;
}
else if (etype == EEZE_UDEV_TYPE_DRIVE_REMOVABLE)
{
if (!(test = udev_device_get_property_value(device, "ID_USB_DRIVER")))
goto out;
}
else
if (etype == EEZE_UDEV_TYPE_DRIVE_INTERNAL)
{
if (udev_device_get_property_value(device, "ID_USB_DRIVER"))
goto out;
}
else
if (etype == EEZE_UDEV_TYPE_DRIVE_REMOVABLE)
{
if (!
(test = udev_device_get_property_value(device, "ID_USB_DRIVER")))
goto out;
}
if (name)
if (!strstr(devname, name))
goto out;
if (!strstr(devname, name))
goto out;
ret = eina_list_append(ret, eina_stringshare_add(devname));
out:
udev_device_unref(device);
}
udev_enumerate_unref(en);
udev_unref(udev);
return ret;
}
udev_enumerate_unref(en);
return ret;
}
/**
@ -306,28 +306,29 @@ out:
* @param type "ID_INPUT_KEY", "ID_INPUT_MOUSE", "ID_INPUT_TOUCHPAD", NULL, etc
* @param name A filter for the device name, or NULL
* @return A Eina_List* of matched devices or NULL on failure
*
*
* Return a list of syspaths (/sys/$syspath) for matching udev devices.
* Requires at least one filter.
*
* @ingroup udev
*/
EAPI Eina_List *
eeze_udev_find_by_filter(const char *subsystem, const char *type, const char *name)
eeze_udev_find_by_filter(const char *subsystem, const char *type,
const char *name)
{
struct udev *udev;
struct udev_enumerate *en;
struct udev_list_entry *devs, *cur;
struct udev_device *device;
_udev_enumerate *en;
_udev_list_entry *devs, *cur;
_udev_device *device;
const char *devname;
Eina_List *ret = NULL;
if ((!subsystem) && (!type) && (!name)) return NULL;
if ((!subsystem) && (!type) && (!name))
return NULL;
udev = udev_new();
if (!udev) return NULL;
en = udev_enumerate_new(udev);
if (!en) return NULL;
en = udev_enumerate_new((udev));
if (!en)
return NULL;
if (subsystem)
udev_enumerate_add_match_subsystem(en, subsystem);
@ -335,67 +336,58 @@ eeze_udev_find_by_filter(const char *subsystem, const char *type, const char *na
udev_enumerate_add_match_property(en, type, "1");
udev_enumerate_scan_devices(en);
devs = udev_enumerate_get_list_entry(en);
udev_list_entry_foreach(cur, devs)
{
devname = udev_list_entry_get_name(cur);
device = udev_device_new_from_syspath(udev, devname);
if (name)
if (!strstr(devname,name))
goto out;
if (!strstr(devname, name))
goto out;
ret = eina_list_append(ret, eina_stringshare_add(devname));
out:
udev_device_unref(device);
}
udev_enumerate_unref(en);
udev_unref(udev);
return ret;
}
udev_enumerate_unref(en);
return ret;
}
/**
* Find a list of devices by a sysattr (and, optionally, a value of that sysattr).
*
*
* @param sysattr The attribute to find
* @param value Optional: the value that the attribute should have
*
*
* @return A list of the devices found with the attribute
*/
EAPI Eina_List *
eeze_udev_find_by_sysattr(const char *sysattr, const char *value)
{
struct udev *udev;
struct udev_enumerate *en;
struct udev_list_entry *devs, *cur;
struct udev_device *device;
_udev_enumerate *en;
_udev_list_entry *devs, *cur;
_udev_device *device;
const char *devname;
Eina_List *ret = NULL;
if (!sysattr) return NULL;
if (!sysattr)
return NULL;
udev = udev_new();
if (!udev) return NULL;
en = udev_enumerate_new(udev);
if (!en) return NULL;
en = udev_enumerate_new((udev));
if (!en)
return NULL;
udev_enumerate_add_match_sysattr(en, sysattr, value);
udev_enumerate_scan_devices(en);
devs = udev_enumerate_get_list_entry(en);
udev_list_entry_foreach(cur, devs)
{
devname = udev_list_entry_get_name(cur);
device = udev_device_new_from_syspath(udev, devname);
ret = eina_list_append(ret, eina_stringshare_add(devname));
udev_device_unref(device);
}
udev_enumerate_unref(en);
udev_unref(udev);
return ret;
}
udev_enumerate_unref(en);
return ret;
}

View File

@ -1,44 +1,56 @@
#include "eeze_udev_private.h"
#include <Eeze_Udev.h>
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
int _e_eeze_udev_log_dom = -1;
int _e_eeze_udev_init_count = 0;
#include <Eeze_Udev.h>
#include "eeze_udev_private.h"
_udev *udev;
int _eeze_udev_log_dom = -1;
int _eeze_udev_init_count = 0;
EAPI int
eeze_udev_init(void)
{
if (++_e_eeze_udev_init_count != 1)
return _e_eeze_udev_init_count;
if (++_eeze_udev_init_count != 1)
return _eeze_udev_init_count;
if (!eina_init())
return --_e_eeze_udev_init_count;
return --_eeze_udev_init_count;
_e_eeze_udev_log_dom = eina_log_domain_register
("eeze_udev", E_EEZE_COLOR_DEFAULT);
if (_e_eeze_udev_log_dom < 0)
_eeze_udev_log_dom = eina_log_domain_register
("eeze_udev", eeze_COLOR_DEFAULT);
if (_eeze_udev_log_dom < 0)
{
EINA_LOG_ERR("Could not register 'eeze_udev' log domain.");
goto fail;
}
return _e_eeze_udev_init_count;
if (!((udev) = udev_new()))
{
EINA_LOG_ERR("Could not initialize udev library!");
goto fail;
}
return _eeze_udev_init_count;
fail:
eina_log_domain_unregister(_e_eeze_udev_log_dom);
_e_eeze_udev_log_dom = -1;
eina_shutdown();
return _e_eeze_udev_init_count;
eina_log_domain_unregister(_eeze_udev_log_dom);
_eeze_udev_log_dom = -1;
eina_shutdown();
return _eeze_udev_init_count;
}
EAPI int
eeze_udev_shutdown(void)
{
if (--_e_eeze_udev_init_count != 0)
return _e_eeze_udev_init_count;
if (--_eeze_udev_init_count != 0)
return _eeze_udev_init_count;
eina_log_domain_unregister(_e_eeze_udev_log_dom);
_e_eeze_udev_log_dom = -1;
udev_unref(udev);
eina_log_domain_unregister(_eeze_udev_log_dom);
_eeze_udev_log_dom = -1;
eina_shutdown();
return _e_eeze_udev_init_count;
return _eeze_udev_init_count;
}

View File

@ -5,42 +5,65 @@
#include <Eeze_Udev.h>
#include "eeze_udev_private.h"
extern _udev *udev;
/**
* copies a device
*/
_udev_device *
_copy_device(_udev_device * device)
{
const char *syspath;
if (!(syspath = udev_device_get_syspath(device)))
return NULL;
return udev_device_new_from_syspath(udev, syspath);
}
/**
* private function to simulate udevadm info -a
* walks up the device tree checking each node for sysattr
* with value $value
*/
Eina_Bool
_walk_parents_test_attr(struct udev_device *device, const char *sysattr, const char* value)
_walk_parents_test_attr(_udev_device * device, const char *sysattr,
const char *value)
{
struct udev_device *parent, *child = device;
_udev_device *parent, *child = device;
const char *test;
if ((test = udev_device_get_sysattr_value(device, sysattr)))
return 1;
parent = udev_device_get_parent(child);
for (; parent; child = parent, parent = udev_device_get_parent(child))
{
if (!(test = udev_device_get_sysattr_value(parent, sysattr)))
continue;
if (!value)
return 1;
else if (!strcmp(test, value))
return 1;
else
if (!strcmp(test, value))
return 1;
}
return 0;
}
const char *
_walk_parents_get_attr(struct udev_device *device, const char *sysattr)
_walk_parents_get_attr(_udev_device * device, const char *sysattr)
{
struct udev_device *parent, *child = device;
_udev_device *parent, *child = device;
const char *test;
if ((test = udev_device_get_sysattr_value(device, sysattr)))
return eina_stringshare_add(test);
parent = udev_device_get_parent(child);
for (; parent; child = parent, parent = udev_device_get_parent(child))
{
if ((test = udev_device_get_sysattr_value(parent, sysattr)))
@ -55,30 +78,39 @@ _walk_parents_get_attr(struct udev_device *device, const char *sysattr)
* stringshare adding all devices that are not in the list
*/
void
_get_unlisted_parents(Eina_List *list, struct udev_device *device)
_get_unlisted_parents(Eina_List * list, _udev_device * device)
{
struct udev_device *parent, *child = device;
const char *test, *devname, *vendor, *vendor2, *model, *model2;
Eina_List *l;
Eina_Bool found;
_udev_device *parent, *child = device;
const char *test, *devname, *vendor, *vendor2, *model, *model2;
Eina_List *l;
Eina_Bool found;
if (!(vendor = udev_device_get_property_value(child, "ID_VENDOR_ID")))
vendor = udev_device_get_property_value(child, "ID_VENDOR");
if (!(model = udev_device_get_property_value(child, "ID_MODEL_ID")))
model = udev_device_get_property_value(child, "ID_MODEL");
parent = udev_device_get_parent(child);
for (; parent; child = parent, parent = udev_device_get_parent(child))
{
found = 0;
if (!(vendor2 = udev_device_get_property_value(child, "ID_VENDOR_ID")))
vendor2 = udev_device_get_property_value(child, "ID_VENDOR");
if (!(model2 = udev_device_get_property_value(child, "ID_MODEL_ID")))
model2 = udev_device_get_property_value(child, "ID_MODEL");
if ((!model2 && model) || (model2 && !model) || (!vendor2 && vendor) || (vendor2 && !vendor))
break;
else if (((model && model2) && (strcmp(model, model2))) ||
((vendor && vendor2) && (strcmp(vendor, vendor2))))
if ((!model2 && model) || (model2 && !model) || (!vendor2 && vendor)
|| (vendor2 && !vendor))
break;
else
if (((model && model2) && (strcmp(model, model2))) ||
((vendor && vendor2) && (strcmp(vendor, vendor2))))
break;
devname = udev_device_get_syspath(parent);
EINA_LIST_FOREACH(list, l, test)
{
@ -88,8 +120,10 @@ _get_unlisted_parents(Eina_List *list, struct udev_device *device)
break;
}
}
if (!found)
list = eina_list_prepend(list, eina_stringshare_add(devname));
}
return;
}

View File

@ -1,10 +1,10 @@
#ifndef EEZE_UDEV_PRIVATE_H
#define EEZE_UDEV_PRIVATE_H
#include "Eeze_Udev.h"
#ifndef E_EEZE_COLOR_DEFAULT
#define E_EEZE_COLOR_DEFAULT EINA_COLOR_CYAN
#ifndef eeze_COLOR_DEFAULT
#define eeze_COLOR_DEFAULT EINA_COLOR_CYAN
#endif
extern int _e_eeze_udev_log_dom;
extern int _eeze_udev_log_dom;
#ifdef ERR
#undef ERR
#endif
@ -18,13 +18,20 @@ extern int _e_eeze_udev_log_dom;
#undef DBG
#endif