terminate on ctrl-c

This commit is contained in:
Kied Llaentenn 2019-12-12 23:03:58 -05:00
parent 6d2b6ffcca
commit 411e8db8a7
3 changed files with 32 additions and 5 deletions

9
draw.c
View File

@ -67,3 +67,12 @@ dofire ( struct buffer *buf )
}
}
}
// free framebuffer and shutdown termbox
void
cleanup ( struct buffer *buf )
{
free(buf->buf);
tb_shutdown();
}

1
draw.h
View File

@ -12,5 +12,6 @@ typedef struct buffer
void init ( struct buffer *buf );
void dofire ( struct buffer *buf );
void cleanup ( struct buffer *buf );
#endif

27
main.c
View File

@ -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;
}