| 1 |
/* |
| 2 |
* Copyright (c) 2022 joshua stein <jcs@jcs.org> |
| 3 |
* |
| 4 |
* Permission to use, copy, modify, and distribute this software for any |
| 5 |
* purpose with or without fee is hereby granted, provided that the above |
| 6 |
* copyright notice and this permission notice appear in all copies. |
| 7 |
* |
| 8 |
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
| 9 |
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 10 |
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
| 11 |
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 12 |
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 13 |
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
| 14 |
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 15 |
*/ |
| 16 |
|
| 17 |
#include <stdio.h> |
| 18 |
#include <string.h> |
| 19 |
|
| 20 |
#include "detritus.h" |
| 21 |
#include "focusable.h" |
| 22 |
#include "tcp.h" |
| 23 |
#include "util.h" |
| 24 |
|
| 25 |
/* be sure this is NULL terminated */ |
| 26 |
const struct default_bookmark default_bookmarks[] = { |
| 27 |
{ "Floodgap Gopher", "gopher://gopher.floodgap.com/", }, |
| 28 |
{ "SDF Gopher", "gopher://sdf.org/", }, |
| 29 |
{ "Gemini Protocol", "gemini://geminiprotocol.net/", }, |
| 30 |
{ 0 }, |
| 31 |
}; |
| 32 |
|
| 33 |
MenuHandle apple_menu, file_menu, edit_menu, bookmarks_menu; |
| 34 |
Cursor finger_cursor; |
| 35 |
|
| 36 |
bool quitting = false; |
| 37 |
|
| 38 |
bool handle_menu(long menu_id); |
| 39 |
void handle_exit(void); |
| 40 |
|
| 41 |
short |
| 42 |
main(void) |
| 43 |
{ |
| 44 |
Handle mbar; |
| 45 |
EventRecord event; |
| 46 |
WindowPtr event_win; |
| 47 |
GrafPtr old_port; |
| 48 |
RgnHandle mouse_region; |
| 49 |
CursHandle cursh; |
| 50 |
struct focusable *found_focusable; |
| 51 |
short event_in, n; |
| 52 |
char key; |
| 53 |
|
| 54 |
InitGraf(&thePort); |
| 55 |
InitFonts(); |
| 56 |
FlushEvents(everyEvent, 0); |
| 57 |
InitWindows(); |
| 58 |
InitMenus(); |
| 59 |
TEInit(); |
| 60 |
InitDialogs(0); |
| 61 |
InitCursor(); |
| 62 |
MaxApplZone(); |
| 63 |
|
| 64 |
util_init(); |
| 65 |
_atexit(handle_exit); |
| 66 |
|
| 67 |
mbar = GetNewMBar(MBAR_ID); |
| 68 |
SetMenuBar(mbar); |
| 69 |
apple_menu = GetMHandle(APPLE_MENU_ID); |
| 70 |
AddResMenu(apple_menu, 'DRVR'); |
| 71 |
file_menu = GetMHandle(FILE_MENU_ID); |
| 72 |
edit_menu = GetMHandle(EDIT_MENU_ID); |
| 73 |
bookmarks_menu = GetMHandle(BOOKMARKS_MENU_ID); |
| 74 |
menu_defaults(); |
| 75 |
|
| 76 |
DrawMenuBar(); |
| 77 |
|
| 78 |
if (_TCPInit() != 0) |
| 79 |
panic("Failed initializing MacTCP"); |
| 80 |
|
| 81 |
settings_load(); |
| 82 |
menu_load_bookmarks(); |
| 83 |
|
| 84 |
cursh = GetCursor(CURSOR_FINGER_ID); |
| 85 |
if (cursh == NULL) |
| 86 |
panic("Can't find my finger!"); |
| 87 |
finger_cursor = **cursh; |
| 88 |
|
| 89 |
/* browser before scsi so the user can look at something while we poll */ |
| 90 |
browser_init(); |
| 91 |
scsi_find_tls(); |
| 92 |
|
| 93 |
while (!quitting) { |
| 94 |
WaitNextEvent(everyEvent, &event, 5L, NULL); |
| 95 |
|
| 96 |
switch (event.what) { |
| 97 |
case nullEvent: |
| 98 |
for (n = 0; n < nfocusables; n++) { |
| 99 |
if (focusables[n]->idle) |
| 100 |
focusables[n]->idle(focusables[n], &event); |
| 101 |
} |
| 102 |
break; |
| 103 |
case keyDown: |
| 104 |
case autoKey: |
| 105 |
key = event.message & charCodeMask; |
| 106 |
if ((event.modifiers & cmdKey) != 0 && |
| 107 |
handle_menu(MenuKey(key)) == true) |
| 108 |
break; |
| 109 |
if (nfocusables && focusables[0]->visible && |
| 110 |
focusables[0]->key_down) |
| 111 |
focusables[0]->key_down(focusables[0], &event); |
| 112 |
break; |
| 113 |
case mouseDown: |
| 114 |
event_in = FindWindow(event.where, &event_win); |
| 115 |
found_focusable = focusable_find(event_win); |
| 116 |
|
| 117 |
if (found_focusable) |
| 118 |
focusable_show(found_focusable); |
| 119 |
|
| 120 |
switch (event_in) { |
| 121 |
case inMenuBar: |
| 122 |
handle_menu(MenuSelect(event.where)); |
| 123 |
break; |
| 124 |
case inSysWindow: |
| 125 |
SystemClick(&event, event_win); |
| 126 |
break; |
| 127 |
case inDrag: |
| 128 |
DragWindow(event_win, event.where, &screenBits.bounds); |
| 129 |
break; |
| 130 |
case inGrow: |
| 131 |
if (found_focusable && found_focusable->resize) |
| 132 |
found_focusable->resize(found_focusable, &event); |
| 133 |
break; |
| 134 |
case inGoAway: |
| 135 |
if (TrackGoAway(event_win, event.where) && found_focusable) |
| 136 |
focusable_close(found_focusable); |
| 137 |
break; |
| 138 |
case inContent: |
| 139 |
if (found_focusable && found_focusable->mouse_down) |
| 140 |
found_focusable->mouse_down(found_focusable, &event); |
| 141 |
break; |
| 142 |
} |
| 143 |
|
| 144 |
break; |
| 145 |
case updateEvt: |
| 146 |
case activateEvt: |
| 147 |
event_win = (WindowPtr)event.message; |
| 148 |
found_focusable = focusable_find(event_win); |
| 149 |
|
| 150 |
GetPort(&old_port); |
| 151 |
SetPort(event_win); |
| 152 |
|
| 153 |
if (event.what == updateEvt) |
| 154 |
BeginUpdate(event_win); |
| 155 |
|
| 156 |
if (found_focusable && found_focusable->update) |
| 157 |
found_focusable->update(found_focusable, &event); |
| 158 |
|
| 159 |
if (event.what == updateEvt) |
| 160 |
EndUpdate(event_win); |
| 161 |
|
| 162 |
SetPort(old_port); |
| 163 |
break; |
| 164 |
case app4Evt: |
| 165 |
if (HiWord(event.message) & (1 << 8)) { |
| 166 |
/* multifinder suspend/resume */ |
| 167 |
switch (event.message & (1 << 0)) { |
| 168 |
case 0: |
| 169 |
/* suspend */ |
| 170 |
for (n = 0; n < nfocusables; n++) { |
| 171 |
if (focusables[n]->suspend) |
| 172 |
focusables[n]->suspend(focusables[n], &event); |
| 173 |
} |
| 174 |
break; |
| 175 |
case 1: |
| 176 |
/* resume */ |
| 177 |
for (n = 0; n < nfocusables; n++) { |
| 178 |
if (focusables[n]->resume) |
| 179 |
focusables[n]->resume(focusables[n], &event); |
| 180 |
} |
| 181 |
break; |
| 182 |
} |
| 183 |
} |
| 184 |
break; |
| 185 |
} |
| 186 |
} |
| 187 |
|
| 188 |
return 0; |
| 189 |
} |
| 190 |
|
| 191 |
bool |
| 192 |
handle_menu(long menu_id) |
| 193 |
{ |
| 194 |
struct browser *browser; |
| 195 |
struct focusable *focused; |
| 196 |
short menu, item, n; |
| 197 |
|
| 198 |
menu = HiWord(menu_id); |
| 199 |
item = LoWord(menu_id); |
| 200 |
|
| 201 |
if ((focused = focusable_focused()) && focused->menu && |
| 202 |
focused->menu(focused, menu, item)) |
| 203 |
goto handled; |
| 204 |
|
| 205 |
browser = (struct browser *)focused->cookie; |
| 206 |
|
| 207 |
switch (menu) { |
| 208 |
case APPLE_MENU_ID: |
| 209 |
switch (item) { |
| 210 |
case APPLE_MENU_ABOUT_ID: |
| 211 |
about(PROGRAM_NAME); |
| 212 |
break; |
| 213 |
default: { |
| 214 |
Str255 da; |
| 215 |
GrafPtr save_port; |
| 216 |
|
| 217 |
GetItem(apple_menu, LoWord(menu_id), &da); |
| 218 |
GetPort(&save_port); |
| 219 |
OpenDeskAcc(da); |
| 220 |
SetPort(save_port); |
| 221 |
break; |
| 222 |
} |
| 223 |
} |
| 224 |
break; |
| 225 |
case FILE_MENU_ID: |
| 226 |
switch (item) { |
| 227 |
case FILE_MENU_QUIT_ID: |
| 228 |
if (focusables_quit()) |
| 229 |
quitting = true; |
| 230 |
break; |
| 231 |
} |
| 232 |
break; |
| 233 |
case BOOKMARKS_MENU_ID: |
| 234 |
switch (item) { |
| 235 |
case BOOKMARKS_MENU_EDIT_ID: |
| 236 |
bookmarks_dialog(); |
| 237 |
break; |
| 238 |
default: |
| 239 |
n = item - BOOKMARKS_MENU_SEP_ID - 1; |
| 240 |
if (settings.bookmark_uris[n][0] != '\0') |
| 241 |
browser_go_uri_str(browser, settings.bookmark_uris[n]); |
| 242 |
break; |
| 243 |
} |
| 244 |
break; |
| 245 |
} |
| 246 |
|
| 247 |
handled: |
| 248 |
HiliteMenu(0); |
| 249 |
return true; |
| 250 |
} |
| 251 |
|
| 252 |
void |
| 253 |
menu_defaults(void) |
| 254 |
{ |
| 255 |
DisableItem(edit_menu, EDIT_MENU_CUT_ID); |
| 256 |
DisableItem(edit_menu, EDIT_MENU_COPY_ID); |
| 257 |
DisableItem(edit_menu, EDIT_MENU_PASTE_ID); |
| 258 |
DisableItem(edit_menu, EDIT_MENU_SELECT_ALL_ID); |
| 259 |
} |
| 260 |
|
| 261 |
void |
| 262 |
menu_load_bookmarks(void) |
| 263 |
{ |
| 264 |
Str255 title; |
| 265 |
short count, n; |
| 266 |
|
| 267 |
count = CountMItems(bookmarks_menu); |
| 268 |
while (count >= BOOKMARKS_MENU_SEP_ID) { |
| 269 |
DelMenuItem(bookmarks_menu, count); |
| 270 |
count--; |
| 271 |
} |
| 272 |
|
| 273 |
for (n = 0; n < BOOKMARKS_COUNT; n++) { |
| 274 |
if (settings.bookmark_uris[n][0] == '\0') |
| 275 |
break; |
| 276 |
|
| 277 |
if (count < BOOKMARKS_MENU_SEP_ID) { |
| 278 |
AppendMenu(bookmarks_menu, "\p(-"); |
| 279 |
count++; |
| 280 |
} |
| 281 |
|
| 282 |
if (settings.bookmark_names[n][0] == '\0') |
| 283 |
strlcpy((char *)&title, (char *)&settings.bookmark_uris[n], |
| 284 |
sizeof(title)); |
| 285 |
else |
| 286 |
strlcpy((char *)&title, (char *)&settings.bookmark_names[n], |
| 287 |
sizeof(title)); |
| 288 |
|
| 289 |
CtoPstr(title); |
| 290 |
AppendMenu(bookmarks_menu, "\p."); |
| 291 |
count++; |
| 292 |
SetItem(bookmarks_menu, count, title); |
| 293 |
} |
| 294 |
} |
| 295 |
|
| 296 |
void |
| 297 |
handle_exit(void) |
| 298 |
{ |
| 299 |
focusables_quit(); |
| 300 |
} |