Author: Göran Weinholt <weinholt@debian.org>, Rhonda D'Vine <rhonda@debian.org>	vim:ft=diff:
Description: Make tetradraw able to work in an utf8 environment

Index: VCS/src/Makefile.in
===================================================================
--- VCS.orig/src/Makefile.in	2016-01-10 19:09:12.255511563 +0100
+++ VCS/src/Makefile.in	2016-01-10 19:09:12.251511543 +0100
@@ -140,7 +140,7 @@
 
 MAINTAINERCLEANFILES = Makefile.in
 EXTRA_DIST = art/* *.h
-INCLUDES = -I$(top_srcdir) -I.
+INCLUDES = -I$(top_srcdir) -I. -I/usr/include/ncursesw -DWIDE_CURSES -D_XOPEN_SOURCE_EXTENDED
 
 bin_PROGRAMS = tetradraw tetraview
 
@@ -148,13 +148,13 @@
  network.c options_io.c sauce.c save.c term.c tetradraw.c 
 
 
-tetradraw_LDADD = -lncurses -lmenu
+tetradraw_LDADD = -lncursesw -lmenuw
 
 tetraview_SOURCES = editor.c interface.c internal.c load.c network.c \
 	multidraw.c options_io.c block.c sauce.c term.c tetraview.c
 
 
-tetraview_LDADD = -lncurses -lmenu
+tetraview_LDADD = -lncursesw -lmenuw
 subdir = src
 ACLOCAL_M4 = $(top_srcdir)/aclocal.m4
 mkinstalldirs = $(SHELL) $(top_srcdir)/mkinstalldirs
Index: VCS/src/interface.c
===================================================================
--- VCS.orig/src/interface.c	2016-01-10 19:09:12.255511563 +0100
+++ VCS/src/interface.c	2016-01-10 19:09:12.251511543 +0100
@@ -19,8 +19,8 @@
 
 
 
-#include <ncurses.h>
-#include <menu.h>
+#include <ncursesw/ncurses.h>
+#include <ncursesw/menu.h>
 #include <ctype.h>
 #include <sys/types.h>
 #include <sys/stat.h>
@@ -61,6 +61,22 @@
 #include "art/exit_logo.h"
 
 
+#ifdef WIDE_CURSES
+# include "unitable.h"
+/* Convert cp437 characters to unicode if in a unicode locale. */
+void showchar(int y, int x, const chtype ch) {
+	if (unicode_term) {
+		cchar_t cc;
+
+		attrset(ch & ~0xFF & ~A_ALTCHARSET);
+		setcchar(&cc, unicode_table[ch & 0xFF], 0, 0, NULL);
+		mvadd_wch(y, x, &cc);
+	} else {
+		mvwaddch(stdscr, y, x, ch);
+	}
+}
+#endif
+
 /* this function draws the main editor screen */
 void draw_editor(canvas *page) {
 
Index: VCS/src/interface.h
===================================================================
--- VCS.orig/src/interface.h	2016-01-10 19:09:12.255511563 +0100
+++ VCS/src/interface.h	2016-01-10 19:09:12.251511543 +0100
@@ -75,4 +75,11 @@
 void options_screen();
 int quicksave_prompt();
 
+#ifdef WIDE_CURSES
+/* Replace the mvaddch function with our showchar function. */
+void showchar(int y, int x, const chtype ch);
+# undef mvaddch
+# define mvaddch(y,x,ch) showchar(y,x,ch)
+#endif
+
 #endif
Index: VCS/src/term.c
===================================================================
--- VCS.orig/src/term.c	2016-01-10 19:09:12.255511563 +0100
+++ VCS/src/term.c	2016-01-10 19:09:12.251511543 +0100
@@ -17,8 +17,10 @@
 
 
 
-#include <ncurses.h>
+#include <ncursesw/ncurses.h>
 #include <stdlib.h>
+#include <locale.h>
+#include <langinfo.h>
 
 #include "types.h"
 #include "colours.h"
@@ -29,8 +31,12 @@
 #include "keys.h"
 
 
+/* Terminal is Linux and/or unicoded. */
+int linux_term = 0, unicode_term = 0;
+
 /* this function binds the key */
 void setup_keys() {
+#ifndef NEW_KEYCODES
 	define_key("\e[1~", TD_KEY_HOME);
 	define_key("\e[2~", TD_KEY_INSERT);
 	define_key("\e[3~", TD_KEY_DEL);
@@ -50,6 +56,7 @@
 	define_key("\e[21~", TD_KEY_F10);
 	define_key("\e[23~", TD_KEY_F11);
 	define_key("\e[24~", TD_KEY_F12);
+#endif
 
 	define_key("\eb", TD_KEY_BLOCK);
 	define_key("\eB", TD_KEY_BLOCK);
@@ -119,14 +126,26 @@
 	define_key("\e\t", TD_KEY_BTAB);
 }
 
-/* this sets the ibm character set */
+/* This sets the terminal's character set. */
 void setup_charset() {
-	/* \033(U IBM VGA font */
-	/* \033(B Latin-1 font */
-	/* \033(0 DEC font */
-
-	/* set ibm font since it has the higher ascii characters */
-	printf("\e(U");
+	setlocale(LC_CTYPE, "");
+#ifdef WIDE_CURSES
+	unicode_term = !strcmp(nl_langinfo(CODESET), "UTF-8");
+#endif
+
+	if (unicode_term) {
+		printf("\e%%G");
+	} else if (linux_term) {
+		/* \033(U IBM VGA font */
+		/* \033(B Latin-1 font */
+		/* \033(0 DEC font */
+		/* set ibm font since it has the higher ascii characters */
+		printf("\e(U");
+	} else {
+		fprintf(stderr, "Warning: tetradraw and tetraview"
+						" have only been tested under the linux-console\n"
+						"and in unicode capable terminals and locales\n");
+	}
 }
 
 /* this function clears the screen */
@@ -152,13 +171,8 @@
 
 /* this function starts up ncurses */
 void setup_ncurses() {
-	char *term = NULL;
-
-	term = getenv("TERM");
-	if(strcmp(term, "linux")) {
-		fprintf(stderr, "Warning: tetradraw and tetraview"
-				" have only been tested under the linux-console\n");
-	}
+	if(!strcmp(getenv("TERM"), "linux"))
+		linux_term = 1;
 	initscr();
 	cbreak();
 	noecho();
@@ -176,7 +190,7 @@
 	/* as the linux console doesnt support it yet */
 	/* And ansi is supposed to be 80x25 */
 	//td_maxy = 24;
-	td_maxx = 80;
+	td_maxy--;
 }
 
 /* this function sets up the colours */
Index: VCS/src/term.h
===================================================================
--- VCS.orig/src/term.h	2016-01-10 19:09:12.255511563 +0100
+++ VCS/src/term.h	2016-01-10 19:09:12.251511543 +0100
@@ -25,4 +25,6 @@
  *  */
 void bind_string(char *, int);
 
+extern int linux_term, unicode_term;
+
 #endif
Index: VCS/src/unitable.h
===================================================================
--- /dev/null	1970-01-01 00:00:00.000000000 +0000
+++ VCS/src/unitable.h	2016-01-10 19:09:12.251511543 +0100
@@ -0,0 +1,45 @@
+#ifndef _UNITABLE_H
+#define _UNITABLE_H
+
+/* Table for conversion from cp437 to unicode, almost. */
+const wchar_t *unicode_table[0x100] = {
+       L" ", L"\x263A", L"\x263B", L"\x2665", L"\x2666", L"\x2663", L"\x2660",
+       L" ",      L" ",      L" ",      L" ",      L" ",      L" ",      L" ",
+       L" ",      L" ", L"\x25C0", L"\x25B6", L"\x2195",      L" ",   L"\xB6",
+    L"\xA7",      L" ",      L" ", L"\x2191", L"\x2193",      L" ",      L" ",
+       L" ", L"\x2194", L"\x25B2", L"\x25BC",      L" ",      L"!",     L"\"",
+       L"#",      L"$",      L"%",      L"&",      L"'",      L"(",      L")",
+       L"*",      L"+",      L",",      L"-",      L".",      L"/",      L"0",
+       L"1",      L"2",      L"3",      L"4",      L"5",      L"6",      L"7",
+       L"8",      L"9",      L":",      L";",      L"<",      L"=",      L">",
+       L"?",      L"@",      L"A",      L"B",      L"C",      L"D",      L"E",
+       L"F",      L"G",      L"H",      L"I",      L"J",      L"K",      L"L",
+       L"M",      L"N",      L"O",      L"P",      L"Q",      L"R",      L"S",
+       L"T",      L"U",      L"V",      L"W",      L"X",      L"Y",      L"Z",
+       L"[",     L"\\",      L"]",      L"^",      L"_",      L"`",      L"a",
+       L"b",      L"c",      L"d",      L"e",      L"f",      L"g",      L"h",
+       L"i",      L"j",      L"k",      L"l",      L"m",      L"n",      L"o",
+       L"p",      L"q",      L"r",      L"s",      L"t",      L"u",      L"v",
+       L"w",      L"x",      L"y",      L"z",      L"{",      L"|",      L"}",
+       L"~",      L" ",   L"\xC7",   L"\xFC",   L"\xE9",   L"\xE2",   L"\xE4",
+    L"\xE0",   L"\xE5",   L"\xE7",   L"\xEA",   L"\xEB",   L"\xE8",   L"\xEF",
+    L"\xEE",   L"\xEC",   L"\xC4",   L"\xC5",   L"\xC9",   L"\xE6",   L"\xC6",
+    L"\xF4",   L"\xF6",   L"\xF2",   L"\xFB",   L"\xF9",   L"\xFF",   L"\xD6",
+    L"\xDC",      L" ",   L"\xA3",   L"\xA5", L"\x20A7",  L"\x192",   L"\xE1",
+    L"\xED",   L"\xF3",   L"\xFA",   L"\xF1",   L"\xD1",   L"\xAA",   L"\xBA",
+    L"\xBF", L"\x2310",   L"\xAC",   L"\xBD",   L"\xBC",   L"\xA1",   L"\xAB",
+    L"\xBB", L"\x2591", L"\x2592", L"\x2593", L"\x2502", L"\x2524", L"\x2561",
+  L"\x2562", L"\x2556", L"\x2555", L"\x2563", L"\x2551", L"\x2557", L"\x255D",
+  L"\x255C", L"\x255B", L"\x2510", L"\x2514", L"\x2534", L"\x252C", L"\x251C",
+  L"\x2500", L"\x253C", L"\x255E", L"\x255F", L"\x255A", L"\x2554", L"\x2569",
+  L"\x2566", L"\x2560", L"\x2550", L"\x256C", L"\x2567", L"\x2568", L"\x2564",
+  L"\x2565", L"\x2559", L"\x2558", L"\x2552", L"\x2553", L"\x256B", L"\x256A",
+  L"\x2518", L"\x250C", L"\x2588", L"\x2584", L"\x258C", L"\x2590", L"\x2580",
+   L"\x3B1",   L"\xDF",  L"\x393",  L"\x3C0",  L"\x3A3",  L"\x3C3",   L"\xB5",
+   L"\x3C4",  L"\x3A6",  L"\x398",  L"\x3A9",  L"\x3B4", L"\x221E",  L"\x3C6",
+   L"\x3B5", L"\x2229", L"\x2261",   L"\xB1", L"\x2265", L"\x2264", L"\x2320",
+  L"\x2321",   L"\xF7", L"\x2248",   L"\xB0", L"\x2219",   L"\xB7", L"\x221A",
+  L"\x207F",   L"\xB2", L"\x25A0",      L" "
+};
+
+#endif
Index: VCS/src/block.c
===================================================================
--- VCS.orig/src/block.c	2016-01-10 19:09:12.255511563 +0100
+++ VCS/src/block.c	2016-01-10 19:09:12.251511543 +0100
@@ -18,7 +18,7 @@
 
 
 
-#include <ncurses.h>
+#include <ncursesw/ncurses.h>
 
 #include "types.h"
 #include "colours.h"
Index: VCS/src/editor.c
===================================================================
--- VCS.orig/src/editor.c	2016-01-10 19:09:12.255511563 +0100
+++ VCS/src/editor.c	2016-01-10 19:09:12.251511543 +0100
@@ -20,7 +20,7 @@
 #include <stdlib.h>
 #include <string.h>
 #include <ctype.h>
-#include <ncurses.h>
+#include <ncursesw/ncurses.h>
 
 #include "internal.h"
 #include "types.h"
Index: VCS/src/load.c
===================================================================
--- VCS.orig/src/load.c	2016-01-10 19:09:12.255511563 +0100
+++ VCS/src/load.c	2016-01-10 19:09:12.251511543 +0100
@@ -19,7 +19,7 @@
 
 
 
-#include <ncurses.h>
+#include <ncursesw/ncurses.h>
 #include <ctype.h>
 #include <string.h>
 #include <unistd.h>
Index: VCS/src/multidraw.c
===================================================================
--- VCS.orig/src/multidraw.c	2016-01-10 19:09:12.255511563 +0100
+++ VCS/src/multidraw.c	2016-01-10 19:09:12.251511543 +0100
@@ -23,7 +23,7 @@
 #include <stdio.h>
 #include <sys/types.h>
 #include <sys/socket.h>
-#include <ncurses.h>
+#include <ncursesw/ncurses.h>
 #include <stdarg.h>
 
 #include "internal.h"
Index: VCS/src/network.c
===================================================================
--- VCS.orig/src/network.c	2016-01-10 19:09:12.255511563 +0100
+++ VCS/src/network.c	2016-01-10 19:09:12.251511543 +0100
@@ -31,7 +31,7 @@
 #include <unistd.h>
 #include <string.h>
 
-#include <ncurses.h>
+#include <ncursesw/ncurses.h>
 
 #include "types.h"
 #include "colours.h"
Index: VCS/src/options_io.c
===================================================================
--- VCS.orig/src/options_io.c	2016-01-10 19:09:12.255511563 +0100
+++ VCS/src/options_io.c	2016-01-10 19:09:12.255511563 +0100
@@ -16,7 +16,7 @@
  * */
 
 
-#include <ncurses.h>
+#include <ncursesw/ncurses.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
Index: VCS/src/sauce.c
===================================================================
--- VCS.orig/src/sauce.c	2016-01-10 19:09:12.255511563 +0100
+++ VCS/src/sauce.c	2016-01-10 19:09:12.255511563 +0100
@@ -18,7 +18,7 @@
 
 
 
-#include <ncurses.h>
+#include <ncursesw/ncurses.h>
 #include <time.h>
 #include <string.h>
 
Index: VCS/src/save.c
===================================================================
--- VCS.orig/src/save.c	2016-01-10 19:09:12.255511563 +0100
+++ VCS/src/save.c	2016-01-10 19:09:12.255511563 +0100
@@ -19,7 +19,7 @@
 
 
 #include <stdio.h>
-#include <ncurses.h>
+#include <ncursesw/ncurses.h>
 #include <stdlib.h>
 
 #include "internal.h"
Index: VCS/src/tetradraw.c
===================================================================
--- VCS.orig/src/tetradraw.c	2016-01-10 19:09:12.255511563 +0100
+++ VCS/src/tetradraw.c	2016-01-10 19:09:12.255511563 +0100
@@ -18,7 +18,7 @@
  * */
 
 
-#include <ncurses.h>
+#include <ncursesw/ncurses.h>
 #include <ctype.h>
 #include <stdlib.h>
 #include <string.h>
Index: VCS/src/tetraview.c
===================================================================
--- VCS.orig/src/tetraview.c	2016-01-10 19:09:12.255511563 +0100
+++ VCS/src/tetraview.c	2016-01-10 19:09:12.255511563 +0100
@@ -17,8 +17,8 @@
  * */
 
 
-#include <ncurses.h>
-#include <menu.h>
+#include <ncursesw/ncurses.h>
+#include <ncursesw/menu.h>
 #include <ctype.h>
 
 #include "types.h"
Index: VCS/src/Makefile.am
===================================================================
--- VCS.orig/src/Makefile.am	2016-01-10 19:09:12.255511563 +0100
+++ VCS/src/Makefile.am	2016-01-10 19:09:33.951619148 +0100
@@ -13,9 +13,9 @@
  network.c options_io.c sauce.c save.c term.c tetradraw.c 
 
 
-tetradraw_LDADD = -lncurses -lmenu
+tetradraw_LDADD = -lncursesw -lmenuw
 
 tetraview_SOURCES = editor.c interface.c internal.c load.c network.c \
 	multidraw.c options_io.c block.c sauce.c term.c tetraview.c
 
-tetraview_LDADD = -lncurses -lmenu
+tetraview_LDADD = -lncursesw -lmenuw
