diff options
Diffstat (limited to 'apps/plugins/plugin_crt0.c')
-rwxr-xr-x | apps/plugins/plugin_crt0.c | 116 |
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 | |||
30 | PLUGIN_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 | |||
38 | extern enum plugin_status plugin_start(const void*); | ||
39 | |||
40 | static jmp_buf __exit_env; | ||
41 | /* only 1 atexit handler for now, chain in the exit handler if you need more */ | ||
42 | static void (*atexit_handler)(void); | ||
43 | |||
44 | int atexit(void (*fn)(void)) | ||
45 | { | ||
46 | if (atexit_handler) | ||
47 | return -1; | ||
48 | atexit_handler = fn; | ||
49 | return 0; | ||
50 | } | ||
51 | |||
52 | void exit(int status) | ||
53 | { /* jump back in time to before starting the plugin */ | ||
54 | longjmp(__exit_env, status != 0 ? status : EXIT_MAGIC); | ||
55 | } | ||
56 | |||
57 | void _exit(int status) | ||
58 | { /* don't call exit handler */ | ||
59 | atexit_handler = NULL; | ||
60 | exit(status); | ||
61 | } | ||
62 | |||
63 | enum 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 | |||
97 | static void cleanup_wrapper(void *param) | ||
98 | { | ||
99 | (void)param; | ||
100 | if (atexit_handler) | ||
101 | atexit_handler(); | ||
102 | } | ||
103 | |||
104 | void 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 | } | ||