Skip to content

Commit 99bc367

Browse files
committed
Fix application exit
1 parent ed2a3b8 commit 99bc367

File tree

5 files changed

+40
-43
lines changed

5 files changed

+40
-43
lines changed

call.c

+6-5
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,11 @@ static void commandVersion() {
1414

1515
// exit
1616

17-
static void commandExit() {
18-
pipe_unwatch();
19-
commandVersion();
20-
g_application_quit(G_APPLICATION(app));
17+
static void commandExit(pipe_buffer *target) {
18+
io_stop(target);
19+
if (io_exited()) {
20+
g_application_quit(G_APPLICATION(app));
21+
}
2122
}
2223

2324
// clear command
@@ -410,7 +411,7 @@ void callcommand(char command, pipe_buffer *target) {
410411
commandTitle(target);
411412
break;
412413
case 'X':
413-
commandExit();
414+
commandExit(target);
414415
break;
415416
case 'V':
416417
commandVersion();

io.c

+27-10
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,14 @@ typedef struct pipe_buffer {
1313
int32_t alloc_length;
1414
char *data;
1515
void (*last_func)(void *);
16+
guint in_id;
17+
guint hup_id;
1618
} pipe_buffer;
1719

1820
static pipe_buffer sync_chan, stream_chan;
1921

2022
static void reset_buffer(pipe_buffer *target);
2123

22-
pipe_buffer *io_input() {
23-
reset_buffer(&sync_chan);
24-
return &sync_chan;
25-
}
26-
27-
pipe_buffer *io_stream() {
28-
reset_buffer(&stream_chan);
29-
return &stream_chan;
30-
}
31-
3224
static void call_func(pipe_buffer *target) {
3325
reset_buffer(target);
3426
target->call_func();
@@ -104,3 +96,28 @@ gboolean async_read_chan(GIOChannel *source, GIOCondition condition, gpointer da
10496
}
10597
return TRUE;
10698
}
99+
100+
gboolean chan_error_func(GIOChannel *source, GIOCondition condition, gpointer data) {
101+
perror("connection has been broken");
102+
exit(EXIT_FAILURE);
103+
return TRUE;
104+
}
105+
106+
static void io_start(FILE *source, pipe_buffer *target) {
107+
reset_buffer(target);
108+
GIOChannel *chan = g_io_channel_unix_new(fileno(source));
109+
target->in_id = g_io_add_watch(chan, G_IO_IN, async_read_chan, target);
110+
target->hup_id = g_io_add_watch(chan, G_IO_HUP, chan_error_func, target);
111+
g_io_channel_unref(chan);
112+
}
113+
114+
void io_input_start(FILE *source) { io_start(source, &sync_chan); }
115+
void io_stream_start(FILE *source) { io_start(source, &stream_chan); }
116+
117+
void io_stop(pipe_buffer *target) {
118+
g_source_remove(target->hup_id);
119+
g_source_remove(target->in_id);
120+
target->in_id = 0;
121+
}
122+
123+
gboolean io_exited() { return sync_chan.in_id == 0 && stream_chan.in_id == 0; }

pipe.c

+2-24
Original file line numberDiff line numberDiff line change
@@ -28,35 +28,13 @@ static FILE *pipe_open(char *name, char *suffix, char *filemode) {
2828
return handle;
2929
}
3030

31-
gboolean pipe_error_func(GIOChannel *source, GIOCondition condition, gpointer data) {
32-
perror("connection has been broken");
33-
exit(EXIT_FAILURE);
34-
return TRUE;
35-
}
36-
37-
static guint chan_input_in_id;
38-
static guint chan_input_hup_id;
39-
static guint chan_stream_in_id;
40-
static guint chan_stream_hup_id;
41-
4231
void pipe_init(char *pipe_suffix, GIOFunc func) {
4332
handle_output = pipe_open(FIFO_OUTPUT_PATH, pipe_suffix, "w");
4433
handle_event = pipe_open(FIFO_EVENT_PATH, pipe_suffix, "w");
4534
handle_input = pipe_open(FIFO_INPUT_PATH, pipe_suffix, "r");
4635
handle_stream = pipe_open(FIFO_STREAM_PATH, pipe_suffix, "r");
47-
GIOChannel *chan_input = g_io_channel_unix_new(fileno(handle_input));
48-
chan_input_in_id = g_io_add_watch(chan_input, G_IO_IN, func, io_input());
49-
chan_input_hup_id = g_io_add_watch(chan_input, G_IO_HUP, pipe_error_func, NULL);
50-
GIOChannel *chan_stream = g_io_channel_unix_new(fileno(handle_stream));
51-
chan_stream_in_id = g_io_add_watch(chan_stream, G_IO_IN, func, io_stream());
52-
chan_stream_hup_id = g_io_add_watch(chan_stream, G_IO_HUP, pipe_error_func, NULL);
53-
}
54-
55-
void pipe_unwatch() {
56-
g_source_remove(chan_input_in_id);
57-
g_source_remove(chan_input_hup_id);
58-
g_source_remove(chan_stream_in_id);
59-
g_source_remove(chan_stream_hup_id);
36+
io_input_start(handle_input);
37+
io_stream_start(handle_stream);
6038
}
6139

6240
void pipe_done() {

terminal.h

+4-3
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ extern GMenu *barmenu;
1717

1818
void pipe_init(char *pipe_suffix, GIOFunc func);
1919
void pipe_done();
20-
void pipe_unwatch();
2120
void pipe_output_write(const void *data, const int length);
2221
void pipe_output_write_string(const char *data);
2322
void pipe_output_flush();
@@ -28,11 +27,13 @@ void pipe_event_flush();
2827
// io
2928

3029
typedef struct pipe_buffer pipe_buffer;
31-
pipe_buffer *io_input();
32-
pipe_buffer *io_stream();
3330
void parameters_to_call(pipe_buffer *target, void *buffer, int size, void (*f)());
3431
void parameters_alloc_to_call(pipe_buffer *target, void *buffer, int size, void (*f)(void *));
3532
gboolean async_read_chan(GIOChannel *source, GIOCondition condition, gpointer data);
33+
void io_input_start(FILE *source);
34+
void io_stream_start(FILE *source);
35+
void io_stop(pipe_buffer *target);
36+
gboolean io_exited();
3637

3738
// event
3839

version.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
char *it_version = "0.4.1";
1+
char *it_version = "0.4.2";

0 commit comments

Comments
 (0)