aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJesse Luehrs <doy@tozt.net>2016-05-10 16:03:37 -0400
committerJesse Luehrs <doy@tozt.net>2016-05-10 16:03:37 -0400
commit9118ead5ae15548c8a5b96340f9a0a24b3104e32 (patch)
tree5931f2b6e538cb369e97e2ccfa089615a7415e0a
parent082d730944950c202f24d3701878c06cf5e44f5f (diff)
downloadrunes-9118ead5ae15548c8a5b96340f9a0a24b3104e32.tar.gz
runes-9118ead5ae15548c8a5b96340f9a0a24b3104e32.zip
make terms handle cleaning themselves up
-rw-r--r--src/pty-unix.c5
-rw-r--r--src/runes.c8
-rw-r--r--src/term.c20
-rw-r--r--src/term.h4
-rw-r--r--src/window-xlib.c5
5 files changed, 33 insertions, 9 deletions
diff --git a/src/pty-unix.c b/src/pty-unix.c
index 7975964..27419ce 100644
--- a/src/pty-unix.c
+++ b/src/pty-unix.c
@@ -94,6 +94,8 @@ void runes_pty_backend_spawn_subprocess(RunesTerm *t)
fprintf(old_stderr, "Couldn't run %s: %s\n", cmd, strerror(errno));
exit(1);
}
+
+ runes_term_refcnt_inc(t);
}
void runes_pty_backend_init_loop(RunesTerm *t, RunesLoop *loop)
@@ -155,7 +157,10 @@ static int runes_pty_backend_got_data(void *t)
return 1;
}
else {
+ runes_pty_backend_cleanup(pty);
+ free(pty);
runes_window_backend_request_close(t);
+ runes_term_refcnt_dec(t);
return 0;
}
diff --git a/src/runes.c b/src/runes.c
index 1078ee9..af32778 100644
--- a/src/runes.c
+++ b/src/runes.c
@@ -1,4 +1,5 @@
#include <locale.h>
+#include <stdlib.h>
#include "runes.h"
@@ -8,17 +9,18 @@
int main (int argc, char *argv[])
{
RunesLoop loop;
- RunesTerm t;
+ RunesTerm *t;
setlocale(LC_ALL, "");
runes_loop_init(&loop);
- runes_term_init(&t, &loop, argc, argv);
+
+ t = calloc(1, sizeof(RunesTerm));
+ runes_term_init(t, &loop, argc, argv);
runes_loop_run(&loop);
#ifdef RUNES_VALGRIND
- runes_term_cleanup(&t);
runes_loop_cleanup(&loop);
#endif
diff --git a/src/term.c b/src/term.c
index 0584653..fe79cc5 100644
--- a/src/term.c
+++ b/src/term.c
@@ -42,6 +42,20 @@ void runes_term_init(RunesTerm *t, RunesLoop *loop, int argc, char *argv[])
runes_term_init_loop(t, loop);
}
+void runes_term_refcnt_inc(RunesTerm *t)
+{
+ t->refcnt++;
+}
+
+void runes_term_refcnt_dec(RunesTerm *t)
+{
+ t->refcnt--;
+ if (t->refcnt <= 0) {
+ runes_term_cleanup(t);
+ free(t);
+ }
+}
+
void runes_term_set_window_size(RunesTerm *t, int xpixel, int ypixel)
{
int row = ypixel / t->display->fonty, col = xpixel / t->display->fontx;
@@ -55,12 +69,6 @@ void runes_term_cleanup(RunesTerm *t)
vt100_screen_cleanup(t->scr);
free(t->scr);
- runes_pty_backend_cleanup(t->pty);
- free(t->pty);
-
- runes_window_backend_cleanup(t->w);
- free(t->w);
-
runes_display_cleanup(t->display);
free(t->display);
diff --git a/src/term.h b/src/term.h
index 7da9ac1..51fc327 100644
--- a/src/term.h
+++ b/src/term.h
@@ -11,10 +11,14 @@ struct runes_term {
RunesPtyBackend *pty;
VT100Screen *scr;
RunesLoop *loop;
+
+ int refcnt;
};
void runes_term_init(RunesTerm *t, RunesLoop *loop, int argc, char *argv[]);
void runes_term_set_window_size(RunesTerm *t, int xpixel, int ypixel);
+void runes_term_refcnt_inc(RunesTerm *t);
+void runes_term_refcnt_dec(RunesTerm *t);
void runes_term_cleanup(RunesTerm *t);
#endif
diff --git a/src/window-xlib.c b/src/window-xlib.c
index c41bedf..ecfc6cb 100644
--- a/src/window-xlib.c
+++ b/src/window-xlib.c
@@ -237,6 +237,8 @@ void runes_window_backend_create_window(RunesTerm *t, int argc, char *argv[])
XMapWindow(w->dpy, w->w);
XMapWindow(w->dpy, w->border_w);
+
+ runes_term_refcnt_inc(t);
}
void runes_window_backend_init_loop(RunesTerm *t, RunesLoop *loop)
@@ -437,7 +439,10 @@ static int runes_window_backend_process_event(void *t)
}
if (should_close) {
+ runes_window_backend_cleanup(w);
+ free(w);
runes_pty_backend_request_close(t);
+ runes_term_refcnt_dec(t);
}
return !should_close;