frontend: generic: preliminary SDL support
authornotaz <notasas@gmail.com>
Sun, 22 Apr 2012 23:00:48 +0000 (02:00 +0300)
committernotaz <notasas@gmail.com>
Sun, 29 Apr 2012 16:32:08 +0000 (19:32 +0300)
Makefile
configure
frontend/common/in_sdl.c [new file with mode: 0644]
frontend/common/in_sdl.h [new file with mode: 0644]
frontend/linux/plat.c
frontend/menu.c
frontend/plat_omap.c
frontend/plat_pollux.c
frontend/plat_sdl.c [new file with mode: 0644]

index 141929a..0c203be 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -120,7 +120,7 @@ OBJS += frontend/menu.o frontend/linux/in_evdev.o
 OBJS += frontend/common/input.o frontend/linux/xenv.o
 
 ifeq "$(PLATFORM)" "generic"
-OBJS += frontend/plat_dummy.o
+OBJS += frontend/plat_sdl.o frontend/common/in_sdl.o
 endif
 ifeq "$(PLATFORM)" "pandora"
 OBJS += frontend/linux/fbdev.o
@@ -145,7 +145,7 @@ frontend/%.o: CFLAGS += -DHAVE_TSLIB
 OBJS += frontend/pl_gun_ts.o
 endif
 frontend/%.o: CFLAGS += -DIN_EVDEV
-frontend/menu.o: frontend/revision.h
+frontend/menu.o frontend/plat_sdl.o: frontend/revision.h
 
 libpcsxcore/gte_nf.o: libpcsxcore/gte.c
        $(CC) -c -o $@ $^ $(CFLAGS) -DFLAGLESS
index 47c68b8..ecd2215 100755 (executable)
--- a/configure
+++ b/configure
@@ -193,7 +193,12 @@ if [ "$ram_fixed" = "yes" ]; then
   CFLAGS="$CFLAGS -DRAM_FIXED"
 fi
 
-if [ "$platform" = "maemo" ]; then
+if [ "$platform" = "generic" ]; then
+  generic_cflags=`sdl-config --cflags`
+  generic_ldlibs=`sdl-config --libs`
+  CFLAGS="$CFLAGS $generic_cflags"
+  LDFLAGS="$LDFLAGS $generic_ldlibs"
+elif [ "$platform" = "maemo" ]; then
   maemo_cflags=`pkg-config --cflags hildon-1`
   maemo_ldlibs=`pkg-config --libs hildon-1`
   CFLAGS="$CFLAGS -DMAEMO -DMAEMO_CHANGES $maemo_cflags"
@@ -223,6 +228,7 @@ echo "platform            $platform"
 echo "sound driver        $sound_driver"
 echo "C compiler          $CC"
 echo "C compiler flags    $CFLAGS"
+echo "linker flags        $LDFLAGS"
 echo "enable dynarec      $enable_dynarec"
 echo "ARMv7 optimizations $have_armv7"
 echo "enable ARM NEON     $have_arm_neon"
diff --git a/frontend/common/in_sdl.c b/frontend/common/in_sdl.c
new file mode 100644 (file)
index 0000000..27c183b
--- /dev/null
@@ -0,0 +1,309 @@
+/*
+ * (C) Gražvydas "notaz" Ignotas, 2012
+ *
+ * This work is licensed under the terms of any of these licenses
+ * (at your option):
+ *  - GNU GPL, version 2 or later.
+ *  - GNU LGPL, version 2.1 or later.
+ * See the COPYING file in the top-level directory.
+ */
+
+#include <stdio.h>
+#include <SDL.h>
+#include "input.h"
+#include "in_sdl.h"
+
+#define IN_SDL_PREFIX "sdl:"
+/* should be machine word for best performace */
+typedef unsigned long keybits_t;
+#define KEYBITS_WORD_BITS (sizeof(keybits_t) * 8)
+
+static const char * const in_sdl_keys[SDLK_LAST] = {
+       [SDLK_BACKSPACE] = "backspace",
+       [SDLK_TAB] = "tab",
+       [SDLK_CLEAR] = "clear",
+       [SDLK_RETURN] = "return",
+       [SDLK_PAUSE] = "pause",
+       [SDLK_ESCAPE] = "escape",
+       [SDLK_SPACE] = "space",
+       [SDLK_EXCLAIM]  = "!",
+       [SDLK_QUOTEDBL]  = "\"",
+       [SDLK_HASH]  = "#",
+       [SDLK_DOLLAR]  = "$",
+       [SDLK_AMPERSAND]  = "&",
+       [SDLK_QUOTE] = "'",
+       [SDLK_LEFTPAREN] = "(",
+       [SDLK_RIGHTPAREN] = ")",
+       [SDLK_ASTERISK] = "*",
+       [SDLK_PLUS] = "+",
+       [SDLK_COMMA] = ",",
+       [SDLK_MINUS] = "-",
+       [SDLK_PERIOD] = ".",
+       [SDLK_SLASH] = "/",
+       [SDLK_0] = "0",
+       [SDLK_1] = "1",
+       [SDLK_2] = "2",
+       [SDLK_3] = "3",
+       [SDLK_4] = "4",
+       [SDLK_5] = "5",
+       [SDLK_6] = "6",
+       [SDLK_7] = "7",
+       [SDLK_8] = "8",
+       [SDLK_9] = "9",
+       [SDLK_COLON] = ":",
+       [SDLK_SEMICOLON] = ",",
+       [SDLK_LESS] = "<",
+       [SDLK_EQUALS] = "=",
+       [SDLK_GREATER] = ">",
+       [SDLK_QUESTION] = "?",
+       [SDLK_AT] = "@",
+       [SDLK_LEFTBRACKET] = "[",
+       [SDLK_BACKSLASH] = "\\",
+       [SDLK_RIGHTBRACKET] = "]",
+       [SDLK_CARET] = "^",
+       [SDLK_UNDERSCORE] = "_",
+       [SDLK_BACKQUOTE] = "`",
+       [SDLK_a] = "a",
+       [SDLK_b] = "b",
+       [SDLK_c] = "c",
+       [SDLK_d] = "d",
+       [SDLK_e] = "e",
+       [SDLK_f] = "f",
+       [SDLK_g] = "g",
+       [SDLK_h] = "h",
+       [SDLK_i] = "i",
+       [SDLK_j] = "j",
+       [SDLK_k] = "k",
+       [SDLK_l] = "l",
+       [SDLK_m] = "m",
+       [SDLK_n] = "n",
+       [SDLK_o] = "o",
+       [SDLK_p] = "p",
+       [SDLK_q] = "q",
+       [SDLK_r] = "r",
+       [SDLK_s] = "s",
+       [SDLK_t] = "t",
+       [SDLK_u] = "u",
+       [SDLK_v] = "v",
+       [SDLK_w] = "w",
+       [SDLK_x] = "x",
+       [SDLK_y] = "y",
+       [SDLK_z] = "z",
+       [SDLK_DELETE] = "delete",
+
+       [SDLK_KP0] = "[0]",
+       [SDLK_KP1] = "[1]",
+       [SDLK_KP2] = "[2]",
+       [SDLK_KP3] = "[3]",
+       [SDLK_KP4] = "[4]",
+       [SDLK_KP5] = "[5]",
+       [SDLK_KP6] = "[6]",
+       [SDLK_KP7] = "[7]",
+       [SDLK_KP8] = "[8]",
+       [SDLK_KP9] = "[9]",
+       [SDLK_KP_PERIOD] = "[.]",
+       [SDLK_KP_DIVIDE] = "[/]",
+       [SDLK_KP_MULTIPLY] = "[*]",
+       [SDLK_KP_MINUS] = "[-]",
+       [SDLK_KP_PLUS] = "[+]",
+       [SDLK_KP_ENTER] = "enter",
+       [SDLK_KP_EQUALS] = "equals",
+
+       [SDLK_UP] = "up",
+       [SDLK_DOWN] = "down",
+       [SDLK_RIGHT] = "right",
+       [SDLK_LEFT] = "left",
+       [SDLK_DOWN] = "down",
+       [SDLK_INSERT] = "insert",
+       [SDLK_HOME] = "home",
+       [SDLK_END] = "end",
+       [SDLK_PAGEUP] = "page up",
+       [SDLK_PAGEDOWN] = "page down",
+
+       [SDLK_F1] = "f1",
+       [SDLK_F2] = "f2",
+       [SDLK_F3] = "f3",
+       [SDLK_F4] = "f4",
+       [SDLK_F5] = "f5",
+       [SDLK_F6] = "f6",
+       [SDLK_F7] = "f7",
+       [SDLK_F8] = "f8",
+       [SDLK_F9] = "f9",
+       [SDLK_F10] = "f10",
+       [SDLK_F11] = "f11",
+       [SDLK_F12] = "f12",
+       [SDLK_F13] = "f13",
+       [SDLK_F14] = "f14",
+       [SDLK_F15] = "f15",
+
+       [SDLK_NUMLOCK] = "numlock",
+       [SDLK_CAPSLOCK] = "caps lock",
+       [SDLK_SCROLLOCK] = "scroll lock",
+       [SDLK_RSHIFT] = "right shift",
+       [SDLK_LSHIFT] = "left shift",
+       [SDLK_RCTRL] = "right ctrl",
+       [SDLK_LCTRL] = "left ctrl",
+       [SDLK_RALT] = "right alt",
+       [SDLK_LALT] = "left alt",
+       [SDLK_RMETA] = "right meta",
+       [SDLK_LMETA] = "left meta",
+       [SDLK_LSUPER] = "left super",   /* "Windows" keys */
+       [SDLK_RSUPER] = "right super",  
+       [SDLK_MODE] = "alt gr",
+       [SDLK_COMPOSE] = "compose",
+};
+
+static void in_sdl_probe(void)
+{
+       keybits_t *keystate;
+
+       keystate = calloc(SDLK_LAST / KEYBITS_WORD_BITS + 1, 1);
+       if (keystate == NULL) {
+               fprintf(stderr, "in_sdl: OOM\n");
+               return;
+       }
+
+       in_register(IN_SDL_PREFIX "keys", -1, keystate, SDLK_LAST,
+               in_sdl_keys, 0);
+}
+
+static void in_sdl_free(void *drv_data)
+{
+       keybits_t *keystate = drv_data;
+
+       if (keystate != NULL)
+               free(keystate);
+}
+
+static const char * const *
+in_sdl_get_key_names(int *count)
+{
+       *count = SDLK_LAST;
+       return in_sdl_keys;
+}
+
+/* could use SDL_GetKeyState, but this gives better packing */
+static void update_keystate(keybits_t *keystate, int sym, int is_down)
+{
+       keybits_t *ks_word, mask;
+
+       mask = 1;
+       mask <<= sym & (KEYBITS_WORD_BITS - 1);
+       ks_word = keystate + sym / KEYBITS_WORD_BITS;
+       if (is_down)
+               *ks_word |= mask;
+       else
+               *ks_word &= ~mask;
+}
+
+static int in_sdl_update(void *drv_data, const int *binds, int *result)
+{
+       keybits_t *keystate = drv_data, mask;
+       SDL_Event event;
+       int i, sym, bit, b;
+
+       while (SDL_PollEvent(&event)) {
+               if (event.type != SDL_KEYDOWN && event.type != SDL_KEYUP)
+                       continue;
+
+               update_keystate(keystate, event.key.keysym.sym,
+                       event.type == SDL_KEYDOWN);
+       }
+
+       for (i = 0; i < SDLK_LAST / KEYBITS_WORD_BITS + 1; i++) {
+               mask = keystate[i];
+               if (mask == 0)
+                       continue;
+               for (bit = 0; mask != 0; bit++, mask >>= 1) {
+                       if ((mask & 1) == 0)
+                               continue;
+                       sym = i * KEYBITS_WORD_BITS + bit;
+
+                       for (b = 0; b < IN_BINDTYPE_COUNT; b++)
+                               result[b] |= binds[IN_BIND_OFFS(sym, b)];
+               }
+       }
+
+       return 0;
+}
+
+static int in_sdl_update_keycode(void *drv_data, int *is_down)
+{
+       int ret, ret_kc = -1, ret_down = 0;
+       SDL_Event event;
+
+       ret = SDL_PollEvent(&event);
+       if (ret == 0)
+               goto out;
+       if (event.type != SDL_KEYDOWN && event.type != SDL_KEYUP)
+               goto out;
+
+       if (event.key.type == SDL_KEYDOWN)
+               ret_down = 1;
+       ret_kc = event.key.keysym.sym;
+       update_keystate(drv_data, ret_kc, ret_down);
+out:
+       if (is_down != NULL)
+               *is_down = ret_down;
+
+       return ret_kc;
+}
+
+static const struct {
+       short key;
+       short pbtn;
+} key_pbtn_map[] =
+{
+       { SDLK_UP,      PBTN_UP },
+       { SDLK_DOWN,    PBTN_DOWN },
+       { SDLK_LEFT,    PBTN_LEFT },
+       { SDLK_RIGHT,   PBTN_RIGHT },
+       /* XXX: maybe better set this from it's plat code somehow */
+       { SDLK_RETURN,  PBTN_MOK },
+       { SDLK_ESCAPE,  PBTN_MBACK },
+       { SDLK_a,       PBTN_MA2 },
+       { SDLK_s,       PBTN_MA3 },
+       { SDLK_BACKSLASH,    PBTN_MENU },
+       { SDLK_LEFTBRACKET,  PBTN_L },
+       { SDLK_RIGHTBRACKET, PBTN_R },
+};
+
+#define KEY_PBTN_MAP_SIZE (sizeof(key_pbtn_map) / sizeof(key_pbtn_map[0]))
+
+static int in_sdl_menu_translate(void *drv_data, int keycode)
+{
+       int i;
+
+       if (keycode < 0)
+       {
+               /* menu -> kc */
+               keycode = -keycode;
+               for (i = 0; i < KEY_PBTN_MAP_SIZE; i++)
+                       if (key_pbtn_map[i].pbtn == keycode)
+                               return key_pbtn_map[i].key;
+       }
+       else
+       {
+               for (i = 0; i < KEY_PBTN_MAP_SIZE; i++)
+                       if (key_pbtn_map[i].key == keycode)
+                               return key_pbtn_map[i].pbtn;
+       }
+
+       return 0;
+}
+
+static const in_drv_t in_sdl_drv = {
+       .prefix         = IN_SDL_PREFIX,
+       .probe          = in_sdl_probe,
+       .free           = in_sdl_free,
+       .get_key_names  = in_sdl_get_key_names,
+       .update         = in_sdl_update,
+       .update_keycode = in_sdl_update_keycode,
+       .menu_translate = in_sdl_menu_translate,
+};
+
+void in_sdl_init(void)
+{
+       in_register_driver(&in_sdl_drv);
+}
+
diff --git a/frontend/common/in_sdl.h b/frontend/common/in_sdl.h
new file mode 100644 (file)
index 0000000..9c9d406
--- /dev/null
@@ -0,0 +1 @@
+void in_sdl_init(void);
index ec2aa8b..b7152b5 100644 (file)
@@ -8,7 +8,7 @@
  * See the COPYING file in the top-level directory.
  */
 
-#define _GNU_SOURCE
+#define _GNU_SOURCE 1
 #include <stdio.h>
 #include <string.h>
 #include <stdarg.h>
index 989c6ec..d99156b 100644 (file)
@@ -1504,7 +1504,7 @@ static int menu_loop_options(int id, int keys)
        int i;
 
        i = me_id2offset(e_menu_options, MA_OPT_CPU_CLOCKS);
-       e_menu_options[i].enabled = cpu_clock_st != 0 ? 1 : 0;
+       e_menu_options[i].enabled = cpu_clock_st > 0 ? 1 : 0;
        me_enable(e_menu_options, MA_OPT_SAVECFG_GAME, ready_to_go && CdromId[0]);
 
        me_loop(e_menu_options, &sel);
@@ -1714,8 +1714,10 @@ static void draw_frame_main(void)
 {
        struct tm *tmp;
        time_t ltime;
+       int capacity;
        char ltime_s[16];
        char buff[64];
+       char *out;
 
        if (CdromId[0] != 0) {
                snprintf(buff, sizeof(buff), "%.32s/%.9s (running as %s, with %s)",
@@ -1725,11 +1727,17 @@ static void draw_frame_main(void)
        }
 
        if (ready_to_go) {
+               capacity = plat_get_bat_capacity();
                ltime = time(NULL);
                tmp = localtime(&ltime);
                strftime(ltime_s, sizeof(ltime_s), "%H:%M", tmp);
-               snprintf(buff, sizeof(buff), "%s %3d%%", ltime_s, plat_get_bat_capacity());
-               smalltext_out16(4, 1 + me_sfont_h, buff, 0x105f);
+               if (capacity >= 0) {
+                       snprintf(buff, sizeof(buff), "%s %3d%%", ltime_s, capacity);
+                       out = buff;
+               }
+               else
+                       out = ltime_s;
+               smalltext_out16(4, 1 + me_sfont_h, out, 0x105f);
        }
 }
 
@@ -2241,8 +2249,12 @@ void menu_init(void)
        last_psx_bpp = 16;
 
        g_menubg_src_ptr = calloc(g_menuscreen_w * g_menuscreen_h * 2, 1);
-       if (g_menubg_src_ptr == NULL)
+       g_menubg_ptr = calloc(g_menuscreen_w * g_menuscreen_h * 2, 1);
+       if (g_menubg_src_ptr == NULL || g_menubg_ptr == NULL) {
+               fprintf(stderr, "OOM\n");
                exit(1);
+       }
+
        emu_make_path(buff, "skin/background.png", sizeof(buff));
        readpng(g_menubg_src_ptr, buff, READPNG_BG, g_menuscreen_w, g_menuscreen_h);
 
@@ -2362,7 +2374,8 @@ void menu_prepare_emu(void)
        menu_sync_config();
        apply_lcdrate(Config.PsxType);
        apply_filter(filter);
-       plat_cpu_clock_apply(cpu_clock);
+       if (cpu_clock > 0)
+               plat_cpu_clock_apply(cpu_clock);
 
        // push config to GPU plugin
        plugin_call_rearmed_cbs();
index 96f75ce..1a725ff 100644 (file)
@@ -166,7 +166,6 @@ void *plat_prepare_screenshot(int *w, int *h, int *bpp)
 void plat_init(void)
 {
        const char *main_fb_name, *layer_fb_name;
-       void *temp_frame;
        int fd, ret, w, h;
 
        main_fb_name = getenv("FBDEV_MAIN");
@@ -218,13 +217,6 @@ void plat_init(void)
                goto fail0;
        }
 
-       temp_frame = calloc(g_menuscreen_w * g_menuscreen_h * 2, 1);
-       if (temp_frame == NULL) {
-               fprintf(stderr, "OOM\n");
-               goto fail1;
-       }
-       g_menubg_ptr = temp_frame;
-
        plat_pandora_init(); // XXX
 
        return;
index 012d9eb..3cf3a9b 100644 (file)
@@ -529,12 +529,6 @@ void plat_init(void)
        g_menuscreen_h = 240;
        g_menuscreen_ptr = fb_flip();
 
-       g_menubg_ptr = calloc(320*240*2, 1);
-       if (g_menubg_ptr == NULL) {
-               fprintf(stderr, "OOM\n");
-               exit(1);
-       }
-
        warm_ret = warm_init();
        have_warm = warm_ret == 0;
        warm_change_cb_upper(WCB_B_BIT, 1);
diff --git a/frontend/plat_sdl.c b/frontend/plat_sdl.c
new file mode 100644 (file)
index 0000000..015450d
--- /dev/null
@@ -0,0 +1,154 @@
+/*
+ * (C) Gražvydas "notaz" Ignotas, 2011,2012
+ *
+ * This work is licensed under the terms of any of these licenses
+ * (at your option):
+ *  - GNU GPL, version 2 or later.
+ *  - GNU LGPL, version 2.1 or later.
+ * See the COPYING file in the top-level directory.
+ */
+
+#include <stdio.h>
+#include <SDL.h>
+#include "common/input.h"
+#include "common/in_sdl.h"
+#include "common/menu.h"
+#include "plat.h"
+#include "revision.h"
+
+// XXX
+struct in_default_bind in_evdev_defbinds[] = {
+       { 0, 0, 0 }
+};
+
+static SDL_Surface *screen;
+
+static int change_video_mode(int w, int h)
+{
+  screen = SDL_SetVideoMode(w, h, 16, 0);
+  if (screen == NULL) {
+    fprintf(stderr, "SDL_SetVideoMode failed: %s\n", SDL_GetError());
+    return -1;
+  }
+
+  return 0;
+}
+
+void plat_init(void)
+{
+  int ret;
+
+  ret = SDL_Init(SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE);
+  if (ret != 0) {
+    fprintf(stderr, "SDL_Init failed: %s\n", SDL_GetError());
+    exit(1);
+  }
+
+  g_menuscreen_w = 640;
+  g_menuscreen_h = 480;
+  ret = change_video_mode(g_menuscreen_w, g_menuscreen_h);
+  if (ret != 0) {
+    ret = change_video_mode(0, 0);
+    if (ret != 0)
+      goto fail;
+
+    if (screen->w < 320 || screen->h < 240) {
+      fprintf(stderr, "resolution %dx%d is too small, sorry.\n",
+              screen->w, screen->h);
+      goto fail;
+    }
+    g_menuscreen_w = screen->w;
+    g_menuscreen_h = screen->h;
+  }
+  SDL_WM_SetCaption("PCSX-ReARMed " REV, NULL);
+
+  in_sdl_init();
+  in_probe();
+  return;
+
+fail:
+  SDL_Quit();
+  exit(1);
+}
+
+void plat_finish(void)
+{
+  SDL_Quit();
+}
+
+void plat_gvideo_open(void)
+{
+}
+
+void *plat_gvideo_set_mode(int *w, int *h, int *bpp)
+{
+  change_video_mode(*w, *h);
+  return screen->pixels;
+}
+
+void *plat_gvideo_flip(void)
+{
+  SDL_Flip(screen);
+  return screen->pixels;
+}
+
+void plat_gvideo_close(void)
+{
+}
+
+void plat_video_menu_enter(int is_rom_loaded)
+{
+  change_video_mode(g_menuscreen_w, g_menuscreen_h);
+}
+
+void plat_video_menu_begin(void)
+{
+  SDL_LockSurface(screen);
+  g_menuscreen_ptr = screen->pixels;
+}
+
+void plat_video_menu_end(void)
+{
+  SDL_UnlockSurface(screen);
+  SDL_Flip(screen);
+  g_menuscreen_ptr = NULL;
+}
+
+void plat_video_menu_leave(void)
+{
+}
+
+/* unused stuff */
+void *plat_prepare_screenshot(int *w, int *h, int *bpp)
+{
+  return 0;
+}
+
+int plat_cpu_clock_get(void)
+{
+  return -1;
+}
+
+int plat_cpu_clock_apply(int cpu_clock)
+{
+  return -1;
+}
+
+int plat_get_bat_capacity(void)
+{
+  return -1;
+}
+
+void plat_step_volume(int is_up)
+{
+}
+
+void plat_trigger_vibrate(int is_strong)
+{
+}
+
+void plat_minimize(void)
+{
+}
+
+// vim:shiftwidth=2:expandtab