OSDN Git Service

v3.0.0 Alpha5 OSDN最終版
[hengband/hengband.git] / src / main-x11.c
index be8f8bf..1c66c9f 100644 (file)
 #include "term/gameterm.h"
 #include "term/term-color-types.h"
 #include "util/int-char-converter.h"
+#include "util/angband-files.h"
+#include "util/string-processor.h"
 
 /*
  * Available graphic modes
@@ -988,7 +990,7 @@ typedef struct term_data term_data;
  */
 struct term_data
 {
-       term t;
+       term_type t;
        infofnt *fnt;
        infowin *win;
 #ifndef USE_XFT
@@ -1029,7 +1031,7 @@ struct x11_selection_type
 {
        bool select; /* The selection is currently in use. */
        bool drawn; /* The selection is currently displayed. */
-       term *t; /* The window where the selection is found. */
+       term_type *t; /* The window where the selection is found. */
        co_ord init; /* The starting co-ordinates. */
        co_ord cur; /* The end co-ordinates (the current ones if still copying). */
        co_ord old; /* The previous end co-ordinates. */
@@ -1039,13 +1041,61 @@ struct x11_selection_type
 static x11_selection_type s_ptr[1];
 
 /*
+ * Convert to EUC-JP
+ */
+#ifdef USE_XIM
+static void convert_to_euc(char *buf)
+{
+       size_t inlen = strlen(buf);
+       size_t outlen = inlen + 1;
+       char tmp[outlen];
+
+       iconv_t iconvd = iconv_open("EUC-JP", "UTF-8");
+       char *inbuf = buf;
+       char *outbuf = tmp;
+       iconv(iconvd, &inbuf, &inlen, &outbuf, &outlen);
+       iconv_close(iconvd);
+
+       int i, l = strlen(tmp);
+       for (i = 0; i < l; i++)
+               buf[i] = tmp[i];
+       buf[l] = '\0';
+}
+#endif
+
+// ゲーム側へキーを送る
+static void send_key(const char key)
+{
+    // Windows ドライバと同様、自前でキューを操作する。
+    // 逆順に term_key_push() する方法だと長い日本語を入力したときにテキストの
+    // 順序が入れ替わってしまう。
+
+    // キーバッファが一杯なら入力を捨てる
+    const int head_nxt = Term->key_head + 1 == Term->key_size ? 0 : Term->key_head + 1;
+    if(head_nxt == Term->key_tail) {
+        plog_fmt("key buffer overflow, ignoring key 0x%02X", key);
+        return;
+    }
+
+    Term->key_queue[Term->key_head] = key;
+    Term->key_head = head_nxt;
+}
+
+// ゲーム側へキー列を送る
+static void send_keys(const char* const keys)
+{
+    for(const char* p = keys; *p != '\0'; ++p)
+        send_key(*p);
+}
+
+/*
  * Process a keypress event
  *
  * Also appears in "main-xaw.c".
  */
 static void react_keypress(XKeyEvent *xev)
 {
-       int i, n, mc, ms, mo, mx;
+       int n, mc, ms, mo, mx;
        uint ks1;
        XKeyEvent *ev = (XKeyEvent*)(xev);
        KeySym ks;
@@ -1077,9 +1127,9 @@ static void react_keypress(XKeyEvent *xev)
        buf[n] = '\0';
 
 #ifdef USE_XIM
-       if(!valid_keysym){
-               for (i = 0; buf[i]; i++) Term_keypress(buf[i]);
-
+       if(!valid_keysym) { /* XIMからの入力時のみ FALSE になる */
+               convert_to_euc(buf);
+               send_keys(buf);
                return;
        }
 #endif
@@ -1093,8 +1143,7 @@ static void react_keypress(XKeyEvent *xev)
        mx = (ev->state & Mod2Mask) ? TRUE : FALSE;
        if (n && !mo && !mx && !IsSpecialKey(ks))
        {
-               for (i = 0; buf[i]; i++) Term_keypress(buf[i]);
-
+               send_keys(buf);
                return;
        }
 
@@ -1102,30 +1151,30 @@ static void react_keypress(XKeyEvent *xev)
        {
                case XK_Escape:
                {
-                       Term_keypress(ESCAPE);
+                       send_key(ESCAPE);
                        return;
                }
 
                case XK_Return:
                {
-                       Term_keypress('\r');
+                       send_key('\r');
                        return;
                }
 
                case XK_Tab:
                {
-                       Term_keypress('\t');
+                       send_key('\t');
                        return;
                }
 
                case XK_Delete:
                {
-                       Term_keypress(0x7f);
+                       send_key(0x7f);
                        return;
                }
                case XK_BackSpace:
                {
-                       Term_keypress('\010');
+                       send_key('\010');
                        return;
                }
        }
@@ -1145,7 +1194,7 @@ static void react_keypress(XKeyEvent *xev)
                        ev->keycode, 13);
        }
 
-       for (i = 0; msg[i]; i++) Term_keypress(msg[i]);
+       send_keys(msg);
 
        if (n && (macro_find_exact(msg) < 0))
        {
@@ -1190,7 +1239,7 @@ static void sort_co_ord(co_ord *min, co_ord *max,
  */
 static void mark_selection_clear(int x1, int y1, int x2, int y2)
 {
-       Term_redraw_section(x1,y1,x2,y2);
+       term_redraw_section(x1,y1,x2,y2);
 }
 
 /*
@@ -1216,10 +1265,10 @@ static void mark_selection_mark(int x1, int y1, int x2, int y2)
 static void mark_selection(void)
 {
        co_ord min, max;
-       term *old = Term;
+       term_type *old = Term;
        bool draw = s_ptr->select;
        bool clear = s_ptr->drawn;
-       if (s_ptr->t != old) Term_activate(s_ptr->t);
+       if (s_ptr->t != old) term_activate(s_ptr->t);
 
        if (clear)
        {
@@ -1232,7 +1281,7 @@ static void mark_selection(void)
                mark_selection_mark(min.x, min.y, max.x, max.y);
        }
 
-       if (s_ptr->t != old) Term_activate(old);
+       if (s_ptr->t != old) term_activate(old);
 
        s_ptr->old.x = s_ptr->cur.x;
        s_ptr->old.y = s_ptr->cur.y;
@@ -1452,7 +1501,7 @@ static bool paste_x11_send_text(XSelectionRequestEvent *rq)
 #ifdef JP
                        if (x > max.x) break;
 
-                       Term_what(x, y, &a, &c);
+                       term_what(x, y, &a, &c);
                        if (1 == kanji) kanji = 2;
                        else if (iskanji(c)) kanji = 1;
                        else kanji = 0;
@@ -1470,7 +1519,7 @@ static bool paste_x11_send_text(XSelectionRequestEvent *rq)
                        if (x > max.x) break;
                        if (x < min.x) continue;
 
-                       Term_what(x, y, &a, &c);
+                       term_what(x, y, &a, &c);
 #endif
 
                        buf[l] = c;
@@ -1646,7 +1695,7 @@ static errr CheckEvent(bool wait)
 
        if (!td || !iwin) return (0);
 
-       Term_activate(&td->t);
+       term_activate(&td->t);
        Infowin_set(iwin);
        switch (xev->type)
        {
@@ -1703,7 +1752,7 @@ static errr CheckEvent(bool wait)
                }
                case KeyPress:
                {
-                       Term_activate(&old_td->t);
+                       term_activate(&old_td->t);
                        react_keypress(&(xev->xkey));
                        break;
                }
@@ -1718,7 +1767,7 @@ static errr CheckEvent(bool wait)
                        y2 = (xev->xexpose.y + xev->xexpose.height -
                                 Infowin->oy)/Infofnt->hgt;
                        
-                       Term_redraw_section(x1, y1, x2, y2);
+                       term_redraw_section(x1, y1, x2, y2);
                        break;
                }
                case MapNotify:
@@ -1755,7 +1804,7 @@ static errr CheckEvent(bool wait)
 
                        wid = cols * td->fnt->wid + (ox + ox);
                        hgt = rows * td->fnt->hgt + (oy + oy);
-                       Term_resize(cols, rows);
+                       term_resize(cols, rows);
                        if ((Infowin->w != wid) || (Infowin->h != hgt))
                        {
                                Infowin_set(td->win);
@@ -1785,7 +1834,7 @@ static errr CheckEvent(bool wait)
 #endif
        }
 
-       Term_activate(&old_td->t);
+       term_activate(&old_td->t);
        Infowin_set(old_td->win);
        return (0);
 }
@@ -2188,7 +2237,7 @@ static void IMDestroyCallback(XIM xim, XPointer client_data, XPointer call_data)
 
 static char force_lower(char a)
 {
-       return ((isupper((a))) ? tolower((a)) : (a))
+       return ((isupper((a))) ? tolower((a)) : (a));
 }
 
 /*
@@ -2196,7 +2245,7 @@ static char force_lower(char a)
  */
 static errr term_data_init(term_data *td, int i)
 {
-       term *t = &td->t;
+       term_type *t = &td->t;
 
        concptr name = angband_term_name[i];
 
@@ -2399,7 +2448,7 @@ static errr term_data_init(term_data *td, int i)
        t->wipe_hook = Term_wipe_x11;
        t->text_hook = Term_text_x11;
        t->data = td;
-       Term_activate(t);
+       term_activate(t);
        return (0);
 }
 
@@ -2534,7 +2583,7 @@ errr init_x11(int argc, char *argv[])
 
        Infowin_set(data[0].win);
        Infowin_raise();
-       Term_activate(&data[0].t);
+       term_activate(&data[0].t);
 
 #ifdef USE_XIM
        {
@@ -2559,8 +2608,8 @@ errr init_x11(int argc, char *argv[])
                        use_graphics = TRUE;
                        pict_wid = pict_hgt = 8;
                        ANGBAND_GRAF = "old";
-                       break;
                }
+               break;
        case GRAPHICS_ADAM_BOLT:
                path_build(filename, sizeof(filename), ANGBAND_DIR_XTRA, "graf/16x16.bmp");
                if (0 == fd_close(fd_open(filename, O_RDONLY)))
@@ -2568,8 +2617,8 @@ errr init_x11(int argc, char *argv[])
                        use_graphics = TRUE;
                        pict_wid = pict_hgt = 16;
                        ANGBAND_GRAF = "new";
-                       break;
                }
+               break;
        }
 
        if (use_graphics)
@@ -2580,7 +2629,7 @@ errr init_x11(int argc, char *argv[])
                for (i = 0; i < num_term; i++)
                {
                        term_data *td = &data[i];
-                       term *t = &td->t;
+                       term_type *t = &td->t;
                        t->pict_hook = Term_pict_x11;
                        t->higher_pict = TRUE;
                        td->tiles =