client/main.c

changeset 942
488178e3e328
child 944
cc23aad6335e
equal deleted inserted replaced
941:e7459e9fbed2 942:488178e3e328
1 /*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3 *
4 * Copyright 2025 Olaf Wintermann. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <unistd.h>
33
34 #include <ui/ui.h>
35 #include <cx/printf.h>
36 #include <pthread.h>
37
38 #include "main.h"
39 #include "uiclient.h"
40
41 /*
42 * debug window that is always created
43 */
44 static UiObject *debug_window;
45
46 /*
47 * test window, that is only created in testing mode
48 */
49 static UiObject *test_window;
50
51 static int test_mode = 0;
52
53 static int input_fd[2];
54 static int output_fd[2];
55
56 int main(int argc, char **argv) {
57 test_mode = 1;
58
59 ui_init(NULL, argc, argv);
60 ui_onstartup(application_onstartup, NULL);
61 ui_onopen(application_onopen, NULL);
62 ui_onexit(application_onexit, NULL);
63
64 int in = STDIN_FILENO;
65 int out = STDOUT_FILENO;
66 if(test_mode) {
67 if(pipe(input_fd)) {
68 perror("pipe");
69 return 1;
70 }
71 if(pipe(output_fd)) {
72 perror("pipe");
73 return 1;
74 }
75
76 in = input_fd[0];
77 out = output_fd[1];
78
79 pthread_t tid;
80 if(pthread_create(&tid, NULL, testwindow_read_thread, NULL)) {
81 perror("pthread_create");
82 return 1;
83 }
84 if(pthread_detach(tid)) {
85 perror("pthread_detach");
86 return 1;
87 }
88 }
89 MessageHandler *h = simple_msg_handler(in, out, client_msg_received);
90 client_init(h);
91 h->start(h);
92
93 ui_main();
94
95 h->stop(h);
96 fprintf(stderr, "client: end");
97
98 return 0;
99 }
100
101 void application_onstartup(UiEvent *event, void *userdata) {
102 // We need at least one window for the event loop to work.
103 // Create a debug window, that is invisible by default
104 debug_window = ui_simple_window("debug", NULL);
105 // TODO: debug UI
106
107 if(test_mode) {
108 testwindow_create();
109 }
110 }
111
112 void application_onopen(UiEvent *event, void *userdata) {
113
114 }
115
116 void application_onexit(UiEvent *event, void *userdata) {
117
118 }
119
120 static void testwindow_close(UiEvent *event, void *userdata) {
121 ui_close(debug_window);
122 ui_app_quit();
123 exit(0);
124 }
125
126 static void testwindow_send(UiEvent *event, void *userdata) {
127 TestWindow *window = event->window;
128
129 char *str = ui_get(window->input);
130 int len = strlen(str);
131 cxmutstr msg = cx_asprintf("%d\n%s", len, str);
132 write(input_fd[1], msg.ptr, msg.length);
133 free(msg.ptr);
134
135 ui_set(window->input, "");
136 }
137
138 static void testwindow_clear_output(UiEvent *event, void *userdata) {
139 TestWindow *window = event->window;
140 ui_set(window->output, "");
141 }
142
143 void testwindow_create(void) {
144 UiObject *obj = ui_simple_window("Test", NULL);
145 ui_context_closefunc(obj->ctx, testwindow_close, NULL);
146 ui_window_size(obj, 1800, 1400);
147
148 TestWindow *window = ui_malloc(obj->ctx, sizeof(TestWindow));
149 window->input = ui_text_new(obj->ctx, NULL);
150 window->output = ui_text_new(obj->ctx, NULL);
151 obj->window = window;
152
153 ui_hsplitpane(obj, .fill = TRUE, .initial_position = 900) {
154 // left
155 ui_vbox(obj, .fill = TRUE) {
156 ui_grid(obj, .margin = 10, .columnspacing = 10, .rowspacing = 10, .fill = TRUE) {
157 ui_llabel(obj, .label = "Input", .style = UI_LABEL_STYLE_TITLE, .hexpand = TRUE, .hfill = TRUE);
158 ui_newline(obj);
159 ui_textarea(obj, .value = window->input, .fill = TRUE);
160 ui_newline(obj);
161 ui_button(obj, .label = "Send", .onclick = testwindow_send);
162 }
163 }
164
165
166 // right
167 ui_vbox(obj, .fill = TRUE) {
168 ui_grid(obj, .margin = 10, .columnspacing = 10, .rowspacing = 10, .fill = TRUE) {
169 ui_llabel(obj, .label = "Output", .style = UI_LABEL_STYLE_TITLE, .hexpand = TRUE, .hfill = TRUE);
170 ui_newline(obj);
171 ui_textarea(obj, .value = window->output, .fill = TRUE);
172 ui_newline(obj);
173 ui_button(obj, .label = "Clear", .onclick = testwindow_clear_output);
174 }
175 }
176 }
177
178 ui_show(obj);
179
180 test_window = obj;
181 }
182
183 static int append_log(void *data) {
184 char *msg = data;
185 TestWindow *window = test_window->window;
186 UiText *out = window->output;
187 int length = out->length(out);
188 out->insert(out, length, msg);
189 free(msg);
190 return 0;
191 }
192
193 void* testwindow_read_thread(void *data) {
194 char buf[4096];
195 ssize_t r;
196 while((r = read(output_fd[0], buf, 4096)) > 0) {
197 char *str = malloc(r+1);
198 memcpy(str, buf, r);
199 str[r] = 0;
200 ui_call_mainthread(append_log, str);
201 }
202
203 return NULL;
204 }

mercurial