Add support for standard pdcurses, thanks to Leonard for pointing out the solutions to a few mysteries.

Thu, 23 Jul 2015 02:15:21 +0300

author
Teemu Piippo <tsapii@utu.fi>
date
Thu, 23 Jul 2015 02:15:21 +0300
changeset 89
777b2a10b835
parent 88
08ccaf26cffd
child 90
a30130330981

Add support for standard pdcurses, thanks to Leonard for pointing out the solutions to a few mysteries.

CMakeLists.txt file | annotate | diff | comparison | revisions
sources/basics.h file | annotate | diff | comparison | revisions
sources/coloredline.h file | annotate | diff | comparison | revisions
sources/cursesstuff.h file | annotate | diff | comparison | revisions
sources/interface.cpp file | annotate | diff | comparison | revisions
sources/interface.h file | annotate | diff | comparison | revisions
sources/main.cpp file | annotate | diff | comparison | revisions
sources/main.h file | annotate | diff | comparison | revisions
--- a/CMakeLists.txt	Thu Jul 23 01:52:04 2015 +0300
+++ b/CMakeLists.txt	Thu Jul 23 02:15:21 2015 +0300
@@ -57,16 +57,27 @@
 	add_definitions ("-D_CRT_SEURE_NO_WARNINGS")
 	target_link_libraries (${TARGET_NAME} wsock32 ws2_32)
 
-	if (PDCURSES_WIN32A_PATH)
-		include_directories (${PDCURSES_WIN32A_PATH}/include)
+	if (PDCURSES_PATH OR PDCURSES_WIN32A_PATH)
+		if (NOT PDCURSES_PATH)
+			set (PDCURSES_PATH "${PDCURSES_WIN32A_PATH}")
+			add_definitions (-DHAVE_PDCURSES_WIN32A)
+		endif()
+
+		include_directories (${PDCURSES_PATH}/include)
 
 		if (MINGW)
-			target_link_libraries (${TARGET_NAME} ${PDCURSES_WIN32A_PATH}/lib/pdcurses.a)
+			target_link_libraries (${TARGET_NAME} ${PDCURSES_PATH}/lib/pdcurses.a)
 		else()
-			target_link_libraries (${TARGET_NAME} ${PDCURSES_WIN32A_PATH}/lib/pdcurses.lib)
+			target_link_libraries (${TARGET_NAME} ${PDCURSES_PATH}/lib/pdcurses.lib)
 		endif()
 	else()
-		message (SEND_ERROR "Must give PDCURSES_WIN32A_PATH on Windows")
+		message (SEND_ERROR "Must provide PDCURSES_PATH or PDCURSES_WIN32A_PATH on Windows")
+
+		if (MINGW)
+			message (SEND_ERROR "This path must contain pdcurses.a in lib/, and curses.h in include/.")
+		else()
+			message (SEND_ERROR "This path must contain pdcurses.lib in lib/, and curses.h in include/.")
+		endif()
 	endif()
 else()
 	include_directories (${CURSES_INCUDE_DIRS}) # sic
--- a/sources/basics.h	Thu Jul 23 01:52:04 2015 +0300
+++ b/sources/basics.h	Thu Jul 23 02:15:21 2015 +0300
@@ -36,30 +36,11 @@
 #define MACRO_TO_STRING_2(A) #A
 #define MACRO_TO_STRING(A) MACRO_TO_STRING_2(A)
 
+// The Windows SDK appears to use identifiers that conflicts with the identifiers defined in ZFC,
+// so they have to be put in a namespace.
 #define BEGIN_ZFC_NAMESPACE namespace zfc {
 #define END_ZFC_NAMESPACE }
 
-BEGIN_ZFC_NAMESPACE
-
-class String;
-
-// -------------------------------------------------------------------------------------------------
-//
-enum Color
-{
-	BLACK,
-	RED,
-	GREEN,
-	YELLOW,
-	BLUE,
-	MAGENTA,
-	CYAN,
-	WHITE,
-	DEFAULT,
-
-	NUM_COLORS
-};
-
 // Goddamnit, MSVC
 #ifdef _MSC_VER
 # define and &&
@@ -85,6 +66,8 @@
 #define TEXTCOLOR_BrightCyan	TEXTCOLOR_Escape "V"
 #define TEXTCOLOR_Reset			TEXTCOLOR_Escape "-"
 
+BEGIN_ZFC_NAMESPACE
+
 template<typename T>
 T min (T a, T b)
 {
@@ -104,4 +87,5 @@
 }
 
 struct Exitception {};
+
 END_ZFC_NAMESPACE
--- a/sources/coloredline.h	Thu Jul 23 01:52:04 2015 +0300
+++ b/sources/coloredline.h	Thu Jul 23 02:15:21 2015 +0300
@@ -32,6 +32,23 @@
 #include "main.h"
 BEGIN_ZFC_NAMESPACE
 
+// The order of these colors appears to differ between curses distributions (PDCurses and its
+// win32a for instance have blue and red swapped). So we need to explicitly define the values
+// of the enumerators based on their curses values.
+enum Color
+{
+	BLACK = COLOR_BLACK,
+	RED = COLOR_RED,
+	GREEN = COLOR_GREEN,
+	YELLOW = COLOR_YELLOW,
+	BLUE = COLOR_BLUE,
+	MAGENTA = COLOR_MAGENTA,
+	CYAN = COLOR_CYAN,
+	WHITE = COLOR_WHITE,
+	DEFAULT = 8,
+	NUM_COLORS
+};
+
 // -------------------------------------------------------------------------------------------------
 //
 enum
--- a/sources/interface.cpp	Thu Jul 23 01:52:04 2015 +0300
+++ b/sources/interface.cpp	Thu Jul 23 02:15:21 2015 +0300
@@ -41,7 +41,7 @@
 
 // -------------------------------------------------------------------------------------------------
 //
-int Interface::color_pair (Color fg, Color bg)
+chtype Interface::color_pair (Color fg, Color bg)
 {
 	return COLOR_PAIR (1 + (int (fg) * NUM_COLORS) + int (bg));
 }
@@ -209,7 +209,7 @@
 {
 	if (Title.length() <= COLS)
 	{
-		int pair = color_pair (WHITE, BLUE);
+		chtype pair = color_pair (WHITE, BLUE);
 		int startx = (COLS - Title.length()) / 2;
 		int endx = startx + Title.length();
 		attron (pair);
@@ -436,7 +436,7 @@
 //
 void Interface::render_input()
 {
-	int promptColor = color_pair (WHITE, BLUE);
+	chtype promptColor = color_pair (WHITE, BLUE);
 
 	// If we're asking the user if they want to disconnect, we don't render any input strings,
 	// just the confirmation message.
@@ -497,7 +497,7 @@
 //
 void Interface::render_statusbar()
 {
-	int color = color_pair (WHITE, BLUE);
+	chtype color = color_pair (WHITE, BLUE);
 	int y = LINES - 1;
 	attron (color);
 	mvhline (y, 0, ' ', COLS);
@@ -814,6 +814,7 @@
 		break;
 
 	case '\n':
+	case '\r':
 	case KEY_ENTER:
 		switch (CurrentInputState)
 		{
--- a/sources/interface.h	Thu Jul 23 01:52:04 2015 +0300
+++ b/sources/interface.h	Thu Jul 23 02:15:21 2015 +0300
@@ -33,6 +33,8 @@
 #include "network/ipaddress.h"
 #include "coloredline.h"
 #include "network/rconsession.h"
+#include "coloredline.h"
+
 BEGIN_ZFC_NAMESPACE
 
 class Interface
@@ -94,7 +96,7 @@
 	void render_input();
 	void render_statusbar();
 	void position_cursor();
-	int color_pair (Color fg, Color bg);
+	chtype color_pair (Color fg, Color bg);
 	const String& current_input();
 	void detach_input();
 	String& mutable_current_input();
--- a/sources/main.cpp	Thu Jul 23 01:52:04 2015 +0300
+++ b/sources/main.cpp	Thu Jul 23 02:15:21 2015 +0300
@@ -46,7 +46,10 @@
 int main (int argc, char* argv[])
 {
 #ifdef _WIN32
+# ifdef HAVE_PDCURSES_WIN32A
+	// PDCurses-win32a uses a GUI window of its own so we need to destroy the console window.
 	FreeConsole();
+# endif
 
 	WSADATA wsaData;
 	int result = WSAStartup (MAKEWORD (2, 2), &wsaData);
--- a/sources/main.h	Thu Jul 23 01:52:04 2015 +0300
+++ b/sources/main.h	Thu Jul 23 02:15:21 2015 +0300
@@ -36,6 +36,7 @@
 #include <cstring>
 #include <cctype>
 #include <ctime>
+#include <curses.h>
 #include "basics.h"
 #include "mystring.h"
 #include "geometry.h"

mercurial