(svn r1547) -Feature: windows dedicated (if anyone would run that, but ok), is now functioning correctly. There is no other way but to create a new thread, but that's only MS braindeadness
authordarkvater
Sun, 16 Jan 2005 18:19:33 +0000
changeset 1046 d46ccd96c40f
parent 1045 a8de7d7a83f1
child 1047 d4c04332d837
(svn r1547) -Feature: windows dedicated (if anyone would run that, but ok), is now functioning correctly. There is no other way but to create a new thread, but that's only MS braindeadness
-Fix: [1103113] font size changing. Dedicated server did not have code filtering, 'tab' could result in bigger fonts
-Fix: [1103069] console backspace key with ded server; fixed due to normal console handling
-Fix: [1101963] console in dedicated server; see above
-Fix: dedicated server also writes to log file if active
console.c
console_cmds.c
dedicated.c
--- a/console.c	Sun Jan 16 17:19:37 2005 +0000
+++ b/console.c	Sun Jan 16 18:19:33 2005 +0000
@@ -192,9 +192,8 @@
 					IConsolePrintF(_iconsole_color_commands, "] %s", _iconsole_cmdline);
 					_iconsole_cmdbufferpos = 19;
 					IConsoleCmdBufferAdd(_iconsole_cmdline);
-					if (strlen(_iconsole_cmdline) != 0) // only execute if there is something typed obviously
-						IConsoleCmdExec(_iconsole_cmdline);
 
+					IConsoleCmdExec(_iconsole_cmdline);
 					IConsoleClearCommand();
 					break;
 				case WKC_CTRL | WKC_RETURN:
@@ -271,11 +270,31 @@
 		free(_iconsole_buffer[i]);
 }
 
+static void IConsoleWriteToLogFile(const char* string)
+{
+	if (_iconsole_output_file != NULL) {
+		// if there is an console output file ... also print it there
+		fwrite(string, strlen(string), 1, _iconsole_output_file);
+		fwrite("\n", 1, 1, _iconsole_output_file);
+	}
+}
+
+bool CloseConsoleLogIfActive(void)
+{
+	if (_iconsole_output_file != NULL) {
+		IConsolePrintF(_iconsole_color_default, "file output complete");
+		fclose(_iconsole_output_file);
+		return true;
+	}
+
+	return false;
+}
+
 void IConsoleFree(void)
 {
 	_iconsole_inited = false;
 	IConsoleClear();
-	if (_iconsole_output_file != NULL) fclose(_iconsole_output_file);
+	CloseConsoleLogIfActive();
 }
 
 void IConsoleResize(void)
@@ -382,6 +401,7 @@
 
 	if (_network_dedicated) {
 		printf("%s\n", string);
+		IConsoleWriteToLogFile(string);
 		return;
 	}
 
@@ -403,11 +423,7 @@
 	}
 	free(_ex);
 
-	if (_iconsole_output_file != NULL) {
-		// if there is an console output file ... also print it there
-		fwrite(string, strlen(string), 1, _iconsole_output_file);
-		fwrite("\n", 1, 1, _iconsole_output_file);
-	}
+	IConsoleWriteToLogFile(string);
 
 	if (_iconsole_win != NULL) SetWindowDirty(_iconsole_win);
 }
@@ -1072,6 +1088,9 @@
 	uint c;
 	uint i;
 	uint l;
+	
+	if (strlen(cmdstr) == 0) // only execute if there is something typed obviously
+		return;
 
 	if (_stdlib_con_developer)
 		IConsolePrintF(_iconsole_color_debug, "CONDEBUG: execution_cmdline: %s", cmdstr);
--- a/console_cmds.c	Sun Jan 16 17:19:37 2005 +0000
+++ b/console_cmds.c	Sun Jan 16 18:19:33 2005 +0000
@@ -607,17 +607,7 @@
 /* **************************** */
 /*   default console commands   */
 /* **************************** */
-bool CloseConsoleLogIfActive()
-{
-	extern FILE* _iconsole_output_file;
-	if (_iconsole_output_file != NULL) {
-		IConsolePrintF(_iconsole_color_default, "file output complete");
-		fclose(_iconsole_output_file);
-		return true;
-	}
-
-	return false;
-}
+extern bool CloseConsoleLogIfActive(void);
 
 DEF_CONSOLE_CMD(ConScript)
 {
--- a/dedicated.c	Sun Jan 16 17:19:37 2005 +0000
+++ b/dedicated.c	Sun Jan 16 18:19:33 2005 +0000
@@ -88,6 +88,40 @@
 }
 #endif
 
+#ifdef WIN32
+HANDLE hEvent;
+static HANDLE hThread; // Thread to close
+static char _win_console_thread_buffer[200];
+
+/* Windows Console thread. Just loop and signal when input has been received */
+void WINAPI CheckForConsoleInput(void)
+{
+	while (true) {
+		fgets(_win_console_thread_buffer, lengthof(_win_console_thread_buffer), stdin);
+		SetEvent(hEvent); // signal input waiting that the line is ready
+	}
+}
+
+void CreateWindowsConsoleThread(void)
+{
+	/* Create event to signal when console input is ready */
+	hEvent = CreateEvent(NULL, false, false, "keyboard input");    
+
+	hThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)CheckForConsoleInput, 0, 0, NULL);
+	if (hThread == NULL)
+		error("Cannot create console thread!");
+
+	DEBUG(misc, 0) ("Windows console thread started...");
+}
+
+void CloseWindowsConsoleThread(void)
+{
+	CloseHandle(hThread);
+	DEBUG(misc, 0) ("Windows console thread shut down...");
+}
+
+#endif
+
 static const char *DedicatedVideoStart(char **parm) {
 	_screen.width = _screen.pitch = _cur_resolution[0];
 	_screen.height = _cur_resolution[1];
@@ -99,6 +133,7 @@
 #ifdef WIN32
 	// For win32 we need to allocate an console (debug mode does the same)
 	CreateConsole();
+	CreateWindowsConsoleThread();
 	SetConsoleTitle("OpenTTD Dedicated Server");
 #endif
 
@@ -110,13 +145,20 @@
 	DEBUG(misc,0)("Loading dedicated server...");
 	return NULL;
 }
-static void DedicatedVideoStop() { free(_dedicated_video_mem); }
+
+static void DedicatedVideoStop(void)
+{
+#ifdef WIN32
+	CloseWindowsConsoleThread();
+#endif
+	free(_dedicated_video_mem); 
+}
+
 static void DedicatedVideoMakeDirty(int left, int top, int width, int height) {}
 static bool DedicatedVideoChangeRes(int w, int h) { return false; }
 
 #ifdef UNIX
-
-static bool InputWaiting()
+static bool InputWaiting(void)
 {
 	struct timeval tv;
 	fd_set readfds;
@@ -133,49 +175,55 @@
 
 	if (ret > 0)
 		return true;
-	else
-		return false;
+
+	return false;
 }
 #else
-static bool InputWaiting()
+static bool InputWaiting(void)
 {
-	return kbhit();
+	if (WaitForSingleObject(hEvent, 1) == WAIT_OBJECT_0)
+		return true;
+  
+	return false;
 }
 #endif
 
-static void DedicatedHandleKeyInput()
+static void DedicatedHandleKeyInput(void)
 {
-#ifdef WIN32
-	char input;
-#endif
 	static char input_line[200] = "";
 
+	if (!InputWaiting())
+		return;
+
+	if (_exit_game)
+		return;
+
 #if defined(UNIX) || defined(__OS2__)
-	if (InputWaiting()) {
-		if (_exit_game)
-			return;
+		fgets(input_line, lengthof(input_line), stdin);
+#else
+		strncpy(input_line, _win_console_thread_buffer, lengthof(input_line));
+#endif
 
-		fgets(input_line, 200, stdin);
-		// Forget about the final \n (or \r)
-		strtok(input_line, "\r\n");
-		IConsoleCmdExec(input_line);
-	}
-#else
-	if (InputWaiting()) {
-		input = getch();
-		printf("%c", input);
-		if (input != '\r')
-			snprintf(input_line, 200, "%s%c", input_line, input);
-		else {
-			printf("\n");
-			IConsoleCmdExec(input_line);
-			input_line[0] = '\0';
+	/* XXX - strtok() does not 'forget' \n\r if it is the first character! */
+	strtok(input_line, "\r\n"); // Forget about the final \n (or \r)
+	{ /* Remove any special control characters */
+		uint i;
+		for (i = 0; i < lengthof(input_line); i++) {
+			if (input_line[i] == '\n' || input_line[i] == '\r') // cut missed beginning '\0'
+				input_line[i] = '\0';
+
+			if (input_line[i] == '\0')
+				break;
+
+			if (!IS_INT_INSIDE(input_line[i], ' ', 256))
+				input_line[i] = ' ';
 		}
 	}
-#endif
+
+	IConsoleCmdExec(input_line); // execute command
 }
 
-static int DedicatedVideoMainLoop()
+static int DedicatedVideoMainLoop(void)
 {
 #ifndef WIN32
 	struct timeval tim;
@@ -259,10 +307,10 @@
 }
 
 void DedicatedFork(void) {}
-static void DedicatedVideoStop() { free(_dedicated_video_mem); }
+static void DedicatedVideoStop(void) { free(_dedicated_video_mem); }
 static void DedicatedVideoMakeDirty(int left, int top, int width, int height) {}
 static bool DedicatedVideoChangeRes(int w, int h) { return false; }
-static int DedicatedVideoMainLoop() { return ML_QUIT; }
+static int DedicatedVideoMainLoop(void) { return ML_QUIT; }
 
 const HalVideoDriver _dedicated_video_driver = {
 	DedicatedVideoStart,