-
Notifications
You must be signed in to change notification settings - Fork 1
/
gtk_example.c
204 lines (159 loc) · 4.92 KB
/
gtk_example.c
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#include <gtk/gtk.h>
#include <libwebsockets.h>
#include "ws_callbacks.h"
static int status = 0;
static void
print_hello(GtkWidget *widget, gpointer data)
{
g_print("Hello World\n");
}
static void
activate(GtkApplication *app, gpointer user_data)
{
GtkWidget *window;
GtkWidget *button, *bbox;
window = gtk_application_window_new(app);
gtk_window_set_title(GTK_WINDOW(window), "mywindow");
gtk_window_set_default_size(GTK_WINDOW(window), 200, 200);
bbox = gtk_button_box_new(GTK_ORIENTATION_HORIZONTAL);
gtk_container_add(GTK_CONTAINER(window), bbox);
button = gtk_button_new_with_label("Hello World");
g_signal_connect(button, "clicked", G_CALLBACK(print_hello), NULL);
g_signal_connect_swapped(button, "clicked",
G_CALLBACK(gtk_widget_destroy), window);
gtk_container_add(GTK_CONTAINER(bbox), button);
gtk_widget_show_all(window);
}
static int
system_notify_cb(lws_state_manager_t *mgr, lws_state_notify_link_t *link, int current, int target)
{
printf("SYSTEM NOTIFY CALLBACK!!!\n");
struct lws_context *context = mgr->parent;
struct lws_client_connect_info i;
if (current != LWS_SYSTATE_OPERATIONAL || target != LWS_SYSTATE_OPERATIONAL)
return 0;
lwsl_notice("%s: operational\n", __func__);
memset(&i, 0, sizeof i); /* otherwise uninitialized garbage */
i.context = context;
i.ssl_connection = LCCSCF_USE_SSL | LCCSCF_H2_QUIRK_OVERFLOWS_TXCR |
LCCSCF_H2_QUIRK_NGHTTP2_END_STREAM;
i.port = 443;
i.address = "warmcat.com";
i.path = "/";
i.host = i.address;
i.origin = i.address;
i.method = "GET";
i.protocol = "http";
return !lws_client_connect_via_info(&i);
}
static int
callback_http(struct lws *wsi, enum lws_callback_reasons reason,
void *user, void *in, size_t len)
{
switch (reason) {
/* because we are protocols[0] ... */
case LWS_CALLBACK_CLIENT_CONNECTION_ERROR:
lwsl_err("CLIENT_CONNECTION_ERROR: %s\n",
in ? (char *)in : "(null)");
break;
case LWS_CALLBACK_ESTABLISHED_CLIENT_HTTP:
{
char buf[128];
lws_get_peer_simple(wsi, buf, sizeof(buf));
status = lws_http_client_http_response(wsi);
lwsl_user("Connected to %s, http response: %d\n",
buf, status);
}
break;
/* chunks of chunked content, with header removed */
case LWS_CALLBACK_RECEIVE_CLIENT_HTTP_READ:
lwsl_user("RECEIVE_CLIENT_HTTP_READ: read %d\n", (int)len);
return 0; /* don't passthru */
/* uninterpreted http content */
case LWS_CALLBACK_RECEIVE_CLIENT_HTTP:
{
char buffer[1024 + LWS_PRE];
char *px = buffer + LWS_PRE;
int lenx = sizeof(buffer) - LWS_PRE;
if (lws_http_client_read(wsi, &px, &lenx) < 0)
return -1;
}
return 0; /* don't passthru */
case LWS_CALLBACK_COMPLETED_CLIENT_HTTP:
lwsl_user("LWS_CALLBACK_COMPLETED_CLIENT_HTTP\n");
lws_cancel_service(lws_get_context(wsi)); /* abort poll wait */
break;
case LWS_CALLBACK_CLOSED_CLIENT_HTTP:
lws_cancel_service(lws_get_context(wsi)); /* abort poll wait */
break;
default:
break;
}
return lws_callback_http_dummy(wsi, reason, user, in, len);
}
static const struct lws_protocols protocols[] = {
{
"twillio-ws",
ws_callbacks,
0,
0,
},
{ NULL, NULL, 0, 0 }
};
static gpointer
t1_main (gpointer user_data)
{
lws_state_notify_link_t notifier = { {0}, system_notify_cb, "app" };
lws_state_notify_link_t *na[] = { ¬ifier, NULL };
GMainContext *t1_mc = (GMainContext *)user_data;
struct lws_context_creation_info info;
struct lws_context *context;
void *foreign_loops[1];
GMainLoop *loop;
g_print("%s: started\n", __func__);
g_main_context_push_thread_default(t1_mc);
loop = g_main_loop_new(t1_mc, FALSE);
/* attach our lws activities to the main loop of this thread */
lws_set_log_level(LLL_USER | LLL_ERR | LLL_WARN | LLL_NOTICE, NULL);
memset(&info, 0, sizeof info);
info.port = CONTEXT_PORT_NO_LISTEN;
info.options = LWS_SERVER_OPTION_DO_SSL_GLOBAL_INIT | LWS_SERVER_OPTION_GLIB;
info.protocols = protocols;
info.timeout_secs = 10;
info.connect_timeout_secs = 30;
foreign_loops[0] = (void *)loop;
info.foreign_loops = foreign_loops;
info.register_notifier_list = na;
context = lws_create_context(&info);
if (!context) {
lwsl_err("lws init failed\n");
return NULL;
}
printf("context was created\n");
/*
* We created the lws_context and bound it to this thread's main loop,
* let's run the thread's main loop now...
*/
g_main_loop_run(loop);
g_main_loop_unref(loop);
g_main_context_pop_thread_default(t1_mc);
g_main_context_unref(t1_mc);
g_print("%s: ending\n", __func__);
lws_context_destroy(context);
return NULL;
}
int
main(int argc, char **argv)
{
GMainContext *t1_mc = g_main_context_new();
GtkApplication *app;
GThread *t1;
int status;
t1 = g_thread_new ("t1", t1_main, g_main_context_ref (t1_mc));
(void)t1;
app = gtk_application_new("org.gtk.example", G_APPLICATION_FLAGS_NONE);
g_signal_connect(app, "activate", G_CALLBACK(activate), NULL);
status = g_application_run(G_APPLICATION(app), argc, argv);
g_object_unref(app);
return status;
}