fixup webfreaks PR

This commit is contained in:
Adam D. Ruppe 2018-03-08 17:22:41 -05:00
parent 02af0ad09b
commit 53525181d4
2 changed files with 93 additions and 83 deletions

View File

@ -4419,8 +4419,8 @@ enum RasterOp {
// being phobos-free keeps the size WAY down // being phobos-free keeps the size WAY down
private const(char)* toStringz(string s) { return (s ~ '\0').ptr; } private const(char)* toStringz(string s) { return (s ~ '\0').ptr; }
private const(wchar)* toWStringz(wstring s) { return (s ~ '\0').ptr; } package(arsd) const(wchar)* toWStringz(wstring s) { return (s ~ '\0').ptr; }
private const(wchar)* toWStringz(string s) { package(arsd) const(wchar)* toWStringz(string s) {
wstring r; wstring r;
foreach(dchar c; s) foreach(dchar c; s)
r ~= c; r ~= c;
@ -7269,7 +7269,7 @@ version(Windows) {
if (!len) if (!len)
return null; return null;
char[] buffer = new char[len]; char[] buffer = new char[len];
auto len2 = GetWindowTextA(hwnd, buffer.ptr, buffer.length); auto len2 = GetWindowTextA(hwnd, buffer.ptr, cast(int) buffer.length);
if (len != len2) if (len != len2)
throw new Exception("Window title changed while checking"); throw new Exception("Window title changed while checking");
return cast(string)buffer; return cast(string)buffer;

View File

@ -1,16 +1,18 @@
/++ /++
Cross platform window manager utilities for interacting with other unknown windows on the OS. Cross platform window manager utilities for interacting with other unknown windows on the OS.
Based on arsd.simpledisplay. Based on [arsd.simpledisplay].
+/ +/
module arsd.wmutil; module arsd.wmutil;
public import arsd.simpledisplay; public import arsd.simpledisplay;
version(Windows)
import core.sys.windows.windows;
static assert(UsingSimpledisplayX11 || UsingSimpledisplayWindows, "wmutil only works on X11 or Windows"); static assert(UsingSimpledisplayX11 || UsingSimpledisplayWindows, "wmutil only works on X11 or Windows");
static if (UsingSimpledisplayX11) static if (UsingSimpledisplayX11) {
{
extern(C) nothrow @nogc { extern(C) nothrow @nogc {
Atom* XListProperties(Display *display, Window w, int *num_prop_return); Atom* XListProperties(Display *display, Window w, int *num_prop_return);
Status XGetTextProperty(Display *display, Window w, XTextProperty *text_prop_return, Atom property); Status XGetTextProperty(Display *display, Window w, XTextProperty *text_prop_return, Atom property);
@ -18,20 +20,22 @@ Status XQueryTree(Display *display, Window w, Window *root_return, Window *paren
} }
} }
/// A foreachable object that iterates window children
struct WindowChildrenIterator { struct WindowChildrenIterator {
NativeWindowHandle parent; NativeWindowHandle parent;
int opApply(int delegate(NativeWindowHandle) dg) const { version(Windows)
version (Windows) {
struct EnumParams { struct EnumParams {
int result; int result;
int delegate(NativeWindowHandle) dg; int delegate(NativeWindowHandle) dg;
Exception ex; Exception ex;
} }
EnumParams params;
EnumChildWindows(parent, function (window, lparam) nothrow {
version(Windows)
extern(Windows)
nothrow private static int helper(HWND window, LPARAM lparam) {
EnumParams* args = cast(EnumParams*)lparam; EnumParams* args = cast(EnumParams*)lparam;
try { try {
args.result = args.dg(window); args.result = args.dg(window);
@ -43,7 +47,15 @@ struct WindowChildrenIterator {
args.ex = e; args.ex = e;
return 0; return 0;
} }
}, cast(LPARAM)&params); }
///
int opApply(int delegate(NativeWindowHandle) dg) const {
version (Windows) {
EnumParams params;
// the cast is cuz druntime seems to have a wrong definition here, missing the const
EnumChildWindows(cast(void*) parent, &helper, cast(LPARAM)&params);
if (params.ex) if (params.ex)
throw params.ex; throw params.ex;
@ -67,11 +79,12 @@ struct WindowChildrenIterator {
} }
return result; return result;
} else } else
throw new NotYetImplementedException(); static assert(0);
} }
} }
///
WindowChildrenIterator iterateWindows(NativeWindowHandle parent = NativeWindowHandle.init) { WindowChildrenIterator iterateWindows(NativeWindowHandle parent = NativeWindowHandle.init) {
static if (UsingSimpledisplayX11) static if (UsingSimpledisplayX11)
if (parent == NativeWindowHandle.init) if (parent == NativeWindowHandle.init)
@ -86,20 +99,10 @@ WindowChildrenIterator iterateWindows(NativeWindowHandle parent = NativeWindowHa
Params: Params:
className = the class name to check the window for, case-insensitive. className = the class name to check the window for, case-insensitive.
+/ +/
version (Windows)
NativeWindowHandle findWindowByClass(LPCTSTR className) {
return FindWindow(className, null);
}
/// ditto
version (Windows)
NativeWindowHandle findWindowByClass(string className) { NativeWindowHandle findWindowByClass(string className) {
version (Windows)
return findWindowByClass(className.toWStringz); return findWindowByClass(className.toWStringz);
} else static if (UsingSimpledisplayX11) {
/// ditto
static if (UsingSimpledisplayX11)
NativeWindowHandle findWindowByClass(string className) {
import std.algorithm : splitter; import std.algorithm : splitter;
import std.uni : sicmp; import std.uni : sicmp;
@ -121,23 +124,29 @@ NativeWindowHandle findWindowByClass(string className) {
} }
} }
return NativeWindowHandle.init; return NativeWindowHandle.init;
}
}
/// ditto
version (Windows)
NativeWindowHandle findWindowByClass(LPCTSTR className) {
return FindWindow(className, null);
} }
/++ /++
Get the PID that owns the window.
Params: Params:
window = The window to check who created it window = The window to check who created it
Returns: the PID of the owner who created this window. On windows this will always work and be accurate. On X11 this might return -1 if none is specified and might not actually be the actual owner. Returns: the PID of the owner who created this window. On windows this will always work and be accurate. On X11 this might return -1 if none is specified and might not actually be the actual owner.
+/ +/
version (Windows)
int ownerPID(NativeWindowHandle window) @property { int ownerPID(NativeWindowHandle window) @property {
version (Windows) {
DWORD ret; DWORD ret;
GetWindowThreadProcessId(window, &ret); GetWindowThreadProcessId(window, &ret);
return cast(int) ret; return cast(int) ret;
} } else static if (UsingSimpledisplayX11) {
/// ditto
static if (UsingSimpledisplayX11)
int ownerPID(NativeWindowHandle window) @property {
auto pidAtom = GetAtom!"_NET_WM_PID"(XDisplayConnection.get()); auto pidAtom = GetAtom!"_NET_WM_PID"(XDisplayConnection.get());
Atom returnType; Atom returnType;
int returnFormat; int returnFormat;
@ -152,6 +161,7 @@ int ownerPID(NativeWindowHandle window) @property {
} }
return -1; return -1;
} }
}
unittest { unittest {
import std.stdio; import std.stdio;