summaryrefslogtreecommitdiff
path: root/legacy/eeze/src/bin/eeze_udev_test.c
blob: 41e17a57206803aa0608f0235eebb1e82a296416 (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
#include <E_Udev.h>
#include <Ecore.h>
#include <stdio.h>

typedef struct kbdmouse
{
   Eina_List *kbds;
   Eina_List *mice;
   Eina_Hash *hash;
} kbdmouse;

static void
/* event will always be a syspath starting with /sys */
catch_events(const char *event, void *data, Eudev_Watch *watch)
{
   kbdmouse *akbdmouse = data;
   Eina_List *l;
   const char *name, *dev, *type;
   int new = 0;

   /* the event that comes through will be prefixed by "/sys"
    * but the saved name will not, so we check for the saved name
    * inside the event name
    */
   EINA_LIST_FOREACH(akbdmouse->kbds, l, name)
     if (strstr(event, name)) goto end;
   EINA_LIST_FOREACH(akbdmouse->mice, l, name)
     if (strstr(event, name)) goto end;

   /* check to see if the device was just plugged in */
   if (e_udev_syspath_is_kbd(event) || e_udev_syspath_is_mouse(event))
   {
      new = 1;
      goto end;
   }
   /* if we reach here, the event is neither a keyboard nor a mouse that we saw
    * previously, so we print a moderately amusing message and bail
    */
   printf("Sneaky sneaky!  But %s is not a keyboard or a mouse!!\n", event);
   return;

end:
   /* we stored the devpaths for all the syspaths previously so that
    * we can retrieve them now even though the device has been removed and
    * is inaccessible to udev
    */
   if (new)
     {
        dev = e_udev_syspath_get_devpath(event);
        type = "plugged in";
     }
   else
     {
        dev = eina_hash_find(akbdmouse->hash, name);
        type = "unplugged";
     }
   printf("You %s %s!\n", type, dev);
   printf("All tests completed, exiting successfully!\n");
   /* 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 */
   e_udev_watch_del(watch);
   /* and shut down eudev */
   e_udev_shutdown();
}

static void
hash_free(void *data)
{
   eina_stringshare_del(data);
}

int main()
{
   Eina_List *type, *l;
   const char *name, *check;
   kbdmouse *akbdmouse;
   Eina_Hash *hash;

   ecore_init();
   e_udev_init();

   hash = eina_hash_stringshared_new(hash_free);
   akbdmouse = malloc(sizeof(kbdmouse));
   akbdmouse->hash = hash;
   
   printf("For my first trick, I will find all of your keyboards and return their syspaths.\n");
   /* find all keyboards using type EUDEV_TYPE_KEYBOARD */
   type = e_udev_find_by_type(EUDEV_TYPE_KEYBOARD, NULL);
   EINA_LIST_FOREACH(type, l, name)
     {  /* add the devpath to the hash for use in the cb later */
        eina_hash_direct_add(hash, name, e_udev_syspath_get_devpath(name));
        printf("Found keyboard: %s\n", name);
     }
   /* we save this list for later, because once a device is unplugged it can
    * no longer be detected by udev, and any related properties are unusable unless
    * they have been previously stored
    */
   akbdmouse->kbds = type;

   printf("\nNext, I will find all of your mice and print the corresponding manufacturer.\n");
   /* find all mice using type EUDEV_TYPE_MOUSE */
   type = e_udev_find_by_type(EUDEV_TYPE_MOUSE, NULL);
   EINA_LIST_FOREACH(type, l, name)
     {  /* add the devpath to the hash for use in the cb later */
        eina_hash_direct_add(hash, name, e_udev_syspath_get_devpath(name)); /* get a property using the device's syspath */
        printf("Found mouse %s with vendor: %s\n", name, e_udev_syspath_get_property(name, "ID_VENDOR"));
     }
   /* we save this list for later, because once a device is unplugged it can
    * no longer be detected by udev, and any related properties are unusable unless
    * they have been previously stored
    */
   akbdmouse->mice = type;

   printf("\nNow let's try something a little more difficult.  Mountable filesystems!\n");
   /* find all mountable drives using type EUDEV_TYPE_DRIVE_MOUNTABLE */
   type = e_udev_find_by_type(EUDEV_TYPE_DRIVE_MOUNTABLE, NULL);
   EINA_LIST_FOREACH(type, l, name)
   {
     printf("Found device: %s\n", name);  /* get a property using the device's syspath */
     printf("\tYou probably know it better as %s\n", e_udev_syspath_get_property(name, "DEVNAME"));
     printf("\tIt's formatted as %s", e_udev_syspath_get_property(name, "ID_FS_TYPE"));
     check = e_udev_syspath_get_property(name, "FSTAB_DIR");
     if (check)
       printf(", and gets mounted at %s", check);
     printf("!\n");
   }
   eina_list_free(type);

   printf("\nInternal drives, anyone?  With serial numbers?\n");
   /* find all internal drives using type EUDEV_TYPE_DRIVE_INTERNAL */
   type = e_udev_find_by_type(EUDEV_TYPE_DRIVE_INTERNAL, NULL);
   EINA_LIST_FOREACH(type, l, name) /* get a property using the device's syspath */
        printf("%s: %s\n", name, e_udev_syspath_get_property(name, "ID_SERIAL"));
   eina_list_free(type);

   printf("\nGot any removables?  I'm gonna find em!\n");
   /* find all removable media using type EUDEV_TYPE_DRIVE_REMOVABLE */
   type = e_udev_find_by_type(EUDEV_TYPE_DRIVE_REMOVABLE, NULL);
   EINA_LIST_FOREACH(type, l, name)  /* get a property using the device's syspath */
     printf("\tOoh, a %s attached on your %s bus!\n", e_udev_syspath_get_property(name, "ID_MODEL"),
       e_udev_syspath_get_property(name, "ID_BUS"));
   eina_list_free(type);


   /* set a udev watch, grab all events because no EUDEV_TYPE filter is specified,
    * set the events to be sent to callback function catch_events(), and attach
    * kbdmouse to the watch as associated data
    */
   e_udev_watch_add(EUDEV_TYPE_NONE, catch_events, akbdmouse);
   printf("\nAnd now for something more complicated.  Plug or unplug your keyboard or mouse for me.\n");

   /* main loop must be started to use ecore fd polling */
   ecore_main_loop_begin();
}