1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29 #include "main.h"
30
31 #ifndef _WIN32
32
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <unistd.h>
37
38 #include <ui/ui.h>
39 #include <cx/printf.h>
40 #include <pthread.h>
41
42 #include "main.h"
43 #include "uiclient.h"
44
45
46
47
48 static UiObject *debug_window;
49
50
51
52
53 static UiObject *test_window;
54
55 static int test_mode =
0;
56
57 static int input_fd[
2];
58 static int output_fd[
2];
59
60 int main(
int argc,
char **argv) {
61 test_mode =
1;
62
63 ui_init(
NULL, argc, argv);
64 ui_onstartup(application_onstartup,
NULL);
65 ui_onopen(application_onopen,
NULL);
66 ui_onexit(application_onexit,
NULL);
67
68 int in =
STDIN_FILENO;
69 int out =
STDOUT_FILENO;
70 if(test_mode) {
71 if(pipe(input_fd)) {
72 perror(
"pipe");
73 return 1;
74 }
75 if(pipe(output_fd)) {
76 perror(
"pipe");
77 return 1;
78 }
79
80 in = input_fd[
0];
81 out = output_fd[
1];
82
83 pthread_t tid;
84 if(pthread_create(&tid,
NULL, testwindow_read_thread,
NULL)) {
85 perror(
"pthread_create");
86 return 1;
87 }
88 if(pthread_detach(tid)) {
89 perror(
"pthread_detach");
90 return 1;
91 }
92 }
93 UiMessageHandler *h = uic_simple_msg_handler(in, out, client_msg_received);
94 client_init(h);
95 h->start(h);
96
97 ui_main();
98
99 h->stop(h);
100 fprintf(stderr,
"client: end");
101
102 return 0;
103 }
104
105 void application_onstartup(UiEvent *event,
void *userdata) {
106
107
108 debug_window = ui_simple_window(
"debug",
NULL);
109
110
111 if(test_mode) {
112 testwindow_create();
113 }
114 }
115
116 void application_onopen(UiEvent *event,
void *userdata) {
117
118 }
119
120 void application_onexit(UiEvent *event,
void *userdata) {
121
122 }
123
124 static void testwindow_close(UiEvent *event,
void *userdata) {
125 ui_close(debug_window);
126 ui_app_quit();
127 exit(
0);
128 }
129
130 static void testwindow_send(UiEvent *event,
void *userdata) {
131 TestWindow *window = event->window;
132
133 char *str = ui_get(window->input);
134 int len = strlen(str);
135 cxmutstr msg = cx_asprintf(
"%d\n%s", len, str);
136 write(input_fd[
1], msg.ptr, msg.length);
137 free(msg.ptr);
138
139 ui_set(window->input,
"");
140 }
141
142 static void testwindow_clear_output(UiEvent *event,
void *userdata) {
143 TestWindow *window = event->window;
144 ui_set(window->output,
"");
145 }
146
147 void testwindow_create(
void) {
148 UiObject *obj = ui_simple_window(
"Test",
NULL);
149 ui_context_closefunc(obj->ctx, testwindow_close,
NULL);
150 ui_window_size(obj,
1800,
1400);
151
152 TestWindow *window = ui_malloc(obj->ctx,
sizeof(TestWindow));
153 window->input = ui_text_new(obj->ctx,
NULL);
154 window->output = ui_text_new(obj->ctx,
NULL);
155 obj->window = window;
156
157 ui_hsplitpane(obj, .fill =
TRUE, .initial_position =
900) {
158
159 ui_vbox(obj, .fill =
TRUE) {
160 ui_grid(obj, .margin =
10, .columnspacing =
10, .rowspacing =
10, .fill =
TRUE) {
161 ui_llabel(obj, .label =
"Input", .style =
UI_LABEL_STYLE_TITLE, .hexpand =
TRUE, .hfill =
TRUE);
162 ui_newline(obj);
163 ui_textarea(obj, .value = window->input, .fill =
TRUE);
164 ui_newline(obj);
165 ui_button(obj, .label =
"Send", .onclick = testwindow_send);
166 }
167 }
168
169
170
171 ui_vbox(obj, .fill =
TRUE) {
172 ui_grid(obj, .margin =
10, .columnspacing =
10, .rowspacing =
10, .fill =
TRUE) {
173 ui_llabel(obj, .label =
"Output", .style =
UI_LABEL_STYLE_TITLE, .hexpand =
TRUE, .hfill =
TRUE);
174 ui_newline(obj);
175 ui_textarea(obj, .value = window->output, .fill =
TRUE);
176 ui_newline(obj);
177 ui_button(obj, .label =
"Clear", .onclick = testwindow_clear_output);
178 }
179 }
180 }
181
182 ui_show(obj);
183
184 test_window = obj;
185 }
186
187 static int append_log(
void *data) {
188 char *msg = data;
189 TestWindow *window = test_window->window;
190 UiText *out = window->output;
191 int length = out->length(out);
192 out->insert(out, length, msg);
193 free(msg);
194 return 0;
195 }
196
197 void* testwindow_read_thread(
void *data) {
198 char buf[
4096];
199 ssize_t r;
200 while((r = read(output_fd[
0], buf,
4096)) >
0) {
201 char *str = malloc(r
+1);
202 memcpy(str, buf, r);
203 str[r] =
0;
204 ui_call_mainthread(append_log, str);
205 }
206
207 return NULL;
208 }
209
210 #else
211
212
213 int WinMain(
HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nShowCmd) {
214 return 0;
215 }
216
217 #endif
218