1 /* NetHack 3.6 winnt.c $NHDT-Date: 1524321419 2018/04/21 14:36:59 $ $NHDT-Branch: NetHack-3.6.0 $:$NHDT-Revision: 1.30 $ */
2 /* Copyright (c) NetHack PC Development Team 1993, 1994 */
3 /* NetHack may be freely redistributed. See license for details. */
6 * WIN32 system functions.
8 * Initial Creation: Michael Allison - January 31/93
24 * The following WIN32 API routines are used in this file.
27 * GetVolumeInformation
35 /* globals required within here */
36 HANDLE ffhandle = (HANDLE) 0;
38 typedef HWND(WINAPI *GETCONSOLEWINDOW)();
39 static HWND GetConsoleHandle(void);
40 static HWND GetConsoleHwnd(void);
42 /* The function pointer nt_kbhit contains a kbhit() equivalent
43 * which varies depending on which window port is active.
44 * For the tty port it is tty_kbhit() [from nttty.c]
45 * For the win32 port it is win32_kbhit() [from winmain.c]
46 * It is initialized to point to def_kbhit [in here] for safety.
50 int (*nt_kbhit)() = def_kbhit;
55 /* Could not locate a WIN32 API call for this- MJA */
64 DWORD SectorsPerCluster = 0;
65 DWORD BytesPerSector = 0;
66 DWORD FreeClusters = 0;
67 DWORD TotalClusters = 0;
73 GetDiskFreeSpace(tmppath, &SectorsPerCluster, &BytesPerSector,
74 &FreeClusters, &TotalClusters);
75 return (long) (SectorsPerCluster * BytesPerSector * FreeClusters);
79 * Functions to get filenames using wildcards
87 ffhandle = (HANDLE) 0;
89 ffhandle = FindFirstFile(path, &ffd);
90 return (ffhandle == INVALID_HANDLE_VALUE) ? 0 : 1;
96 return FindNextFile(ffhandle, &ffd) ? 1 : 0;
102 return &ffd.cFileName[0];
109 if (findfirst(file)) {
110 return ((long) ffd.nFileSizeLow);
116 * Chdrive() changes the default drive.
124 if ((ptr = index(str, ':')) != (char *) 0) {
125 drive = toupper((uchar) *(ptr - 1));
126 _chdrive((drive - 'A') + 1);
136 status = GetVolumeInformation((LPTSTR) 0, (LPTSTR) 0, 0, (LPDWORD) 0,
137 &maxflen, (LPDWORD) 0, (LPTSTR) 0, 0);
151 * Strip out troublesome file system characters.
154 void nt_regularize(s) /* normalize file name */
157 register unsigned char *lp;
159 for (lp = s; *lp; lp++)
160 if (*lp == '?' || *lp == '"' || *lp == '\\' || *lp == '/'
161 || *lp == '>' || *lp == '<' || *lp == '*' || *lp == '|'
163 || *lp == ':' || (*lp > 127))
170 * This is used in nhlan.c to implement some of the LAN_FEATURES.
173 get_username(lan_username_size)
174 int *lan_username_size;
176 static TCHAR username_buffer[BUFSZ];
180 /* i gets updated with actual size */
181 status = GetUserName(username_buffer, &i);
183 username_buffer[i] = '\0';
185 Strcpy(username_buffer, "NetHack");
186 if (lan_username_size)
187 *lan_username_size = strlen(username_buffer);
188 return username_buffer;
194 char szFullPath[MAX_PATH] = "";
195 HMODULE hInst = NULL; /* NULL gets the filename of this module */
197 GetModuleFileName(hInst, szFullPath, sizeof(szFullPath));
198 return &szFullPath[0];
205 VA_DECL(const char *, s)
209 VA_INIT(s, const char *);
210 /* error() may get called before tty is initialized */
211 if (iflags.window_inited)
213 if (!strncmpi(windowprocs.name, "tty", 3)) {
215 (void) vsprintf(&buf[1], s, VA_ARGS);
219 (void) vsprintf(buf, s, VA_ARGS);
233 extern void NDECL(backsp);
241 if (!iflags.window_inited)
244 msmsg("Execute debug breakpoint wizard?");
245 while ((ci = nhgetch()) != '\n') {
247 backsp(); /* \b is visible on NT */
253 if (ci == 'y' || ci == 'n' || ci == 'Y' || ci == 'N') {
265 static char interjection_buf[INTERJECTION_TYPES][1024];
266 static int interjection[INTERJECTION_TYPES];
269 interject_assistance(num, interjection_type, ptr1, ptr2)
271 int interjection_type;
277 char *panicmsg = (char *) ptr1;
278 char *datadir = (char *) ptr2;
279 char *tempdir = nh_getenv("TEMP");
280 interjection_type = INTERJECT_PANIC;
281 interjection[INTERJECT_PANIC] = 1;
283 * ptr1 = the panic message about to be delivered.
284 * ptr2 = the directory prefix of the dungeon file
285 * that failed to open.
286 * Check to see if datadir matches tempdir or a
287 * common windows temp location. If it does, inform
288 * the user that they are probably trying to run the
289 * game from within their unzip utility, so the required
290 * files really don't exist at the location. Instruct
291 * them to unpack them first.
293 if (panicmsg && datadir) {
294 if (!strncmpi(datadir, "C:\\WINDOWS\\TEMP", 15)
295 || strstri(datadir, "TEMP")
296 || (tempdir && strstri(datadir, tempdir))) {
298 interjection_buf[INTERJECT_PANIC],
299 "\nOne common cause of this error is attempting to "
301 "the game by double-clicking on it while it is "
303 "inside an unzip utility.\n\n"
304 "You have to unzip the contents of the zip file into a\n"
305 "folder on your system, and then run \"NetHack.exe\" or "
307 "\"NetHackW.exe\" from there.\n\n"
308 "If that is not the situation, you are encouraged to\n"
309 "report the error as shown above.\n\n",
318 interject(interjection_type)
319 int interjection_type;
321 if (interjection_type >= 0 && interjection_type < INTERJECTION_TYPES)
322 msmsg(interjection_buf[interjection_type]);
325 #ifdef RUNTIME_PASTEBUF_SUPPORT
327 void port_insert_pastebuf(buf)
330 /* This implementation will utilize the windows clipboard
331 * to accomplish this.
345 /* last arg=0 means "tell me the size of the buffer that I need" */
346 rc = MultiByteToWideChar(GetConsoleOutputCP(), 0, buf, -1, w2, 0);
349 abytes = rc * sizeof(WCHAR);
350 w = (WCHAR *)alloc(abytes);
351 /* Housekeeping need: +free(w) */
353 rc = MultiByteToWideChar(GetConsoleOutputCP(), 0, buf, -1, w, rc);
358 if (!OpenClipboard(NULL)) {
362 /* Housekeeping need: +CloseClipboard(), free(w) */
366 /* allocate global mem obj to hold the text */
368 hglbCopy = GlobalAlloc(GMEM_MOVEABLE, abytes);
369 if (hglbCopy == NULL) {
374 /* Housekeeping need: +GlobalFree(hglbCopy), CloseClipboard(), free(w) */
376 lpwstrCopy = (LPWSTR)GlobalLock(hglbCopy);
377 /* Housekeeping need: +GlobalUnlock(hglbCopy), GlobalFree(hglbCopy),
378 CloseClipboard(), free(w) */
380 memcpy(lpwstrCopy, w, abytes);
381 GlobalUnlock(hglbCopy);
382 /* Housekeeping need: GlobalFree(hglbCopy), CloseClipboard(), free(w) */
384 /* put it on the clipboard */
385 hresult = SetClipboardData(CF_UNICODETEXT, hglbCopy);
387 raw_printf("Error copying to clipboard.\n");
388 GlobalFree(hglbCopy); /* only needed if clipboard didn't accept data */
390 /* Housekeeping need: CloseClipboard(), free(w) */
398 GetConsoleHandle(void)
400 HMODULE hMod = GetModuleHandle("kernel32.dll");
401 GETCONSOLEWINDOW pfnGetConsoleWindow =
402 (GETCONSOLEWINDOW) GetProcAddress(hMod, "GetConsoleWindow");
403 if (pfnGetConsoleWindow)
404 return pfnGetConsoleWindow();
406 return GetConsoleHwnd();
414 char OldTitle[1024], NewTitle[1024], TestTitle[1024];
416 /* Get current window title */
417 GetConsoleTitle(OldTitle, sizeof OldTitle);
419 (void) sprintf(NewTitle, "NETHACK%d/%d", GetTickCount(),
420 GetCurrentProcessId());
421 SetConsoleTitle(NewTitle);
423 GetConsoleTitle(TestTitle, sizeof TestTitle);
424 while (strcmp(TestTitle, NewTitle) != 0) {
427 GetConsoleTitle(TestTitle, sizeof TestTitle);
429 hwndFound = FindWindow(NULL, NewTitle);
430 SetConsoleTitle(OldTitle);
431 /* printf("%d iterations\n", iterations); */
437 #ifdef RUNTIME_PORT_ID
439 * _M_IX86 is Defined for x86 processors. This is not defined for x64
441 * _M_X64 is Defined for x64 processors.
442 * _M_IA64 is Defined for Itanium Processor Family 64-bit processors.
443 * _WIN64 is Defined for applications for Win64.
447 #define TARGET_PORT "x64"
450 #define TARGET_PORT "IA64"
455 #define TARGET_PORT "x86"
462 Strcpy(buf, TARGET_PORT);
465 #endif /* RUNTIME_PORT_ID */