summaryrefslogtreecommitdiff
path: root/apps/plugins/plugin_crt0.c
diff options
context:
space:
mode:
authorThomas Martitz <kugel@rockbox.org>2010-08-23 16:56:49 +0000
committerThomas Martitz <kugel@rockbox.org>2010-08-23 16:56:49 +0000
commitabdc5935beb7dc3fa63bffeec584921ad2a4c8bd (patch)
tree3eb3ca86063d0fff58ca8ed2c49dbb0af0792570 /apps/plugins/plugin_crt0.c
parent8106c9dc646bbb26131896eb12d23edb26cba476 (diff)
downloadrockbox-abdc5935beb7dc3fa63bffeec584921ad2a4c8bd.tar.gz
rockbox-abdc5935beb7dc3fa63bffeec584921ad2a4c8bd.zip
Introduce plugin_crt0.c that every plugin links.
It handles exit() properly, calling the handler also when the plugin returns normally (also it makes exit() more standard compliant while at it). It also holds PLUGIN_HEADER, so that it doesn't need to be in each plugin anymore. To work better together with callbacks passed to rb->default_event_handler_ex introduce exit_on_usb() which will call the exit handler before showing the usb screen and exit() after it. In most cases it was passed a callback which was manually called at all other return points. This can now be done via atexit(). In future plugin_crt0.c could also handle clearing bss, initializing iram and more. git-svn-id: svn://svn.rockbox.org/rockbox/trunk@27862 a1c6a512-1295-4272-9138-f99709370657
Diffstat (limited to 'apps/plugins/plugin_crt0.c')
-rwxr-xr-xapps/plugins/plugin_crt0.c116
1 files changed, 116 insertions, 0 deletions
diff --git a/apps/plugins/plugin_crt0.c b/apps/plugins/plugin_crt0.c
new file mode 100755
index 0000000000..e9e2bcb8aa
--- /dev/null
+++ b/apps/plugins/plugin_crt0.c
@@ -0,0 +1,116 @@
1/***************************************************************************
2* __________ __ ___.
3* Open \______ \ ____ ____ | | _\_ |__ _______ ___
4* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7* \/ \/ \/ \/ \/
8* $Id$
9*
10* Copyright (C) 2010 by Thomas Martitz
11*
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.
16*
17* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18* KIND, either express or implied.
19*
20****************************************************************************/
21
22
23#include "plugin.h"
24#if (CONFIG_PLATFORM & PLATFORM_NATIVE)
25#include "../codecs/lib/setjmp.h"
26#else
27#include <setjmp.h>
28#endif
29
30PLUGIN_HEADER
31
32/*
33 * EXIT_MAGIC magic, because 0 cannot be used due to setjmp()
34 * must be > 0
35 */
36#define EXIT_MAGIC 0x0CDEBABE
37
38extern enum plugin_status plugin_start(const void*);
39
40static jmp_buf __exit_env;
41/* only 1 atexit handler for now, chain in the exit handler if you need more */
42static void (*atexit_handler)(void);
43
44int atexit(void (*fn)(void))
45{
46 if (atexit_handler)
47 return -1;
48 atexit_handler = fn;
49 return 0;
50}
51
52void exit(int status)
53{ /* jump back in time to before starting the plugin */
54 longjmp(__exit_env, status != 0 ? status : EXIT_MAGIC);
55}
56
57void _exit(int status)
58{ /* don't call exit handler */
59 atexit_handler = NULL;
60 exit(status);
61}
62
63enum plugin_status plugin__start(const void *param)
64{
65 int exit_ret;
66 enum plugin_status ret;
67
68 /* we come back here if exit() was called or the plugin returned normally */
69 exit_ret = setjmp(__exit_env);
70 if (exit_ret == 0)
71 { /* start the plugin */
72 ret = plugin_start(param);
73 }
74 else
75 { /* plugin exit via exit() */
76 if (exit_ret == EXIT_MAGIC)
77 { /* exit(EXIT_SUCCESS) */
78 ret = PLUGIN_OK;
79 }
80 else if (exit_ret < INTERNAL_PLUGIN_RETVAL_START)
81 { /* exit(EXIT_FAILURE) */
82 ret = PLUGIN_ERROR;
83 }
84 else
85 { /* exit(PLUGIN_XXX) */
86 ret = (enum plugin_status)exit_ret;
87 }
88 }
89
90 /* before finishing, call the exit handler if there was one */
91 if (atexit_handler != NULL)
92 atexit_handler();
93
94 return ret;
95}
96
97static void cleanup_wrapper(void *param)
98{
99 (void)param;
100 if (atexit_handler)
101 atexit_handler();
102}
103
104void exit_on_usb(int button)
105{ /* the default handler will call the exit handler before
106 * showing the usb screen; after that we don't want the exit handler
107 * to be called a second time, hence _exit()
108 *
109 * if not usb, then the handler will only be called if powering off
110 * if poweroff, the plugin doesn't want to run any further so exit as well*/
111 long result = rb->default_event_handler_ex(button, cleanup_wrapper, NULL);
112 if (result == SYS_USB_CONNECTED)
113 _exit(PLUGIN_USB_CONNECTED);
114 else if (result == SYS_POWEROFF)
115 _exit(PLUGIN_POWEROFF);
116}