AmendHub

jcs

/

wallops

/

amendments

/

28

focusable: Let each dictate the minimum sleep time for WaitNextEvent

When a window is hiding in the background, we can sleep up to a whole
second

jcs made amendment 28 11 months ago
--- chatter.c Thu Feb 10 10:27:03 2022 +++ chatter.c Thu Feb 10 13:09:59 2022 @@ -27,6 +27,7 @@ static Handle scrp_rec_h = NULL; void chatter_layout(struct chatter *chatter, bool init, Rect *init_bounds); +short chatter_wait_type(struct focusable *focusable); void chatter_key_down(struct focusable *focusable, EventRecord *event); void chatter_mouse_down(struct focusable *focusable, EventRecord *event); bool chatter_menu(struct focusable *focusable, short menu, short item); @@ -87,6 +88,7 @@ chatter_init(const char *server, const unsigned short focusable = xmalloczero(sizeof(struct focusable)); focusable->win = chatter->win; focusable->cookie = chatter; + focusable->wait_type = chatter_wait_type; focusable->idle = chatter_idle; focusable->key_down = chatter_key_down; focusable->mouse_down = chatter_mouse_down; @@ -98,6 +100,19 @@ chatter_init(const char *server, const unsigned short focusable->resume = chatter_resume; add_focusable(focusable); chatter->focusable = focusable; +} + +short +chatter_wait_type(struct focusable *focusable) +{ + struct chatter *chatter = (struct chatter *)(focusable->cookie); + + if (chatter->irc_ibuflen) + return WAIT_TYPE_URGENT; + else if (!focusable->visible) + return WAIT_TYPE_BACKGROUND; + + return WAIT_TYPE_FOREGROUND; } void --- chatter.h Wed Feb 9 12:45:11 2022 +++ chatter.h Thu Feb 10 13:09:41 2022 @@ -67,10 +67,16 @@ #define DEFAULT_REALNAME "A Macintosh User" #define DEFAULT_CHANNEL "#cyberpals" +#define WAIT_TYPE_NONE (1 << 0) +#define WAIT_TYPE_BACKGROUND (1 << 1) +#define WAIT_TYPE_FOREGROUND (1 << 2) +#define WAIT_TYPE_URGENT (1 << 3) + struct focusable { WindowPtr win; bool visible; void *cookie; + short (*wait_type)(struct focusable *focusable); void (*idle)(struct focusable *focusable, EventRecord *event); void (*update)(struct focusable *focusable, EventRecord *event); void (*key_down)(struct focusable *focusable, EventRecord *event); @@ -109,7 +115,6 @@ struct chatter { char irc_obuf[512]; char irc_ibuf[512]; /* RFC2812 says max line will be 512 */ char irc_line[512]; - short irc_obuflen; short irc_ibuflen; short irc_linelen; /* docs say 4*MTU+1024, but MTU will probably be <1500 */ --- irc.c Thu Feb 10 11:33:08 2022 +++ irc.c Thu Feb 10 13:09:47 2022 @@ -142,6 +142,8 @@ irc_abort(struct chatter *chatter) false); _TCPRelease(&chatter->irc_close_pb, chatter->irc_stream, nil, nil, false); + + chatter->irc_ibuflen = 0; } void @@ -223,7 +225,6 @@ irc_send(struct chatter *chatter, char *line, size_t s } memcpy(&chatter->irc_obuf, line, size); - chatter->irc_obuflen = size; /* * _TCPSend only knows how many wds pointers were passed in when it --- main.c Tue Feb 8 22:05:03 2022 +++ main.c Thu Feb 10 12:58:50 2022 @@ -73,6 +73,8 @@ main(void) struct focusable *found_focusable; short event_in, n; char key; + long wait_ticks; + short wait_type; SetApplLimit(GetApplLimit() - (1024 * 8)); @@ -102,7 +104,22 @@ main(void) show_connect_dialog(); for (;;) { - WaitNextEvent(everyEvent, &event, 5L, 0L); + wait_type = WAIT_TYPE_BACKGROUND; + for (n = 0; n < nfocusables; n++) { + if (focusables[n]->wait_type) + wait_type |= focusables[n]->wait_type(focusables[n]); + else if (focusables[n]->visible) + wait_type |= WAIT_TYPE_FOREGROUND; + } + + if (wait_type & WAIT_TYPE_URGENT) + wait_ticks = 0; + else if (wait_type & WAIT_TYPE_FOREGROUND) + wait_ticks = 5L; + else + wait_ticks = 60L; + + WaitNextEvent(everyEvent, &event, wait_ticks, 0L); if (event.what != nullEvent) { event_in = FindWindow(event.where, &event_win);