9front - general discussion about 9front
 help / color / mirror / Atom feed
* [9front] drawterm: add integral scaling flag for macos
@ 2025-02-03 16:49 Nikita Georgiou
  0 siblings, 0 replies; only message in thread
From: Nikita Georgiou @ 2025-02-03 16:49 UTC (permalink / raw)
  To: 9front

Drawterm on macOS runs without scaling by default, which can be cumbersome on the high DPI displays macOS often runs on as screen elements such as borders and text become tiny. This change adds an -x flag that allows the user to enable scaling, making screen elements larger at the cost of some visual quality. Passing the flag without an argument allows macOS to pick a scale factor, while providing a positive integer argument allows the user to choose what factor to scale the screen by. Not passing the flag leaves the default drawterm behavior of not scaling unchanged. 

---
diff daf2ab4550e555cdb6c58f2a9e647c2259a634de 68123acc0d4277b76debb2745bc6b71b79e8c5e3
--- a/cpu.c
+++ b/cpu.c
@@ -41,6 +41,8 @@
 char secstorebuf[65536];
 char *geometry;
 
+int scalef;
+
 extern void	guimain(void);
 
 char*
@@ -249,6 +251,7 @@
 		"[-p] [-t timeout] "
 		"[-r root] "
 		"[-g geometry] "
+		"[-x [amount]] "
 		"[-c cmd ...]\n", argv0);
 	exits("usage");
 }
@@ -325,6 +328,15 @@
 		 *	[=][<width>{xX}<height>][{+-}<xoffset>{+-}<yoffset>]
 		 */
 		geometry = EARGF(usage());
+		break;
+	case 'x':
+		scalef = 1;
+		if((s = ARGF()) != nil)
+			scalef = atoi(s);
+		if(scalef < 0){
+			fprintf(stderr, "%s: scale factor must be >= 0\n", argv0);
+			EARGF(usage());
+		}
 		break;
 	default:
 		usage();
--- a/drawterm.1
+++ b/drawterm.1
@@ -37,6 +37,9 @@
 .B -g
 .I geometry
 ] [
+.B -x
+.I [amount]
+] [
 .B -c
 .I cmd \fR...]
 
@@ -122,6 +125,15 @@
 Specifies the root directory on the client. The default is
 .I /root
 and all further paths are relative thereto.
+
+.TP
+.B -x \fI[amount]
+(macOS only) Enables scaling. The scale factor is optionally set by 
+.I amount, 
+a positive integer. Without this flag, no
+scaling is performed and graphics are rendered 1:1 with the display resolution. Not specifying an
+amount or specifying 1 scales using a default picked by macOS, while increasing values of n 
+scale accordingly. 0 disables scaling, nullifying this flag.
 
 .TP
 .B -c \fIcmd \fR...
--- a/gui-cocoa/screen.m
+++ b/gui-cocoa/screen.m
@@ -22,7 +22,8 @@
 #endif
 #define LOG(fmt, ...) if(DEBUG)NSLog((@"%s:%d %s " fmt), __FILE__, __LINE__, __PRETTY_FUNCTION__, ##__VA_ARGS__)
 
-Memimage *gscreen;
+Memimage *gscreen; 
+extern int scalef;
 
 @interface DrawLayer : CAMetalLayer
 @property id<MTLTexture> texture;
@@ -68,11 +69,27 @@
 	}
 }
 
+/* 
+ * 0: No scaling, use display resolution
+ * 1: Use macOS default scaling
+ * n: Scale by n based off display res
+ */
+static NSSize
+scalesize(NSSize s)
+{
+	NSSize r = [myview convertSizeToBacking:s];
+	switch(scalef){
+		case 0: return r;
+		case 1: return myview.frame.size;
+		default: return NSMakeSize(r.width/scalef, r.height/scalef);
+	}
+}
+
 void
 screeninit(void)
 {
 	memimageinit();
-	NSSize s = [myview convertSizeToBacking:myview.frame.size];
+	NSSize s = scalesize(myview.frame.size);
 	screensize(Rect(0, 0, s.width, s.height), ARGB32);
 	gscreen->clipr = Rect(0, 0, s.width, s.height);
 	LOG(@"%g %g", s.width, s.height);
@@ -102,7 +119,7 @@
 	textureDesc.cpuCacheMode = MTLCPUCacheModeWriteCombined;
 	layer.texture = [layer.device newTextureWithDescriptor:textureDesc];
 
-	CGFloat scale = myview.window.backingScaleFactor;
+	CGFloat scale = myview.window.backingScaleFactor * (scalef ? scalef : 1);
 	[layer setDrawableSize:NSMakeSize(Dx(r), Dy(r))];
 	[layer setContentsScale:scale];
 }
@@ -276,6 +293,18 @@
 	});
 }
 
+static NSPoint
+unscalemouse(NSPoint p, NSWindow *w)
+{
+	NSPoint s;
+	s = [w convertPointFromBacking:p];
+	switch(scalef){
+		case 0: return s;
+		case 1: return p;
+		default: return NSMakePoint(s.x*scalef, s.y*scalef);
+	}
+}
+
 void
 mouseset(Point p)
 {
@@ -285,7 +314,7 @@
 		if([[myview window] isKeyWindow]){
 			s = NSMakePoint(p.x, p.y);
 			LOG(@"-> pixel  %g %g", s.x, s.y);
-			s = [[myview window] convertPointFromBacking:s];
+			s = unscalemouse(s, [myview window]);
 			LOG(@"-> point  %g %g", s.x, s.y);
 			s = [myview convertPoint:s toView:nil];
 			LOG(@"-> window %g %g", s.x, s.y);
@@ -374,11 +403,25 @@
 	return YES;
 }
 
+static NSPoint
+scalemouse(NSPoint p, NSWindow *w)
+{
+	NSPoint s;
+	s = [w convertPointToBacking:p];
+	switch(scalef){
+		case 0: return s;
+		case 1: return p;
+		default: return NSMakePoint(s.x/scalef, s.y/scalef);
+	}
+}
+
 - (void) windowDidBecomeKey:(id)arg
 {
 	NSPoint p;
-	p = [_window convertPointToBacking:[_window mouseLocationOutsideOfEventStream]];
-	absmousetrack(p.x, [myview convertSizeToBacking:myview.frame.size].height - p.y, 0, ticks());
+	NSSize s;
+	p = scalemouse([_window mouseLocationOutsideOfEventStream], _window);
+	s = scalesize(myview.frame.size);
+	absmousetrack(p.x, s.height - p.y, 0, ticks());
 }
 
 - (void) windowDidResignKey:(id)arg
@@ -591,11 +634,13 @@
 - (void) mouseevent:(NSEvent*)event
 {
 	NSPoint p;
+	NSSize s;
 	Point q;
 	NSUInteger u;
 	NSEventModifierFlags m;
 
-	p = [self.window convertPointToBacking:[self.window mouseLocationOutsideOfEventStream]];
+	p = scalemouse([self.window mouseLocationOutsideOfEventStream], self.window);
+	s = scalesize(self.frame.size);
 	u = [NSEvent pressedMouseButtons];
 	q.x = p.x;
 	q.y = p.y;
@@ -609,7 +654,7 @@
 		}else if(m & NSEventModifierFlagCommand)
 			u = 4;
 	}
-	absmousetrack(p.x, [self convertSizeToBacking:self.frame.size].height - p.y, u, ticks());
+	absmousetrack(p.x, s.height - p.y, u, ticks());
 	if(u && _lastInputRect.size.width && _lastInputRect.size.height)
 		[self resetLastInputRect];
 }
@@ -697,7 +742,7 @@
 
 - (void) reshape
 {
-	NSSize s = [self convertSizeToBacking:self.frame.size];
+	NSSize s = scalesize(self.frame.size);
 	LOG(@"%g %g", s.width, s.height);
 	if(gscreen != nil){
 		screenresize(Rect(0, 0, s.width, s.height));


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2025-02-03 16:50 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2025-02-03 16:49 [9front] drawterm: add integral scaling flag for macos Nikita Georgiou

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).