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 init ( struct buffer *buf );
void dofire ( struct buffer *buf ); void dofire ( struct buffer *buf );
void cleanup ( struct buffer *buf );
#endif #endif

27
main.c
View File

@ -38,6 +38,7 @@ main ( int argc, char *argv[] )
tb_select_output_mode(TB_OUTPUT_NORMAL); tb_select_output_mode(TB_OUTPUT_NORMAL);
tb_clear(); tb_clear();
struct buffer buf; struct buffer buf;
struct tb_event e;
// initialize drawing // initialize drawing
init(&buf); init(&buf);
@ -53,13 +54,29 @@ main ( int argc, char *argv[] )
// draw framebuffer to terminal // draw framebuffer to terminal
tb_present(); 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 // perform cleanup
tb_shutdown(); cleanup(&buf);
// cleanup framebuffer
free(buf.buf);
return 0; return 0;
} }