1 /**
2  * Miscellaneous Windows utility code.
3  *
4  * License:
5  *   This Source Code Form is subject to the terms of
6  *   the Mozilla Public License, v. 2.0. If a copy of
7  *   the MPL was not distributed with this file, You
8  *   can obtain one at http://mozilla.org/MPL/2.0/.
9  *
10  * Authors:
11  *   Vladimir Panteleev <vladimir@thecybershadow.net>
12  */
13 
14 module ae.sys.windows.misc;
15 
16 import std.string;
17 
18 import win32.winbase;
19 import win32.wincon;
20 import win32.winnt;
21 import win32.winuser;
22 
23 import ae.sys.windows.exception;
24 import ae.sys.windows.text;
25 
26 LARGE_INTEGER largeInteger(long n)
27 {
28 	LARGE_INTEGER li; li.QuadPart = n; return li;
29 }
30 
31 ULARGE_INTEGER ulargeInteger(ulong n)
32 {
33 	ULARGE_INTEGER li; li.QuadPart = n; return li;
34 }
35 
36 ulong makeUlong(DWORD dwLow, DWORD dwHigh)
37 {
38 	ULARGE_INTEGER li;
39 	li.LowPart  = dwLow;
40 	li.HighPart = dwHigh;
41 	return li.QuadPart;
42 }
43 
44 // ***************************************************************************
45 
46 // Messages
47 
48 void processWindowsMessages()
49 {
50 	MSG m;
51 	while (PeekMessageW(&m, null, 0, 0, PM_REMOVE))
52 	{
53 		TranslateMessage(&m);
54 		DispatchMessageW(&m);
55 	}
56 }
57 
58 void messageLoop()
59 {
60 	MSG m;
61 	while (GetMessageW(&m, null, 0, 0))
62 	{
63 		TranslateMessage(&m);
64 		DispatchMessageW(&m);
65 	}
66 }
67 
68 // ***************************************************************************
69 
70 int messageBox(string message, string title, int style=0)
71 {
72 	return MessageBoxW(null, toWStringz(message), toWStringz(title), style);
73 }
74 
75 uint getLastInputInfo()
76 {
77 	LASTINPUTINFO lii = { LASTINPUTINFO.sizeof };
78 	wenforce(GetLastInputInfo(&lii), "GetLastInputInfo");
79 	return lii.dwTime;
80 }
81 
82 // ***************************************************************************
83 
84 /// Hides the console window, but only if we are the owner.
85 void hideOwnConsoleWindow()
86 {
87 	HWND w = GetConsoleWindow();
88 	if (!w)
89 		return;
90 	DWORD pid;
91 	GetWindowThreadProcessId(w, &pid);
92 	if (pid == GetCurrentProcessId())
93 		ShowWindow(w, SW_HIDE);
94 }