shithub: map

Download patch

ref: 825b1760ac8f5ec7930dedcd47199095f2634134
parent: 726126c176ac091ac28a14ae5efcded255dac71b
author: sirjofri <sirjofri@sirjofri.de>
date: Sat Mar 29 10:39:17 EDT 2025

fixes NaN bug, adds plumbing

--- a/README
+++ b/README
@@ -9,6 +9,7 @@
 
 - Display map tiles from mapfs
 - fetch GPS position from gpsfs(8)
+- react to plumbs
 
 
 2. INSTALLATION
@@ -32,3 +33,21 @@
 - q | Del  : Quit
 - , | .    : Zoom out/in
 - ←↑↓→     : move around
+
+3.1 PLUMBING
+
+Map listens to the 'map' channel:
+
+	type	is	text
+	data	matches	'[0-9.]+[NSWEnswe][0-9.]+[NSWEnswe][0-9]*'
+	plumb	to	map
+
+The message is defined as this:
+
+- Number [NSWEnswe]: first coordinate pair
+- Number [NSWEnswe]: second coordinate pair
+- (Number): optional zoom level
+
+Examples:
+
+	50.5N30.4W5
--- a/dat.h
+++ b/dat.h
@@ -1,10 +1,8 @@
 typedef struct GPos GPos;
 typedef struct GBundle GBundle;
 typedef struct GProvider GProvider;
+typedef struct Point Point;
 
-extern Image* mapimage;
-extern int tilesize;
-extern Point drawoffset;
 extern int debug;
 
 struct GPos {
--- a/fns.h
+++ b/fns.h
@@ -2,6 +2,8 @@
 GPos getlocation(void);
 void tile2gps(double *x, double *y, int z);
 
+int parsepos(char *data, int ndata, GPos *pos, int *zoom);
+
 void lockmapimage(void);
 void unlockmapimage(void);
 
--- a/map.c
+++ b/map.c
@@ -2,6 +2,7 @@
 #include <libc.h>
 #include <draw.h>
 #include <event.h>
+#include <plumb.h>
 #include <keyboard.h>
 #include <nate/nate.h>
 #include <nate/n_window.h>
@@ -55,11 +56,11 @@
 	char we;
 	
 	ns = gpsloc.lat > 90 ? 'N' : 'S';
-	we = gpsloc.lon > 360 ? 'W' : 'E';
+	we = gpsloc.lon > 180 ? 'E' : 'W';
 	
-	snprint(statusline, sizeof statusline, "Loc: %f %c %f %c Z: %d",
-		gpsloc.lon - 180, we,
-		gpsloc.lat - 90, ns,
+	snprint(statusline, sizeof statusline, "Loc: %f%c %f%c Z: %d",
+		fabs(gpsloc.lon - 180), we,
+		fabs(gpsloc.lat - 90), ns,
 		currentloc.z);
 	return statusline;
 }
@@ -122,7 +123,6 @@
 gpsoff(void)
 {
 	GPos p;
-	Point off;
 	double x, y;
 	
 	if (gpsloc.lat > 180)
@@ -130,10 +130,9 @@
 	if (gpsloc.lat < 0)
 		gpsloc.lat = 0;
 	
-	off = divpt(mapimagesize, 2);
-	x = off.x / (double)tilesize;
-	y = off.y / (double)tilesize;
-	
+	x = mapimagesize.x / 2. / (double)tilesize;
+	y = mapimagesize.y / 2. / (double)tilesize;
+
 	tile2gps(&x, &y, currentloc.z);
 	p.lon = gpsloc.lon - x;
 	p.lat = gpsloc.lat - y;
@@ -293,7 +292,7 @@
 	return Credraw;
 }
 
-void
+static void
 handlecmd(int cmd)
 {
 	switch (cmd) {
@@ -306,11 +305,36 @@
 	}
 }
 
+static void
+handleplumb(Plumbmsg* pm)
+{
+	GPos pos;
+	int zoom;
+	if (pm->ndata < 0)
+		goto Out;
+	
+	if (!parsepos(pm->data, pm->ndata, &pos, &zoom))
+		goto Out;
+	
+	gpsloc = pos;
+	if (zoom >= 0)
+		currentloc.z = zoom;
+	currentloc = getbundle(gpsoff(), currentloc.z, &drawoffset);
+	redrawmap();
+	
+Out:
+	plumbfree(pm);
+}
+
+enum {
+	Etimer = 4,
+	Eplumb = 8,
+};
+
 void
 main(int argc, char **argv)
 {
 	Event ev;
-	int timer;
 	int cmd;
 	
 	ARGBEGIN{
@@ -335,7 +359,8 @@
 		natedebugfd = 2;
 	
 	einit(Emouse|Ekeyboard);
-	timer = etimer(0, 100);
+	etimer(Etimer, 100);
+	eplumb(Eplumb, "map");
 	
 	nateinit();
 	
@@ -381,12 +406,13 @@
 	redrawmap();
 	
 	for (;;) {
-		cmd = event(&ev);
-		if (cmd == timer) {
+		switch (event(&ev)) {
+		case Etimer:
 			checkcondredraw();
-			continue;
-		}
-		switch (cmd) {
+			break;
+		case Eplumb:
+			handleplumb(ev.v);
+			break;
 		case Emouse:
 			if (ev.mouse.buttons & 4)
 				exits(nil);
--- a/mkfile
+++ b/mkfile
@@ -7,6 +7,7 @@
 	tile.$O\
 	gps.$O\
 	img.$O\
+	parse.$O\
 
 HFILES=fns.h dat.h
 
--- a/tile.c
+++ b/tile.c
@@ -4,14 +4,18 @@
 #include "dat.h"
 #include "fns.h"
 
+extern int tilesize;
+
 static double
 asinh(double x)
 {
+	if (isNaN(x))
+		x = 1.;
 	double s = sqrt(x*x + 1);
 	return log(x + s);
 }
 
-int
+static int
 lon2tilex(double lon, int z, int *offset)
 {
 	double tile = (lon + 180.0) / 360.0 * (1<<z);
@@ -20,7 +24,7 @@
 	return t;
 }
 
-int
+static int
 lat2tiley(double lat, int z, int *offset)
 {
 	double latrad = lat * PI/180.0;
@@ -30,12 +34,14 @@
 	return t;
 }
 
-double tilex2lon(double x, int z)
+static double
+tilex2lon(double x, int z)
 {
 	return x / (double)(1<<z) * 360.0 - 180.0;
 }
 
-double tiley2lat(double y, int z)
+static double
+tiley2lat(double y, int z)
 {
 	double n = PI - 2.0 * PI * y / (double)(1<<z);
 	return 180.0 / PI * atan(0.5 * (exp(n) - exp(-n)));
@@ -52,6 +58,8 @@
 getbundle(GPos pos, int z, Point *offset)
 {
 	GBundle ret;
+	if (z < 0)
+		z = 0;
 	ret.x = lon2tilex(pos.lon, z, &offset->x);
 	ret.y = lat2tiley(pos.lat, z, &offset->y);
 	ret.z = z;
--