Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ Features
* [#1696](https://github.com/java-native-access/jna/pull/1696): Add `LARGE_INTEGER.ByValue` to `LARGE_INTEGER` in `WinNT.java` - [@baier233](https://github.com/baier233).
* [#1697](https://github.com/java-native-access/jna/pull/1697): Add WlanApi module - [@eranl](https://github.com/eranl).
* [#1718](https://github.com/java-native-access/jna/pull/1718): Add `Cups` to `c.s.j.p.unix` providing CUPS printing system bindings for destinations, jobs, options, and server configuration - [@dbwiddis](https://github.com/dbwiddis).
* [#1719](https://github.com/java-native-access/jna/pull/1719): Add `CoreGraphics` to `c.s.j.p.mac` with Quartz Window Services and Display Services bindings; implement `getAllWindows()` in `MacWindowUtils` - [@dbwiddis](https://github.com/dbwiddis).

Bug Fixes
---------
Expand Down
90 changes: 90 additions & 0 deletions contrib/platform/src/com/sun/jna/platform/WindowUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,11 @@
import com.sun.jna.platform.win32.WinDef.HICON;
import com.sun.jna.platform.win32.WinDef.HRGN;
import com.sun.jna.platform.win32.WinDef.HWND;
import com.sun.jna.platform.mac.CoreFoundation;
import com.sun.jna.platform.mac.CoreFoundation.CFArrayRef;
import com.sun.jna.platform.mac.CoreFoundation.CFDictionaryRef;
import com.sun.jna.platform.mac.CoreFoundation.CFStringRef;
import com.sun.jna.platform.mac.CoreGraphics;
import com.sun.jna.platform.win32.WinDef.LPARAM;
import com.sun.jna.platform.win32.WinDef.LRESULT;
import com.sun.jna.platform.win32.WinDef.POINT;
Expand Down Expand Up @@ -1560,6 +1565,91 @@ private void setBackgroundTransparent(Window w, boolean transparent, String cont
}
fixWindowDragging(w, context);
}

@Override
protected List<DesktopWindow> getAllWindows(final boolean onlyVisibleWindows) {
final List<DesktopWindow> result = new LinkedList<>();
int options = onlyVisibleWindows
? CoreGraphics.kCGWindowListOptionOnScreenOnly | CoreGraphics.kCGWindowListExcludeDesktopElements
: CoreGraphics.kCGWindowListOptionAll;
CFArrayRef windowList = CoreGraphics.INSTANCE.CGWindowListCopyWindowInfo(
options, CoreGraphics.kCGNullWindowID);
if (windowList == null) {
return result;
}
try {
CFStringRef kName = CFStringRef.createCFString(CoreGraphics.kCGWindowName);
CFStringRef kOwnerName = CFStringRef.createCFString(CoreGraphics.kCGWindowOwnerName);
CFStringRef kBounds = CFStringRef.createCFString(CoreGraphics.kCGWindowBounds);
CFStringRef kNumber = CFStringRef.createCFString(CoreGraphics.kCGWindowNumber);
CFStringRef kLayer = CFStringRef.createCFString(CoreGraphics.kCGWindowLayer);
try {
int count = windowList.getCount();
for (int i = 0; i < count; i++) {
Pointer p = windowList.getValueAtIndex(i);
if (p == null) {
continue;
}
CFDictionaryRef dict = new CFDictionaryRef(p);

// Skip non-normal windows (layer != 0)
Pointer layerPtr = CoreFoundation.INSTANCE.CFDictionaryGetValue(dict, kLayer);
if (layerPtr != null) {
CoreFoundation.CFNumberRef layerNum = new CoreFoundation.CFNumberRef(layerPtr);
if (layerNum.intValue() != 0) {
continue;
}
}

// Window title (may be null)
String title = "";
Pointer namePtr = CoreFoundation.INSTANCE.CFDictionaryGetValue(dict, kName);
if (namePtr != null) {
title = new CFStringRef(namePtr).stringValue();
}

// Owner name as filePath equivalent
String ownerName = "";
Pointer ownerPtr = CoreFoundation.INSTANCE.CFDictionaryGetValue(dict, kOwnerName);
if (ownerPtr != null) {
ownerName = new CFStringRef(ownerPtr).stringValue();
}

// Window ID as HWND
HWND hwnd = null;
Pointer numPtr = CoreFoundation.INSTANCE.CFDictionaryGetValue(dict, kNumber);
if (numPtr != null) {
int windowId = new CoreFoundation.CFNumberRef(numPtr).intValue();
hwnd = new HWND(Pointer.createConstant(windowId));
}

// Bounds
Rectangle locAndSize = new Rectangle();
Pointer boundsPtr = CoreFoundation.INSTANCE.CFDictionaryGetValue(dict, kBounds);
if (boundsPtr != null) {
CoreGraphics.CGRect rect = new CoreGraphics.CGRect();
if (CoreGraphics.INSTANCE.CGRectMakeWithDictionaryRepresentation(
new CFDictionaryRef(boundsPtr), rect)) {
locAndSize = new Rectangle(
(int) rect.origin.x, (int) rect.origin.y,
(int) rect.size.width, (int) rect.size.height);
}
}

result.add(new DesktopWindow(hwnd, title, ownerName, locAndSize));
}
} finally {
kName.release();
kOwnerName.release();
kBounds.release();
kNumber.release();
kLayer.release();
}
} finally {
windowList.release();
}
return result;
}
}
private static class X11WindowUtils extends NativeWindowUtils {
private static Pixmap createBitmap(final Display dpy,
Expand Down
Loading
Loading