Implement pretty translatable SDL key names
authorparasti <parasti@78b8d119-cf0a-0410-b17c-f493084dd1d7>
Tue, 8 Apr 2008 19:06:20 +0000 (19:06 +0000)
committerparasti <parasti@78b8d119-cf0a-0410-b17c-f493084dd1d7>
Tue, 8 Apr 2008 19:06:20 +0000 (19:06 +0000)
git-svn-id: https://s.snth.net/svn/neverball/trunk@2045 78b8d119-cf0a-0410-b17c-f493084dd1d7

Makefile
ball/st_help.c
scripts/extract-sdl-keynames.awk [new file with mode: 0644]
share/keynames.c [new file with mode: 0644]
share/keynames.h [new file with mode: 0644]

index 21d4ed6..382d04c 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -148,6 +148,7 @@ BALL_OBJS := \
        share/sync.o        \
        share/tilt.o        \
        share/common.o      \
+       share/keynames.o    \
        ball/hud.o          \
        ball/game.o         \
        ball/score.o        \
index 5f9698b..9ca8e5f 100644 (file)
@@ -17,6 +17,7 @@
 #include "audio.h"
 #include "config.h"
 #include "demo.h"
+#include "keynames.h"
 
 #include "st_shared.h"
 #include "st_title.h"
@@ -172,12 +173,12 @@ static int help_controls(int id)
     const char *sA = _("Manual View");
     const char *sC = _("Screenshot");
     
-    const char *k0 = _(SDL_GetKeyName((SDLKey) config_get_d(CONFIG_KEY_PAUSE)));
-    const char *k1 = _("Escape");
-    const char *k2 = SDL_GetKeyName((SDLKey) config_get_d(CONFIG_KEY_CAMERA_1));
-    const char *k3 = SDL_GetKeyName((SDLKey) config_get_d(CONFIG_KEY_CAMERA_2));
-    const char *k4 = SDL_GetKeyName((SDLKey) config_get_d(CONFIG_KEY_CAMERA_3));
-    const char *k6 = SDL_GetKeyName(SDLK_F10);
+    const char *k0 = pretty_keyname((SDLKey) config_get_d(CONFIG_KEY_PAUSE));
+    const char *k1 = pretty_keyname(SDLK_ESCAPE);
+    const char *k2 = pretty_keyname((SDLKey) config_get_d(CONFIG_KEY_CAMERA_1));
+    const char *k3 = pretty_keyname((SDLKey) config_get_d(CONFIG_KEY_CAMERA_2));
+    const char *k4 = pretty_keyname((SDLKey) config_get_d(CONFIG_KEY_CAMERA_3));
+    const char *k6 = pretty_keyname(SDLK_F10);
 
     int jd;
 
diff --git a/scripts/extract-sdl-keynames.awk b/scripts/extract-sdl-keynames.awk
new file mode 100644 (file)
index 0000000..5c99c12
--- /dev/null
@@ -0,0 +1,21 @@
+BEGIN {
+    print "static const char *table[][2] = {"
+}
+
+/\tkeynames\[SDLK_[^\]]+] =/ {
+    if (match($0, /\[[^\]]+\]/))
+        key = substr($0, RSTART + 1, RLENGTH - 2)
+
+    if (match($0, /"[^"]+"/))
+        name = substr($0, RSTART + 1, RLENGTH - 2)
+
+    if (key && name)
+    {
+        print "    { \"" name "\", " "N_(\"" name "\") },"
+    }
+}
+
+END {
+    print "};"
+}
+
diff --git a/share/keynames.c b/share/keynames.c
new file mode 100644 (file)
index 0000000..65f5166
--- /dev/null
@@ -0,0 +1,108 @@
+/*
+ * Copyright (C) 2008 Jānis Rūcis
+ *
+ * NEVERBALL is  free software; you can redistribute  it and/or modify
+ * it under the  terms of the GNU General  Public License as published
+ * by the Free  Software Foundation; either version 2  of the License,
+ * or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT  ANY  WARRANTY;  without   even  the  implied  warranty  of
+ * MERCHANTABILITY or  FITNESS FOR A PARTICULAR PURPOSE.   See the GNU
+ * General Public License for more details.
+ */
+
+#include <SDL_keyboard.h>
+#include <string.h>
+
+#include "lang.h"
+
+/*---------------------------------------------------------------------------*/
+
+/* Initial template generated from $SDL/src/events/SDL_keyboard.c */
+
+static const char *table[][2] = {
+    /* TRANSLATORS,
+     *
+     * This is a mostly-complete list of human-readable SDL key names.  There
+     * might not be corresponding names for all these keys in your language,
+     * and that's perfectly fine -- in such cases just copy the source string.
+     */
+    { "backspace", N_("Backspace") },
+    { "tab", N_("Tab") },
+    { "clear", N_("Clear") },
+    { "return", N_("Return") },
+    { "pause", N_("Pause") },
+    { "escape", N_("Escape") },
+    { "space", N_("Space") },
+    { "delete", N_("Delete") },
+    { "enter", N_("Enter") },
+    { "equals", N_("Equals") },
+    { "up", N_("Up") },
+    { "down", N_("Down") },
+    { "right", N_("Right") },
+    { "left", N_("Left") },
+    { "down", N_("Down") },
+    { "insert", N_("Insert") },
+    { "home", N_("Home") },
+    { "end", N_("End") },
+    { "page up", N_("Page Up") },
+    { "page down", N_("Page Down") },
+    { "f1", N_("F1") },
+    { "f2", N_("F2") },
+    { "f3", N_("F3") },
+    { "f4", N_("F4") },
+    { "f5", N_("F5") },
+    { "f6", N_("F6") },
+    { "f7", N_("F7") },
+    { "f8", N_("F8") },
+    { "f9", N_("F9") },
+    { "f10", N_("F10") },
+    { "f11", N_("F11") },
+    { "f12", N_("F12") },
+    { "f13", N_("F13") },
+    { "f14", N_("F14") },
+    { "f15", N_("F15") },
+    { "numlock", N_("Num Lock") },
+    { "caps lock", N_("Caps Lock") },
+    { "scroll lock", N_("Scroll Lock") },
+    { "right shift", N_("Right Shift") },
+    { "left shift", N_("Left Shift") },
+    { "right ctrl", N_("Right CTRL") },
+    { "left ctrl", N_("Left CTRL") },
+    { "right alt", N_("Right Alt") },
+    { "left alt", N_("Left Alt") },
+    { "right meta", N_("Right Meta") },
+    { "left meta", N_("Left Meta") },
+    { "left super", N_("Left Super") },
+    { "right super", N_("Right Super") },
+    { "alt gr", N_("Alt Gr") },
+    { "compose", N_("Compose") },
+    { "help", N_("Help") },
+    { "print screen", N_("Print Screen") },
+    { "sys req", N_("Sys Req") },
+    { "break", N_("Break") },
+    { "menu", N_("Menu") },
+    { "power", N_("Power") },
+    { "euro", N_("Euro") },
+    { "undo", N_("Undo") },
+};
+
+/*---------------------------------------------------------------------------*/
+
+const char *pretty_keyname(SDLKey key)
+{
+    const char *ugly_keyname;
+    int i;
+
+    if ((ugly_keyname = SDL_GetKeyName(key)) == NULL)
+        return NULL;
+
+    for (i = 0; i < sizeof (table); i++)
+        if (strcmp(table[i][0], ugly_keyname) == 0)
+            return _(table[i][1]);
+
+    return ugly_keyname;
+}
+
+/*---------------------------------------------------------------------------*/
diff --git a/share/keynames.h b/share/keynames.h
new file mode 100644 (file)
index 0000000..efde2f0
--- /dev/null
@@ -0,0 +1,8 @@
+#ifndef KEYNAMES_H
+#define KEYNAMES_H
+
+#include <SDL_keyboard.h>
+
+const char *pretty_keyname(SDLKey);
+
+#endif