Skip to content

Commit

Permalink
added platform api; moved all platform initialization code into separ…
Browse files Browse the repository at this point in the history
…ate folders; added debug module that catches segfaults and prints stacktrace
  • Loading branch information
crowriot committed Jan 16, 2013
1 parent c134ddc commit 0cae281
Show file tree
Hide file tree
Showing 11 changed files with 633 additions and 327 deletions.
145 changes: 39 additions & 106 deletions apkenv.c
Expand Up @@ -35,21 +35,14 @@
#include <dirent.h>

#include <SDL/SDL.h>
#ifdef FREMANTLE
# include <SDL/SDL_gles.h>
#elif defined(PANDORA)
# include "pandora/glesinit.h"
#endif

#include <SDL/SDL_syswm.h>
#include <X11/Xlib.h>
#include <X11/Xatom.h>

#include "jni/jnienv.h"
#include "jni/shlib.h"
#include "apklib/apklib.h"
#include "debug/debug.h"

#include "apkenv.h"
#include "platform.h"

/* Global application state */
struct GlobalState global;
Expand Down Expand Up @@ -186,11 +179,11 @@ void install_overrides(struct SupportModule *module)
void
usage()
{
#if !defined(LOCAL_SHARE_APPLICATIONS)
printf("Usage: %s <file.apk>\n",global.apkenv_executable);
#else
printf("Usage: %s [--install] <file.apk>\n",global.apkenv_executable);
#endif
if (platform_getinstalldirectory()==0) {
printf("Usage: %s <file.apk>\n",global.apkenv_executable);
} else {
printf("Usage: %s [--install] <file.apk>\n",global.apkenv_executable);
}
exit(1);
}

Expand Down Expand Up @@ -229,8 +222,7 @@ recursive_mkdir(const char *directory)
void
operation(const char *operation, const char *filename)
{
#if defined(LOCAL_SHARE_APPLICATIONS)
if (strcmp(operation, "--install") == 0) {
if (strcmp(operation, "--install") == 0 && platform_getinstalldirectory()!=0) {
char apkenv_absolute[PATH_MAX];
char apk_absolute[PATH_MAX];

Expand All @@ -241,7 +233,7 @@ operation(const char *operation, const char *filename)
char *app_name = apk_basename(filename);

char icon_filename[PATH_MAX];
sprintf(icon_filename, "%s%s.png", DATA_DIRECTORY_BASE, app_name);
sprintf(icon_filename, "%s%s.png", platform_getdatadirectory(), app_name);

struct stat st;
if (stat(icon_filename, &st) != 0) {
Expand Down Expand Up @@ -275,8 +267,8 @@ operation(const char *operation, const char *filename)
}

char desktop_filename[PATH_MAX];
recursive_mkdir();
sprintf(desktop_filename, "%s/%s.desktop", LOCAL_SHARE_APPLICATIONS,
recursive_mkdir(platform_getinstalldirectory());
sprintf(desktop_filename, "%s/%s.desktop", platform_getinstalldirectory(),
app_name);

FILE *desktop = fopen(desktop_filename, "w");
Expand All @@ -296,110 +288,54 @@ operation(const char *operation, const char *filename)
}

usage();
#endif
}

#define MEEGOTOUCH_BORDER 16

void* platform_data = 0;

SDL_Surface* platform_init()
int system_init()
{
SDL_Surface* screen;
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_JOYSTICK) < 0) {
printf("SDL Init failed.\n");
return 0;
}


/**
* The following block looks scary, but it just creates an SDL surface
* with the right OpenGL ES context version. The block looks so scary,
* because on Fremantle, javispedro's SDL_gles is used and on Harmattan
* (where the GLES features are integrated directly into libSDL), only
* SDL is used.
**/
#ifdef PANDORA
screen = SDL_SetVideoMode(SCREEN_WIDTH,SCREEN_HEIGHT,0,SDL_FULLSCREEN);
#if defined(APKENV_GLES2)
platform_data = GLES_Init(2);
#else
platform_data = GLES_Init(1);
#endif
if (!platform_data) {
fprintf(stderr,"ERROR: GLES_Init failed.\n");
if ( !platform_init() ) {
printf("platform_init failed.\n");
return 0;
}
#else
#ifdef FREMANTLE
#ifdef APKENV_GLES2
SDL_GLES_Init(SDL_GLES_VERSION_2_0);
#else /* APKENV_GLES2 */
SDL_GLES_Init(SDL_GLES_VERSION_1_1);
#endif /* APKENV_GLES2 */
screen = SDL_SetVideoMode(0, 0, 0, SDL_FULLSCREEN);
SDL_GLES_MakeCurrent(SDL_GLES_CreateContext());
#else /* FREMANTLE */
#ifdef APKENV_GLES2
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
#else /* APKENV_GLES2 */
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 1);
#endif /* APKENV_GLES2 */
screen = SDL_SetVideoMode(0, 0, 0, SDL_OPENGLES | SDL_FULLSCREEN);
#endif /* FREMANTLE */
#endif /* PANDORA */

SDL_ShowCursor(0);

#if !defined(FREMANTLE) && !defined(PANDORA)
/* Set up swipe lock (left and right) */
SDL_SysWMinfo wm;
SDL_VERSION(&wm.version);
SDL_GetWMInfo(&wm);
Display *dpy = wm.info.x11.display;
Atom atom = XInternAtom(dpy, "_MEEGOTOUCH_CUSTOM_REGION", False);
unsigned int region[] = {
0,
MEEGOTOUCH_BORDER,
screen->w,
screen->h - 2*MEEGOTOUCH_BORDER,
};
XChangeProperty(dpy, wm.info.x11.wmwindow, atom, XA_CARDINAL, 32,
PropModeReplace, (unsigned char*)region, 4);
#endif
return screen;
return 1;
}

void platform_swap()
void system_update()
{
#ifdef PANDORA
GLES_SwapBuffers(platform_data);
#elif defined(FREMANTLE)
SDL_GLES_SwapBuffers();
#else
SDL_GL_SwapBuffers();
#endif
platform_update();
}

void platform_exit()
void system_exit()
{
#ifdef PANDORA
GLES_Exit(platform_data);
#endif
platform_exit();
}


int main(int argc, char **argv)
{
#ifdef APKENV_DEBUG
debug_init();
#endif

char **tmp;

recursive_mkdir(DATA_DIRECTORY_BASE);
recursive_mkdir(platform_getdatadirectory());

global.apkenv_executable = argv[0];
global.apkenv_headline = APKENV_HEADLINE;
global.apkenv_copyright = APKENV_COPYRIGHT;

printf("%s\n%s\n\n", global.apkenv_headline, global.apkenv_copyright);

#if 0
switch (argc) {
case 2:
/* One argument - the .apk (continue below) */
Expand All @@ -412,26 +348,20 @@ int main(int argc, char **argv)
/* Wrong number of arguments */
usage();
}
#endif

global.lookup_symbol = lookup_symbol_impl;
global.lookup_lib_symbol = lookup_lib_symbol_impl;
global.foreach_file = foreach_file_impl;
global.read_file = read_file_impl;
global.recursive_mkdir = recursive_mkdir;

jnienv_init(&global);
javavm_init(&global);
global.apk_filename = strdup(argv[argc-1]);
global.apklib_handle = apk_open(global.apk_filename);
global.support_modules = NULL;


/*
const char *shlib = apk_get_shared_library(global.apklib_handle,"");
if (shlib == NULL) {
printf("Not a native APK.\n");
return 0;
}
*/
const char* libdir[] = {
"assets/libs/armeabi-v7a",
"assets/libs/armeabi",
Expand Down Expand Up @@ -477,7 +407,7 @@ int main(int argc, char **argv)
global.libraries = head;

load_modules(".");
load_modules(MODULE_DIRECTORY_BASE);
load_modules(platform_getmoduledirectory());

if (global.support_modules == NULL) {
printf("No support modules found.\n");
Expand Down Expand Up @@ -507,8 +437,7 @@ int main(int argc, char **argv)
goto finish;
}

SDL_Surface* screen = platform_init();
if (screen==0) {
if (!system_init()) {
return 0;
}

Expand All @@ -517,15 +446,20 @@ int main(int argc, char **argv)
install_overrides(module);

char data_directory[PATH_MAX];
strcpy(data_directory, DATA_DIRECTORY_BASE);
strcpy(data_directory, platform_getdatadirectory());
strcat(data_directory, apk_basename(global.apk_filename));
strcat(data_directory, "/");
recursive_mkdir(data_directory);

module->init(module, screen->w, screen->h, data_directory);
module->init(module, platform_getscreenwidth(), platform_getscreenheight(), data_directory);

while (1) {

if (module->requests_exit(module)) {
module->deinit(module);
break;
}

SDL_Event e;
while (SDL_PollEvent(&e)) {
if (e.type == SDL_KEYDOWN) {
Expand Down Expand Up @@ -562,8 +496,7 @@ int main(int argc, char **argv)
}
}
module->update(module);

platform_swap();
system_update();
}

finish:
Expand All @@ -581,7 +514,7 @@ int main(int argc, char **argv)
}

apk_close(global.apklib_handle);

system_exit();

return 0;
}
Expand Down
16 changes: 13 additions & 3 deletions apkenv.cbp
Expand Up @@ -14,7 +14,7 @@
<Compiler>
<Add option="-g" />
<Add option="-DDEBUG=1" />
<Add option="-DLINKER_DEBUG=0" />
<Add option="-DLINKER_DEBUG=2" />
<Add option="-DAPKENV_DEBUG" />
<Add option="-DAPKENV_GLES" />
</Compiler>
Expand Down Expand Up @@ -57,6 +57,7 @@
<Compiler>
<Add option="-Wall" />
<Add option="-DPANDORA" />
<Add option="-DARCH_ARM_HAVE_TLS_REGISTER" />
</Compiler>
<Linker>
<Add library="SDL" />
Expand Down Expand Up @@ -116,6 +117,10 @@
<Option compilerVar="CC" />
</Unit>
<Unit filename="compat/pthread_wrappers.h" />
<Unit filename="debug/debug.c">
<Option compilerVar="CC" />
</Unit>
<Unit filename="debug/debug.h" />
<Unit filename="jni/jni.h" />
<Unit filename="jni/jnienv.c">
<Option compilerVar="CC" />
Expand Down Expand Up @@ -150,10 +155,15 @@
</Unit>
<Unit filename="linker/strlcpy.h" />
<Unit filename="modules/common.h" />
<Unit filename="pandora/glesinit.c">
<Unit filename="n9xx/platform.c">
<Option compilerVar="CC" />
<Option compile="0" />
<Option link="0" />
</Unit>
<Unit filename="pandora/platform.c">
<Option compilerVar="CC" />
</Unit>
<Unit filename="pandora/glesinit.h" />
<Unit filename="platform.h" />
<Extensions>
<code_completion />
<debugger>
Expand Down
19 changes: 3 additions & 16 deletions apkenv.h
Expand Up @@ -58,6 +58,7 @@ struct SupportModule {
void (*deinit)(struct SupportModule *self);
void (*pause)(struct SupportModule *self);
void (*resume)(struct SupportModule *self);
int (*requests_exit)(struct SupportModule *self);

struct SupportModule *next;
};
Expand All @@ -66,6 +67,7 @@ typedef void *(*lookup_symbol_t)(const char *method);
typedef void *(*lookup_lib_symbol_t)(const char *lib, const char *method);
typedef void (*foreach_file_t)(const char *prefix, apk_for_each_file_callback callback);
typedef int (*read_file_t)(const char *filename, char **buffer, size_t *size);
typedef void (*recursive_mkdir_t)(const char* path);

struct JniLibrary {
struct JniLibrary *next;
Expand Down Expand Up @@ -97,27 +99,12 @@ struct GlobalState {
lookup_lib_symbol_t lookup_lib_symbol;
foreach_file_t foreach_file;
read_file_t read_file;
recursive_mkdir_t recursive_mkdir;
};

#define VM(global_ptr) (&((global_ptr)->vm))
#define ENV(global_ptr) (&((global_ptr)->env))

#if defined(PANDORA)
#define DATA_DIRECTORY_BASE "./data/"
#else
#define DATA_DIRECTORY_BASE "/home/user/.apkenv/"
#endif


#if defined(PANDORA)
#define MODULE_DIRECTORY_BASE "./modules/"
#undef LOCAL_SHARE_APPLICATIONS
#else
#define MODULE_DIRECTORY_BASE "/opt/apkenv/modules/"
#define LOCAL_SHARE_APPLICATIONS "/home/user/.local/share/applications/"
#endif



/* Android MotionEvent */
#define ACTION_DOWN 0
Expand Down

0 comments on commit 0cae281

Please sign in to comment.