| 1 |
/************************************************************************ |
| 2 |
* winmain.c Startup code and support for the Win32 platform * |
| 3 |
* Copyright (C) 1998-2022 Ben Webb * |
| 4 |
* Email: [email protected] * |
| 5 |
* WWW: https://dopewars.sourceforge.io/ * |
| 6 |
* * |
| 7 |
* This program is free software; you can redistribute it and/or * |
| 8 |
* modify it under the terms of the GNU General Public License * |
| 9 |
* as published by the Free Software Foundation; either version 2 * |
| 10 |
* of the License, or (at your option) any later version. * |
| 11 |
* * |
| 12 |
* This program is distributed in the hope that it will be useful, * |
| 13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 15 |
* GNU General Public License for more details. * |
| 16 |
* * |
| 17 |
* You should have received a copy of the GNU General Public License * |
| 18 |
* along with this program; if not, write to the Free Software * |
| 19 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, * |
| 20 |
* MA 02111-1307, USA. * |
| 21 |
************************************************************************/ |
| 22 |
|
| 23 |
#ifdef HAVE_CONFIG_H |
| 24 |
#include <config.h> |
| 25 |
#endif |
| 26 |
|
| 27 |
#ifdef CYGWIN |
| 28 |
|
| 29 |
#include <direct.h> |
| 30 |
#include <winsock2.h> |
| 31 |
#include <windows.h> |
| 32 |
#include <commctrl.h> |
| 33 |
#include <glib.h> |
| 34 |
#include <stdlib.h> |
| 35 |
|
| 36 |
#include "dopewars.h" |
| 37 |
#include "log.h" |
| 38 |
#include "nls.h" |
| 39 |
#include "tstring.h" |
| 40 |
#include "util.h" |
| 41 |
#include "AIPlayer.h" |
| 42 |
#include "message.h" |
| 43 |
#include "serverside.h" |
| 44 |
#include "sound.h" |
| 45 |
#include "winmain.h" |
| 46 |
|
| 47 |
#ifdef CURSES_CLIENT |
| 48 |
#include "curses_client/curses_client.h" |
| 49 |
#endif |
| 50 |
|
| 51 |
#ifdef GUI_CLIENT |
| 52 |
#include "gui_client/gtk_client.h" |
| 53 |
#endif |
| 54 |
|
| 55 |
#ifdef GUI_SERVER |
| 56 |
#include "gtkport/gtkport.h" |
| 57 |
#endif |
| 58 |
|
| 59 |
static void ServerLogMessage(const gchar *log_domain, |
| 60 |
GLogLevelFlags log_level, |
| 61 |
const gchar *message, gpointer user_data) |
| 62 |
{ |
| 63 |
GString *text; |
| 64 |
|
| 65 |
text = GetLogString(log_level, message); |
| 66 |
if (text) { |
| 67 |
g_print("%s\n", text->str); |
| 68 |
g_string_free(text, TRUE); |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
static void ServerPrintFunc(const gchar *string) |
| 73 |
{ |
| 74 |
DWORD NumChar; |
| 75 |
|
| 76 |
WriteConsole(GetStdHandle(STD_OUTPUT_HANDLE), string, strlen(string), |
| 77 |
&NumChar, NULL); |
| 78 |
} |
| 79 |
|
| 80 |
static void LogMessage(const gchar *log_domain, GLogLevelFlags log_level, |
| 81 |
const gchar *message, gpointer user_data) |
| 82 |
{ |
| 83 |
MessageBox(NULL, message, "Error", MB_OK | MB_ICONSTOP); |
| 84 |
} |
| 85 |
|
| 86 |
static GString *TextOutput = NULL; |
| 87 |
|
| 88 |
static void WindowPrintStart() |
| 89 |
{ |
| 90 |
TextOutput = g_string_new(""); |
| 91 |
} |
| 92 |
|
| 93 |
static void WindowPrintFunc(const gchar *string) |
| 94 |
{ |
| 95 |
g_string_append(TextOutput, string); |
| 96 |
} |
| 97 |
|
| 98 |
static void WindowPrintEnd() |
| 99 |
{ |
| 100 |
MessageBox(NULL, TextOutput->str, "dopewars", |
| 101 |
MB_OK | MB_ICONINFORMATION); |
| 102 |
g_string_free(TextOutput, TRUE); |
| 103 |
TextOutput = NULL; |
| 104 |
} |
| 105 |
|
| 106 |
static FILE *LogFile = NULL; |
| 107 |
|
| 108 |
gchar *appdata_path = NULL; |
| 109 |
|
| 110 |
static void GetAppDataPath() |
| 111 |
{ |
| 112 |
appdata_path = g_strdup_printf("%s/dopewars", g_get_user_config_dir()); |
| 113 |
mkdir(appdata_path); |
| 114 |
} |
| 115 |
|
| 116 |
static void LogFileStart() |
| 117 |
{ |
| 118 |
char *logfile = g_strdup_printf("%s/dopewars-log.txt", |
| 119 |
appdata_path ? appdata_path : "."); |
| 120 |
LogFile = fopen(logfile, "w"); |
| 121 |
g_free(logfile); |
| 122 |
} |
| 123 |
|
| 124 |
static void LogFilePrintFunc(const gchar *string) |
| 125 |
{ |
| 126 |
if (LogFile) { |
| 127 |
fputs(string, LogFile); |
| 128 |
fflush(LogFile); |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
static void LogFileEnd(void) |
| 133 |
{ |
| 134 |
if (LogFile) |
| 135 |
fclose(LogFile); |
| 136 |
} |
| 137 |
|
| 138 |
gchar *GetBinaryDir(void) |
| 139 |
{ |
| 140 |
gchar *filename = NULL, *lastslash; |
| 141 |
gint filelen = 80; |
| 142 |
DWORD retval; |
| 143 |
|
| 144 |
while (1) { |
| 145 |
filename = g_realloc(filename, filelen); |
| 146 |
filename[filelen - 1] = '\0'; |
| 147 |
retval = GetModuleFileName(NULL, filename, filelen); |
| 148 |
|
| 149 |
if (retval == 0) { |
| 150 |
g_free(filename); |
| 151 |
filename = NULL; |
| 152 |
break; |
| 153 |
} else if (filename[filelen - 1]) { |
| 154 |
filelen *= 2; |
| 155 |
} else |
| 156 |
break; |
| 157 |
} |
| 158 |
|
| 159 |
if (filename) { |
| 160 |
lastslash = strrchr(filename, '\\'); |
| 161 |
if (lastslash) |
| 162 |
*lastslash = '\0'; |
| 163 |
} |
| 164 |
|
| 165 |
return filename; |
| 166 |
} |
| 167 |
|
| 168 |
#ifdef ENABLE_NLS |
| 169 |
static gchar *GetWindowsLocale(void) |
| 170 |
{ |
| 171 |
LCID userID; |
| 172 |
WORD lang, sublang; |
| 173 |
static gchar langenv[30]; /* Should only be 5 chars, so |
| 174 |
* this'll be plenty */ |
| 175 |
gchar *oldlang; |
| 176 |
|
| 177 |
langenv[0] = '\0'; |
| 178 |
strcpy(langenv, "LANG="); |
| 179 |
|
| 180 |
oldlang = getenv("LANG"); |
| 181 |
|
| 182 |
if (oldlang) { |
| 183 |
g_print("Started with LANG = %s\n", oldlang); |
| 184 |
return NULL; |
| 185 |
} |
| 186 |
|
| 187 |
userID = GetUserDefaultLCID(); |
| 188 |
lang = PRIMARYLANGID(LANGIDFROMLCID(userID)); |
| 189 |
sublang = SUBLANGID(LANGIDFROMLCID(userID)); |
| 190 |
|
| 191 |
switch (lang) { |
| 192 |
case LANG_ENGLISH: |
| 193 |
strcat(langenv, "en"); |
| 194 |
if (sublang == SUBLANG_ENGLISH_UK) { |
| 195 |
strcat(langenv, "_GB"); |
| 196 |
} |
| 197 |
break; |
| 198 |
case LANG_FRENCH: |
| 199 |
strcat(langenv, "fr"); |
| 200 |
if (sublang == SUBLANG_FRENCH_CANADIAN) { |
| 201 |
strcat(langenv, "_CA"); |
| 202 |
} |
| 203 |
break; |
| 204 |
case LANG_GERMAN: |
| 205 |
strcat(langenv, "de"); |
| 206 |
break; |
| 207 |
case LANG_POLISH: |
| 208 |
strcat(langenv, "pl"); |
| 209 |
break; |
| 210 |
case LANG_SPANISH: |
| 211 |
strcat(langenv, "es"); |
| 212 |
if (sublang == SUBLANG_SPANISH) { |
| 213 |
strcat(langenv, "_ES"); |
| 214 |
} |
| 215 |
break; |
| 216 |
case LANG_NORWEGIAN: |
| 217 |
if (sublang == SUBLANG_NORWEGIAN_NYNORSK) { |
| 218 |
strcat(langenv, "nn"); |
| 219 |
} else { |
| 220 |
strcat(langenv, "no"); |
| 221 |
} |
| 222 |
break; |
| 223 |
case LANG_PORTUGUESE: |
| 224 |
strcat(langenv, "pt"); |
| 225 |
if (sublang == SUBLANG_PORTUGUESE_BRAZILIAN) { |
| 226 |
strcat(langenv, "_BR"); |
| 227 |
} |
| 228 |
break; |
| 229 |
} |
| 230 |
|
| 231 |
if (strlen(langenv) > 5) { |
| 232 |
g_print("Using Windows language %s\n", langenv); |
| 233 |
return langenv; |
| 234 |
} else |
| 235 |
return NULL; |
| 236 |
} |
| 237 |
#endif /* ENABLE_NLS */ |
| 238 |
|
| 239 |
/* |
| 240 |
* Converts the passed lpszCmdParam WinMain-style command line into |
| 241 |
* Unix-style (argc, argv) parameters (returned). |
| 242 |
*/ |
| 243 |
void get_cmdline(const LPSTR lpszCmdParam, int *argc, gchar ***argv) |
| 244 |
{ |
| 245 |
gchar **split; |
| 246 |
gchar *tmp_cmdline; |
| 247 |
|
| 248 |
tmp_cmdline = g_strdup_printf("%s %s", PACKAGE, lpszCmdParam); |
| 249 |
split = g_strsplit(tmp_cmdline, " ", 0); |
| 250 |
*argc = 0; |
| 251 |
while (split[*argc] && split[*argc][0]) { |
| 252 |
(*argc)++; |
| 253 |
} |
| 254 |
*argv = split; |
| 255 |
g_free(tmp_cmdline); |
| 256 |
} |
| 257 |
|
| 258 |
int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, |
| 259 |
LPSTR lpszCmdParam, int nCmdShow) |
| 260 |
{ |
| 261 |
gchar **argv; |
| 262 |
int argc; |
| 263 |
gboolean is_service; |
| 264 |
gchar *modpath; |
| 265 |
struct CMDLINE *cmdline; |
| 266 |
|
| 267 |
#ifdef ENABLE_NLS |
| 268 |
gchar *winlocale; |
| 269 |
const char *charset; |
| 270 |
#endif |
| 271 |
|
| 272 |
/* Are we running as an NT service? */ |
| 273 |
is_service = (lpszCmdParam && strncmp(lpszCmdParam, "-N", 2) == 0); |
| 274 |
|
| 275 |
if (is_service) { |
| 276 |
modpath = GetBinaryDir(); |
| 277 |
SetCurrentDirectory(modpath); |
| 278 |
g_free(modpath); |
| 279 |
} |
| 280 |
|
| 281 |
GetAppDataPath(); |
| 282 |
|
| 283 |
LogFileStart(); |
| 284 |
g_set_print_handler(LogFilePrintFunc); |
| 285 |
|
| 286 |
g_log_set_handler(NULL, LogMask() | G_LOG_LEVEL_MESSAGE, |
| 287 |
ServerLogMessage, NULL); |
| 288 |
|
| 289 |
if (!is_service) { |
| 290 |
g_log_set_handler(NULL, G_LOG_LEVEL_WARNING | G_LOG_LEVEL_CRITICAL, |
| 291 |
LogMessage, NULL); |
| 292 |
} |
| 293 |
#ifdef ENABLE_NLS |
| 294 |
winlocale = GetWindowsLocale(); |
| 295 |
if (winlocale) |
| 296 |
putenv(winlocale); |
| 297 |
|
| 298 |
setlocale(LC_ALL, ""); |
| 299 |
bindtextdomain(PACKAGE, LOCALEDIR); |
| 300 |
textdomain(PACKAGE); |
| 301 |
LocaleIsUTF8 = g_get_charset(&charset); |
| 302 |
#endif |
| 303 |
|
| 304 |
/* Informational comment placed at the start of the Windows log file |
| 305 |
(this is used for messages printed during processing of the config |
| 306 |
files - under Unix these are just printed to stdout) */ |
| 307 |
g_print(_("# This is the dopewars startup log, containing any\n" |
| 308 |
"# informative messages resulting from configuration\n" |
| 309 |
"# file processing and the like.\n\n")); |
| 310 |
|
| 311 |
get_cmdline(lpszCmdParam, &argc, &argv); |
| 312 |
|
| 313 |
cmdline = GeneralStartup(argc, argv); |
| 314 |
if (cmdline->logfile) { |
| 315 |
AssignName(&Log.File, cmdline->logfile); |
| 316 |
} |
| 317 |
OpenLog(); |
| 318 |
SoundInit(); |
| 319 |
if (cmdline->version || cmdline->help) { |
| 320 |
WindowPrintStart(); |
| 321 |
g_set_print_handler(WindowPrintFunc); |
| 322 |
HandleHelpTexts(cmdline->help); |
| 323 |
WindowPrintEnd(); |
| 324 |
#ifdef NETWORKING |
| 325 |
} else if (is_service) { |
| 326 |
InitNetwork(); |
| 327 |
Network = Server = TRUE; |
| 328 |
win32_init(hInstance, hPrevInstance, "mainicon"); |
| 329 |
ServiceMain(cmdline); |
| 330 |
StopNetworking(); |
| 331 |
#endif |
| 332 |
} else if (cmdline->convert) { |
| 333 |
WindowPrintStart(); |
| 334 |
g_set_print_handler(WindowPrintFunc); |
| 335 |
ConvertHighScoreFile(cmdline->convertfile); |
| 336 |
WindowPrintEnd(); |
| 337 |
} else { |
| 338 |
InitNetwork(); |
| 339 |
if (cmdline->server) { |
| 340 |
#ifdef NETWORKING |
| 341 |
#ifdef GUI_SERVER |
| 342 |
Server = TRUE; |
| 343 |
g_log_set_handler(NULL, G_LOG_LEVEL_CRITICAL, LogMessage, NULL); |
| 344 |
win32_init(hInstance, hPrevInstance, "mainicon"); |
| 345 |
GuiServerLoop(cmdline, FALSE); |
| 346 |
#else |
| 347 |
AllocConsole(); |
| 348 |
SetConsoleTitle(_("dopewars server")); |
| 349 |
g_log_set_handler(NULL, |
| 350 |
LogMask() | G_LOG_LEVEL_MESSAGE | |
| 351 |
G_LOG_LEVEL_WARNING, ServerLogMessage, NULL); |
| 352 |
g_set_print_handler(ServerPrintFunc); |
| 353 |
newterm(NULL, NULL, NULL); |
| 354 |
ServerLoop(cmdline); |
| 355 |
#endif /* GUI_SERVER */ |
| 356 |
#else |
| 357 |
WindowPrintStart(); |
| 358 |
g_set_print_handler(WindowPrintFunc); |
| 359 |
g_print(_("This binary has been compiled without networking " |
| 360 |
"support, and thus cannot run\nin server mode. " |
| 361 |
"Recompile passing --enable-networking to the " |
| 362 |
"configure script.\n")); |
| 363 |
WindowPrintEnd(); |
| 364 |
#endif /* NETWORKING */ |
| 365 |
} else if (cmdline->ai) { |
| 366 |
AllocConsole(); |
| 367 |
|
| 368 |
/* Title of the Windows window used for AI player output */ |
| 369 |
SetConsoleTitle(_("dopewars AI")); |
| 370 |
|
| 371 |
g_log_set_handler(NULL, |
| 372 |
LogMask() | G_LOG_LEVEL_MESSAGE | |
| 373 |
G_LOG_LEVEL_WARNING, ServerLogMessage, NULL); |
| 374 |
g_set_print_handler(ServerPrintFunc); |
| 375 |
AIPlayerLoop(cmdline); |
| 376 |
} else { |
| 377 |
switch (cmdline->client) { |
| 378 |
case CLIENT_AUTO: |
| 379 |
if (!GtkLoop(hInstance, hPrevInstance, cmdline, TRUE)) { |
| 380 |
AllocConsole(); |
| 381 |
SetConsoleTitle(_("dopewars")); |
| 382 |
CursesLoop(cmdline); |
| 383 |
} |
| 384 |
break; |
| 385 |
case CLIENT_WINDOW: |
| 386 |
GtkLoop(hInstance, hPrevInstance, cmdline, FALSE); |
| 387 |
break; |
| 388 |
case CLIENT_CURSES: |
| 389 |
AllocConsole(); |
| 390 |
SetConsoleTitle(_("dopewars")); |
| 391 |
CursesLoop(cmdline); |
| 392 |
break; |
| 393 |
} |
| 394 |
} |
| 395 |
#ifdef NETWORKING |
| 396 |
StopNetworking(); |
| 397 |
#endif |
| 398 |
} |
| 399 |
FreeCmdLine(cmdline); |
| 400 |
CloseLog(); |
| 401 |
LogFileEnd(); |
| 402 |
g_strfreev(argv); |
| 403 |
CloseHighScoreFile(); |
| 404 |
g_free(PidFile); |
| 405 |
g_free(Log.File); |
| 406 |
SoundClose(); |
| 407 |
return 0; |
| 408 |
} |
| 409 |
|
| 410 |
#endif /* CYGWIN */ |