palm-client: Map screen with an OSM basemap (tools/make_map.py) and positioned nodes

This commit is contained in:
rich committed 2026-11-02 12:33:52 -05:00
1 parent f0ef46bef5
commit deec3112d4
12 files changed
+1330 -2

No files matched your search

+3
View File
@@ -15,6 +15,7 @@
#include "sounds.h"
#include "mutes.h"
#include "scopes.h"
#include "map_form.h"
#include "node_form.h"
#include "settings.h"
#include "ports.h"
@@ -46,6 +47,7 @@ static Boolean HandleMenu(UInt16 itemID)
case MainMenuHome: FrmGotoForm(HomeForm); return true;
case MainMenuContacts: FrmGotoForm(ContactsForm); return true;
case MainMenuNode: FrmGotoForm(MainForm); return true;
case MainMenuMap: FrmGotoForm(MapForm); return true;
case MainMenuPorts: FrmGotoForm(PortsForm); return true;
case MainMenuFontPreview: FrmGotoForm(FontPreviewForm); return true;
case MainMenuSounds: SoundsFormOpen(FrmGetActiveFormID()); return true;
@@ -84,6 +86,7 @@ static Boolean AppHandleEvent(EventType *event)
case RemoteForm: FrmSetEventHandler(form, RemoteFormHandleEvent); break;
case SoundsForm: FrmSetEventHandler(form, SoundsFormHandleEvent); break;
case ScopesForm: FrmSetEventHandler(form, ScopesFormHandleEvent); break;
case MapForm: FrmSetEventHandler(form, MapFormHandleEvent); break;
}
return true;
}
+506
View File
@@ -0,0 +1,506 @@
#include <PalmOS.h>
#include "resources.h"
#include "app.h"
#include "contacts.h"
#include "contact_form.h"
#include "session.h"
#include "map_geo.h"
#include "map_form.h"
#include "ui/draw.h"
#include "ui/text.h"
static Boolean OpenMap(void) VM_SECT3;
static void CloseMap(void) VM_SECT3;
static void LoadNodes(void) VM_SECT3;
static BitmapType *Tile(UInt16 rec) VM_SECT3;
static void ClampCenter(void) VM_SECT3;
static void Draw(void) VM_SECT3;
static void DrawNodes(const RectangleType *r, Int32 vx, Int32 vy) VM_SECT3;
static void DrawInfo(const RectangleType *r) VM_SECT3;
static void DrawFooter(void) VM_SECT3;
static void Halo(const Char *s, UInt16 len, Coord x, Coord y) VM_SECT3;
static Char TypeGlyph(UInt8 type) VM_SECT3;
static Int16 NodeAt(Coord x, Coord y) VM_SECT3;
static void Zoom(Int16 by) VM_SECT3;
static void SaveView(void) VM_SECT3;
static Int32 FloorDiv(Int32 a, Int32 b) VM_SECT3;
#define MAP_DB_NAME "VisorMeshMap"
#define MAP_PREFS_ID 5
#define MAP_PREFS_VER 1
#define MAX_NODES 150
#define CACHE_TILES 4
#define TAP_SLOP 4 /* pixels a pen can move and still be a tap */
#define HIT_RADIUS 8
#define NAME_LEN 16
typedef struct {
Int32 wx, wy; /* world pixels at the max zoom */
UInt16 index; /* contact record */
UInt8 type;
Boolean direct; /* a 0-hop neighbour */
UInt32 lastAdvert;
Char name[NAME_LEN + 1];
} MapNode;
typedef struct {
UInt16 version;
UInt8 zi;
UInt8 pad;
Int32 cx, cy;
} MapPrefs;
static DmOpenRef gDb = NULL;
static MemHandle gHdrH = NULL;
static VmMapHeader gH;
static UInt32 gOldDepth = 0;
static Boolean gDepthSet = false;
static struct { UInt16 rec; BitmapType *bmp; } gCache[CACHE_TILES];
static UInt16 gCacheNext;
static MapNode *gNodes = NULL;
static UInt16 gNodeCount;
static Int16 gSel = -1;
static UInt8 gZi;
static Int32 gCx, gCy; /* view centre, world pixels at the max zoom */
static Coord gDownX, gDownY;
static Boolean gPenDown;
static Int32 FloorDiv(Int32 a, Int32 b)
{
return a >= 0 ? a / b : -((-a + b - 1) / b);
}
static Char TypeGlyph(UInt8 type)
{
switch (type) {
case VM_ADV_TYPE_CHAT: return GLYPH_COMPANION;
case VM_ADV_TYPE_REPEATER: return GLYPH_REPEATER;
case VM_ADV_TYPE_ROOM: return GLYPH_ROOM;
case VM_ADV_TYPE_SENSOR: return GLYPH_SENSOR;
default: return GLYPH_UNKNOWN;
}
}
static Boolean OpenMap(void)
{
LocalID id = DmFindDatabase(0, MAP_DB_NAME);
MapPrefs prefs;
UInt16 size = sizeof(prefs), i;
Int32 x0, x1, y0, y1;
if (!id || (gDb = DmOpenDatabase(0, id, dmModeReadOnly)) == NULL)
return false;
gHdrH = DmQueryRecord(gDb, 0);
if (!gHdrH || !VmMapParseHeader((const UInt8 *)MemHandleLock(gHdrH), MemHandleSize(gHdrH), &gH)) {
if (gHdrH)
MemHandleUnlock(gHdrH);
gHdrH = NULL;
DmCloseDatabase(gDb);
gDb = NULL;
return false;
}
for (i = 0; i < CACHE_TILES; i++) {
gCache[i].rec = 0;
gCache[i].bmp = NULL;
}
gCacheNext = 0;
/* the last view, or the whole map at its widest zoom */
if (PrefGetAppPreferences(VM_CREATOR, MAP_PREFS_ID, &prefs, &size, true) != noPreferenceFound &&
size == sizeof(prefs) && prefs.version == MAP_PREFS_VER && prefs.zi < gH.zoomCount) {
gZi = prefs.zi;
gCx = prefs.cx;
gCy = prefs.cy;
} else {
VmMapWorld(&gH, gH.latS, gH.lonW, &x0, &y1);
VmMapWorld(&gH, gH.latN, gH.lonE, &x1, &y0);
gZi = 0;
gCx = (x0 + x1) / 2;
gCy = (y0 + y1) / 2;
}
ClampCenter();
return true;
}
static void CloseMap(void)
{
UInt16 i;
for (i = 0; i < CACHE_TILES; i++)
if (gCache[i].bmp)
BmpDelete(gCache[i].bmp);
if (gHdrH)
MemHandleUnlock(gHdrH);
gHdrH = NULL;
if (gDb)
DmCloseDatabase(gDb);
gDb = NULL;
if (gNodes)
MemPtrFree(gNodes);
gNodes = NULL;
}
static void SaveView(void)
{
MapPrefs prefs;
prefs.version = MAP_PREFS_VER;
prefs.zi = gZi;
prefs.pad = 0;
prefs.cx = gCx;
prefs.cy = gCy;
PrefSetAppPreferences(VM_CREATOR, MAP_PREFS_ID, MAP_PREFS_VER, &prefs, sizeof(prefs), true);
}
/* Contacts with a position inside the map; the rest aren't shown at all. */
static void LoadNodes(void)
{
UInt16 count, i;
VmContact c;
MapNode *n;
gNodeCount = 0;
gSel = -1;
gNodes = (MapNode *)MemPtrNew(MAX_NODES * sizeof(MapNode));
if (!gNodes)
return;
ContactsOpen();
count = ContactsCount();
for (i = 0; i < count && gNodeCount < MAX_NODES; i++) {
if (!ContactsGet(i, &c) || (c.latE6 == 0 && c.lonE6 == 0))
continue;
n = &gNodes[gNodeCount];
if (!VmMapWorld(&gH, c.latE6, c.lonE6, &n->wx, &n->wy))
continue;
n->index = i;
n->type = c.type;
n->direct = c.outPathLen != 0xFF && (c.outPathLen & 0x3F) == 0;
n->lastAdvert = c.lastAdvert;
StrNCopy(n->name, c.name, NAME_LEN);
n->name[NAME_LEN] = 0;
gNodeCount++;
}
}
/* A decoded tile, from a small cache; NULL for a blank one or on error. */
static BitmapType *Tile(UInt16 rec)
{
MemHandle h;
const UInt8 *src;
BitmapType *bmp;
UInt16 i, err;
UInt32 bytes = (UInt32)VM_MAP_TILE * VM_MAP_TILE * gH.bpp / 8;
Boolean ok;
for (i = 0; i < CACHE_TILES; i++)
if (gCache[i].rec == rec)
return gCache[i].bmp;
h = DmQueryRecord(gDb, rec);
if (!h || MemHandleSize(h) <= 1)
return NULL; /* an all-white tile */
i = gCacheNext;
gCacheNext = (gCacheNext + 1) % CACHE_TILES;
if (!gCache[i].bmp) {
gCache[i].bmp = BmpCreate(VM_MAP_TILE, VM_MAP_TILE, gH.bpp, NULL, &err);
if (!gCache[i].bmp)
return NULL;
}
bmp = gCache[i].bmp;
src = (const UInt8 *)MemHandleLock(h);
ok = VmUnpackBits(src, MemHandleSize(h), (UInt8 *)BmpGetBits(bmp), bytes);
MemHandleUnlock(h);
gCache[i].rec = ok ? rec : 0;
return ok ? bmp : NULL;
}
static void ClampCenter(void)
{
Int32 x0, x1, y0, y1;
VmMapWorld(&gH, gH.latS, gH.lonW, &x0, &y1);
VmMapWorld(&gH, gH.latN, gH.lonE, &x1, &y0);
if (gCx < x0) gCx = x0;
if (gCx > x1) gCx = x1;
if (gCy < y0) gCy = y0;
if (gCy > y1) gCy = y1;
}
/* Text with a one-pixel white outline, so it reads over roads. */
static void Halo(const Char *s, UInt16 len, Coord x, Coord y)
{
RGBColorType white = { 0, 255, 255, 255 }, black = { 0, 0, 0, 0 };
WinSetDrawMode(winOverlay);
WinSetTextColor(WinRGBToIndex(&white));
WinPaintChars(s, len, x - 1, y);
WinPaintChars(s, len, x + 1, y);
WinPaintChars(s, len, x, y - 1);
WinPaintChars(s, len, x, y + 1);
WinSetTextColor(WinRGBToIndex(&black));
WinPaintChars(s, len, x, y);
}
static void DrawNodes(const RectangleType *r, Int32 vx, Int32 vy)
{
RectangleType labels[MAX_NODES / 4];
UInt16 nLabels = 0, i, j, len;
Int32 x, y;
Coord sx, sy, w;
Char g;
Boolean clear, named = gZi + 2 >= gH.zoomCount; /* names at the two closest zooms */
MapNode *n;
FntSetFont(vmSmallFont);
for (i = 0; i < gNodeCount; i++) {
n = &gNodes[i];
VmMapToZoom(&gH, gZi, n->wx, n->wy, &x, &y);
x -= vx;
y -= vy;
if (x < -4 || y < -4 || x >= r->extent.x + 4 || y >= r->extent.y + 4)
continue;
sx = r->topLeft.x + (Coord)x;
sy = r->topLeft.y + (Coord)y;
g = TypeGlyph(n->type);
w = FntCharWidth(g);
Halo(&g, 1, sx - w / 2, sy - gUiFontHeight / 2);
if (n->direct) /* bold: heard directly */
WinPaintChars(&g, 1, sx - w / 2 + 1, sy - gUiFontHeight / 2);
if (!named && (Int16)i != gSel)
continue;
len = StrLen(n->name);
labels[nLabels].topLeft.x = sx + w / 2 + 2;
labels[nLabels].topLeft.y = sy - gUiFontHeight / 2;
labels[nLabels].extent.x = FntCharsWidth(n->name, len);
labels[nLabels].extent.y = gUiFontHeight;
clear = true;
for (j = 0; j < nLabels && clear; j++) {
RectangleType o;
RctGetIntersection(&labels[j], &labels[nLabels], &o);
clear = o.extent.x <= 0 || o.extent.y <= 0;
}
if (!clear && (Int16)i != gSel)
continue;
Halo(n->name, len, labels[nLabels].topLeft.x, labels[nLabels].topLeft.y);
if (nLabels < sizeof(labels) / sizeof(labels[0]) - 1)
nLabels++;
}
}
/* The selected node: a bar along the bottom of the map. */
static void DrawInfo(const RectangleType *r)
{
RectangleType bar;
MapNode *n;
/* glyph, name, age (up to 4) and the hint: 46 at most */
Char line[NAME_LEN + 48], age[12];
UInt32 now;
if (gSel < 0)
return;
n = &gNodes[gSel];
bar.topLeft.x = r->topLeft.x;
bar.topLeft.y = r->topLeft.y + r->extent.y - gUiFontHeight - 2;
bar.extent.x = r->extent.x;
bar.extent.y = gUiFontHeight + 2;
if (n->lastAdvert) {
now = SessionNow();
TxtFormatAge(age, now > n->lastAdvert ? now - n->lastAdvert : 0);
} else {
StrCopy(age, "-");
}
StrPrintF(line, "%c %s %s (tap again to open)", TypeGlyph(n->type), n->name, age);
WinEraseRectangle(&bar, 0);
FntSetFont(vmSmallFont);
WinSetDrawMode(winPaint);
DrawCut(line, bar.topLeft.x + 2, bar.topLeft.y + 1, bar.extent.x - 4);
WinInvertRectangle(&bar, 0);
}
/* The attribution, right of the buttons (which end at x 63): cut if it doesn't fit. */
#define FOOTER_X 66
static void DrawFooter(void)
{
FontID old = FntSetFont(vmSmallFont);
Coord room = 159 - FOOTER_X;
if (FntCharsWidth(gH.attribution, StrLen(gH.attribution)) > room)
DrawCut(gH.attribution, FOOTER_X, 150, room);
else
DrawRight(gH.attribution, 159, 150);
FntSetFont(old);
}
static void Draw(void)
{
FormType *form = FrmGetActiveForm();
RectangleType r;
Int32 cx, cy, vx, vy, col, row, col0, col1, row0, row1;
UInt16 rec;
BitmapType *bmp;
RectangleType t;
FrmGetObjectBounds(form, FrmGetObjectIndex(form, MapGadget), &r);
WinPushDrawState();
WinSetClip(&r);
VmMapToZoom(&gH, gZi, gCx, gCy, &cx, &cy);
vx = cx - r.extent.x / 2;
vy = cy - r.extent.y / 2;
col0 = FloorDiv(vx, VM_MAP_TILE);
col1 = FloorDiv(vx + r.extent.x - 1, VM_MAP_TILE);
row0 = FloorDiv(vy, VM_MAP_TILE);
row1 = FloorDiv(vy + r.extent.y - 1, VM_MAP_TILE);
for (row = row0; row <= row1; row++) {
for (col = col0; col <= col1; col++) {
t.topLeft.x = r.topLeft.x + (Coord)(col * VM_MAP_TILE - vx);
t.topLeft.y = r.topLeft.y + (Coord)(row * VM_MAP_TILE - vy);
t.extent.x = t.extent.y = VM_MAP_TILE;
rec = VmMapTileRecord(&gH, gZi, col, row);
bmp = rec ? Tile(rec) : NULL;
if (bmp)
WinDrawBitmap(bmp, t.topLeft.x, t.topLeft.y);
else
WinEraseRectangle(&t, 0);
}
}
DrawNodes(&r, vx, vy);
DrawInfo(&r);
WinResetClip();
WinPopDrawState();
}
static Int16 NodeAt(Coord px, Coord py)
{
FormType *form = FrmGetActiveForm();
RectangleType r;
Int32 cx, cy, x, y, dx, dy, best = (Int32)HIT_RADIUS * HIT_RADIUS + 1;
Int16 hit = -1;
UInt16 i;
FrmGetObjectBounds(form, FrmGetObjectIndex(form, MapGadget), &r);
VmMapToZoom(&gH, gZi, gCx, gCy, &cx, &cy);
for (i = 0; i < gNodeCount; i++) {
VmMapToZoom(&gH, gZi, gNodes[i].wx, gNodes[i].wy, &x, &y);
dx = r.topLeft.x + (x - (cx - r.extent.x / 2)) - px;
dy = r.topLeft.y + (y - (cy - r.extent.y / 2)) - py;
if (dx * dx + dy * dy < best) {
best = dx * dx + dy * dy;
hit = (Int16)i;
}
}
return hit;
}
static void Zoom(Int16 by)
{
Int16 zi = (Int16)gZi + by;
if (zi < 0 || zi >= gH.zoomCount)
return;
gZi = (UInt8)zi;
Draw();
}
Boolean MapFormHandleEvent(EventType *event)
{
FormType *form = FrmGetActiveForm();
RectangleType r;
UInt32 depths, want;
Coord dx, dy;
Int16 hit;
VmContact c;
switch (event->eType) {
case frmOpenEvent:
if (OpenMap()) {
/* the tiles' grey depth, if the screen can do it */
WinScreenMode(winScreenModeGet, NULL, NULL, &gOldDepth, NULL);
want = gH.bpp;
if (WinScreenMode(winScreenModeGetSupportedDepths, NULL, NULL, &depths, NULL) == errNone &&
(depths & (1UL << (want - 1))) && want != gOldDepth &&
WinScreenMode(winScreenModeSet, NULL, NULL, &want, NULL) == errNone)
gDepthSet = true;
LoadNodes();
FrmDrawForm(form);
DrawFooter();
Draw();
} else {
FrmDrawForm(form);
FrmCustomAlert(MapAlert, "No map installed. Build one with palm-client/tools/make_map.py "
"and HotSync build/VisorMeshMap.pdb.", "", "");
}
return true;
case frmCloseEvent:
if (gDb)
SaveView();
CloseMap();
if (gDepthSet)
WinScreenMode(winScreenModeSet, NULL, NULL, &gOldDepth, NULL);
gDepthSet = false;
break; /* let the form close normally */
case frmUpdateEvent:
FrmDrawForm(form);
if (gDb) {
DrawFooter();
Draw();
}
return true;
case penDownEvent:
if (!gDb)
break;
FrmGetObjectBounds(form, FrmGetObjectIndex(form, MapGadget), &r);
if (!RctPtInRectangle(event->screenX, event->screenY, &r))
break;
gPenDown = true;
gDownX = event->screenX;
gDownY = event->screenY;
return true;
case penUpEvent:
if (!gPenDown)
break;
gPenDown = false;
dx = event->screenX - gDownX;
dy = event->screenY - gDownY;
if (dx > TAP_SLOP || dx < -TAP_SLOP || dy > TAP_SLOP || dy < -TAP_SLOP) {
UInt8 shift = gH.zooms[gH.zoomCount - 1].z - gH.zooms[gZi].z;
gCx -= (Int32)dx << shift; /* drag the map along with the pen */
gCy -= (Int32)dy << shift;
ClampCenter();
} else {
hit = NodeAt(gDownX, gDownY);
if (hit >= 0 && hit == gSel && ContactsGet(gNodes[hit].index, &c)) {
ContactFormOpen(&c);
return true;
}
gSel = hit;
}
Draw();
return true;
case keyDownEvent:
if (!gDb)
break;
if (event->data.keyDown.chr == pageUpChr) {
Zoom(1);
return true;
}
if (event->data.keyDown.chr == pageDownChr) {
Zoom(-1);
return true;
}
break;
case ctlSelectEvent:
switch (event->data.ctlSelect.controlID) {
case MapInButton: if (gDb) Zoom(1); return true;
case MapOutButton: if (gDb) Zoom(-1); return true;
case MapDoneButton: FrmGotoForm(HomeForm); return true;
}
break;
default:
break;
}
return false;
}
+14
View File
@@ -0,0 +1,14 @@
/*
* Map screen: the basemap (the VisorMeshMap database from tools/make_map.py)
* with the contacts that advertise a position plotted on it. Nodes without a
* position aren't shown.
*/
#ifndef VM_MAP_FORM_H
#define VM_MAP_FORM_H
#include <PalmOS.h>
#include "sections.h"
Boolean MapFormHandleEvent(EventType *event) VM_SECT3;
#endif
+139
View File
@@ -0,0 +1,139 @@
#include "map_geo.h"
#define HDR_FIXED 66 /* '>4sHHBBiiiiIHH32s' */
#define HDR_ZOOM 16 /* '>BBHHHii' */
static UInt16 Be16(const UInt8 *p) VM_SECT3;
static Int32 Be32(const UInt8 *p) VM_SECT3;
static Int32 Lookup(const UInt8 *table, UInt16 count, Int32 v0, UInt32 step, Int32 v, Boolean *ok) VM_SECT3;
static UInt16 Be16(const UInt8 *p)
{
return (UInt16)((p[0] << 8) | p[1]);
}
static Int32 Be32(const UInt8 *p)
{
return (Int32)(((UInt32)p[0] << 24) | ((UInt32)p[1] << 16) | ((UInt32)p[2] << 8) | p[3]);
}
Boolean VmMapParseHeader(const UInt8 *p, UInt32 len, VmMapHeader *h)
{
UInt32 need;
UInt16 i;
const UInt8 *z;
if (len < HDR_FIXED || p[0] != 'V' || p[1] != 'M' || p[2] != 'a' || p[3] != 'p' || Be16(p + 4) != 1)
return 0;
if (Be16(p + 6) != VM_MAP_TILE)
return 0;
h->bpp = p[8];
h->zoomCount = p[9];
if (h->zoomCount == 0 || h->zoomCount > VM_MAP_MAX_ZOOMS || (h->bpp != 1 && h->bpp != 2 && h->bpp != 4))
return 0;
h->latS = Be32(p + 10);
h->lonW = Be32(p + 14);
h->latN = Be32(p + 18);
h->lonE = Be32(p + 22);
h->step = (UInt32)Be32(p + 26);
h->latCount = Be16(p + 30);
h->lonCount = Be16(p + 32);
for (i = 0; i < VM_MAP_ATTR_SIZE; i++)
h->attribution[i] = (Char)p[34 + i];
h->attribution[VM_MAP_ATTR_SIZE] = 0;
need = HDR_FIXED + (UInt32)h->zoomCount * HDR_ZOOM + 4UL * (h->latCount + h->lonCount);
if (len < need || h->step == 0 || h->latCount < 2 || h->lonCount < 2)
return 0;
for (i = 0; i < h->zoomCount; i++) {
z = p + HDR_FIXED + i * HDR_ZOOM;
h->zooms[i].z = z[0];
h->zooms[i].cols = Be16(z + 2);
h->zooms[i].rows = Be16(z + 4);
h->zooms[i].first = Be16(z + 6);
h->zooms[i].ox = Be32(z + 8);
h->zooms[i].oy = Be32(z + 12);
}
h->latTable = p + HDR_FIXED + h->zoomCount * HDR_ZOOM;
h->lonTable = h->latTable + 4 * h->latCount;
return 1;
}
/* Linear interpolation in a table of values at v0, v0 + step, ... */
static Int32 Lookup(const UInt8 *table, UInt16 count, Int32 v0, UInt32 step, Int32 v, Boolean *ok)
{
UInt32 d, i, frac;
Int32 a, b;
if (v < v0) {
*ok = 0;
return 0;
}
d = (UInt32)(v - v0);
i = d / step;
frac = d % step;
if (i >= (UInt32)count - 1) {
if (i == (UInt32)count - 1 && frac == 0)
return Be32(table + 4 * i);
*ok = 0;
return 0;
}
a = Be32(table + 4 * i);
b = Be32(table + 4 * (i + 1));
/* (b - a) is a few hundred pixels at most and frac < step = 10000: no overflow */
return a + (Int32)(((b - a) * (Int32)frac) / (Int32)step);
}
Boolean VmMapWorld(const VmMapHeader *h, Int32 latE6, Int32 lonE6, Int32 *wx, Int32 *wy)
{
Boolean ok = 1;
if (latE6 < h->latS || latE6 > h->latN || lonE6 < h->lonW || lonE6 > h->lonE)
return 0;
*wy = Lookup(h->latTable, h->latCount, h->latS, h->step, latE6, &ok);
*wx = Lookup(h->lonTable, h->lonCount, h->lonW, h->step, lonE6, &ok);
return ok;
}
void VmMapToZoom(const VmMapHeader *h, UInt8 zi, Int32 wx, Int32 wy, Int32 *x, Int32 *y)
{
UInt8 shift = h->zooms[h->zoomCount - 1].z - h->zooms[zi].z;
*x = (wx >> shift) - h->zooms[zi].ox;
*y = (wy >> shift) - h->zooms[zi].oy;
}
UInt16 VmMapTileRecord(const VmMapHeader *h, UInt8 zi, Int32 col, Int32 row)
{
const VmMapZoom *z = &h->zooms[zi];
if (col < 0 || row < 0 || col >= (Int32)z->cols || row >= (Int32)z->rows)
return 0;
return (UInt16)(z->first + row * z->cols + col);
}
Boolean VmUnpackBits(const UInt8 *src, UInt32 srcLen, UInt8 *dst, UInt32 dstLen)
{
UInt32 i = 0, o = 0, n;
UInt8 c, v;
while (o < dstLen) {
if (i >= srcLen)
return 0;
c = src[i++];
if (c < 128) {
n = (UInt32)c + 1;
if (i + n > srcLen || o + n > dstLen)
return 0;
while (n--)
dst[o++] = src[i++];
} else if (c > 128) {
n = 257 - (UInt32)c;
if (i >= srcLen || o + n > dstLen)
return 0;
v = src[i++];
while (n--)
dst[o++] = v;
}
}
return 1;
}
+52
View File
@@ -0,0 +1,52 @@
/*
* The basemap database (tools/make_map.py writes it; the layout is in its
* docstring): reading the header, placing a position on the map, and
* unpacking tiles. Positions are Web Mercator pixels; the tool stores world y
* and x at the max zoom every 0.01 degree across the map, and lower zooms are
* that shifted right, so no floating point is needed here.
*
* Plain C with no Palm OS calls, so it also builds on the host for tests.
*/
#ifndef VM_MAP_GEO_H
#define VM_MAP_GEO_H
#include "protocol.h"
#define VM_MAP_TILE 160
#define VM_MAP_MAX_ZOOMS 8
#define VM_MAP_ATTR_SIZE 32
typedef struct {
UInt8 z;
UInt16 cols, rows;
UInt16 first; /* record of tile (0, 0) */
Int32 ox, oy; /* world pixels at z of tile (0, 0)'s top-left */
} VmMapZoom;
typedef struct {
UInt8 bpp;
UInt8 zoomCount;
Int32 latS, lonW, latN, lonE; /* degrees * 1e6 */
UInt32 step; /* table step, degrees * 1e6 */
UInt16 latCount, lonCount;
Char attribution[VM_MAP_ATTR_SIZE + 1];
VmMapZoom zooms[VM_MAP_MAX_ZOOMS]; /* lowest zoom first */
const UInt8 *latTable, *lonTable; /* big-endian Int32s, in the header record */
} VmMapHeader;
/* The header record; the tables point into p, which must stay valid. */
Boolean VmMapParseHeader(const UInt8 *p, UInt32 len, VmMapHeader *h) VM_SECT3;
/* World pixels at the map's max zoom; false outside the map's area. */
Boolean VmMapWorld(const VmMapHeader *h, Int32 latE6, Int32 lonE6, Int32 *wx, Int32 *wy) VM_SECT3;
/* World pixels at the max zoom to pixels at zoom index zi, from the zoom's tile (0, 0). */
void VmMapToZoom(const VmMapHeader *h, UInt8 zi, Int32 wx, Int32 wy, Int32 *x, Int32 *y) VM_SECT3;
/* The record holding tile (col, row) at zoom index zi, or 0 if outside the grid. */
UInt16 VmMapTileRecord(const VmMapHeader *h, UInt8 zi, Int32 col, Int32 row) VM_SECT3;
/* PackBits: false if src runs out or overflows dst before dstLen bytes. */
Boolean VmUnpackBits(const UInt8 *src, UInt32 srcLen, UInt8 *dst, UInt32 dstLen) VM_SECT3;
#endif
+8
View File
@@ -26,6 +26,7 @@
#define MainMenuMuteChannel 1009
#define MainMenuChannelScope 1010
#define MainMenuScopes 1011
#define MainMenuMap 1012
#define MainLinkGroup 1
#define SettingsForm 1200
@@ -147,6 +148,13 @@
#define ChanScopeOKButton 2402
#define ChanScopeCancelButton 2403
#define MapForm 2500
#define MapGadget 2501
#define MapDoneButton 2502
#define MapOutButton 2503
#define MapInButton 2504
#define MapAlert 2505
#define SmallFontRes 1000
#define LargeFontRes 1001
#define FontPreviewForm 1400