NHDT->ANH, nethack->anethack, nhdat->anhdat
[aNetHack.git] / sys / wince / winhack.c
blob21cbd8d01e3f60f45e8d5345110e05922d2e67e7
1 /* aNetHack 0.0.1 winhack.c $ANH-Date: 1432512799 2015/05/25 00:13:19 $ $ANH-Branch: master $:$ANH-Revision: 1.18 $ */
2 /* Copyright (C) 2001 by Alex Kompel */
3 // winhack.cpp : Defines the entry point for the application.
4 //
6 #include "winMS.h"
7 #include "hack.h"
8 #include "dlb.h"
9 #include "mhmain.h"
10 #include "mhmap.h"
12 extern char orgdir[PATHLEN]; /* also used in pcsys.c, amidos.c */
14 extern void FDECL(anethack_exit, (int));
15 static TCHAR *_get_cmd_arg(TCHAR *pCmdLine);
17 // Global Variables:
18 NHWinApp _anethack_app;
20 // Foward declarations of functions included in this code module:
21 BOOL InitInstance(HINSTANCE, int);
23 static void win_hack_init(int, char **);
24 static void __cdecl mswin_moveloop(void *);
25 static BOOL setMapTiles(const char *fname);
27 extern boolean FDECL(pcmain, (int, char **));
29 #define MAX_CMDLINE_PARAM 255
31 int APIENTRY
32 WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLine,
33 int nCmdShow)
35 INITCOMMONCONTROLSEX InitCtrls;
36 HWND anethackWnd;
37 int argc;
38 char *argv[MAX_CMDLINE_PARAM];
39 size_t len;
40 TCHAR *p;
41 TCHAR wbuf[NHSTR_BUFSIZE];
42 char buf[NHSTR_BUFSIZE];
43 boolean resuming;
45 sys_early_init();
47 /* ensure that we don't access violate on a panic() */
48 windowprocs.win_raw_print = mswin_raw_print;
49 windowprocs.win_raw_print_bold = mswin_raw_print_bold;
51 /* init applicatio structure */
52 _anethack_app.hApp = hInstance;
53 _anethack_app.nCmdShow = nCmdShow;
54 _anethack_app.hAccelTable =
55 LoadAccelerators(hInstance, (LPCTSTR) IDC_WINHACK);
56 _anethack_app.hMainWnd = NULL;
57 _anethack_app.hPopupWnd = NULL;
58 _anethack_app.hMenuBar = NULL;
59 _anethack_app.bmpTiles = LoadBitmap(hInstance, MAKEINTRESOURCE(IDB_TILES));
60 if (_anethack_app.bmpTiles == NULL)
61 panic("cannot load tiles bitmap");
62 _anethack_app.bmpPetMark =
63 LoadBitmap(hInstance, MAKEINTRESOURCE(IDB_PETMARK));
64 if (_anethack_app.bmpPetMark == NULL)
65 panic("cannot load pet mark bitmap");
66 _anethack_app.bmpMapTiles = _anethack_app.bmpTiles;
67 _anethack_app.mapTile_X = TILE_X;
68 _anethack_app.mapTile_Y = TILE_Y;
69 _anethack_app.mapTilesPerLine = TILES_PER_LINE;
70 _anethack_app.bNoHScroll = FALSE;
71 _anethack_app.bNoVScroll = FALSE;
73 _anethack_app.bCmdPad = !mswin_has_keyboard();
75 _anethack_app.bWrapText = TRUE;
76 _anethack_app.bFullScreen = TRUE;
78 #if defined(WIN_CE_SMARTPHONE)
79 _anethack_app.bHideScrollBars = TRUE;
80 #else
81 _anethack_app.bHideScrollBars = FALSE;
82 #endif
84 _anethack_app.bUseSIP = TRUE;
86 // check for running anethack programs
87 anethackWnd = FindWindow(szMainWindowClass, NULL);
88 if (anethackWnd) {
89 // bring on top
90 SetForegroundWindow(anethackWnd);
91 return FALSE;
94 // init controls
95 ZeroMemory(&InitCtrls, sizeof(InitCtrls));
96 InitCtrls.dwSize = sizeof(InitCtrls);
97 InitCtrls.dwICC = ICC_LISTVIEW_CLASSES;
98 if (!InitCommonControlsEx(&InitCtrls)) {
99 MessageBox(NULL, TEXT("Cannot init common controls"), TEXT("ERROR"),
100 MB_OK | MB_ICONSTOP);
101 return FALSE;
104 // Perform application initialization:
105 if (!InitInstance(hInstance, nCmdShow)) {
106 return FALSE;
109 /* get command line parameters */
110 p = _get_cmd_arg(
111 #if defined(WIN_CE_PS2xx) || defined(WIN32_PLATFORM_HPCPRO)
112 lpCmdLine
113 #else
114 GetCommandLine()
115 #endif
117 for (argc = 1; p && argc < MAX_CMDLINE_PARAM; argc++) {
118 len = _tcslen(p);
119 if (len > 0) {
120 argv[argc] = _strdup(NH_W2A(p, buf, BUFSZ));
121 } else {
122 argv[argc] = "";
124 p = _get_cmd_arg(NULL);
126 GetModuleFileName(NULL, wbuf, BUFSZ);
127 argv[0] = _strdup(NH_W2A(wbuf, buf, BUFSZ));
129 resuming = pcmain(argc, argv);
131 moveloop(resuming);
133 return 0;
137 // FUNCTION: InitInstance(HANDLE, int)
139 // PURPOSE: Saves instance handle and creates main window
141 // COMMENTS:
143 // In this function, we save the instance handle in a global variable
144 // and
145 // create and display the main program window.
147 BOOL
148 InitInstance(HINSTANCE hInstance, int nCmdShow)
150 return TRUE;
153 PNHWinApp
154 GetNHApp()
156 return &_anethack_app;
159 static int
160 eraseoldlocks()
162 register int i;
164 /* cannot use maxledgerno() here, because we need to find a lock name
165 * before starting everything (including the dungeon initialization
166 * that sets astral_level, needed for maxledgerno()) up
168 for (i = 1; i <= MAXDUNGEON * MAXLEVEL + 1; i++) {
169 /* try to remove all */
170 set_levelfile_name(lock, i);
171 (void) unlink(fqname(lock, LEVELPREFIX, 0));
173 set_levelfile_name(lock, 0);
174 if (unlink(fqname(lock, LEVELPREFIX, 0)))
175 return 0; /* cannot remove it */
176 return (1); /* success! */
179 void
180 getlock()
182 const char *fq_lock;
183 char tbuf[BUFSZ];
184 TCHAR wbuf[BUFSZ];
185 HANDLE f;
186 int fd;
187 int choice;
189 /* regularize(lock); */ /* already done in pcmain */
190 Sprintf(tbuf, "%s", fqname(lock, LEVELPREFIX, 0));
191 set_levelfile_name(lock, 0);
192 fq_lock = fqname(lock, LEVELPREFIX, 1);
194 f = CreateFile(NH_A2W(fq_lock, wbuf, BUFSZ), GENERIC_READ, 0, NULL,
195 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
196 if (f == INVALID_HANDLE_VALUE) {
197 if (GetLastError() == ERROR_FILE_NOT_FOUND)
198 goto gotlock; /* no such file */
199 error("Cannot open %s", fq_lock);
202 CloseHandle(f);
204 /* prompt user that the game alredy exist */
205 choice = MessageBox(GetNHApp()->hMainWnd,
206 TEXT("There are files from a game in progress under "
207 "your name. Recover?"),
208 TEXT("Nethack"), MB_YESNO | MB_DEFBUTTON1);
209 switch (choice) {
210 case IDYES:
211 if (recover_savefile()) {
212 goto gotlock;
213 } else {
214 error("Couldn't recover old game.");
216 break;
218 case IDNO:
219 unlock_file(HLOCK);
220 error("%s", "Cannot start a new game.");
221 break;
224 gotlock:
225 fd = creat(fq_lock, FCMASK);
226 if (fd == -1) {
227 error("cannot creat lock file (%s.)", fq_lock);
228 } else {
229 if (write(fd, (char *) &hackpid, sizeof(hackpid))
230 != sizeof(hackpid)) {
231 error("cannot write lock (%s)", fq_lock);
233 if (close(fd) == -1) {
234 error("cannot close lock (%s)", fq_lock);
239 /* misc functions */
240 void error
241 VA_DECL(const char *, s)
243 TCHAR wbuf[1024];
244 char buf[1024];
245 DWORD last_error = GetLastError();
247 VA_START(s);
248 VA_INIT(s, const char *);
249 /* error() may get called before tty is initialized */
250 if (iflags.window_inited)
251 end_screen();
253 vsprintf(buf, s, VA_ARGS);
254 NH_A2W(buf, wbuf, sizeof(wbuf) / sizeof(wbuf[0]));
255 if (last_error > 0) {
256 LPVOID lpMsgBuf;
257 if (FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER
258 | FORMAT_MESSAGE_FROM_SYSTEM
259 | FORMAT_MESSAGE_IGNORE_INSERTS,
260 NULL, last_error,
261 0, // Default language
262 (LPTSTR) &lpMsgBuf, 0, NULL)) {
263 _tcsncat(wbuf, TEXT("\nSystem Error: "),
264 sizeof(wbuf) / sizeof(wbuf[0]) - _tcslen(wbuf));
265 _tcsncat(wbuf, lpMsgBuf,
266 sizeof(wbuf) / sizeof(wbuf[0]) - _tcslen(wbuf));
268 // Free the buffer.
269 LocalFree(lpMsgBuf);
272 MessageBox(NULL, wbuf, TEXT("Error"), MB_OK | MB_ICONERROR);
273 VA_END();
274 exit(EXIT_FAILURE);
277 TCHAR *
278 _get_cmd_arg(TCHAR *pCmdLine)
280 static TCHAR *pArgs = NULL;
281 TCHAR *pRetArg;
282 BOOL bQuoted;
284 if (!pCmdLine && !pArgs)
285 return NULL;
286 if (!pArgs)
287 pArgs = pCmdLine;
289 /* skip whitespace */
290 for (pRetArg = pArgs; *pRetArg && _istspace(*pRetArg);
291 pRetArg = CharNext(pRetArg))
293 if (!*pRetArg) {
294 pArgs = NULL;
295 return NULL;
298 /* check for quote */
299 if (*pRetArg == TEXT('"')) {
300 bQuoted = TRUE;
301 pRetArg = CharNext(pRetArg);
302 pArgs = _tcschr(pRetArg, TEXT('"'));
303 } else {
304 /* skip to whitespace */
305 for (pArgs = pRetArg; *pArgs && !_istspace(*pArgs);
306 pArgs = CharNext(pArgs))
310 if (pArgs && *pArgs) {
311 TCHAR *p;
312 p = pArgs;
313 pArgs = CharNext(pArgs);
314 *p = (TCHAR) 0;
315 } else {
316 pArgs = NULL;
319 return pRetArg;
323 * Strip out troublesome file system characters.
326 void nt_regularize(s) /* normalize file name */
327 register char *s;
329 register unsigned char *lp;
331 for (lp = s; *lp; lp++)
332 if (*lp == '?' || *lp == '"' || *lp == '\\' || *lp == '/'
333 || *lp == '>' || *lp == '<' || *lp == '*' || *lp == '|'
334 || *lp == ':' || (*lp > 127))
335 *lp = '_';
338 void
339 win32_abort()
341 if (wizard)
342 DebugBreak();
343 abort();
346 void
347 append_port_id(buf)
348 char *buf;
350 char *portstr = PORT_CE_PLATFORM " " PORT_CE_CPU;
351 Sprintf(eos(buf), " %s", portstr);