From 411e8db8a73e75375841c44cd6911def97d7c97a Mon Sep 17 00:00:00 2001 From: Kied Llaentenn <32681240+Kiedtl@users.noreply.github.com> Date: Thu, 12 Dec 2019 23:03:58 -0500 Subject: [PATCH] terminate on ctrl-c --- draw.c | 9 +++++++++ draw.h | 1 + main.c | 27 ++++++++++++++++++++++----- 3 files changed, 32 insertions(+), 5 deletions(-) diff --git a/draw.c b/draw.c index 352c03b..6163ea9 100644 --- a/draw.c +++ b/draw.c @@ -67,3 +67,12 @@ dofire ( struct buffer *buf ) } } } + + +// free framebuffer and shutdown termbox +void +cleanup ( struct buffer *buf ) +{ + free(buf->buf); + tb_shutdown(); +} diff --git a/draw.h b/draw.h index 968b8d7..7b3c51a 100644 --- a/draw.h +++ b/draw.h @@ -12,5 +12,6 @@ typedef struct buffer void init ( struct buffer *buf ); void dofire ( struct buffer *buf ); +void cleanup ( struct buffer *buf ); #endif diff --git a/main.c b/main.c index f61bf99..ff7c4a8 100644 --- a/main.c +++ b/main.c @@ -38,6 +38,7 @@ main ( int argc, char *argv[] ) tb_select_output_mode(TB_OUTPUT_NORMAL); tb_clear(); struct buffer buf; + struct tb_event e; // initialize drawing init(&buf); @@ -53,13 +54,29 @@ main ( int argc, char *argv[] ) // draw framebuffer to terminal tb_present(); + + // event handling + usize err = (usize) tb_peek_event(&e, 5); + + if (err != 0) + continue; + + if (e.type == TB_EVENT_KEY) + { + switch (e.key) + { + case 0x03: + fprintf(stdout, "exiting\n"); + cleanup(&buf); + exit(0); + default: + break; + } + } } - // cleanup termbox - tb_shutdown(); - - // cleanup framebuffer - free(buf.buf); + // perform cleanup + cleanup(&buf); return 0; }