OSDN Git Service

change xputc2 to take strings instead of two chars
[jnethack/source.git] / sys / winnt / nttty.c
index 7e1fb9b..3da2b6a 100644 (file)
@@ -13,7 +13,6 @@
  *
  */
 
-
 #ifdef WIN32
 #define NEED_VARARGS /* Uses ... */
 #include "win32api.h"
@@ -83,7 +82,7 @@ cell_t undefined_cell = { CONSOLE_UNDEFINED_CHARACTER,
 static BOOL FDECL(CtrlHandler, (DWORD));
 static void FDECL(xputc_core, (char));
 #if 1 /*JP*/
-static void FDECL(xputc2_core, (unsigned int, unsigned int));
+static void FDECL(xputc2_core, (unsigned char *));
 #endif
 void FDECL(cmov, (int, int));
 void FDECL(nocmov, (int, int));
@@ -92,7 +91,9 @@ int FDECL(process_keystroke,
 static void NDECL(init_ttycolor);
 static void NDECL(really_move_cursor);
 static void NDECL(check_and_set_font);
+#if 0 /*JP*/
 static boolean NDECL(check_font_widths);
+#endif
 static void NDECL(set_known_good_console_font);
 static void NDECL(restore_original_console_font);
 extern void NDECL(safe_routines);
@@ -236,8 +237,9 @@ static void back_buffer_flip()
             if (back->iskanji == 1) {
                 cell_t * back2 = back + 1;
                 cell_t * front2 = front + 1;
+                /* pos.x == 0 \82Ì\8fð\8c\8f\82Í\95s\97v\82Ì\82Í\82¸\82¾\82ª #42072 \91Î\8dô\82Å\92Ç\89Á\81B */
                 if (back->attribute != front->attribute ||
-                    back2->attribute != front2->attribute) {
+                    back2->attribute != front2->attribute || pos.X == 0) {
                     WORD attrs[2];
                     attrs[0] = attrs[1] = back->attribute;
                     WriteConsoleOutputAttribute(console.hConOut, attrs,
@@ -245,15 +247,15 @@ static void back_buffer_flip()
                     front->attribute = back->attribute;
                     front2->attribute = back2->attribute;
                 }
+                /* pos.x == 0 \82Ì\8fð\8c\8f\82Í\95s\97v\82Ì\82Í\82¸\82¾\82ª #42072 \91Î\8dô\82Å\92Ç\89Á\81B */
                 if (back->character != front->character ||
-                    back2->character != front2->character) {
-                    unsigned char buf[2];
-                    buf[0] = (unsigned char)(back->character);
-                    buf[1] = (unsigned char)(back2->character);
-                    WriteConsoleOutputCharacter(console.hConOut, buf, 2, pos,
+                    back2->character != front2->character || pos.X == 0) {
+                    wchar_t wbuf[1];
+                    wbuf[0] = back->character;
+                    WriteConsoleOutputCharacterW(console.hConOut, wbuf, 1, pos,
                                                     &unused);
-                    front->character = back->character;
-                    front2->character = back2->character;
+                    *front = *back;
+                    *front2 = *back2;
                 }
                 pos.X++;
                 back += 2;
@@ -275,6 +277,13 @@ static void back_buffer_flip()
                     WriteConsoleOutputCharacterA(console.hConOut, &ch, 1, pos,
                                                     &unused);
                 }
+#if 1 /*JP*/
+                /* \8a¿\8e\9a\82Ì1\83o\83C\83g\96Ú\82¾\82Á\82½\8fê\8d\87\81A
+                   2\83o\83C\83g\96Ú\82ð\83N\83\8a\83A\82µ\82Ä\8am\8eÀ\82É\8dX\90V\82·\82é */
+                if (front->iskanji == 1) {
+                    (front + 1)->character = '\0';
+                }
+#endif
                 *front = *back;
             }
             back++;
@@ -469,8 +478,17 @@ boolean *valid;
 boolean numberpad;
 int portdebug;
 {
-    int ch = keyboard_handler.pProcessKeystroke(
+    int ch;
+
+#ifdef QWERTZ_SUPPORT
+    if (Cmd.swap_yz)
+        numberpad |= 0x10;
+#endif
+    ch = keyboard_handler.pProcessKeystroke(
                     console.hConIn, ir, valid, numberpad, portdebug);
+#ifdef QWERTZ_SUPPORT
+    numberpad &= ~0x10;
+#endif
     /* check for override */
     if (ch && ch < MAX_OVERRIDES && key_overrides[ch])
         ch = key_overrides[ch];
@@ -489,13 +507,20 @@ tgetch()
     int mod;
     coord cc;
     DWORD count;
+    boolean numpad = iflags.num_pad;
+
     really_move_cursor();
     if (iflags.debug_fuzzer)
         return randomkey();
+#ifdef QWERTZ_SUPPORT
+    if (Cmd.swap_yz)
+        numpad |= 0x10;
+#endif
+
     return (program_state.done_hup)
                ? '\033'
                : keyboard_handler.pCheckInput(
-                   console.hConIn, &ir, &count, iflags.num_pad, 0, &mod, &cc);
+                   console.hConIn, &ir, &count, numpad, 0, &mod, &cc);
 }
 
 int
@@ -505,13 +530,22 @@ int *x, *y, *mod;
     int ch;
     coord cc;
     DWORD count;
+    boolean numpad = iflags.num_pad;
+
     really_move_cursor();
     if (iflags.debug_fuzzer)
         return randomkey();
+#ifdef QWERTZ_SUPPORT
+    if (Cmd.swap_yz)
+        numpad |= 0x10;
+#endif
     ch = (program_state.done_hup)
              ? '\033'
              : keyboard_handler.pCheckInput(
-                   console.hConIn, &ir, &count, iflags.num_pad, 1, mod, &cc);
+                   console.hConIn, &ir, &count, numpad, 1, mod, &cc);
+#ifdef QWERTZ_SUPPORT
+    numpad &= ~0x10;
+#endif
     if (!ch) {
         *x = cc.x;
         *y = cc.y;
@@ -571,25 +605,27 @@ int x, y;
     set_console_cursor(x, y);
 }
 
-void
+/* same signature as 'putchar()' with potential failure result ignored */
+int
 xputc(ch)
-char ch;
+int ch;
 {
     set_console_cursor(ttyDisplay->curx, ttyDisplay->cury);
-    xputc_core(ch);
+    xputc_core((char) ch);
+    return 0;
 }
 
 #if 1 /*JP*/
 void
-xputc2_core(ch1, ch2)
-unsigned int ch1;
-unsigned int ch2;
+xputc2_core(str)
+unsigned char *str;
 {
     nhassert(console.cursor.X >= 0 && console.cursor.X < console.width);
     nhassert(console.cursor.Y >= 0 && console.cursor.Y < console.height);
 
     boolean inverse = FALSE;
     cell_t cell;
+    wchar_t wbuf[1];
 
     /* xputc_core()\82©\82ç\82Ì\83R\83s\81[ */
     inverse = (console.current_nhattr[ATR_INVERSE] && iflags.wc_inverse);
@@ -619,12 +655,22 @@ unsigned int ch2;
         }
     }
 
-    cell.character = ch1;
+    int ret = MultiByteToWideChar(
+        CP_ACP,
+        MB_PRECOMPOSED,
+        str,
+        strlen(str),
+        wbuf,
+        1);
+
+    /* \8d\91¤\82É\83\8f\83C\83h\95\8e\9a\8fî\95ñ\82ð\8bl\82ß\82é */
+    cell.character = wbuf[0];
     cell.iskanji = 1;
     buffer_write(console.back_buffer, &cell, console.cursor);
     console.cursor.X++;
 
-    cell.character = ch2;
+    /* \89E\91¤\82Í\83_\83~\81[ */
+    cell.character = 255;
     cell.iskanji = 2;
     buffer_write(console.back_buffer, &cell, console.cursor);
 
@@ -642,17 +688,16 @@ unsigned int ch2;
 }
 
 void
-xputc2(ch1, ch2)
-int ch1;
-int ch2;
+xputc2(str)
+unsigned char *str;
 {
     /* wintty.c \82Å\82Í 1 \83o\83C\83g\96\88\82É curx \82ð\89Á\8eZ\82·\82é\82ª\81A\82±\82±\82Í
-       \83o\83C\83g\82½\82Ü\82Á\82Ä\82©\82ç\8cÄ\82Ñ\8fo\82³\82ê\82é\82Ì\82Å\81A\95\8e\9a\95ª\90æ\82É\90i\82ñ\82Å
-      \82µ\82Ü\82Á\82Ä\82¢\82é\81B\8f]\82Á\82Ä 1 \82ð\88ø\82­\81B */
-    console.cursor.X = ttyDisplay->curx - 1;
+       \83o\83C\83g\82½\82Ü\82Á\82Ä\82©\82ç\8cÄ\82Ñ\8fo\82³\82ê\82é\82Ì\82Å\81An-\95\8e\9a\95ª\90æ\82É\90i\82ñ\82Å
+      \82µ\82Ü\82Á\82Ä\82¢\82é\81B\8f]\82Á\82Ä n-\82ð\88ø\82­\81B */
+    console.cursor.X = ttyDisplay->curx - (strlen(str) - 1);
     console.cursor.Y = ttyDisplay->cury;
 
-    xputc2_core(ch1, ch2);
+    xputc2_core(str);
 }
 #endif
 
@@ -661,7 +706,7 @@ xputs(s)
 const char *s;
 {
     int k;
-    int slen = strlen(s);
+    int slen = (int) strlen(s);
 
     if (ttyDisplay)
         set_console_cursor(ttyDisplay->curx, ttyDisplay->cury);
@@ -713,7 +758,11 @@ char ch;
                                 BACKGROUND_INTENSITY : FOREGROUND_INTENSITY;
 
         cell.attribute = console.attr;
+#if 0 /*JP*/
         cell.character = (console.has_unicode ? console.cpMap[ch] : ch);
+#else
+        cell.character = ch;
+#endif
 #if 1 /*JP*//*\8fí\82É1\83o\83C\83g\95\8e\9a*/
         cell.iskanji = 0;
 #endif
@@ -758,7 +807,11 @@ int in_ch;
     cell_t cell;
 
     cell.attribute = console.attr;
+#if 0 /*JP*/
     cell.character = (console.has_unicode ? cp437[ch] : ch);
+#else
+    cell.character = ch;
+#endif
 #if 1 /*JP*//*\8fí\82É1\83o\83C\83g\95\8e\9a*/
     cell.iskanji = 0;
 #endif
@@ -778,13 +831,37 @@ cl_end()
 void
 raw_clear_screen()
 {
-    buffer_fill_to_end(console.back_buffer, &clear_cell, 0, 0);
+    if (WINDOWPORT("tty")) {
+        cell_t * back = console.back_buffer;
+        cell_t * front = console.front_buffer;
+        COORD pos;
+        DWORD unused;
+
+        for (pos.Y = 0; pos.Y < console.height; pos.Y++) {
+            for (pos.X = 0; pos.X < console.width; pos.X++) {
+                 WriteConsoleOutputAttribute(console.hConOut, &back->attribute,
+                                             1, pos, &unused);
+                 front->attribute = back->attribute;
+                 if (console.has_unicode) {
+                     WriteConsoleOutputCharacterW(console.hConOut,
+                             &back->character, 1, pos, &unused);
+                 } else {
+                     char ch = (char)back->character;
+                     WriteConsoleOutputCharacterA(console.hConOut, &ch, 1, pos,
+                                                         &unused);
+                 }
+                 *front = *back;
+                 back++;
+                 front++;
+            }
+        }
+    }
 }
 
 void
 clear_screen()
 {
-    raw_clear_screen();
+    buffer_fill_to_end(console.back_buffer, &clear_cell, 0, 0);    
     home();
 }
 
@@ -837,7 +914,6 @@ tty_delay_output()
     }
 }
 
-#ifdef TEXTCOLOR
 /*
  * CLR_BLACK           0
  * CLR_RED             1
@@ -910,10 +986,10 @@ init_ttycolor()
 #endif
     init_ttycolor_completed = TRUE;
 }
-#endif /* TEXTCOLOR */
 
+#if 0
 int
-has_color(int color)
+has_color(int color)        /* this function is commented out */
 {
 #ifdef TEXTCOLOR
     if ((color >= 0) && (color < CLR_MAX))
@@ -925,6 +1001,7 @@ has_color(int color)
     else
         return 0;
 }
+#endif
 
 int
 term_attr_fixup(int attrmask)
@@ -1229,7 +1306,7 @@ VA_DECL(const char *, s)
     if (iflags.window_inited)
         end_screen();
     buf[0] = '\n';
-    (void) vsprintf(&buf[1], s, VA_ARGS);
+    (void) vsnprintf(&buf[1], sizeof buf - 1, s, VA_ARGS);
     msmsg(buf);
     really_move_cursor();
     VA_END();
@@ -1660,6 +1737,7 @@ check_and_set_font()
 #endif
 }
 
+#if 0 /*JP*/
 /* check_font_widths returns TRUE if all glyphs in current console font
  * fit within the width of a single console cell.
  */
@@ -1723,8 +1801,8 @@ check_font_widths()
     boolean used[256];
     memset(used, 0, sizeof(used));
     for (int i = 0; i < SYM_MAX; i++) {
-        used[l_syms[i]] = TRUE;
-        used[r_syms[i]] = TRUE;
+        used[primary_syms[i]] = TRUE;
+        used[rogue_syms[i]] = TRUE;
     }
 
     int wcUsedCount = 0;
@@ -1759,6 +1837,7 @@ clean_up:
 
     return all_glyphs_fit;
 }
+#endif
 
 /* set_known_good_console_font sets the code page and font used by the console
  * to settings know to work well with NetHack.  It also saves the original
@@ -1810,6 +1889,7 @@ restore_original_console_font()
     }
 }
 
+#if 0 /*JP*/
 /* set_cp_map() creates a mapping of every possible character of a code
  * page to its corresponding WCHAR.  This is necessary due to the high
  * cost of making calls to MultiByteToWideChar() for every character we
@@ -1840,6 +1920,7 @@ void set_cp_map()
 
     }
 }
+#endif
 
 #if 0
 /* early_raw_print() is used during early game intialization prior to the
@@ -2002,7 +2083,9 @@ void nethack_enter_nttty()
 
     /* check the font before we capture the code page map */
     check_and_set_font();
+#if 0 /*JP*/
     set_cp_map();
+#endif
 
     /* Set console mode */
     DWORD cmode, mask;
@@ -2025,13 +2108,23 @@ void nethack_enter_nttty()
     HKL keyboard_layout = GetKeyboardLayout(0);
     DWORD primary_language = (UINT_PTR) keyboard_layout & 0x3f;
 
-    if (primary_language == LANG_ENGLISH) {
-        if (!load_keyboard_handler("nhdefkey"))
-            error("Unable to load nhdefkey.dll");
-    } else {
-        if (!load_keyboard_handler("nhraykey"))
-            error("Unable to load nhraykey.dll");
+    /* This was overriding the handler that had already
+       been loaded during options parsing. Needs to
+       check first */
+#if 0 /*JP*//* \93ú\96{\8cê\82Å\82Ínhdefkey\82µ\82©\8eg\82í\82È\82¢ */
+    if (!iflags.altkeyhandler[0]) {
+        if (primary_language == LANG_ENGLISH) {
+            if (!load_keyboard_handler("nhdefkey"))
+                error("Unable to load nhdefkey.dll");
+        } else {
+            if (!load_keyboard_handler("nhraykey"))
+                error("Unable to load nhraykey.dll");
+        }
     }
+#else
+    if (!load_keyboard_handler("nhdefkey"))
+        error("nhdefkey.dll\82ð\93Ç\82Ý\8d\9e\82ß\82Ü\82¹\82ñ");
+#endif
 }
 #endif /* TTY_GRAPHICS */
 
@@ -2044,7 +2137,7 @@ VA_DECL(const char *, fmt)
     char buf[ROWNO * COLNO]; /* worst case scenario */
     VA_START(fmt);
     VA_INIT(fmt, const char *);
-    Vsprintf(buf, fmt, VA_ARGS);
+    (void) vsnprintf(buf, sizeof buf, fmt, VA_ARGS);
     if (redirect_stdout)
         fprintf(stdout, "%s", buf);
     else {