Parcourir la source

* Properly handling, when closing the consoles via X button on windows.

git-svn-id: https://svn.code.sf.net/p/rathena/svn/trunk@15706 54d463be-8e91-2dee-dedb-b68131a5f0ec
xazax-hun il y a 13 ans
Parent
commit
7e0fe2960f
1 fichiers modifiés avec 35 ajouts et 0 suppressions
  1. 35 0
      src/common/core.c

+ 35 - 0
src/common/core.c

@@ -18,6 +18,8 @@
 #include <string.h>
 #ifndef _WIN32
 #include <unistd.h>
+#else
+#include <windows.h> // Console close event handling
 #endif
 
 
@@ -68,6 +70,35 @@ sigfunc *compat_signal(int signo, sigfunc *func)
 }
 #endif
 
+/*======================================
+ *	CORE : Console events for Windows
+ *--------------------------------------*/
+#ifdef _WIN32
+static BOOL WINAPI console_handler(DWORD c_event)
+{
+    switch(c_event)
+    {
+    case CTRL_CLOSE_EVENT:
+    case CTRL_LOGOFF_EVENT:
+    case CTRL_SHUTDOWN_EVENT:
+		if( shutdown_callback != NULL )
+			shutdown_callback();
+		else
+			runflag = CORE_ST_STOP;// auto-shutdown
+        break;
+	default:
+		break;
+    }
+    return TRUE;
+}
+
+static void cevents_init()
+{
+	if (SetConsoleCtrlHandler(console_handler,TRUE)==FALSE)
+		ShowWarning ("Unable to install the console handler!\n");
+}
+#endif
+
 /*======================================
  *	CORE : Signal Sub Function
  *--------------------------------------*/
@@ -251,6 +282,10 @@ int main (int argc, char **argv)
 	db_init();
 	signals_init();
 
+#ifdef _WIN32
+	cevents_init();
+#endif
+
 	timer_init();
 	socket_init();
 	plugins_init();