AmendHub

Download:

jcs

/

wifi_da

/

amendments

/

32

*: Make debug logging permanent, outputting if the logger is open

The logger can now be opened by clicking on the signal icon.

jcs made amendment 32 about 1 month ago
--- main.c Thu Sep 19 20:59:25 2024 +++ main.c Tue Dec 3 16:50:05 2024 @@ -25,16 +25,15 @@ DCtlPtr dce = NULL; SysEnvRec world; #define ON_SYSTEM_7 (world.systemVersion >= 0x0700) -short da_state = STATE_CLOSED; +struct wifi_da wifi_da = { 0 }; + short wifi_scsi_id = WIFI_SCSI_ID_FINDING; unsigned long wifi_scan_started = 0; struct wifi_network_entry wifi_cur_info = { 0 }; struct wifi_network_entry wifi_scan_networks[10] = { 0 }; MenuHandle menu, apple_menu; short menuID = 0; -WindowPtr win = 0L; Cursor watch; -WindowPtr logger = 0; short main(cntrlParam *p, DCtlPtr d, short n); void da_open(void); @@ -43,18 +42,17 @@ void da_close(void); bool handle_menu(short i); void handle_event(EventRecord *e); void handle_timer(void); -#ifdef DEBUG_LOGGING void logger_vprintf(const char *format, va_list ap); -#endif short main(cntrlParam *p, DCtlPtr d, short n) { if (d->dCtlStorage == 0) { - /* open */ - if (n == 0) { + /* no global memory allocated by THINK C stub */ + if (n == 0) { /* open request */ SysBeep(3); CloseDriver(d->dCtlRefNum); + return -1; } return 0; } @@ -101,28 +99,16 @@ da_open(void) dce->dCtlFlags |= dNeedLock | dNeedTime | dNeedGoodBye; dce->dCtlDelay = 61; -#ifdef DEBUG_LOGGING - if (!logger) { - bounds.top = 150; - bounds.bottom = 320; - bounds.left = 20; - bounds.right = 400; - logger = NewWindow(0L, &bounds, "\pLogger", true, rDocProc, 0L, - true, 0L); - ((WindowPeek)logger)->windowKind = dce->dCtlRefNum; - } -#endif - - if (win) - SelectWindow(win); + if (wifi_da.win) + SelectWindow(wifi_da.win); - if (da_state != STATE_CLOSED) { + if (wifi_da.state != STATE_CLOSED) { dce->dCtlMenu = menuID; return; } - da_state = STATE_INIT; - + wifi_da.state = STATE_INIT; + SetResLoad(false); h = GetCursor(watchCursor); dispose = (GetHandleSize((Handle)h) == 0); @@ -139,7 +125,8 @@ da_open(void) DrawMenuBar(); } - dce->dCtlWindow = create_window(dce->dCtlRefNum); + create_window(dce->dCtlRefNum); + dce->dCtlWindow = wifi_da.win; HiliteMenu(0); } @@ -172,13 +159,9 @@ da_close(void) } dce->dCtlMenu = 0; - destroy_window(); - if (logger) { - DisposeWindow(logger); - logger = 0; - } + destroy_windows(); dce->dCtlWindow = 0; - da_state = STATE_CLOSED; + wifi_da.state = STATE_CLOSED; } bool @@ -235,26 +218,27 @@ handle_timer(void) static long last_update = 0; static long last_scan = 0; - if (!win) + if (!wifi_da.win) return; - switch (da_state) { + switch (wifi_da.state) { case STATE_SCANNING: /* check every second */ if ((Ticks - last_update) < (60 * 1)) break; - DEBUG_LOG(("%ld - handle_timer - checking for scan completion", - Ticks)); + logger_printf("%ld - handle_timer - checking for scan completion", + Ticks); last_update = Ticks; if (!scsi_wifi_scan_finished(wifi_scsi_id)) { - DEBUG_LOG(("%ld - handle_timer - scan not done", Ticks)); + logger_printf("%ld - handle_timer - scan not done", Ticks); if (Time - wifi_scan_started <= 5) break; - DEBUG_LOG(("Wi-Fi scan failed to finish, canceling")); + logger_printf("Wi-Fi scan failed to finish, canceling"); } last_scan = Ticks; - da_state = STATE_IDLE; - DEBUG_LOG(("%ld - handle_timer - scan done, updating list", Ticks)); + wifi_da.state = STATE_IDLE; + logger_printf("%ld - handle_timer - scan done, updating list", + Ticks); update_wifi_ssid_list(true); break; case STATE_IDLE: @@ -264,24 +248,24 @@ handle_timer(void) last_update = Ticks; if ((Ticks - last_scan) > (60 * 30)) { - da_state = STATE_SCANNING; - DEBUG_LOG(("%ld - handle_timer - idle, scanning", Ticks)); + wifi_da.state = STATE_SCANNING; + logger_printf("%ld - handle_timer - idle, scanning", Ticks); scsi_wifi_scan(wifi_scsi_id); } else { - DEBUG_LOG(("%ld - handle_timer - idle, updating info", Ticks)); + logger_printf("%ld - handle_timer - idle, updating info", + Ticks); update_wifi_cur_info(); } break; } } -#ifdef DEBUG_LOGGING void logger_printf(const char *format, ...) { va_list va; - if (!logger) + if (!wifi_da.logger) return; va_start(va, format); @@ -297,21 +281,23 @@ logger_vprintf(const char *format, va_list ap) ssize_t len; RgnHandle clip; + if (!wifi_da.logger) + return; + len = vsnprintf(buf, sizeof(buf), format, ap); GetPort(&savePort); - SetPort(logger); + SetPort(wifi_da.logger); clip = NewRgn(); - ScrollRect(&logger->portRect, 0, -11, clip); + ScrollRect(&wifi_da.logger->portRect, 0, -11, clip); DisposeRgn(clip); - MoveTo(4, logger->portRect.bottom - 4); + MoveTo(4, wifi_da.logger->portRect.bottom - 4); TextFont(geneva); TextSize(9); DrawText(buf, 0, len); - ValidRect(&logger->portRect); + ValidRect(&wifi_da.logger->portRect); SetPort(savePort); } -#endif \ No newline at end of file --- wi-fi.h Thu Sep 19 21:03:44 2024 +++ wi-fi.h Tue Dec 3 16:51:15 2024 @@ -20,9 +20,6 @@ #include <stdlib.h> #include "util.h" -/* disable in production */ -//#define DEBUG_LOGGING 1 - #define MAIN_MENU_ID 1 #define MAIN_MENU_ABOUT_ID 1 #define MAIN_MENU_QUIT_ID 3 @@ -52,13 +49,12 @@ typedef short SICN[16]; typedef SICN **SICNHandle; enum state { - STATE_CLOSED, + STATE_CLOSED = 0, STATE_INIT, STATE_FINDING, STATE_SCANNING, STATE_IDLE }; -extern short da_state; enum signal_icon_id { SIGNAL_NO_DEVICE = 0, @@ -70,6 +66,21 @@ enum signal_icon_id { SIGNAL_4 = 6 }; +struct wifi_da { + short state; + WindowPtr win; + Rect bounds; + ControlHandle ssid_list; + MenuHandle ssid_menu; + Rect ssid_menu_rect; + SICNHandle signal_icons; + Rect icon_rect; + BitMap win_shadow; + WindowPtr logger; +}; + +extern struct wifi_da wifi_da; + struct wifi_network_entry { char ssid[64]; unsigned char bssid[6]; @@ -88,7 +99,6 @@ struct wifi_join_request { unsigned char _padding; }; -extern WindowPtr win; extern DCtlPtr dce; extern short wifi_scsi_id; @@ -104,18 +114,13 @@ extern Cursor watch; void wifi_about(void); -#if DEBUG_LOGGING -#define DEBUG_LOG(args) logger_printf args void logger_printf(const char *format, ...); -#else -#define DEBUG_LOG(args) ((void)0) -#endif -WindowPtr create_window(short ctlrefnum); +void create_window(short ctlrefnum); void update_window(bool force); void update_wifi_cur_info(void); void update_wifi_ssid_list(bool update_networks); -void destroy_window(void); +void destroy_windows(void); void activate_window(bool activate); void window_mousedown(Point p); --- window.c Thu Sep 19 20:28:57 2024 +++ window.c Tue Dec 3 17:08:58 2024 @@ -25,55 +25,56 @@ #define DA_HEIGHT 25 #define DA_WIDTH 180 -SICNHandle signal_icons; -ControlHandle ssid_list; -MenuHandle ssid_menu; -Rect ssid_menu_rect; -BitMap win_shadow; short nwifi_scan_networks = 0; struct wifi_network_entry wifi_menu_networks[nitems(wifi_scan_networks)] = { 0 }; short nwifi_menu_networks = 0; +void create_logger(void); pascal Boolean WiFiPasswordDialogFieldFilter(DialogPtr dlg, EventRecord *event, short *hit); -WindowPtr +void create_window(short ctlrefnum) { GrafPtr savePort, tport; - Rect bounds; - + GetPort(&savePort); /* screenBits is not available from a DA */ tport = (GrafPtr)NewPtr(sizeof(GrafPort)); OpenPort(tport); /* upper right corner, but leave space for desktop icon */ - bounds.top = 45; - bounds.bottom = bounds.top + DA_HEIGHT; - bounds.left = tport->portRect.right - DA_WIDTH - 70; - bounds.right = bounds.left + DA_WIDTH; + wifi_da.bounds.top = 45; + wifi_da.bounds.bottom = wifi_da.bounds.top + DA_HEIGHT; + wifi_da.bounds.left = tport->portRect.right - DA_WIDTH - 70; + wifi_da.bounds.right = wifi_da.bounds.left + DA_WIDTH; ClosePort(tport); - win = NewWindow(0L, &bounds, "\pWi-Fi", true, rDocProc, 0L, true, 0L); - ((WindowPeek)win)->windowKind = ctlrefnum; + wifi_da.win = NewWindow(0L, &wifi_da.bounds, "\pWi-Fi", true, + rDocProc, 0L, true, 0L); + ((WindowPeek)(wifi_da.win))->windowKind = ctlrefnum; - win_shadow.rowBytes = (((DA_WIDTH - 1) / 16) + 1) * 2; - win_shadow.baseAddr = xmalloczero(win_shadow.rowBytes * DA_HEIGHT); - SetRect(&win_shadow.bounds, 0, 0, DA_WIDTH, DA_HEIGHT); + wifi_da.win_shadow.rowBytes = (((DA_WIDTH - 1) / 16) + 1) * 2; + wifi_da.win_shadow.baseAddr = + xmalloczero(wifi_da.win_shadow.rowBytes * DA_HEIGHT); + SetRect(&wifi_da.win_shadow.bounds, 0, 0, DA_WIDTH, DA_HEIGHT); - signal_icons = (SICNHandle)GetResource('SICN', OwnedResourceID(0)); - if (signal_icons == NULL) - warn("Can't find SICN"); + wifi_da.signal_icons = (SICNHandle)GetResource('SICN', + OwnedResourceID(0)); + if (wifi_da.signal_icons == NULL) + panic("Can't find SICN"); + + SetRect(&wifi_da.icon_rect, 6, 4, 22, 20); /* ltrb */ - ssid_menu = NewMenu(OwnedResourceID(10), "\p"); - InsMenuItem(ssid_menu, "\pFinding Wi-Fi...", 1); + wifi_da.ssid_menu = NewMenu(OwnedResourceID(10), "\p"); + InsMenuItem(wifi_da.ssid_menu, "\pFinding Wi-Fi...", 1); - SetRect(&ssid_menu_rect, 30, 3, 170, 21); /* ltrb */ + SetRect(&wifi_da.ssid_menu_rect, 30, 3, 170, 21); /* ltrb */ #if 0 /* system 6 does not seem to support popupMenuProc */ - ssid_list = NewControl(win, &ssid_menu_rect, 0, true, 0, - OwnedResourceID(SSID_MENU_ID), 0, popupMenuProc + popupFixedWidth, 0L); + ssid_list = NewControl(wifi_da.win, &wifi_da.ssid_menu_rect, 0, true, + 0, OwnedResourceID(SSID_MENU_ID), 0, popupMenuProc + popupFixedWidth, + 0L); #endif update_window(true); @@ -82,18 +83,30 @@ create_window(short ctlrefnum) wifi_scsi_id = scsi_find_wifi(); if (wifi_scsi_id == WIFI_SCSI_ID_NONE) { - da_state = STATE_IDLE; - SetItem(ssid_menu, 1, NO_DEVICE_TEXT); + wifi_da.state = STATE_IDLE; + SetItem(wifi_da.ssid_menu, 1, NO_DEVICE_TEXT); update_window(true); } else { scsi_wifi_info(wifi_scsi_id, &wifi_cur_info); - da_state = STATE_SCANNING; + wifi_da.state = STATE_SCANNING; scsi_wifi_scan(wifi_scsi_id); } SetPort(savePort); +} - return win; +void +create_logger(void) +{ + Rect bounds; + + bounds.top = 150; + bounds.bottom = 320; + bounds.left = 20; + bounds.right = 400; + wifi_da.logger = NewWindow(0L, &bounds, "\pLogger", true, rDocProc, 0L, + true, 0L); + ((WindowPeek)wifi_da.logger)->windowKind = dce->dCtlRefNum; } void @@ -108,35 +121,35 @@ update_window(bool force) BitMap bm, cur_bits; GetPort(&savePort); - SetPort(win); + SetPort(wifi_da.win); - DEBUG_LOG(("redrawing window")); - cur_bits = win->portBits; - SetPortBits(&win_shadow); + logger_printf("redrawing window"); + cur_bits = wifi_da.win->portBits; + SetPortBits(&wifi_da.win_shadow); if (!force) - BeginUpdate(win); + BeginUpdate(wifi_da.win); - EraseRect(&win->portRect); - DrawControls(win); + EraseRect(&wifi_da.win->portRect); + DrawControls(wifi_da.win); /* system 6 does not seem to support popupMenuProc, fake it */ - FrameRect(&ssid_menu_rect); - MoveTo(ssid_menu_rect.left + 1, ssid_menu_rect.bottom); - LineTo(ssid_menu_rect.right, ssid_menu_rect.bottom); - MoveTo(ssid_menu_rect.right, ssid_menu_rect.top + 1); - LineTo(ssid_menu_rect.right, ssid_menu_rect.bottom); + FrameRect(&wifi_da.ssid_menu_rect); + MoveTo(wifi_da.ssid_menu_rect.left + 1, wifi_da.ssid_menu_rect.bottom); + LineTo(wifi_da.ssid_menu_rect.right, wifi_da.ssid_menu_rect.bottom); + MoveTo(wifi_da.ssid_menu_rect.right, wifi_da.ssid_menu_rect.top + 1); + LineTo(wifi_da.ssid_menu_rect.right, wifi_da.ssid_menu_rect.bottom); TextFont(systemFont); TextSize(0); - GetItem(ssid_menu, 1, &menuText); + GetItem(wifi_da.ssid_menu, 1, &menuText); x = 15; - MoveTo(ssid_menu_rect.left + x, 16); + MoveTo(wifi_da.ssid_menu_rect.left + x, 16); for (i = 1; i <= menuText[0]; i++) { w = CharWidth(menuText[i]); - if (i < menuText[0] && - x + w >= (ssid_menu_rect.right - ssid_menu_rect.left - 14)) { + if (i < menuText[0] && x + w >= (wifi_da.ssid_menu_rect.right - + wifi_da.ssid_menu_rect.left - 14)) { /* truncate */ GetPen(&p); MoveTo(p.h - 1, p.v); @@ -147,10 +160,8 @@ update_window(bool force) x += w; } - HLock(signal_icons); + HLock(wifi_da.signal_icons); - SetRect(&destRect, 6, 4, 22, 20); /* ltrb */ - if (wifi_scsi_id == WIFI_SCSI_ID_NONE) icon = SIGNAL_NO_DEVICE; else if (wifi_scsi_id == WIFI_SCSI_ID_FINDING) @@ -166,22 +177,23 @@ update_window(bool force) else icon = SIGNAL_1; - bm.baseAddr = (Ptr)(*signal_icons)[icon]; + bm.baseAddr = (Ptr)(*(wifi_da.signal_icons))[icon]; bm.rowBytes = 2; SetRect(&bm.bounds, 0, 0, 16, 16); - CopyBits(&bm, &win->portBits, &bm.bounds, &destRect, srcOr, NULL); + CopyBits(&bm, &wifi_da.win->portBits, &bm.bounds, &wifi_da.icon_rect, + srcOr, NULL); - HUnlock(signal_icons); + HUnlock(wifi_da.signal_icons); if (!force) - EndUpdate(win); + EndUpdate(wifi_da.win); SetPortBits(&cur_bits); - CopyBits(&win_shadow, &win->portBits, &win_shadow.bounds, - &win_shadow.bounds, srcCopy, nil); + CopyBits(&wifi_da.win_shadow, &wifi_da.win->portBits, + &wifi_da.win_shadow.bounds, &wifi_da.win_shadow.bounds, srcCopy, nil); - ValidRect(&win->portRect); + ValidRect(&wifi_da.win->portRect); SetPort(savePort); } @@ -201,11 +213,11 @@ update_wifi_cur_info(void) if (memcmp(&old_info.ssid, &wifi_cur_info.ssid, sizeof(old_info.ssid)) == 0) { /* just updating RSSI */ - DEBUG_LOG(("update_wifi_cur_info: just updating rssi to %d", - wifi_cur_info.rssi)); + logger_printf("update_wifi_cur_info: just updating rssi to %d", + wifi_cur_info.rssi); update_window(true); } else { - DEBUG_LOG(("update_wifi_cur_info: updating ssid list")); + logger_printf("update_wifi_cur_info: updating ssid list"); update_wifi_ssid_list(false); } } @@ -223,8 +235,8 @@ update_wifi_ssid_list(bool update_networks) if (update_networks) { nwifi_scan_networks = scsi_wifi_scan_results(wifi_scsi_id, wifi_scan_networks, nitems(wifi_scan_networks)); - DEBUG_LOG(("scsi_wifi_scan_results: %d networks", - nwifi_scan_networks)); + logger_printf("scsi_wifi_scan_results: %d networks", + nwifi_scan_networks); if (nwifi_scan_networks == 0) { memcpy(&wifi_scan_networks, &wifi_cur_info, @@ -233,7 +245,7 @@ update_wifi_ssid_list(bool update_networks) } } - DEBUG_LOG(("current info: ssid:%s", wifi_cur_info.ssid)); + logger_printf("current info: ssid:%s", wifi_cur_info.ssid); /* put the current network first, if any */ memcpy(&wifi_menu_networks[0], &wifi_cur_info, @@ -242,19 +254,19 @@ update_wifi_ssid_list(bool update_networks) nwifi_menu_networks = nwifi_scan_networks; if (nwifi_menu_networks > nitems(wifi_menu_networks)) { nwifi_menu_networks = nitems(wifi_menu_networks); - DEBUG_LOG(("capping nets to %d", nwifi_menu_networks)); + logger_printf("capping nets to %d", nwifi_menu_networks); } /* add each additional network from the scan, excluding current */ for (n = 0, m = 1; n < nwifi_menu_networks; n++) { if (strcmp(wifi_scan_networks[n].ssid, wifi_cur_info.ssid) == 0) { - DEBUG_LOG(("skipping %s, is current", - wifi_scan_networks[n].ssid)); + logger_printf("skipping %s, is current", + wifi_scan_networks[n].ssid); continue; } - DEBUG_LOG(("net %d: %s", m, wifi_scan_networks[n].ssid)); + logger_printf("net %d: %s", m, wifi_scan_networks[n].ssid); memcpy(&wifi_menu_networks[m++], &wifi_scan_networks[n], sizeof(wifi_menu_networks[0])); @@ -275,8 +287,8 @@ update_wifi_ssid_list(bool update_networks) WIFI_NETWORK_FLAG_AUTH | WIFI_NETWORK_FLAG_HIDDEN; /* leave the first item and we'll set its text later */ - while (CountMItems(ssid_menu) > 1) - DelMenuItem(ssid_menu, 2); + while (CountMItems(wifi_da.ssid_menu) > 1) + DelMenuItem(wifi_da.ssid_menu, 2); for (n = 0, mitem = 1; n < nwifi_menu_networks; n++) { /* @@ -288,14 +300,14 @@ update_wifi_ssid_list(bool update_networks) CtoPstr(ssid); if (n > 0) - InsMenuItem(ssid_menu, "\p...", mitem); + InsMenuItem(wifi_da.ssid_menu, "\p...", mitem); - SetItem(ssid_menu, mitem, ssid); + SetItem(wifi_da.ssid_menu, mitem, ssid); if (wifi_cur_info.ssid[0] == '\0' && n == 0) - CheckItem(ssid_menu, mitem, true); + CheckItem(wifi_da.ssid_menu, mitem, true); else - CheckItem(ssid_menu, mitem, + CheckItem(wifi_da.ssid_menu, mitem, (strcmp(wifi_menu_networks[n].ssid, wifi_cur_info.ssid) == 0)); @@ -323,40 +335,54 @@ activate_window(bool activate) } void -destroy_window(void) +destroy_windows(void) { - ReleaseResource(signal_icons); - - if (!win) - return; + ReleaseResource(wifi_da.signal_icons); - DisposeWindow(win); - win = 0; + if (wifi_da.win) { + DisposeWindow(wifi_da.win); + wifi_da.win = 0; + } + + if (wifi_da.logger) { + DisposeWindow(wifi_da.logger); + wifi_da.logger = 0; + } } void window_mousedown(Point p) { Str255 password, ssid; - Rect r, irect; + Rect menu_r, icon_r, r, irect; GrafPtr savePort; short mitems, n, selitem, hit, itype; long new_net; struct wifi_network_entry *net; struct wifi_join_request wjr; DialogPtr dg; - EventRecord e; ControlHandle ihandle; GetPort(&savePort); - SetPort(win); + SetPort(wifi_da.win); - r = ssid_menu_rect; - LocalToGlobal(&r); + icon_r = wifi_da.icon_rect; + LocalToGlobal(&icon_r); - /* XXX: why does PtInRect not work here? */ - if (!(p.h >= r.left && p.h <= r.left + r.right && - p.v >= r.top && p.v <= r.top + r.bottom)) { + /* XXX: why does PtInRect not work? */ + + if (p.h >= icon_r.left && p.h <= icon_r.left + icon_r.right && + p.v >= icon_r.top && p.v <= icon_r.top + icon_r.bottom) { + if (!wifi_da.logger) + create_logger(); + return; + } + + menu_r = wifi_da.ssid_menu_rect; + LocalToGlobal(&menu_r); + + if (!(p.h >= menu_r.left && p.h <= menu_r.left + menu_r.right && + p.v >= menu_r.top && p.v <= menu_r.top + menu_r.bottom)) { SetPort(savePort); return; } @@ -364,9 +390,9 @@ window_mousedown(Point p) if (wifi_scsi_id == WIFI_SCSI_ID_NONE) return; - InsertMenu(ssid_menu, -1); + InsertMenu(wifi_da.ssid_menu, -1); - mitems = CountMItems(ssid_menu); + mitems = CountMItems(wifi_da.ssid_menu); selitem = 1; for (n = 0; n < mitems; n++) { if (strcmp(wifi_menu_networks[n].ssid, wifi_cur_info.ssid) == 0) { @@ -375,9 +401,10 @@ window_mousedown(Point p) } } - new_net = PopUpMenuSelect(ssid_menu, r.top + 1, r.left + 1, selitem); + new_net = PopUpMenuSelect(wifi_da.ssid_menu, menu_r.top + 1, + menu_r.left + 1, selitem); - DeleteMenu((*ssid_menu)->menuID); + DeleteMenu((*(wifi_da.ssid_menu))->menuID); if (hiword(new_net) == 0 || loword(new_net) == selitem) goto menu_done;