ui/server/toolkit.c

changeset 982
9102a53c5385
parent 981
1d47e71f26b6
child 985
93f07ccfd997
equal deleted inserted replaced
981:1d47e71f26b6 982:9102a53c5385
35 35
36 #include "../common/message.h" 36 #include "../common/message.h"
37 #include "../common/threadpool.h" 37 #include "../common/threadpool.h"
38 #include "../common/app.h" 38 #include "../common/app.h"
39 39
40 #include <cx/hash_map.h>
41 #include <cx/printf.h>
42
40 static const char *ui_app_name; 43 static const char *ui_app_name;
41 44
42 static UiMessageHandler *message_handler; 45 static UiMessageHandler *message_handler;
43 46
44 static ui_callback onstartup; 47 static ui_callback onstartup;
45 static void *onstartupdata; 48 static void *onstartupdata;
46 49
47 static UiQueue *event_queue; 50 static UiQueue *event_queue;
48 51
52 static CxMap *srv_obj_map;
53 static uint64_t srv_obj_id_counter = 0;
54
49 void ui_init(const char *appname, int argc, char **argv) { 55 void ui_init(const char *appname, int argc, char **argv) {
50 ui_app_name = appname; 56 ui_app_name = appname;
51 57
52 message_handler = uic_simple_msg_handler(STDIN_FILENO, STDOUT_FILENO, ui_server_message_received); 58 message_handler = uic_simple_msg_handler(STDIN_FILENO, STDOUT_FILENO, ui_server_message_received);
59
60 srv_obj_map = cxHashMapCreateSimple(CX_STORE_POINTERS);
53 } 61 }
54 62
55 const char* ui_appname() { 63 const char* ui_appname() {
56 return ui_app_name; 64 return ui_app_name;
57 } 65 }
64 void ui_server_message_received(cxstring msg) { 72 void ui_server_message_received(cxstring msg) {
65 73
66 } 74 }
67 75
68 void ui_main(void) { 76 void ui_main(void) {
77 uic_simple_msg_handler_start(message_handler);
78 uic_application_startup(NULL);
69 event_queue = ui_queue_create(); 79 event_queue = ui_queue_create();
70 UiServerEvent *event = NULL; 80 UiServerEvent *event = NULL;
71 while((event = ui_queue_get_wait(event_queue)) != NULL) { 81 while((event = ui_queue_get_wait(event_queue)) != NULL) {
72 if(event->callback) { 82 if(event->callback) {
73 event->callback(&event->event, event->userdata); 83 event->callback(&event->event, event->userdata);
94 CallMain *c = malloc(sizeof(CallMain)); 104 CallMain *c = malloc(sizeof(CallMain));
95 c->func = tf; 105 c->func = tf;
96 c->data = td; 106 c->data = td;
97 ui_queue_put(event_queue, c); 107 ui_queue_put(event_queue, c);
98 } 108 }
109
110 void ui_show(UiObject *obj) {
111 if(!obj->widget->sent) {
112 cxmutstr msg = obj->widget->serialize(obj->widget);
113 obj->widget->sent = TRUE;
114 uic_message_send(message_handler, msg);
115 free(msg.ptr);
116 }
117 cxmutstr msg = cx_asprintf("{\"type\":\"show\", \"obj\":\"%s\"}", obj->widget->obj->id.ptr);
118 obj->widget->visible = TRUE;
119 uic_message_send(message_handler, msg);
120 free(msg.ptr);
121 }
122
123 UiSrvObj* ui_create_server_object(UiContext *ctx) {
124 const CxAllocator *a = ctx->allocator;
125 UiSrvObj *obj = cxZalloc(a, sizeof(UiSrvObj));
126
127 char id[32];
128 snprintf(id, 32, "%" PRIu64, srv_obj_id_counter++);
129 obj->id = cx_strdup_a(a, id);
130 obj->ctx = ctx;
131
132 obj->widgets = cxHashMapCreate(a, CX_STORE_POINTERS, 64);
133
134 return obj;
135 }
136
137 void ui_reg_widget(UiWidget *widget) {
138 UiSrvObj *obj = widget->obj;
139
140 char id[32];
141 snprintf(id, 32, "%" PRIu64, obj->widget_id_counter++);
142
143 widget->id = cx_strdup_a(widget->obj->ctx->allocator, cx_str(id));
144 cxMapPut(obj->widgets, id, widget);
145 }
146

mercurial