/* * Copyright (c) 2023 joshua stein * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above * copyright notice and this permission notice appear in all copies. * * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ #include #include #include "wi-fi.h" #define NO_DEVICE_TEXT "\pNo Device Found" #define NO_NETWORK_TEXT "(No Network)" #define OTHER_NETWORK_TEXT "(Other Network)" #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; pascal Boolean WiFiPasswordDialogFieldFilter(DialogPtr dlg, EventRecord *event, short *hit); WindowPtr 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; ClosePort(tport); win = NewWindow(0L, &bounds, "\pWi-Fi", true, rDocProc, 0L, true, 0L); ((WindowPeek)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); signal_icons = (SICNHandle)GetResource('SICN', OwnedResourceID(0)); if (signal_icons == NULL) warn("Can't find SICN"); ssid_menu = NewMenu(OwnedResourceID(10), "\p"); InsMenuItem(ssid_menu, "\pFinding Wi-Fi...", 1); SetRect(&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); #endif update_window(true); if (wifi_scsi_id == WIFI_SCSI_ID_FINDING) 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); update_window(true); } else { scsi_wifi_info(wifi_scsi_id, &wifi_cur_info); da_state = STATE_SCANNING; scsi_wifi_scan(wifi_scsi_id); } SetPort(savePort); return win; } void update_window(bool force) { Str255 menuText; GrafPtr savePort; Point p; short i, icon, x, w; RgnHandle tmpRgn; Rect r, destRect; BitMap bm, cur_bits; GetPort(&savePort); SetPort(win); DEBUG_LOG(("redrawing window")); cur_bits = win->portBits; SetPortBits(&win_shadow); if (!force) BeginUpdate(win); EraseRect(&win->portRect); DrawControls(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); TextFont(systemFont); TextSize(0); GetItem(ssid_menu, 1, &menuText); x = 15; MoveTo(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)) { /* truncate */ GetPen(&p); MoveTo(p.h - 1, p.v); DrawChar('É'); break; } DrawChar(menuText[i]); x += w; } HLock(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) icon = SIGNAL_FINDING_DEVICE; else if (wifi_cur_info.ssid[0] == '\0') icon = SIGNAL_NONE; else if (wifi_cur_info.rssi >= -60) icon = SIGNAL_4; else if (wifi_cur_info.rssi >= -70) icon = SIGNAL_3; else if (wifi_cur_info.rssi >= -75) icon = SIGNAL_2; else icon = SIGNAL_1; bm.baseAddr = (Ptr)(*signal_icons)[icon]; bm.rowBytes = 2; SetRect(&bm.bounds, 0, 0, 16, 16); CopyBits(&bm, &win->portBits, &bm.bounds, &destRect, srcOr, NULL); HUnlock(signal_icons); if (!force) EndUpdate(win); SetPortBits(&cur_bits); CopyBits(&win_shadow, &win->portBits, &win_shadow.bounds, &win_shadow.bounds, srcCopy, nil); ValidRect(&win->portRect); SetPort(savePort); } void update_wifi_cur_info(void) { struct wifi_network_entry old_info; if (wifi_scsi_id == WIFI_SCSI_ID_NONE) return; memcpy(&old_info, &wifi_cur_info, sizeof(old_info)); scsi_wifi_info(wifi_scsi_id, &wifi_cur_info); if (memcmp(&old_info, &wifi_cur_info, sizeof(old_info)) != 0) { 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)); update_window(true); } else { DEBUG_LOG(("update_wifi_cur_info: updating ssid list")); update_wifi_ssid_list(false); } } } void update_wifi_ssid_list(bool update_networks) { char ssid[64] = { 0 }; short n, m, mitem; if (wifi_scsi_id == WIFI_SCSI_ID_NONE) return; 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)); if (nwifi_scan_networks == 0) { memcpy(&wifi_scan_networks, &wifi_cur_info, sizeof(wifi_cur_info)); nwifi_scan_networks = 1; } } DEBUG_LOG(("current info: ssid:%s", wifi_cur_info.ssid)); /* put the current network first, if any */ memcpy(&wifi_menu_networks[0], &wifi_cur_info, sizeof(wifi_menu_networks[0])); 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)); } /* 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)); continue; } DEBUG_LOG(("net %d: %s", m, wifi_scan_networks[n].ssid)); memcpy(&wifi_menu_networks[m++], &wifi_scan_networks[n], sizeof(wifi_menu_networks[0])); } nwifi_menu_networks = m; if (wifi_menu_networks[0].ssid[0] == '\0') { strlcpy(wifi_menu_networks[0].ssid, NO_NETWORK_TEXT, sizeof(wifi_menu_networks[0].ssid)); nwifi_menu_networks++; } /* keep "other network" as last entry */ strlcpy(wifi_menu_networks[nwifi_menu_networks - 1].ssid, OTHER_NETWORK_TEXT, sizeof(wifi_menu_networks[0].ssid)); wifi_menu_networks[nwifi_menu_networks - 1].flags = 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); for (n = 0, mitem = 1; n < nwifi_menu_networks; n++) { /* * InsMenuItem supports meta chars in item text which can * have side effects, so insert a blank item and then set its * name with SetItem which does not suport meta chars. */ strlcpy(ssid, wifi_menu_networks[n].ssid, sizeof(ssid)); CtoPstr(ssid); if (n > 0) InsMenuItem(ssid_menu, "\p...", mitem); SetItem(ssid_menu, mitem, ssid); if (wifi_cur_info.ssid[0] == '\0' && n == 0) CheckItem(ssid_menu, mitem, true); else CheckItem(ssid_menu, mitem, (strcmp(wifi_menu_networks[n].ssid, wifi_cur_info.ssid) == 0)); mitem++; } update_window(true); } void activate_window(bool activate) { Rect r; GrafPtr savePort; #if 0 GetPort(&savePort); SetPort(win); r = win->portRect; r.top = r.bottom - 16; r.left = r.left - 16; InvalRect(&r); SetPort(savePort); #endif } void destroy_window(void) { ReleaseResource(signal_icons); if (!win) return; DisposeWindow(win); win = 0; } void window_mousedown(Point p) { Str255 password, ssid; Rect 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); r = ssid_menu_rect; LocalToGlobal(&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)) { SetPort(savePort); return; } if (wifi_scsi_id == WIFI_SCSI_ID_NONE) return; InsertMenu(ssid_menu, -1); mitems = CountMItems(ssid_menu); selitem = 1; for (n = 0; n < mitems; n++) { if (strcmp(wifi_menu_networks[n].ssid, wifi_cur_info.ssid) == 0) { selitem = n + 1; break; } } new_net = PopUpMenuSelect(ssid_menu, r.top + 1, r.left + 1, selitem); DeleteMenu((*ssid_menu)->menuID); if (hiword(new_net) == 0 || loword(new_net) == selitem) goto menu_done; net = &wifi_menu_networks[loword(new_net) - 1]; memset(&wjr, 0, sizeof(wjr)); if (net->flags & WIFI_NETWORK_FLAG_HIDDEN) { dg = GetNewDialog(OwnedResourceID(SSID_DIALOG_ID), 0L, (WindowPtr)-1L); center_in_screen(((DialogPeek)dg)->window.port.portRect.right, ((DialogPeek)dg)->window.port.portRect.bottom, false, &r); MoveWindow(dg, r.left, r.top, false); ShowWindow(dg); SetPort(dg); /* outline continue button */ GetDItem(dg, ok, &itype, &ihandle, &irect); PenSize(3, 3); InsetRect(&irect, -4, -4); FrameRoundRect(&irect, 16, 16); PenNormal(); for (;;) { ModalDialog(ModalDialogFilter, &hit); if (hit == ok || hit == cancel) break; } if (hit != ok) { DisposDialog(dg); goto menu_done; } GetDItem(dg, SSID_DIALOG_NAME_FIELD_ID, &itype, &ihandle, &irect); GetIText(ihandle, &ssid); PtoCstr(ssid); strlcpy(wjr.ssid, (char *)ssid, sizeof(wjr.ssid)); DisposDialog(dg); } else strlcpy(wjr.ssid, net->ssid, sizeof(wjr.ssid)); if (net->flags & WIFI_NETWORK_FLAG_AUTH) { strlcpy((char *)ssid, wjr.ssid, sizeof(ssid)); CtoPstr(ssid); ParamText(ssid, "\p", "\p", "\p"); PtoCstr(ssid); dg = GetNewDialog(OwnedResourceID(PASSWORD_DIALOG_ID), 0L, (WindowPtr)-1L); center_in_screen(((DialogPeek)dg)->window.port.portRect.right, ((DialogPeek)dg)->window.port.portRect.bottom, false, &r); MoveWindow(dg, r.left, r.top, false); ShowWindow(dg); SetPort(dg); GetDItem(dg, PASSWORD_DIALOG_PASSWORD_STORAGE_ID, &itype, &ihandle, &irect); SetIText(ihandle, "\p"); /* outline ok button */ GetDItem(dg, ok, &itype, &ihandle, &irect); PenSize(3, 3); InsetRect(&irect, -4, -4); FrameRoundRect(&irect, 16, 16); PenNormal(); for (;;) { ModalDialog(WiFiPasswordDialogFieldFilter, &hit); if (hit == ok || hit == cancel) break; } if (hit != ok) { DisposDialog(dg); goto menu_done; } GetDItem(dg, PASSWORD_DIALOG_PASSWORD_STORAGE_ID, &itype, &ihandle, &irect); GetIText(ihandle, &password); PtoCstr(password); //DEBUG_LOG(("pass is \"%s\"", password)); strlcpy(wjr.key, (char *)password, sizeof(wjr.key)); DisposDialog(dg); } scsi_wifi_join(wifi_scsi_id, &wjr); /* force an update of the list */ scsi_wifi_info(wifi_scsi_id, &wifi_cur_info); update_wifi_ssid_list(false); menu_done: SetPort(savePort); } pascal Boolean WiFiPasswordDialogFieldFilter(DialogPtr dlg, EventRecord *event, short *hit) { Str255 password; DialogPeek dlgp; ControlHandle ihandle; Rect irect; short sel_start, sel_end, itype; char key; dlgp = (DialogPeek)dlg; switch (event->what) { case keyDown: case autoKey: sel_start = (*(dlgp->textH))->selStart; sel_end = (*(dlgp->textH))->selEnd; key = event->message & charCodeMask; if (event->modifiers & cmdKey) { /* TODO: implement DlgPaste for cmd+v? */ event->what = nullEvent; return FALSE; } GetDItem(dlg, PASSWORD_DIALOG_PASSWORD_STORAGE_ID, &itype, &ihandle, &irect); GetIText(ihandle, &password); PtoCstr(password); if (key == 8) { /* backspace */ if (sel_start == sel_end && sel_start > 0) memmove(password + sel_start - 1, password + sel_start, sizeof(password) - sel_start - 1); else if (sel_start != sel_end) memmove(password + sel_start, password + sel_end, sizeof(password) - sel_end - 1); } else if (sel_start >= sizeof(password)) { event->what = nullEvent; return FALSE; } else if (key >= ' ' && key <= '~') { if (sel_start != sel_end) /* delete selection before making space for new char */ memmove(password + sel_start, password + sel_end, sizeof(password) - sel_end - 1); memmove(password + sel_start + 1, password + sel_start, sizeof(password) - sel_start - 1); password[sel_start] = key; event->message = '¥'; } password[(*(dlgp->textH))->teLength + 1] = '\0'; CtoPstr(password); SetIText(ihandle, password); sel_start = 0; break; } return ModalDialogFilter(dlg, event, hit); }