Skip to content

Commit 9fed2f2

Browse files
committed
Drain keyboard buffer after running program
Sometimes when a program exits stray keystrokes are left in the keyboard buffer, and the menu then responds to them (e.g. by launching the first menu item). For example, one of the games crashes (due to unrelated reasons) and then the first game on the menu automatically launches, and Blake gets upset. This won't stop the crash but will hopefully stop the first game from launching when it does.
1 parent acd8e6d commit 9fed2f2

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

dosmenu.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,25 @@ int countSections(struct section *sections) {
295295
return i;
296296
}
297297

298+
// Check if a key is pending.
299+
// Key is pending if head and tail pointers of keyboard buffer in
300+
// BIOS data area (BDA) are different.
301+
int keyPending() {
302+
int head, tail;
303+
disable();
304+
head = peek(0x40,0x1A); // 40:1A = keybuf head pointer
305+
tail = peek(0x40,0x1C); // 40:1C = keybuf tail pointer
306+
enable();
307+
return head != tail;
308+
}
309+
310+
// Ignore all pending keys
311+
// This helps consume any stray keys after exiting
312+
void drainInput() {
313+
while (keyPending())
314+
readScanCode();
315+
}
316+
298317
int main(int argc, char**argv) {
299318
struct section *sections;
300319
char *title, *saveCWD, *borderColorName;
@@ -366,6 +385,7 @@ int main(int argc, char**argv) {
366385
chdir(saveCWD);
367386
resetVideo();
368387
_setcursortype(_NOCURSOR);
388+
drainInput();
369389
}
370390
}
371391
}

0 commit comments

Comments
 (0)