commit 4391680
Michael Forney
·
2013-09-14 03:54:42 +0000 UTC
parent 1893d92
evdev_device: Use new/destroy instead of initialize/finish
3 files changed,
+32,
-48
+17,
-12
1@@ -133,32 +133,34 @@ static int handle_data(int fd, uint32_t mask, void * data)
2 return 1;
3 }
4
5-bool swc_evdev_device_initialize(struct swc_evdev_device * device,
6- const char * path,
7- const struct swc_evdev_device_handler * handler)
8+struct swc_evdev_device * swc_evdev_device_new
9+ (const char * path, const struct swc_evdev_device_handler * handler)
10 {
11+ struct swc_evdev_device * device;
12 uint32_t index;
13
14+ if (!(device = malloc(sizeof *device)))
15+ goto error0;
16+
17 device->fd = open(path, O_RDWR | O_NONBLOCK | O_CLOEXEC);
18- memset(&device->motion, 0, sizeof device->motion);
19
20 if (device->fd == -1)
21 {
22 printf("couldn't open input device at %s\n", path);
23- goto error_base;
24+ goto error0;
25 }
26
27 if (libevdev_new_from_fd(device->fd, &device->dev) != 0)
28 {
29 fprintf(stderr, "Could not create libevdev device\n");
30- goto error_fd;
31+ goto error1;
32 }
33
34 printf("Adding device %s\n", libevdev_get_name(device->dev));
35
36 device->handler = handler;
37 device->capabilities = 0;
38- /* XXX: touch devices */
39+ memset(&device->motion, 0, sizeof device->motion);
40
41 if (libevdev_has_event_code(device->dev, EV_KEY, KEY_ENTER))
42 {
43@@ -174,19 +176,22 @@ bool swc_evdev_device_initialize(struct swc_evdev_device * device,
44 printf("\tthis device is a pointer\n");
45 }
46
47- return true;
48+ /* XXX: touch devices */
49+
50+ return device;
51
52- error_fd:
53+ error1:
54 close(device->fd);
55- error_base:
56- return false;
57+ error0:
58+ return NULL;
59 }
60
61-void swc_evdev_device_finish(struct swc_evdev_device * device)
62+void swc_evdev_device_destroy(struct swc_evdev_device * device)
63 {
64 wl_event_source_remove(device->source);
65 libevdev_free(device->dev);
66 close(device->fd);
67+ free(device);
68 }
69
70 void swc_evdev_device_add_event_sources(struct swc_evdev_device * device,
+3,
-4
1@@ -53,11 +53,10 @@ struct swc_evdev_device
2 struct wl_list link;
3 };
4
5-bool swc_evdev_device_initialize
6- (struct swc_evdev_device * device, const char * path,
7- const struct swc_evdev_device_handler * handler);
8+struct swc_evdev_device * swc_evdev_device_new
9+ (const char * path, const struct swc_evdev_device_handler * handler);
10
11-void swc_evdev_device_finish(struct swc_evdev_device * device);
12+void swc_evdev_device_destroy(struct swc_evdev_device * device);
13
14 void swc_evdev_device_add_event_sources(struct swc_evdev_device * device,
15 struct wl_event_loop * event_loop);
+12,
-32
1@@ -9,14 +9,6 @@
2 #include <stdio.h>
3 #include <string.h>
4
5-struct evdev_device_entry
6-{
7- struct swc_evdev_device device;
8- struct wl_listener event_listener;
9- struct swc_seat * seat;
10- struct wl_list link;
11-};
12-
13 static void handle_key(const struct swc_evdev_device_handler * handler,
14 uint32_t time, uint32_t key, uint32_t state)
15 {
16@@ -160,7 +152,6 @@ static void add_device(struct swc_seat * seat, struct udev_device * udev_device)
17 const char * device_seat;
18 const char * device_path;
19 struct swc_evdev_device * device;
20- struct evdev_device_entry * entry;
21
22 device_seat = udev_device_get_property_value(udev_device, "ID_SEAT");
23
24@@ -172,31 +163,21 @@ static void add_device(struct swc_seat * seat, struct udev_device * udev_device)
25 return;
26
27 device_path = udev_device_get_devnode(udev_device);
28+ device = swc_evdev_device_new(device_path, &seat->evdev_handler);
29
30- entry = malloc(sizeof *entry);
31-
32- if (!entry)
33- {
34- printf("could not allocate evdev device\n");
35- return;
36- }
37-
38- entry->seat = seat;
39-
40- if (!swc_evdev_device_initialize(&entry->device, device_path,
41- &seat->evdev_handler))
42+ if (!device)
43 {
44- free(entry);
45+ fprintf(stderr, "Could not create evdev device\n");
46 return;
47 }
48
49- if (~seat->capabilities & entry->device.capabilities)
50+ if (~seat->capabilities & device->capabilities)
51 {
52- seat->capabilities |= entry->device.capabilities;
53+ seat->capabilities |= device->capabilities;
54 update_capabilities(seat);
55 }
56
57- wl_list_insert(&seat->devices, &entry->link);
58+ wl_list_insert(&seat->devices, &device->link);
59 }
60
61 bool swc_seat_initialize(struct swc_seat * seat, struct udev * udev,
62@@ -253,7 +234,7 @@ bool swc_seat_initialize(struct swc_seat * seat, struct udev * udev,
63
64 void swc_seat_finish(struct swc_seat * seat)
65 {
66- struct evdev_device_entry * entry, * tmp;
67+ struct swc_evdev_device * device, * tmp;
68
69 wl_signal_emit(&seat->destroy_signal, seat);
70
71@@ -262,10 +243,9 @@ void swc_seat_finish(struct swc_seat * seat)
72
73 free(seat->name);
74
75- wl_list_for_each_safe(entry, tmp, &seat->devices, link)
76+ wl_list_for_each_safe(device, tmp, &seat->devices, link)
77 {
78- swc_evdev_device_finish(&entry->device);
79- free(entry);
80+ swc_evdev_device_destroy(device);
81 }
82 }
83
84@@ -277,11 +257,11 @@ void swc_seat_add_globals(struct swc_seat * seat, struct wl_display * display)
85 void swc_seat_add_event_sources(struct swc_seat * seat,
86 struct wl_event_loop * event_loop)
87 {
88- struct evdev_device_entry * entry;
89+ struct swc_evdev_device * device;
90
91- wl_list_for_each(entry, &seat->devices, link)
92+ wl_list_for_each(device, &seat->devices, link)
93 {
94- swc_evdev_device_add_event_sources(&entry->device, event_loop);
95+ swc_evdev_device_add_event_sources(device, event_loop);
96 }
97 }
98