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 version (Windows):
16 
17 import std.string;
18 
19 import ae.sys.windows.imports;
20 mixin(importWin32!q{winbase});
21 mixin(importWin32!q{wincon});
22 mixin(importWin32!q{winnt});
23 mixin(importWin32!q{winuser});
24 
25 import ae.sys.windows.exception;
26 import ae.sys.windows.text;
27 
28 LARGE_INTEGER largeInteger(long n)
29 {
30 	LARGE_INTEGER li; li.QuadPart = n; return li;
31 }
32 
33 ULARGE_INTEGER ulargeInteger(ulong n)
34 {
35 	ULARGE_INTEGER li; li.QuadPart = n; return li;
36 }
37 
38 ulong makeUlong(DWORD dwLow, DWORD dwHigh)
39 {
40 	ULARGE_INTEGER li;
41 	li.LowPart  = dwLow;
42 	li.HighPart = dwHigh;
43 	return li.QuadPart;
44 }
45 
46 // ***************************************************************************
47 
48 // Messages
49 
50 void processWindowsMessages()
51 {
52 	MSG m;
53 	while (PeekMessageW(&m, null, 0, 0, PM_REMOVE))
54 	{
55 		TranslateMessage(&m);
56 		DispatchMessageW(&m);
57 	}
58 }
59 
60 void messageLoop()
61 {
62 	MSG m;
63 	while (GetMessageW(&m, null, 0, 0))
64 	{
65 		TranslateMessage(&m);
66 		DispatchMessageW(&m);
67 	}
68 }
69 
70 // ***************************************************************************
71 
72 int messageBox(string message, string title, int style=0)
73 {
74 	return MessageBoxW(null, toWStringz(message), toWStringz(title), style);
75 }
76 
77 uint getLastInputInfo()
78 {
79 	LASTINPUTINFO lii = { LASTINPUTINFO.sizeof };
80 	wenforce(GetLastInputInfo(&lii), "GetLastInputInfo");
81 	return lii.dwTime;
82 }
83 
84 // ***************************************************************************
85 
86 /// Hides the console window, but only if we are the owner.
87 void hideOwnConsoleWindow()
88 {
89 	HWND w = GetConsoleWindow();
90 	if (!w)
91 		return;
92 	DWORD pid;
93 	GetWindowThreadProcessId(w, &pid);
94 	if (pid == GetCurrentProcessId())
95 		ShowWindow(w, SW_HIDE);
96 }
97 
98 // ***************************************************************************
99 
100 /// Returns Wine version, or null if not running under Wine.
101 string getWineVersion()
102 {
103 	auto ntdll = GetModuleHandle("ntdll.dll");
104 	if (!ntdll)
105 		return null;
106 	alias wine_get_version_t = extern(C) const(char*) function();
107 	auto wine_get_version = cast(wine_get_version_t)GetProcAddress(ntdll, "wine_get_version");
108 	if (!wine_get_version)
109 		return null;
110 	import std.conv : to;
111 	return wine_get_version().to!string();
112 }