1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
10 * Copyright (C) 2010 by Thomas Martitz
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
29 * EXIT_MAGIC magic, because 0 cannot be used due to setjmp()
32 #define EXIT_MAGIC 0x0CDEBABE
34 extern enum plugin_status
plugin_start(const void*);
35 extern unsigned char plugin_bss_start
[];
36 extern unsigned char plugin_end_addr
[];
38 static jmp_buf __exit_env
;
39 /* only 1 atexit handler for now, chain in the exit handler if you need more */
40 static void (*atexit_handler
)(void);
42 int rb_atexit(void (*fn
)(void))
51 { /* jump back in time to before starting the plugin */
52 longjmp(__exit_env
, status
!= 0 ? status
: EXIT_MAGIC
);
55 void _exit(int status
)
56 { /* don't call exit handler */
57 atexit_handler
= NULL
;
61 enum plugin_status
plugin__start(const void *param
)
64 enum plugin_status ret
;
66 /* zero out the bss section */
67 #if (CONFIG_PLATFORM & PLATFORM_NATIVE)
68 rb
->memset(plugin_bss_start
, 0, plugin_end_addr
- plugin_bss_start
);
70 /* we come back here if exit() was called or the plugin returned normally */
71 exit_ret
= setjmp(__exit_env
);
73 { /* start the plugin */
74 ret
= plugin_start(param
);
77 { /* plugin exit via exit() */
78 if (exit_ret
== EXIT_MAGIC
)
79 { /* exit(EXIT_SUCCESS) */
82 else if (exit_ret
< INTERNAL_PLUGIN_RETVAL_START
)
83 { /* exit(EXIT_FAILURE) */
87 { /* exit(PLUGIN_XXX) */
88 ret
= (enum plugin_status
)exit_ret
;
92 /* before finishing, call the exit handler if there was one */
93 if (atexit_handler
!= NULL
)
99 static void cleanup_wrapper(void *param
)
106 void exit_on_usb(int button
)
107 { /* the default handler will call the exit handler before
108 * showing the usb screen; after that we don't want the exit handler
109 * to be called a second time, hence _exit()
111 * if not usb, then the handler will only be called if powering off
112 * if poweroff, the plugin doesn't want to run any further so exit as well*/
113 long result
= rb
->default_event_handler_ex(button
, cleanup_wrapper
, NULL
);
114 if (result
== SYS_USB_CONNECTED
)
115 _exit(PLUGIN_USB_CONNECTED
);
116 else if (result
== SYS_POWEROFF
)
117 _exit(PLUGIN_POWEROFF
);