freemyipod r130 - Code Review

Jump to: navigation, search
Repository:freemyipod
Revision:r129‎ | r130 | r131 >
Date:23:40, 13 August 2010
Author:theseven
Status:new
Tags:
Comment:
Add generic button event proxy
Modified paths:
  • /embios/trunk/SOURCES (modified) (history)
  • /embios/trunk/button.c (added) (history)
  • /embios/trunk/button.h (added) (history)
  • /embios/trunk/export/syscallapi.h (modified) (history)
  • /embios/trunk/export/syscallwrappers.h (modified) (history)
  • /embios/trunk/init.c (modified) (history)
  • /embios/trunk/syscallapi.c (modified) (history)
  • /embios/trunk/target/ipodnano2g/target.h (modified) (history)
  • /embios/trunk/thread.c (modified) (history)

Diff [purge]

Index: embios/trunk/init.c
@@ -170,6 +170,9 @@
171171 #ifdef HAVE_USB
172172 usb_init();
173173 #endif
 174+#ifdef HAVE_BUTTON
 175+ button_init();
 176+#endif
174177 #ifdef HAVE_STORAGE
175178 DEBUGF("Initializing storage drivers...");
176179 storage_init();
Index: embios/trunk/button.c
@@ -0,0 +1,94 @@
 2+//
 3+//
 4+// Copyright 2010 TheSeven
 5+//
 6+//
 7+// This file is part of emBIOS.
 8+//
 9+// emBIOS is free software: you can redistribute it and/or
 10+// modify it under the terms of the GNU General Public License as
 11+// published by the Free Software Foundation, either version 2 of the
 12+// License, or (at your option) any later version.
 13+//
 14+// emBIOS is distributed in the hope that it will be useful,
 15+// but WITHOUT ANY WARRANTY; without even the implied warranty of
 16+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 17+// See the GNU General Public License for more details.
 18+//
 19+// You should have received a copy of the GNU General Public License along
 20+// with emBIOS. If not, see <http://www.gnu.org/licenses/>.
 21+//
 22+//
 23+
 24+
 25+#include "global.h"
 26+#include "button.h"
 27+#include "thread.h"
 28+
 29+
 30+#ifndef BUTTON_MAX_HOOKS
 31+#define BUTTON_MAX_HOOKS 16
 32+#endif
 33+
 34+
 35+extern struct scheduler_thread* current_thread;
 36+static struct button_hook_entry button_hooks[BUTTON_MAX_HOOKS] IBSS_ATTR;
 37+static struct mutex button_mutex;
 38+
 39+
 40+void button_init()
 41+{
 42+ memset(button_hooks, 0, sizeof(button_hooks));
 43+ mutex_init(&button_mutex);
 44+}
 45+
 46+int button_register_handler(void (*handler)(enum button_event, int which, int value))
 47+{
 48+ int i;
 49+ mutex_lock(&button_mutex, TIMEOUT_BLOCK);
 50+ for (i = 0; i < BUTTON_MAX_HOOKS; i++)
 51+ if (button_hooks[i].owner == NULL)
 52+ {
 53+ button_hooks[i].owner = current_thread;
 54+ button_hooks[i].handler = handler;
 55+ return 0;
 56+ }
 57+ mutex_unlock(&button_mutex);
 58+ return -1;
 59+}
 60+
 61+int button_unregister_handler(void (*handler)(enum button_event, int which, int value))
 62+{
 63+ int i;
 64+ mutex_lock(&button_mutex, TIMEOUT_BLOCK);
 65+ for (i = 0; i < BUTTON_MAX_HOOKS; i++)
 66+ if (button_hooks[i].handler == handler)
 67+ {
 68+ button_hooks[i].owner = NULL;
 69+ button_hooks[i].handler = NULL;
 70+ return 0;
 71+ }
 72+ mutex_unlock(&button_mutex);
 73+ return -1;
 74+}
 75+
 76+void button_send_event(enum button_event eventtype, int which, int value)
 77+{
 78+ int i;
 79+ for (i = 0; i < BUTTON_MAX_HOOKS; i++)
 80+ if (button_hooks[i].owner != NULL)
 81+ handler(eventtype, which, value);
 82+}
 83+
 84+void button_unregister_all_of_thread(struct scheduler_thread* process)
 85+{
 86+ int i;
 87+ mutex_lock(&button_mutex, TIMEOUT_BLOCK);
 88+ for (i = 0; i < BUTTON_MAX_HOOKS; i++)
 89+ if (button_hooks[i].owner == process)
 90+ {
 91+ button_hooks[i].owner = NULL;
 92+ button_hooks[i].handler = NULL;
 93+ }
 94+ mutex_unlock(&button_mutex);
 95+}
Index: embios/trunk/button.h
@@ -0,0 +1,60 @@
 2+//
 3+//
 4+// Copyright 2010 TheSeven
 5+//
 6+//
 7+// This file is part of emBIOS.
 8+//
 9+// emBIOS is free software: you can redistribute it and/or
 10+// modify it under the terms of the GNU General Public License as
 11+// published by the Free Software Foundation, either version 2 of the
 12+// License, or (at your option) any later version.
 13+//
 14+// emBIOS is distributed in the hope that it will be useful,
 15+// but WITHOUT ANY WARRANTY; without even the implied warranty of
 16+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 17+// See the GNU General Public License for more details.
 18+//
 19+// You should have received a copy of the GNU General Public License along
 20+// with emBIOS. If not, see <http://www.gnu.org/licenses/>.
 21+//
 22+//
 23+
 24+
 25+#ifndef __BUTTON_H__
 26+#define __BUTTON_H__
 27+
 28+
 29+#include "global.h"
 30+#include "thread.h"
 31+
 32+
 33+enum button_event
 34+{
 35+ BUTTON_PRESS,
 36+ BUTTON_RELEASE,
 37+ BUTTON_SHORTPRESS,
 38+ BUTTON_LONGPRESS,
 39+ WHEEL_TOUCH,
 40+ WHEEL_UNTOUCH,
 41+ WHEEL_POSITION,
 42+ WHEEL_MOVED,
 43+ WHEEL_FORWARD,
 44+ WHEEL_BACKWARD
 45+};
 46+
 47+struct button_hook_entry
 48+{
 49+ struct scheduler_thread* owner;
 50+ void (*handler)(enum button_event, int which, int value);
 51+};
 52+
 53+
 54+void button_init() INITCODE_ATTR;
 55+int button_register_handler(void (*handler)(enum button_event, int which, int value));
 56+int button_unregister_handler(void (*handler)(enum button_event, int which, int value));
 57+void button_send_event(enum button_event eventtype, int which, int value) ICODE_ATTR;
 58+void button_unregister_all_of_thread(struct scheduler_thread* process);
 59+
 60+
 61+#endif
Index: embios/trunk/target/ipodnano2g/target.h
@@ -50,6 +50,8 @@
5151
5252 #define HAVE_I2C
5353
 54+#define HAVE_BUTTON
 55+
5456 #define HAVE_BOOTFLASH
5557 #define BOOTFLASH_IS_MEMMAPPED
5658
Index: embios/trunk/export/syscallwrappers.h
@@ -175,6 +175,8 @@
176176 #define tlsf_block_size(args...) __embios_syscall->tlsf_block_size(args)
177177 #define tlsf_overhead(args...) __embios_syscall->tlsf_overhead(args)
178178 #define execfirmware(args...) __embios_syscall->execfirmware(args)
 179+#define button_register_handler(args...) __embios_syscall->button_register_handler(args)
 180+#define button_unregister_handler(args...) __embios_syscall->button_unregister_handler(args)
179181
180182
181183 #endif
Index: embios/trunk/export/syscallapi.h
@@ -48,6 +48,7 @@
4949 #include "../backlight.h"
5050 #include "../syscall.h"
5151 #include "../progressbar.h"
 52+#include "../button.h"
5253 #include "../contextswitch.h"
5354 #include "../libc/include/string.h"
5455 #include "../libc/include/stdlib.h"
@@ -211,6 +212,8 @@
212213 typeof(tlsf_block_size) *tlsf_block_size;
213214 typeof(tlsf_overhead) *tlsf_overhead;
214215 typeof(execfirmware) *execfirmware;
 216+ typeof(button_register_handler) *button_register_handler;
 217+ typeof(button_unregister_handler) *button_unregister_handler;
215218 };
216219
217220
Index: embios/trunk/SOURCES
@@ -60,6 +60,9 @@
6161 dir.c
6262 storage.c
6363 #endif
 64+#ifdef HAVE_BUTTON
 65+button.c
 66+#endif
6467 strcasestr.c
6568 strcasecmp.c
6669 strlcpy.c
Index: embios/trunk/syscallapi.c
@@ -179,4 +179,8 @@
180180 .nand_write_page_collect = nand_write_page_collect,
181181 .nand_get_device_type = nand_get_device_type,
182182 #endif
 183+#ifdef HAVE_BUTTON
 184+ .button_register_handler = button_register_handler,
 185+ .button_unregister_handler = button_unregister_handler,
 186+#endif
183187 };
Index: embios/trunk/thread.c
@@ -30,6 +30,9 @@
3131 #include "dir.h"
3232 #include "file.h"
3333 #endif
 34+#ifdef HAVE_BUTTON
 35+#include "button.h"
 36+#endif
3437
3538
3639 struct scheduler_thread scheduler_threads[MAX_THREADS] IBSS_ATTR;
@@ -438,6 +441,9 @@
439442 close_all_of_process(t);
440443 closedir_all_of_process(t);
441444 #endif
 445+#ifdef HAVE_BUTTON
 446+ button_unregister_all_of_thread(t);
 447+#endif
442448 }
443449
444450 leave_critical_section(mode);