Introduce a small api for loading code (codecs,plugins) from disk/memory.
[maemo-rb.git] / apps / plugins / plugin_crt0.c
blobe34124c5a216ef63729248269c56ad490c6800f7
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
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 ****************************************************************************/
23 #include "plugin.h"
24 #include <setjmp.h>
26 PLUGIN_HEADER
29 * EXIT_MAGIC magic, because 0 cannot be used due to setjmp()
30 * must be > 0
31 */
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))
44 if (atexit_handler)
45 return -1;
46 atexit_handler = fn;
47 return 0;
50 void exit(int status)
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;
58 exit(status);
61 enum plugin_status plugin__start(const void *param)
63 int exit_ret;
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);
69 #endif
70 /* we come back here if exit() was called or the plugin returned normally */
71 exit_ret = setjmp(__exit_env);
72 if (exit_ret == 0)
73 { /* start the plugin */
74 ret = plugin_start(param);
76 else
77 { /* plugin exit via exit() */
78 if (exit_ret == EXIT_MAGIC)
79 { /* exit(EXIT_SUCCESS) */
80 ret = PLUGIN_OK;
82 else if (exit_ret < INTERNAL_PLUGIN_RETVAL_START)
83 { /* exit(EXIT_FAILURE) */
84 ret = PLUGIN_ERROR;
86 else
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)
94 atexit_handler();
96 return ret;
99 static void cleanup_wrapper(void *param)
101 (void)param;
102 if (atexit_handler)
103 atexit_handler();
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);