diff --git a/configure.sh b/configure.sh index e9a4892..863e05a 100755 --- a/configure.sh +++ b/configure.sh @@ -18,6 +18,17 @@ else zlib_libs="pkg-config --libs zlib" fi +echo -n "Checking pkg-config --cflags gtk+-2.0..." +if ! pkg-config --cflags gtk+-2.0 >/dev/null 2>&1; then + echo "no open file dialog" + gtk_cflags= + gtk_libs= +else + echo "yes" + gtk_cflags="pkg-config --cflags gtk+-2.0" + gtk_libs="pkg-config --libs gtk+-2.0" +fi + echo -n "Checking pkg-config --cflags lua[5.1]..." if ! pkg-config --cflags lua5.1 >/dev/null 2>&1; then @@ -97,6 +108,11 @@ if ! make clean >/dev/null 2>&1; then fi echo -n "Generating config.make..." echo "# autamatically generated by configure.sh" >config.make +if [[ ! -z "$gtk_cflags" ]]; then + echo "EXTRA_CFLAGS+=-D_USE_GTK" >> config.make + echo "EXTRA_CFLAGS+=\$(shell $gtk_cflags)" >> config.make + echo "EXTRA_LDFLAGS+=\$(shell $gtk_libs)" >> config.make +fi if [[ -z "$zlib_cflags" ]]; then echo "SUBDIRS=src/zlib" >> config.make echo "ZLIB_CFLAGS=-I../zlib" >> config.make diff --git a/src/sdl-instead/Makefile b/src/sdl-instead/Makefile index 4107a14..6f8462e 100644 --- a/src/sdl-instead/Makefile +++ b/src/sdl-instead/Makefile @@ -1,9 +1,9 @@ include ../../Rules.make include ../../config.make -CFLAGS += $(SDL_CFLAGS) $(LUA_CFLAGS) $(ZLIB_CFLAGS) -DLANG_PATH=\"${LANGPATH}/\" -DSTEAD_PATH=\"${STEADPATH}/\" -DGAMES_PATH=\"${GAMESPATH}/\" -DTHEMES_PATH=\"${THEMESPATH}/\" -DVERSION=\"$(VERSION)\" -DICON_PATH=\"${ICONPATH}/\" +CFLAGS += $(SDL_CFLAGS) $(LUA_CFLAGS) $(ZLIB_CFLAGS) $(EXTRA_CFLAGS) -DLANG_PATH=\"${LANGPATH}/\" -DSTEAD_PATH=\"${STEADPATH}/\" -DGAMES_PATH=\"${GAMESPATH}/\" -DTHEMES_PATH=\"${THEMESPATH}/\" -DVERSION=\"$(VERSION)\" -DICON_PATH=\"${ICONPATH}/\" -LDFLAGS += $(SDL_LFLAGS) $(LUA_LFLAGS) $(ZLIB_LFLAGS) +LDFLAGS += $(SDL_LFLAGS) $(LUA_LFLAGS) $(ZLIB_LFLAGS) $(EXTRA_LDFLAGS) SRC := graphics.c input.c game.c main.c instead.c sound.c SDL_rotozoom.c SDL_anigif.c SDL_gfxBlitFunc.c config.c themes.c menu.c util.c cache.c unzip.c ioapi.c unpack.c $(PLATFORM) diff --git a/src/sdl-instead/externals.h b/src/sdl-instead/externals.h index 019e9bd..7cb5e1d 100644 --- a/src/sdl-instead/externals.h +++ b/src/sdl-instead/externals.h @@ -21,3 +21,4 @@ #include #include #include +#include diff --git a/src/sdl-instead/game.c b/src/sdl-instead/game.c index 6717190..6baeeb3 100644 --- a/src/sdl-instead/game.c +++ b/src/sdl-instead/game.c @@ -111,10 +111,46 @@ static void games_sort() { qsort(games, games_nr, sizeof(struct game), cmp_game); } +int games_add(const char *path, const char *dir) +{ + char *p; + if (!is_game(path, dir)) + return -1; + p = getpath(path, dir); + if (!p) + return -1; + games[games_nr].path = p; + games[games_nr].dir = strdup(dir); + games[games_nr].name = game_name(p, dir); + games_nr ++; + return 0; +} + +int games_replace(const char *path, const char *dir) +{ + int i; + char *p; + if (!is_game(path, dir)) + return -1; + for (i = 0; id_type != DT_DIR) continue;*/ - if (!is_game(path, de->d_name)) + if (games_add(path, de->d_name)) continue; - p = getpath(path, de->d_name); - games[games_nr].path = p; - games[games_nr].dir = strdup(de->d_name); - games[games_nr].name = game_name(p, de->d_name); - games_nr ++; i ++; } out: @@ -2288,6 +2319,55 @@ static int game_input(int down, const char *key, int x, int y, int mb) return 0; } +extern char zip_game_dirname[]; +extern int unpack(const char *zipfilename, const char *dirname); + +int game_from_disk(void) +{ + int i = 0; + char *g, *p, *b, *d; + char dir[PATH_MAX]; + char base[PATH_MAX]; + if (opt_fs) { + int old_menu = (menu_shown) ? cur_menu: -1; + opt_fs ^= 1; + game_restart(); + if (old_menu != -1) + game_menu(old_menu); + } + + g = p = open_file_dialog(); + if (!p) + return -1; + + strcpy(dir, p); + strcpy(base, p); + d = dir; b = base; + + i = strlen(d); + if (i && d[i - 1] != '/') { /* file */ + d = dirname(d); + strcpy(b, d); + } + d = dirname(d); + b = basename(b); +/* fprintf(stderr,"%s:%s\n", d, b); */ + p = game_tmp_path(); + fprintf(stderr,"Trying to install: %s\n", g); + if (!unpack(g, p) && zip_game_dirname[0]) { + games_replace(p, zip_game_dirname); + p = zip_game_dirname; + } else if (games_replace(d, b)) { + return -1; + } else + p = b; + game_done(0); + if (game_init(p)) { + game_error(p); + } + return 0; +} + int game_loop(void) { static int alt_pressed = 0; @@ -2352,6 +2432,13 @@ int game_loop(void) } else if (!is_key(&ev, "f1")) { if (!menu_shown) menu_toggle(); + } else if (!is_key(&ev, "f4")) { +#ifdef _USE_UNPACK + mouse_reset(1); + if (!game_from_disk()) { + shift_pressed = alt_pressed = 0; + } +#endif } else if (!is_key(&ev, "escape")) { if (use_xref) disable_use(); diff --git a/src/sdl-instead/game.h b/src/sdl-instead/game.h index 7a4c9d0..33841aa 100644 --- a/src/sdl-instead/game.h +++ b/src/sdl-instead/game.h @@ -38,6 +38,7 @@ extern int game_change_vol(int d, int val); extern int game_change_hz(int hz); extern int games_lookup(const char *path); +extern int games_replace(const char *path, const char *dir); extern void game_err_msg(const char *s); extern int game_error(const char *name); @@ -60,6 +61,8 @@ extern char *game_locale(void); extern int game_paused(void); +extern char *open_file_dialog(void); + #define CURSOR_CLEAR -1 #define CURSOR_OFF 0 #define CURSOR_ON 1 diff --git a/src/sdl-instead/main.c b/src/sdl-instead/main.c index ee43b6f..4c14987 100644 --- a/src/sdl-instead/main.c +++ b/src/sdl-instead/main.c @@ -25,6 +25,9 @@ int main(int argc, char **argv) { int err = 0; int i; +#ifdef _USE_GTK + gtk_init(&argc, &argv); +#endif putenv("SDL_MOUSE_RELATIVE=0"); /* test this! */ for (i = 1; i < argc; i++) { if (!strcmp(argv[i],"-alsa")) diff --git a/src/sdl-instead/unix.c b/src/sdl-instead/unix.c index d5ec9cb..1588a24 100644 --- a/src/sdl-instead/unix.c +++ b/src/sdl-instead/unix.c @@ -15,6 +15,9 @@ #include #endif #include "internals.h" +#ifdef _USE_GTK +#include +#endif #ifndef PATH_MAX #define PATH_MAX 4096 @@ -48,12 +51,100 @@ char *game_tmp_path(void) return tmp; } +#ifdef _USE_GTK +static volatile int gtk_response = -1; +static void +run_response_handler (GtkDialog *dialog, + gint response_id, + gpointer data) +{ + gtk_response = response_id; +} +#endif + +char *open_file_dialog(void) +{ +#ifndef _USE_GTK + /* unix people don't need win solutions */ + return NULL; +#else + gulong response_handler; + char *filename = NULL; + static char file[PATH_MAX]; + GtkWidget *file_dialog; + + GtkFileFilter *file_filter_all; + GtkFileFilter *file_filter_zip; + GtkFileFilter *file_filter_lua; + + file_filter_all = gtk_file_filter_new(); + gtk_file_filter_add_pattern(file_filter_all, "*"); + gtk_file_filter_set_name(file_filter_all, "*"); + + + file_filter_zip = gtk_file_filter_new(); + gtk_file_filter_add_pattern(file_filter_zip, "*.zip"); + gtk_file_filter_set_name(file_filter_zip, "*.zip"); + + file_filter_lua = gtk_file_filter_new(); + gtk_file_filter_add_pattern(file_filter_lua, "main.lua"); + gtk_file_filter_set_name(file_filter_lua, "main.lua"); + + + file[0] = 0; + file_dialog = gtk_file_chooser_dialog_new ("Open File", + NULL, GTK_FILE_CHOOSER_ACTION_OPEN, + GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL, + GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT, NULL); + + gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(file_dialog), + file_filter_all); + + gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(file_dialog), + file_filter_zip); + + gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(file_dialog), + file_filter_lua); + + gtk_file_chooser_set_filter(GTK_FILE_CHOOSER(file_dialog), file_filter_zip); + + response_handler = g_signal_connect (file_dialog, "response", + G_CALLBACK (run_response_handler), NULL); + + gtk_window_set_modal (GTK_WINDOW (file_dialog), TRUE); + gtk_widget_show(file_dialog); + + gtk_response = -1; /* dirty, but we need both SDL and gtk */ + + while (gtk_response == -1) { + struct inp_event ev; + gtk_main_iteration(); + memset(&ev, 0, sizeof(struct inp_event)); + while ((input(&ev, 0)) == AGAIN); + } +/* if (gtk_dialog_run (GTK_DIALOG (file_dialog)) == GTK_RESPONSE_ACCEPT) { */ + if (gtk_response == GTK_RESPONSE_ACCEPT) { + filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (file_dialog)); + if (filename) { + strcpy(file, filename); + g_free (filename); + } + } + g_signal_handler_disconnect (file_dialog, response_handler); + gtk_widget_destroy(file_dialog); + while(gtk_events_pending()) + gtk_main_iteration(); + return (file[0])?file:NULL; +#endif +} + char *game_local_games_path(void) { struct passwd *pw; pw = getpwuid(getuid()); if (!pw) return NULL; + snprintf(local_games_path, sizeof(local_games_path) - 1 , "%s/.instead/games/", pw->pw_dir); return local_games_path; } diff --git a/src/sdl-instead/windows.c b/src/sdl-instead/windows.c index b11e694..d145e08 100644 --- a/src/sdl-instead/windows.c +++ b/src/sdl-instead/windows.c @@ -187,6 +187,27 @@ int debug_init(void) void debug_done() { - FreeConsole(); + FreeConsole(); } +char *open_file_dialog(void) +{ + OPENFILENAME ofn; + static char szFile[MAX_PATH]; + ZeroMemory( &ofn , sizeof( ofn)); + ofn.lStructSize = sizeof ( ofn ); + ofn.hwndOwner = NULL ; + ofn.lpstrFile = szFile ; + ofn.lpstrFile[0] = '\0'; + ofn.nMaxFile = sizeof( szFile ); + ofn.lpstrFilter = "*.*\0*.*\0*.zip\0*.zip\0main.lua\0main.lua"; + ofn.nFilterIndex = 2; + ofn.lpstrFileTitle = NULL; + ofn.nMaxFileTitle = 0; + ofn.lpstrInitialDir=NULL; + ofn.Flags = OFN_PATHMUSTEXIST|OFN_FILEMUSTEXIST|OFN_HIDEREADONLY|OFN_READONLY; + if (!GetOpenFileName(&ofn)) + return NULL; + unix_path(ofn.lpstrFile); + return ofn.lpstrFile; +}