freemyipod r503 - Code Review

Jump to: navigation, search
Repository:freemyipod
Revision:r502‎ | r503 | r504 >
Date:22:44, 1 February 2011
Author:theseven
Status:new
Tags:
Comment:
emCORE: Rework button event handling
Modified paths:
  • /emcore/trunk/button.c (modified) (history)
  • /emcore/trunk/button.h (modified) (history)
  • /emcore/trunk/target/ipodnano3g/clickwheel.c (modified) (history)

Diff [purge]

Index: emcore/trunk/button.c
@@ -36,45 +36,45 @@
3737 mutex_init(&button_mutex);
3838 }
3939
40 -int button_register_handler(void (*handler)(enum button_event, int which, int value))
 40+struct button_hook_entry* button_register_handler(void (*handler)(void*, enum button_event,
 41+ int, int),
 42+ void* user)
4143 {
4244 struct button_hook_entry* hook;
4345 hook = (struct button_hook_entry*)malloc(sizeof(struct button_hook_entry));
 46+ if (!hook) return NULL;
4447 hook->owner = current_thread;
4548 hook->handler = handler;
 49+ hook->user = user;
4650 mutex_lock(&button_mutex, TIMEOUT_BLOCK);
4751 hook->next = head_button_hook;
4852 head_button_hook = hook;
4953 mutex_unlock(&button_mutex);
50 - return 0;
 54+ return hook;
5155 }
5256
53 -int button_unregister_handler(void (*handler)(enum button_event, int which, int value))
 57+int button_unregister_handler(struct button_hook_entry* hook)
5458 {
5559 struct button_hook_entry* h;
56 - struct button_hook_entry* handle = NULL;
5760 int result = -1;
5861 mutex_lock(&button_mutex, TIMEOUT_BLOCK);
59 - if (head_button_hook && head_button_hook->handler == handler)
 62+ if (head_button_hook == hook)
6063 {
61 - handle = head_button_hook;
6264 head_button_hook = head_button_hook->next;
63 - free(handle);
 65+ free(hook);
6466 result = 0;
6567 }
6668 else
6769 {
6870 for (h = head_button_hook; h && h->next; h = h->next);
69 - if (h->next->handler != handler)
 71+ if (h->next == hook)
7072 {
71 - handle = h->next;
7273 h->next = h->next->next;
73 - free(handle);
 74+ free(hook);
7475 result = 0;
7576 }
7677 }
7778 mutex_unlock(&button_mutex);
78 - if (handle) free(handle);
7979 return result;
8080 }
8181
@@ -84,7 +84,7 @@
8585 struct button_hook_entry* h;
8686 mutex_lock(&button_mutex, TIMEOUT_BLOCK);
8787 for (h = head_button_hook; h; h = h->next)
88 - h->handler(eventtype, which, value);
 88+ h->handler(h->user, eventtype, which, value);
8989 mutex_unlock(&button_mutex);
9090 }
9191
Index: emcore/trunk/button.h
@@ -37,6 +37,7 @@
3838 WHEEL_UNTOUCH,
3939 WHEEL_POSITION,
4040 WHEEL_MOVED,
 41+ WHEEL_MOVED_ACCEL,
4142 WHEEL_FORWARD,
4243 WHEEL_BACKWARD
4344 };
@@ -45,13 +46,16 @@
4647 {
4748 struct button_hook_entry* next;
4849 struct scheduler_thread* owner;
49 - void (*handler)(enum button_event, int which, int value);
 50+ void (*handler)(void*, enum button_event, int, int);
 51+ void* user;
5052 };
5153
5254
5355 void button_init() INITCODE_ATTR;
54 -int button_register_handler(void (*handler)(enum button_event, int which, int value));
55 -int button_unregister_handler(void (*handler)(enum button_event, int which, int value));
 56+struct button_hook_entry* button_register_handler(void (*handler)(void*, enum button_event,
 57+ int, int),
 58+ void* user);
 59+int button_unregister_handler(struct button_hook_entry* hook);
5660 void button_send_event(enum button_event eventtype, int which, int value) ICODE_ATTR;
5761 void button_unregister_all_of_thread(struct scheduler_thread* process);
5862
Index: emcore/trunk/target/ipodnano3g/clickwheel.c
@@ -95,8 +95,9 @@
9696 if (distance < -48) distance += 96;
9797 else if (distance > 48) distance -= 96;
9898 DEBUGF("Wheel moved %d units without accel", distance);
 99+ button_send_event(WHEEL_MOVED, 0, distance);
99100 DEBUGF("Wheel moved %d units with accel", distance * packets);
100 - button_send_event(WHEEL_MOVED, 0, distance);
 101+ button_send_event(WHEEL_MOVED_ACCEL, 0, distance * packets);
101102 collect += distance * packets;
102103 enum button_event e = collect > 0 ? WHEEL_FORWARD : WHEEL_BACKWARD;
103104 int data = (collect > 0 ? collect : -collect) / 128;