diff --git a/Headers/x11/XGCursorScale.h b/Headers/x11/XGCursorScale.h new file mode 100644 index 00000000..94a58003 --- /dev/null +++ b/Headers/x11/XGCursorScale.h @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2026 Simon Peter + * + * SPDX-License-Identifier: BSD-2-Clause OR LGPL-2.1-or-later + */ + +#ifndef _XGCursorScale_h_INCLUDE +#define _XGCursorScale_h_INCLUDE + +#include + +#define XG_CURSOR_BASE_SIZE 24 + +int XGCursorThemeSize(double factor, int allFactorsOne); + +int XGCursorScaledLength(double points, double factor); + +void XGCursorScaleARGB(const uint32_t *src, int sw, int sh, + uint32_t *dst, int dw, int dh); + +#endif diff --git a/Headers/x11/XGServerWindow.h b/Headers/x11/XGServerWindow.h index adca517e..2069ac9d 100644 --- a/Headers/x11/XGServerWindow.h +++ b/Headers/x11/XGServerWindow.h @@ -124,6 +124,10 @@ typedef struct _gswindow_device_t { uint32_t net_wm_sync_request_counter_value_high; XSyncCounter net_wm_sync_request_counter; #endif + float extents[4]; + BOOL extents_cached; + unsigned long resize_serial; + BOOL resize_pending; } gswindow_device_t; #define GET_XDRAWABLE(win) ((win)->buffer ? (win)->buffer: (win)->ident) @@ -139,6 +143,7 @@ typedef struct _gswindow_device_t { /* This needs to go in GSDisplayServer */ - (void) _DPSsetcursor: (Cursor)c : (BOOL)set; +- (void) _pointerMovedToRootX: (int)x y: (int)y; - (int) _wm_state: (Window) win; - (BOOL) _ewmh_isHidden: (Window) win; diff --git a/Source/x11/GNUmakefile b/Source/x11/GNUmakefile index fa08eb5f..46a21d9e 100644 --- a/Source/x11/GNUmakefile +++ b/Source/x11/GNUmakefile @@ -36,11 +36,13 @@ SUBPROJECT_NAME=x11 ifeq ($(USE_WRASTER),1) ifeq ($(WITH_WRASTER),yes) x11_C_FILES = \ +cursorscale.c \ xdnd.c else x11_C_FILES = \ context.c \ convert.c \ +cursorscale.c \ raster.c \ scale.c \ xdnd.c \ @@ -48,6 +50,7 @@ xutil.c endif else x11_C_FILES = \ +cursorscale.c \ xdnd.c \ xlibimage.c endif diff --git a/Source/x11/XGServer.m b/Source/x11/XGServer.m index ef59b434..562409e9 100644 --- a/Source/x11/XGServer.m +++ b/Source/x11/XGServer.m @@ -527,7 +527,8 @@ - (id) _initXContext #ifdef HAVE_XRANDR XRRQueryExtension(dpy, &randrEventBase, &randrErrorBase); - XRRSelectInput(dpy, RootWindow(dpy, defScreen), RRScreenChangeNotifyMask); + XRRSelectInput(dpy, RootWindow(dpy, defScreen), + RRScreenChangeNotifyMask | RROutputPropertyNotifyMask); #endif return self; } diff --git a/Source/x11/XGServerEvent.m b/Source/x11/XGServerEvent.m index 044c085c..a338fc99 100644 --- a/Source/x11/XGServerEvent.m +++ b/Source/x11/XGServerEvent.m @@ -112,6 +112,19 @@ void __objc_xgcontextevent_linking (void) { } +static Bool +realConfigureQueued(Display *dpy, XEvent *ev, XPointer arg) +{ + Window *w = (Window *)arg; + + if (ev->type == ConfigureNotify && ev->xconfigure.window == w[0] + && ev->xconfigure.send_event == False) + { + w[1] = 1; + } + return False; +} + static SEL procSel = 0; static void (*procEvent)(id, SEL, XEvent*) = 0; @@ -996,6 +1009,33 @@ - (void) processEvent: (XEvent *) event x.origin.y = root_y; } + if (xEvent.xconfigure.send_event + || (long)(xEvent.xconfigure.serial - cWin->resize_serial) < 0) + { + x.size = r.size; + } + else if (cWin->resize_pending && !NSEqualSizes(x.size, r.size)) + { + XEvent scratch; + Window probe[2] = { cWin->ident, 0 }; + + XSync(dpy, False); + XCheckIfEvent(dpy, &scratch, realConfigureQueued, + (XPointer)probe); + if (probe[1]) + { + x.size = r.size; + } + else + { + cWin->resize_pending = NO; + } + } + else if (xEvent.xconfigure.send_event == False) + { + cWin->resize_pending = NO; + } + cWin->xframe = x; n = [self _XFrameToOSFrame: x for: cWin]; NSDebugLLog(@"Moving", @@ -1102,6 +1142,8 @@ - (void) processEvent: (XEvent *) event case EnterNotify: NSDebugLLog(@"NSEvent", @"%lu EnterNotify\n", xEvent.xcrossing.window); + [self _pointerMovedToRootX: xEvent.xcrossing.x_root + y: xEvent.xcrossing.y_root]; break; // when the pointer leaves a window @@ -1558,6 +1600,8 @@ - (void) processEvent: (XEvent *) event generic.lastMotion = xEvent.xmotion.time; [self setLastTime: generic.lastMotion]; + [self _pointerMovedToRootX: xEvent.xmotion.x_root + y: xEvent.xmotion.y_root]; state = xEvent.xmotion.state; if (state & generic.lMouseMask) { @@ -1618,6 +1662,35 @@ - (void) processEvent: (XEvent *) event NSDebugLLog(@"NSEvent", @"%lu PropertyNotify - '%s'\n", xEvent.xproperty.window, XGetAtomName(dpy, xEvent.xproperty.atom)); + if (xEvent.xproperty.atom == generic._NET_FRAME_EXTENTS_ATOM) + { + gswindow_device_t *device + = [XGServer _windowForXWindow: xEvent.xproperty.window]; + + if (device != NULL) + { + NSRect osframe; + + device->extents_cached = NO; + osframe = [self _XFrameToOSFrame: device->xframe for: device]; + if (!NSEqualRects(osframe, device->osframe)) + { + NSEvent *r; + + device->osframe = osframe; + r = [NSEvent otherEventWithType: NSAppKitDefined + location: osframe.origin + modifierFlags: 0 + timestamp: xEvent.xproperty.time / 1000 + windowNumber: device->number + context: gcontext + subtype: GSAppKitWindowResized + data1: osframe.size.width + data2: osframe.size.height]; + [[NSApp windowWithWindowNumber: device->number] sendEvent: r]; + } + } + } if (xEvent.xproperty.atom == generic.WM_STATE_ATOM) { if (cWin == 0 || xEvent.xproperty.window != cWin->ident) @@ -1933,6 +2006,18 @@ - (void) processEvent: (XEvent *) event } #endif #ifdef HAVE_XRANDR + if (xEvent.type == randrEventBase + RRNotify + && ((XRRNotifyEvent *)&xEvent)->subtype == RRNotify_OutputProperty + && ((XRROutputPropertyNotifyEvent *)&xEvent)->property + == XInternAtom(dpy, "DPI", True)) + { + [self screenList]; + [NSScreen resetScreens]; + [[NSNotificationCenter defaultCenter] + postNotificationName: NSApplicationDidChangeScreenParametersNotification + object: NSApp]; + break; + } int randr_event_type = randrEventBase + RRScreenChangeNotify; if (xEvent.type == randr_event_type && (xEvent.xconfigure.window == RootWindow(dpy, defScreen))) diff --git a/Source/x11/XGServerWindow.m b/Source/x11/XGServerWindow.m index 80ff8d58..fce1b6ca 100644 --- a/Source/x11/XGServerWindow.m +++ b/Source/x11/XGServerWindow.m @@ -39,6 +39,7 @@ #include #include #include +#include #include #include #include @@ -76,6 +77,7 @@ #include "x11/XGDragView.h" #include "x11/XGInputServer.h" +#include "x11/XGCursorScale.h" #define ROOT generic.appRootWindow @@ -99,6 +101,57 @@ @interface NSCursor (BackendPrivate) - (void *)_cid; @end +#define XG_CURSOR_MAX_VARIANTS 8 + +@interface XGCursorSource : NSObject +{ +@public + int style; + NSImage *image; + NSPoint hotSpot; + BOOL recolored; + XColor fg; + XColor bg; + int count; + int keys[XG_CURSOR_MAX_VARIANTS]; + Cursor cursors[XG_CURSOR_MAX_VARIANTS]; +} +@end + +@implementation XGCursorSource +- (void) dealloc +{ + RELEASE(image); + [super dealloc]; +} +@end + +static NSMapTable *cursorSources = NULL; +static Cursor currentCursor = None; +static int pointerFactorKey = -1; +static BOOL monitorKeysValid = NO; +static unsigned monitorKeyCount = 0; +static NSRect *monitorFrames = NULL; +static int *monitorKeys = NULL; + +static const struct { + int style; + unsigned int glyph; + const char *name; +} standardCursors[] = { + { GSArrowCursor, XC_left_ptr, "left_ptr" }, + { GSIBeamCursor, XC_xterm, "xterm" }, + { GSOpenHandCursor, XC_hand1, "hand1" }, + { GSPointingHandCursor, XC_hand2, "hand2" }, + { GSCrosshairCursor, XC_crosshair, "crosshair" }, + { GSResizeDownCursor, XC_bottom_side, "bottom_side" }, + { GSResizeLeftCursor, XC_left_side, "left_side" }, + { GSResizeLeftRightCursor, XC_sb_h_double_arrow, "sb_h_double_arrow" }, + { GSResizeRightCursor, XC_right_side, "right_side" }, + { GSResizeUpCursor, XC_top_side, "top_side" }, + { GSResizeUpDownCursor, XC_sb_v_double_arrow, "sb_v_double_arrow" }, +}; + @interface NSBitmapImageRep (GSPrivate) - (NSBitmapImageRep *) _convertToFormatBitsPerSample: (NSInteger)bps samplesPerPixel: (NSInteger)spp @@ -2417,6 +2470,15 @@ - (void) styleoffsets: (float *) l : (float *) r : (float *) t : (float *) b [self styleoffsets: l : r : t : b : style : (Window) 0]; } +- (void) styleoffsets: (float *) l : (float *) r : (float *) t : (float *) b + : (unsigned int) style window: (int) win +{ + gswindow_device_t *window = WINDOW_WITH_TAG(win); + + [self styleoffsets: l : r : t : b : style + : (window != NULL) ? window->ident : (Window) 0]; +} + - (void) styleoffsets: (float *) l : (float *) r : (float *) t : (float *) b : (unsigned int) style : (Window) win { @@ -2436,8 +2498,18 @@ - (void) styleoffsets: (float *) l : (float *) r : (float *) t : (float *) b /* First check _NET_FRAME_EXTENTS */ if (win && ((generic.wm & XGWM_EWMH) != 0)) { - unsigned long *extents = [self _getExtents: win]; + gswindow_device_t *device = [XGServer _windowForXWindow: win]; + unsigned long *extents; + if (device != NULL && device->extents_cached) + { + *l = device->extents[0]; + *r = device->extents[1]; + *t = device->extents[2]; + *b = device->extents[3]; + return; + } + extents = [self _getExtents: win]; if (extents) { NSDebugLLog(@"Frame", @@ -2448,6 +2520,14 @@ - (void) styleoffsets: (float *) l : (float *) r : (float *) t : (float *) b *t = extents[2]; *b = extents[3]; XFree(extents); + if (device != NULL) + { + device->extents[0] = *l; + device->extents[1] = *r; + device->extents[2] = *t; + device->extents[3] = *b; + device->extents_cached = YES; + } return; } } @@ -3487,6 +3567,11 @@ - (void) placewindow: (NSRect)rect : (int)win NSDebugLLog(@"Moving", @"Place %lu - o:%@, x:%@", window->number, NSStringFromRect(rect), NSStringFromRect(xFrame)); + if (resize == YES) + { + window->resize_serial = NextRequest(dpy); + window->resize_pending = YES; + } XMoveResizeWindow (dpy, window->ident, window->siz_hints.x, window->siz_hints.y, window->siz_hints.width, window->siz_hints.height); @@ -4261,6 +4346,218 @@ - (void *) windowDevice: (int)win static Cursor xgps_blank_cursor = None; static BOOL cursor_hidden = NO; +- (void) _updateMonitorFactorKeys +{ + NSArray *screens; + unsigned n; + BOOL allOne = YES; + unsigned i; + + if (GSCurrentServer() != self) + { + monitorKeyCount = 0; + return; + } + screens = [NSScreen screens]; + n = [screens count]; + + free(monitorFrames); + free(monitorKeys); + monitorFrames = malloc(n * sizeof(NSRect)); + monitorKeys = malloc(n * sizeof(int)); + monitorKeyCount = n; + for (i = 0; i < n; i++) + { + NSScreen *screen = [screens objectAtIndex: i]; + CGFloat factor = [screen userSpaceScaleFactor]; + + if (factor <= 0.0) + { + factor = 1.0; + } + if (factor != 1.0) + { + allOne = NO; + } + monitorFrames[i] = [screen frame]; + monitorKeys[i] = (int)lround(factor * 100.0); + } + if (allOne) + { + for (i = 0; i < n; i++) + { + monitorKeys[i] = 0; + } + } + monitorKeysValid = YES; +} + +- (int) _factorKeyAtRootX: (int)x y: (int)y +{ + NSPoint p; + unsigned i; + + if (!monitorKeysValid) + { + [self _updateMonitorFactorKeys]; + } + if (monitorKeyCount == 0) + { + return 0; + } + p = NSMakePoint(x + 0.5, xScreenSize.height - y - 0.5); + for (i = 0; i < monitorKeyCount; i++) + { + if (NSPointInRect(p, monitorFrames[i])) + { + return monitorKeys[i]; + } + } + return monitorKeys[0]; +} + +- (int) _pointerFactorKey +{ + if (pointerFactorKey < 0) + { + Window root, child; + int rx, ry, wx, wy; + unsigned int mask; + + if (XQueryPointer(dpy, [self xDisplayRootWindow], &root, &child, + &rx, &ry, &wx, &wy, &mask)) + { + pointerFactorKey = [self _factorKeyAtRootX: rx y: ry]; + } + else + { + pointerFactorKey = [self _factorKeyAtRootX: 0 y: 0]; + } + } + return pointerFactorKey; +} + +- (void) _invalidateCursorFactors +{ + monitorKeysValid = NO; + pointerFactorKey = -1; +} + +- (void) _pointerMovedToRootX: (int)x y: (int)y +{ + int key = [self _factorKeyAtRootX: x y: y]; + + if (key == pointerFactorKey) + { + return; + } + pointerFactorKey = key; + if (!cursor_hidden && currentCursor != None) + { + [self _DPSsetcursor: [self _cursor: currentCursor forFactorKey: key] + : YES]; + } +} + +- (Cursor) _themeCursor: (int)style factorKey: (int)key +{ + unsigned i; + + for (i = 0; i < sizeof(standardCursors) / sizeof(standardCursors[0]); i++) + { + if (standardCursors[i].style == style) + { +#if HAVE_XCURSOR + int size = XGCursorThemeSize(key / 100.0, key == 0); + + if (size > 0) + { + XcursorImages *images = XcursorLibraryLoadImages( + standardCursors[i].name, XcursorGetTheme(dpy), size); + + if (images != NULL) + { + Cursor cursor = XcursorImagesLoadCursor(dpy, images); + + XcursorImagesDestroy(images); + return cursor; + } + } +#endif + return XCreateFontCursor(dpy, standardCursors[i].glyph); + } + } + return None; +} + +- (void) _registerCursor: (Cursor)cursor + style: (int)style + image: (NSImage *)image + hotSpot: (NSPoint)hotp + factorKey: (int)key +{ + XGCursorSource *source = [XGCursorSource new]; + + source->style = style; + source->image = RETAIN(image); + source->hotSpot = hotp; + source->count = 1; + source->keys[0] = key; + source->cursors[0] = cursor; + if (cursorSources == NULL) + { + cursorSources = NSCreateMapTable(NSNonOwnedPointerMapKeyCallBacks, + NSObjectMapValueCallBacks, 16); + } + NSMapInsert(cursorSources, (void *)cursor, source); + RELEASE(source); +} + +- (Cursor) _cursor: (Cursor)cid forFactorKey: (int)key +{ + XGCursorSource *source = (cursorSources ? NSMapGet(cursorSources, (void *)cid) : nil); + Cursor cursor; + int i; + + if (source == nil) + { + return cid; + } + for (i = 0; i < source->count; i++) + { + if (source->keys[i] == key) + { + return source->cursors[i]; + } + } + if (source->count == XG_CURSOR_MAX_VARIANTS) + { + return cid; + } + if (source->style >= 0) + { + cursor = [self _themeCursor: source->style factorKey: key]; + } + else + { + cursor = [self _imageCursor: source->image + hotSpot: source->hotSpot + factorKey: key]; + } + if (cursor == None) + { + return cid; + } + if (source->recolored) + { + XRecolorCursor(dpy, cursor, &source->fg, &source->bg); + } + source->keys[source->count] = key; + source->cursors[source->count] = cursor; + source->count++; + return cursor; +} + - (Cursor) _blankCursor { if (xgps_blank_cursor == None) @@ -4302,7 +4599,8 @@ - (void) _initializeCursorForXWindow: (Window) win { Cursor cid = (Cursor)[[NSCursor currentCursor] _cid]; - XDefineCursor (dpy, win, cid); + XDefineCursor (dpy, win, + [self _cursor: cid forFactorKey: [self _pointerFactorKey]]); } } @@ -4444,51 +4742,42 @@ - (void) showcursor - (void) standardcursor: (int)style : (void **)cid { - Cursor cursor = None; - - switch (style) - { - case GSArrowCursor: - cursor = XCreateFontCursor(dpy, XC_left_ptr); - break; - case GSIBeamCursor: - cursor = XCreateFontCursor(dpy, XC_xterm); - break; - case GSOpenHandCursor: - cursor = XCreateFontCursor(dpy, XC_hand1); - break; - case GSPointingHandCursor: - cursor = XCreateFontCursor(dpy, XC_hand2); - break; - case GSCrosshairCursor: - cursor = XCreateFontCursor(dpy, XC_crosshair); - break; - case GSResizeDownCursor: - cursor = XCreateFontCursor(dpy, XC_bottom_side); - break; - case GSResizeLeftCursor: - cursor = XCreateFontCursor(dpy, XC_left_side); - break; - case GSResizeLeftRightCursor: - cursor = XCreateFontCursor(dpy, XC_sb_h_double_arrow); - break; - case GSResizeRightCursor: - cursor = XCreateFontCursor(dpy, XC_right_side); - break; - case GSResizeUpCursor: - cursor = XCreateFontCursor(dpy, XC_top_side); - break; - case GSResizeUpDownCursor: - cursor = XCreateFontCursor(dpy, XC_sb_v_double_arrow); - break; - default: - return; + int key = [self _pointerFactorKey]; + Cursor cursor = [self _themeCursor: style factorKey: key]; + + if (cursor != None) + { + [self _registerCursor: cursor + style: style + image: nil + hotSpot: NSZeroPoint + factorKey: key]; } - if (cid) - *cid = (void *)cursor; + *cid = (void *)cursor; } - (void) imagecursor: (NSPoint)hotp : (NSImage *)image : (void **)cid +{ + int key = [self _pointerFactorKey]; + Cursor cursor = [self _imageCursor: image hotSpot: hotp factorKey: key]; + + if (cursor != None) + { + [self _registerCursor: cursor + style: -1 + image: image + hotSpot: hotp + factorKey: key]; + } + if (cid) + { + *cid = (cursor == None) ? NULL : (void *)cursor; + } +} + +- (Cursor) _imageCursor: (NSImage *)image + hotSpot: (NSPoint)hotp + factorKey: (int)key { Cursor cursor; NSBitmapImageRep *rep; @@ -4500,8 +4789,7 @@ - (void) imagecursor: (NSPoint)hotp : (NSImage *)image : (void **)cid { /* FIXME: We might create a blank cursor here? */ NSLog(@"Could not convert cursor bitmap data"); - *cid = NULL; - return; + return None; } if (hotp.x >= [rep pixelsWide]) @@ -4516,8 +4804,7 @@ - (void) imagecursor: (NSPoint)hotp : (NSImage *)image : (void **)cid if (w <= 0 || h <= 0) { - *cid = NULL; - return; + return None; } #if HAVE_XCURSOR @@ -4525,8 +4812,7 @@ - (void) imagecursor: (NSPoint)hotp : (NSImage *)image : (void **)cid // alpha, and document the format. if (colors != 4) { - *cid = NULL; - return; + return None; } { @@ -4537,7 +4823,32 @@ - (void) imagecursor: (NSPoint)hotp : (NSImage *)image : (void **)cid // Copy the data from the image rep to the Xcursor structure swapColors((unsigned char *)xcursorImage->pixels, rep); - + + if (key != 0) + { + NSSize size = [image size]; + double factor = key / 100.0; + int tw, th; + + if (size.width <= 0 || size.height <= 0) + { + size = NSMakeSize(w, h); + } + tw = XGCursorScaledLength(size.width, factor); + th = XGCursorScaledLength(size.height, factor); + if (tw != w || th != h) + { + XcursorImage *scaled = XcursorImageCreate(tw, th); + + XGCursorScaleARGB((const uint32_t *)xcursorImage->pixels, w, h, + (uint32_t *)scaled->pixels, tw, th); + scaled->xhot = MIN((int)(hotp.x * tw / size.width), tw - 1); + scaled->yhot = MIN((int)(hotp.y * th / size.height), th - 1); + XcursorImageDestroy(xcursorImage); + xcursorImage = scaled; + } + } + cursor = XcursorImageLoadCursor(dpy, xcursorImage); XcursorImageDestroy(xcursorImage); } @@ -4567,8 +4878,7 @@ - (void) imagecursor: (NSPoint)hotp : (NSImage *)image : (void **)cid } #endif - if (cid) - *cid = (void *)cursor; + return cursor; } - (void) recolorcursor: (NSColor *)fg : (NSColor *)bg : (void*) cid @@ -4600,6 +4910,24 @@ - (void) recolorcursor: (NSColor *)fg : (NSColor *)bg : (void*) cid xb = [self xColorFromColor: xb]; XRecolorCursor(dpy, cursor, &xf, &xb); + { + XGCursorSource *source = (cursorSources ? NSMapGet(cursorSources, (void *)cursor) : nil); + int i; + + if (source != nil) + { + source->recolored = YES; + source->fg = xf; + source->bg = xb; + for (i = 0; i < source->count; i++) + { + if (source->cursors[i] != cursor) + { + XRecolorCursor(dpy, source->cursors[i], &xf, &xb); + } + } + } + } } - (void) setcursor: (void*) cid @@ -4613,7 +4941,10 @@ - (void) setcursor: (void*) cid return; } - [self _DPSsetcursor: cursor : YES]; + currentCursor = cursor; + [self _DPSsetcursor: [self _cursor: cursor + forFactorKey: [self _pointerFactorKey]] + : YES]; } - (void) freecursor: (void*) cid @@ -4627,7 +4958,27 @@ - (void) freecursor: (void*) cid return; } - XFreeCursor(dpy, cursor); + { + XGCursorSource *source = (cursorSources ? NSMapGet(cursorSources, (void *)cursor) : nil); + int i; + + if (source != nil) + { + for (i = 0; i < source->count; i++) + { + XFreeCursor(dpy, source->cursors[i]); + } + NSMapRemove(cursorSources, (void *)cursor); + } + else + { + XFreeCursor(dpy, cursor); + } + } + if (currentCursor == cursor) + { + currentCursor = None; + } } /******************************************************************************* @@ -4771,6 +5122,67 @@ - (void) freecursor: (void*) cid } +#ifdef HAVE_XRANDR +static CGFloat +_scaleFactorForOutput(Display *dpy, RROutput output, + XRROutputInfo *output_info, XRRCrtcInfo *crtc_info) +{ + Atom dpi_atom; + Atom type; + int format; + unsigned long nitems; + unsigned long bytes_after; + unsigned char *data = NULL; + CGFloat factor = 0.0; + + dpi_atom = XInternAtom(dpy, "DPI", True); + if (dpi_atom != None + && XRRGetOutputProperty(dpy, output, dpi_atom, 0, 1, False, False, + AnyPropertyType, &type, &format, &nitems, &bytes_after, + &data) == Success) + { + if (data != NULL && type == XA_INTEGER && format == 32 && nitems == 1) + { + long dpi = *(long *)data; + + if (dpi > 0) + { + factor = dpi / 96.0; + } + } + if (data != NULL) + { + XFree(data); + } + } + + if (factor == 0.0 + && output_info->mm_width > 0 && output_info->mm_height > 0 + && [[NSUserDefaults standardUserDefaults] + boolForKey: @"GSScreenScaleFromSize"]) + { + CGFloat inches = hypot(output_info->mm_width, + output_info->mm_height) / 25.4; + CGFloat dpi = hypot(crtc_info->width, crtc_info->height) / inches; + CGFloat target = (inches >= 20.0) ? 110.0 : 135.0; + CGFloat step = [[NSUserDefaults standardUserDefaults] + floatForKey: @"GSScaleFactorStep"]; + + if (step <= 0.0) + { + step = 0.25; + } + factor = rint(dpi / target / step) * step; + if (factor < 1.0) + { + factor = 1.0; + } + } + + return factor; +} +#endif + /* This method assumes that we deal with one X11 screen - `defScreen`. Basically it means that we have DISPLAY variable set to `:0.0`. Where both digits have artbitrary values, but it defines once on @@ -4787,6 +5199,8 @@ - (NSArray *)screenList NSArray *workAreas = _workAreas(dpy, root); xScreenSize = _screenSize(dpy, defScreen); + [self _invalidateCursorFactors]; + monitorsCount = 0; if (monitors != NULL) { @@ -4827,8 +5241,20 @@ - (NSArray *)screenList monitors[mi].screen_id = defScreen; monitors[mi].depth = [self windowDepthForScreen: mi]; - monitors[mi].resolution - = [self resolutionForScreen: defScreen]; + { + CGFloat factor = _scaleFactorForOutput(dpy, + screen_res->outputs[i], output_info, crtc_info); + + if (factor > 0.0) + { + monitors[mi].resolution + = NSMakeSize(72.0 * factor, 72.0 * factor); + } + else + { + monitors[mi].resolution = NSMakeSize(72, 72); + } + } /* We can only use the work area provided by _NET_WORKAREA * if we have a single screen/monitor, because that property @@ -4873,6 +5299,21 @@ - (NSArray *)screenList } monitorsCount = mi; + for (i = 1; i < monitorsCount; i++) + { + if (!NSEqualSizes(monitors[i].resolution, + monitors[0].resolution)) + { + break; + } + } + if (i == monitorsCount) + { + for (i = 0; i < monitorsCount; i++) + { + monitors[i].resolution = NSMakeSize(72, 72); + } + } if (monitorsCount != 0) { XRRFreeScreenResources(screen_res); @@ -4895,7 +5336,7 @@ - (NSArray *)screenList monitors = NSZoneMalloc([self zone], sizeof(MonitorDevice)); monitors[0].screen_id = defScreen; monitors[0].depth = [self windowDepthForScreen: 0]; - monitors[0].resolution = [self resolutionForScreen: defScreen]; + monitors[0].resolution = NSMakeSize(72, 72); monitors[0].frame = NSMakeRect(0, 0, xScreenSize.width, xScreenSize.height); if (workAreas != nil) @@ -4965,38 +5406,14 @@ - (const NSWindowDepth *) availableDepthsForScreen: (int)screen return depths; } -// `screen_num` is a Xlib screen number. -- (NSSize) resolutionForScreen: (int)screen_num +// `screen` is a monitor index not X11 screen +- (NSSize) resolutionForScreen: (int)screen { - // NOTE: - // -gui now trusts the return value of resolutionForScreen:, - // so if it is not {72, 72} then the entire UI will be scaled. - // - // I commented out the implementation below because it may not - // be safe to use the DPI value we get from the X server. - // (i.e. I don't know if it will be a "fake" DPI like 72 or 96, - // or a real measurement reported from the monitor's firmware - // (possibly incorrect?)) - // More research needs to be done. - - return NSMakeSize(72, 72); - - /* - int res_x, res_y; - - if (screen < 0 || screen_num >= ScreenCount(dpy)) + if (monitors == NULL || screen < 0 || screen >= monitorsCount) { - NSLog(@"Invalidparam: no screen %d", screen); - return NSMakeSize(0,0); + return NSMakeSize(72, 72); } - // This does not take virtual displays into account!! - res_x = DisplayWidth(dpy, screen) / - (DisplayWidthMM(dpy, screen) / 25.4); - res_y = DisplayHeight(dpy, screen) / - (DisplayHeightMM(dpy, screen) / 25.4); - - return NSMakeSize(res_x, res_y); - */ + return monitors[screen].resolution; } // `screen` is a monitor index not X11 screen diff --git a/Source/x11/cursorscale.c b/Source/x11/cursorscale.c new file mode 100644 index 00000000..89d09b88 --- /dev/null +++ b/Source/x11/cursorscale.c @@ -0,0 +1,103 @@ +/* + * Copyright (c) 2026 Simon Peter + * + * SPDX-License-Identifier: BSD-2-Clause OR LGPL-2.1-or-later + */ + +#include +#include +#include "x11/XGCursorScale.h" + +int +XGCursorThemeSize(double factor, int allFactorsOne) +{ + if (allFactorsOne) + { + return 0; + } + if (factor <= 0.0) + { + factor = 1.0; + } + return (int)lround(XG_CURSOR_BASE_SIZE * factor); +} + +int +XGCursorScaledLength(double points, double factor) +{ + long length = lround(points * factor); + + return (length < 1) ? 1 : (int)length; +} + +static double +coverage(int i, double lo, double hi) +{ + double a = (i > lo) ? i : lo; + double b = (i + 1 < hi) ? i + 1 : hi; + + return (b > a) ? b - a : 0.0; +} + +void +XGCursorScaleARGB(const uint32_t *src, int sw, int sh, + uint32_t *dst, int dw, int dh) +{ + double sx = (double)sw / dw; + double sy = (double)sh / dh; + int dx, dy; + + if (sw == dw && sh == dh) + { + memcpy(dst, src, (size_t)sw * sh * sizeof(uint32_t)); + return; + } + + for (dy = 0; dy < dh; dy++) + { + double y0 = dy * sy, y1 = (dy + 1) * sy; + int ys = (int)floor(y0), ye = (int)ceil(y1); + + if (ye > sh) + { + ye = sh; + } + for (dx = 0; dx < dw; dx++) + { + double x0 = dx * sx, x1 = (dx + 1) * sx; + int xs = (int)floor(x0), xe = (int)ceil(x1); + double sum[4] = { 0, 0, 0, 0 }; + double total = 0.0; + uint32_t out = 0; + int x, y, c; + + if (xe > sw) + { + xe = sw; + } + for (y = ys; y < ye; y++) + { + double wy = coverage(y, y0, y1); + + for (x = xs; x < xe; x++) + { + double w = wy * coverage(x, x0, x1); + uint32_t p = src[y * sw + x]; + + for (c = 0; c < 4; c++) + { + sum[c] += w * ((p >> (8 * c)) & 0xff); + } + total += w; + } + } + for (c = 0; c < 4 && total > 0.0; c++) + { + uint32_t v = (uint32_t)(sum[c] / total + 0.5); + + out |= ((v > 255) ? 255 : v) << (8 * c); + } + dst[dy * dw + dx] = out; + } + } +} diff --git a/Tests/x11/cursorscale.m b/Tests/x11/cursorscale.m new file mode 100644 index 00000000..828097a0 --- /dev/null +++ b/Tests/x11/cursorscale.m @@ -0,0 +1,113 @@ +/* + * Copyright (c) 2026 Simon Peter + * + * SPDX-License-Identifier: BSD-2-Clause OR LGPL-2.1-or-later + */ + +/* The size math of per-monitor cursors (Source/x11/cursorscale.c): a theme + * cursor is loaded at 24 px x the factor of the monitor under the pointer, + * except when every monitor runs at 1.0, where libXcursor keeps choosing as + * before; image cursors are resampled from points to that monitor's pixels. + * The source is compiled in, no display is needed. */ +#import +#import "Testing.h" +#include "config.h" + +#if defined(BUILD_SERVER) && defined(SERVER_x11) && BUILD_SERVER == SERVER_x11 + +#include "x11/cursorscale.c" + +#define ARGB(a, r, g, b) (((uint32_t)(a) << 24) | ((uint32_t)(r) << 16) \ + | ((uint32_t)(g) << 8) | (uint32_t)(b)) + +int +main(void) +{ + START_SET("cursor size per monitor") + + PASS(XGCursorThemeSize(1.0, 1) == 0, + "all monitors at 1.0: libXcursor's own size (unchanged)"); + PASS(XGCursorThemeSize(1.0, 0) == 24, + "a 1.0 monitor next to a scaled one: the base size"); + PASS(XGCursorThemeSize(1.25, 0) == 30, + "a 1.25 monitor: 30 px"); + PASS(XGCursorThemeSize(2.0, 0) == 48, + "a 2.0 monitor: 48 px"); + PASS(XGCursorThemeSize(1.1, 0) == 26, + "a 1.1 monitor: rounded to whole pixels"); + PASS(XGCursorThemeSize(0.0, 0) == 24, + "an unknown factor counts as 1.0"); + + PASS(XGCursorScaledLength(16, 1.0) == 16, "16 pt at 1.0: 16 px"); + PASS(XGCursorScaledLength(16, 1.25) == 20, "16 pt at 1.25: 20 px"); + PASS(XGCursorScaledLength(21, 1.25) == 26, "21 pt at 1.25: 26 px"); + PASS(XGCursorScaledLength(0.2, 1.0) == 1, "never below one pixel"); + + END_SET("cursor size per monitor") + + START_SET("cursor image resampling") + { + uint32_t src[4] = { ARGB(255, 255, 0, 0), ARGB(255, 0, 0, 255), + ARGB(0, 0, 0, 0), ARGB(255, 255, 255, 255) }; + uint32_t same[4]; + uint32_t one; + uint32_t big[16]; + int i, ok; + + XGCursorScaleARGB(src, 2, 2, same, 2, 2); + PASS(memcmp(src, same, sizeof(src)) == 0, "the same size is a copy"); + + XGCursorScaleARGB(src, 2, 2, &one, 1, 1); + /* the average of the four premultiplied pixels */ + PASS(one == ARGB(191, 128, 64, 128), + "shrinking averages the covered pixels (got %08x)", one); + + XGCursorScaleARGB(src, 2, 2, big, 4, 4); + ok = 1; + for (i = 0; i < 16; i++) + { + int x = i % 4, y = i / 4; + + if (big[i] != src[(y / 2) * 2 + x / 2]) + { + ok = 0; + } + } + PASS(ok, "doubling repeats each pixel 2x2"); + } + { + /* a one pixel wide opaque line in a 4x4 image survives 4 -> 3 */ + uint32_t src[16], dst[9]; + int i, opaque = 0; + + for (i = 0; i < 16; i++) + { + src[i] = (i % 4 == 1) ? ARGB(255, 0, 0, 0) : 0; + } + XGCursorScaleARGB(src, 4, 4, dst, 3, 3); + for (i = 0; i < 9; i++) + { + if ((dst[i] >> 24) >= 128) + { + opaque++; + } + } + PASS(opaque >= 3, "a thin stroke stays visible when shrinking"); + } + END_SET("cursor image resampling") + + return 0; +} + +#else + +int +main(void) +{ + START_SET("cursor size per monitor") + SKIP("the x11 backend is not being built") + END_SET("cursor size per monitor") + return 0; +} + +#endif