Skip to content

Commit

Permalink
Pass ctrl-c and other signals through to emulator
Browse files Browse the repository at this point in the history
  • Loading branch information
Spritetm committed Jul 2, 2024
1 parent cb464ce commit f21ae39
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .github/workflows/pages.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ jobs:
run: cd Musashi; make
- name: Compile for web
working-directory: ${{ github.workspace }}/
run: source ./emsdk/emsdk_env.sh; ls -l; make plexem.mjs
run: source ./emsdk/emsdk_env.sh; make plexem.mjs
- name: Install web deps and collect artifacts
working-directory: ${{ github.workspace }}/
run: make webdeploy
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ plexem.worker.mjs
cow/
node_modules/
package-lock.json
web/
39 changes: 39 additions & 0 deletions uart.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Copyright (c) 2024 Sprite_tm <[email protected]>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <signal.h>
#include "uart.h"
#include "emu.h"
#include "log.h"
Expand All @@ -28,6 +29,7 @@ Copyright (c) 2024 Sprite_tm <[email protected]>
log_printf(LOG_SRC_UART, msg_level, format_and_args)
#define UART_LOG_DEBUG(format_and_args...) UART_LOG(LOG_DEBUG, format_and_args)
#define UART_LOG_INFO(format_and_args...) UART_LOG(LOG_INFO, format_and_args)
#define UART_LOG_WARNING(format_and_args...) UART_LOG(LOG_WARNING, format_and_args)

// Console input handling
//
Expand All @@ -39,6 +41,15 @@ static void uart_disable_console_raw_mode() {
UART_LOG_INFO("Leaving tty raw mode\n");
}


int char_from_signal=-1;

static void uart_sig_hdl(int sig) {
if (sig==SIGINT) char_from_signal=0x03;
if (sig==SIGQUIT) char_from_signal=0x1C;
if (sig==SIGSTOP) char_from_signal=0x1a;
}

static void uart_set_console_raw_mode() {
tcgetattr(STDIN_FILENO, &orig_termios);
atexit(uart_disable_console_raw_mode);
Expand All @@ -48,8 +59,18 @@ static void uart_set_console_raw_mode() {
raw.c_lflag &= ~(ECHO | ICANON);
int result = tcsetattr(STDIN_FILENO, TCSAFLUSH, &raw);
UART_LOG_INFO("Entering tty raw mode: %d (%d)\n", result, errno);
//Exiting an emulator doesn't make sense in the web version
#ifndef __EMSCRIPTEN__
UART_LOG_WARNING("TO EXIT EMULATOR: Press Ctrl-C three times.\n");
#endif

signal(SIGINT, uart_sig_hdl);
signal(SIGQUIT, uart_sig_hdl);
signal(SIGTSTP, uart_sig_hdl);
}

static int ctrl_c_pressed_times=0;

static int uart_poll_for_console_character() {
char c;
fd_set input;
Expand All @@ -59,6 +80,23 @@ static int uart_poll_for_console_character() {
};
int result;

//If user pressed ctl-c or any other character that generates a
//signal instead, handle that.
if (char_from_signal!=-1) {
int r=char_from_signal;
char_from_signal=-1;
if (r==0x03) { //ctrl-c
#ifndef __EMSCRIPTEN__
ctrl_c_pressed_times++;
#endif
if (ctrl_c_pressed_times==3) {
UART_LOG_WARNING("Ctrl-C pressed three times. Bye!\n");
exit(0);
}
}
return r;
}

FD_ZERO(&input);
FD_SET(STDIN_FILENO, &input);

Expand All @@ -68,6 +106,7 @@ static int uart_poll_for_console_character() {
// read single character
result = read(STDIN_FILENO, &c, 1);
if (result == 1) {
ctrl_c_pressed_times=0; //reset ctrl-c counter
return c;
}
}
Expand Down

0 comments on commit f21ae39

Please sign in to comment.