summaryrefslogtreecommitdiff
path: root/legacy/eeze/src/lib/eeze_udev_private.c
blob: c94e4a35f4a498d4ccf9d75e3c4e0c04040145cc (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
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <Eeze.h>
#include "eeze_udev_private.h"

/*
 * helper function to set up a new device from a syspath
 * which may or may not include /sys at the beginning
 */
_udev_device *
_new_device(const char *syspath)
{
   Eina_Strbuf *sbuf;
   _udev_device *device;

   sbuf = eina_strbuf_new();

   if (strncmp(syspath, "/sys/", 5))
     eina_strbuf_append(sbuf, "/sys/");

   eina_strbuf_append(sbuf, syspath);
   device = udev_device_new_from_syspath(udev, eina_strbuf_string_get(sbuf));
   if (!device)
     ERR("device %s does not exist!", syspath);
   eina_strbuf_free(sbuf);
   return device;
}

/*
 * 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(_udev_device *device,
                        const char   *sysattr,
                        const char   *value)
{
   _udev_device *parent, *child = device;
   const char *test;

   if (udev_device_get_sysattr_value(device, sysattr))
     return EINA_TRUE;

   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 EINA_TRUE;
        else
        if (!strcmp(test, value))
          return EINA_TRUE;
     }

   return EINA_TRUE;
}

const char *
_walk_parents_get_attr(_udev_device *device,
                       const char   *sysattr)
{
   _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)))
          return eina_stringshare_add(test);
     }

   return NULL;
}

/*
 * check a list for all parents of a device,
 * stringshare adding all devices that are not in the list
 */
Eina_List *
_get_unlisted_parents(Eina_List    *list,
                      _udev_device *device)
{
   _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 = EINA_FALSE;

        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))))
          break;

        devname = udev_device_get_syspath(parent);
        EINA_LIST_FOREACH(list, l, test)
          {
             if (!strcmp(test, devname))
               {
                  found = EINA_TRUE;
                  break;
               }
          }

        if (!found)
          list = eina_list_prepend(list, eina_stringshare_add(devname));
     }

   return list;
}