File examples/wsdemo.c changed (mode: 100644) (index a204181..cd18486) |
12 |
12 |
#include <sys/time.h> |
#include <sys/time.h> |
13 |
13 |
#include <sys/ioctl.h> |
#include <sys/ioctl.h> |
14 |
14 |
#include <time.h> |
#include <time.h> |
|
15 |
|
#include <ctype.h> |
15 |
16 |
#include <stdarg.h> |
#include <stdarg.h> |
16 |
17 |
#include <sys/capability.h> |
#include <sys/capability.h> |
17 |
18 |
|
|
|
... |
... |
struct priv { |
35 |
36 |
unsigned int pie1_init:1; |
unsigned int pie1_init:1; |
36 |
37 |
unsigned int pie2_init:1; |
unsigned int pie2_init:1; |
37 |
38 |
unsigned int bar1_init:1; |
unsigned int bar1_init:1; |
|
39 |
|
unsigned int grid1_init:1; |
38 |
40 |
|
|
39 |
41 |
unsigned char per1_percent; |
unsigned char per1_percent; |
40 |
42 |
unsigned char per2_percent; |
unsigned char per2_percent; |
|
... |
... |
static unsigned char band[2][BAND_VALUES]; |
58 |
60 |
static time_t band_last; // when the last value was updated |
static time_t band_last; // when the last value was updated |
59 |
61 |
static int band_up = 30, band_down = 30; // fake start values |
static int band_up = 30, band_down = 30; // fake start values |
60 |
62 |
|
|
|
63 |
|
/* grid1 is global */ |
|
64 |
|
static char grid1_data[3][3][64]; |
|
65 |
|
|
|
66 |
|
|
61 |
67 |
struct Clients |
struct Clients |
62 |
68 |
{ |
{ |
63 |
69 |
struct Conn *C; |
struct Conn *C; |
|
... |
... |
static void push_code(struct Conn *C, const char *code) |
131 |
137 |
json_object_put(j); |
json_object_put(j); |
132 |
138 |
} |
} |
133 |
139 |
|
|
|
140 |
|
/* |
|
141 |
|
* Send code to all other clients |
|
142 |
|
*/ |
|
143 |
|
static void push_code_broadcast(struct Conn *source, const char *code) |
|
144 |
|
{ |
|
145 |
|
struct Clients *q; |
|
146 |
|
|
|
147 |
|
q = Clients_head; |
|
148 |
|
while (q) { |
|
149 |
|
if (q->C != source) |
|
150 |
|
push_code(q->C, code); |
|
151 |
|
q = q->next; |
|
152 |
|
} |
|
153 |
|
} |
|
154 |
|
|
134 |
155 |
static void notify(struct Conn *C, const char *format, ...) |
static void notify(struct Conn *C, const char *format, ...) |
135 |
156 |
{ |
{ |
136 |
157 |
char line[512]; |
char line[512]; |
|
... |
... |
static void pie2(struct Conn *C) |
771 |
792 |
push_code(C, code); |
push_code(C, code); |
772 |
793 |
} |
} |
773 |
794 |
|
|
|
795 |
|
static void grid1(struct Conn *C) |
|
796 |
|
{ |
|
797 |
|
struct priv *p; |
|
798 |
|
unsigned int i, j; |
|
799 |
|
char code[128]; |
|
800 |
|
|
|
801 |
|
p = Conn_get_private(C); |
|
802 |
|
if (!p->grid1_init) { |
|
803 |
|
const char *u = |
|
804 |
|
"window.wsdemo.grid1_oninput = function(id, i, j) {\n" |
|
805 |
|
" var d = document.getElementById(id);\n" |
|
806 |
|
" var data = { op: 'grid1_oninput', i: i, j: j, value: d.innerHTML };\n" |
|
807 |
|
" ws.send(JSON.stringify(data));\n" |
|
808 |
|
" console.log('grid1_oninput: id=' + id + ' i=' + i + ' j=' + j);\n" |
|
809 |
|
"}\n" |
|
810 |
|
"\n" |
|
811 |
|
"window.wsdemo.grid1_update = function(i, j, value) {\n" |
|
812 |
|
" var d = document.getElementById('grid1-' + i + '-' + j);\n" |
|
813 |
|
" d.innerHTML = value;\n" |
|
814 |
|
"}\n" |
|
815 |
|
"\n" |
|
816 |
|
"window.wsdemo.grid1_init = function(a) {\n" |
|
817 |
|
" var i;\n" |
|
818 |
|
" var d;\n" |
|
819 |
|
"\n" |
|
820 |
|
" d = document.getElementById('grid1');\n" |
|
821 |
|
" if (d.children[0] != null)\n" |
|
822 |
|
" return;\n" |
|
823 |
|
"\n" |
|
824 |
|
" var t = document.createElement('table');\n" |
|
825 |
|
"\n" |
|
826 |
|
" t.style.border = '1px solid black';\n" |
|
827 |
|
" t.style.borderCollapse = 'collapse';\n" |
|
828 |
|
" t.style.margin = '3px';\n" |
|
829 |
|
" t.style.padding = '5px';\n" |
|
830 |
|
" for (i = 0; i < 3; i++) {\n" |
|
831 |
|
" var tr = t.insertRow();\n" |
|
832 |
|
" for (j = 0; j < 3; j++) {\n" |
|
833 |
|
" var td = tr.insertCell();\n" |
|
834 |
|
" var v = '?';\n" |
|
835 |
|
" td.style.border = '1px solid black';\n" |
|
836 |
|
" td.contentEditable = true;\n" |
|
837 |
|
" td.id = 'grid1-' + i + '-' + j;\n" |
|
838 |
|
" var tn = document.createTextNode(v);\n" |
|
839 |
|
" td.appendChild(tn);\n" |
|
840 |
|
" td.setAttribute('oninput', 'window.wsdemo.grid1_oninput(this.id,' + i + ',' + j + ');');\n" |
|
841 |
|
" }\n" |
|
842 |
|
" }\n" |
|
843 |
|
" d.appendChild(t);\n" |
|
844 |
|
"}\n" |
|
845 |
|
"\n" |
|
846 |
|
"window.wsdemo.grid1_init();"; |
|
847 |
|
push_code(C, u); |
|
848 |
|
|
|
849 |
|
for (i = 0; i < 3; i++) { |
|
850 |
|
for (j = 0; j < 3; j++) { |
|
851 |
|
snprintf(code, sizeof(code), |
|
852 |
|
"window.wsdemo.grid1_update(%hhu, %hhu, '%s');", |
|
853 |
|
i, j, grid1_data[i][j]); |
|
854 |
|
push_code(C, code); |
|
855 |
|
} |
|
856 |
|
} |
|
857 |
|
|
|
858 |
|
p->grid1_init = 1; |
|
859 |
|
} |
|
860 |
|
} |
|
861 |
|
|
774 |
862 |
static void trigger(struct Conn *C) |
static void trigger(struct Conn *C) |
775 |
863 |
{ |
{ |
776 |
864 |
struct priv *p; |
struct priv *p; |
|
... |
... |
static void trigger(struct Conn *C) |
835 |
923 |
cyl1(C); |
cyl1(C); |
836 |
924 |
pie1(C); |
pie1(C); |
837 |
925 |
pie2(C); |
pie2(C); |
|
926 |
|
grid1(C); |
838 |
927 |
} |
} |
839 |
928 |
|
|
840 |
929 |
static const char main_screen[] = |
static const char main_screen[] = |
|
... |
... |
static const char main_screen[] = |
967 |
1056 |
"</div>\n" |
"</div>\n" |
968 |
1057 |
"\n" |
"\n" |
969 |
1058 |
"<div class=\"gem\">\n" |
"<div class=\"gem\">\n" |
|
1059 |
|
" Editable grid (HTML5 content editable)<br />\n" |
|
1060 |
|
" <div id=\"grid1\"></div>\n" |
|
1061 |
|
" <input type=\"button\" onclick=\"window.wsdemo.source('grid1');\" value=\"Source\" />\n" |
|
1062 |
|
" <div class=\"source\" id=\"grid1_source\"></div>\n" |
|
1063 |
|
" <div class=\"note\" width=\"200px\">" |
|
1064 |
|
" Note: please load this page on two different devices, then try" |
|
1065 |
|
" to change the content of a cell and watch the other device to" |
|
1066 |
|
" see how the value changes." |
|
1067 |
|
" </div>\n" |
|
1068 |
|
"</div>\n" |
|
1069 |
|
"\n" |
|
1070 |
|
"<div class=\"gem\">\n" |
970 |
1071 |
"Buttons<br />\n" |
"Buttons<br />\n" |
971 |
1072 |
"<input type=\"button\" onclick=\"window.wsdemo.button('notify');\"" |
"<input type=\"button\" onclick=\"window.wsdemo.button('notify');\"" |
972 |
1073 |
" value=\"trigger notification\" />\n" |
" value=\"trigger notification\" />\n" |
973 |
|
"</div>\n"; |
|
|
1074 |
|
"</div>\n" |
|
1075 |
|
"\n"; |
974 |
1076 |
|
|
975 |
1077 |
static void accept_cb(struct Conn *C) |
static void accept_cb(struct Conn *C) |
976 |
1078 |
{ |
{ |
|
... |
... |
static void process_json(struct Conn *C, struct json_object *j) |
1043 |
1145 |
const char *s_op, *s_answer, *s_text; |
const char *s_op, *s_answer, *s_text; |
1044 |
1146 |
struct json_object *op, *answer, *text; |
struct json_object *op, *answer, *text; |
1045 |
1147 |
struct priv *priv; |
struct priv *priv; |
|
1148 |
|
char code[4096]; |
1046 |
1149 |
|
|
1047 |
1150 |
Log(10, "%llu %s\n", Conn_get_id(C), __func__); |
Log(10, "%llu %s\n", Conn_get_id(C), __func__); |
1048 |
1151 |
|
|
|
... |
... |
static void process_json(struct Conn *C, struct json_object *j) |
1106 |
1209 |
Log(10, "\tWe have a resize event [x=%u y=%u]!\n", |
Log(10, "\tWe have a resize event [x=%u y=%u]!\n", |
1107 |
1210 |
priv->x, priv->y); |
priv->x, priv->y); |
1108 |
1211 |
|
|
|
1212 |
|
json_object_put(answer); |
1109 |
1213 |
return; |
return; |
1110 |
1214 |
} |
} |
1111 |
1215 |
|
|
|
... |
... |
static void process_json(struct Conn *C, struct json_object *j) |
1117 |
1221 |
notify(C, "Welcome to wsdemo! You can push a notification from" |
notify(C, "Welcome to wsdemo! You can push a notification from" |
1118 |
1222 |
" the server whenever is needed."); |
" the server whenever is needed."); |
1119 |
1223 |
} |
} |
|
1224 |
|
json_object_put(answer); |
|
1225 |
|
return; |
|
1226 |
|
} |
|
1227 |
|
|
|
1228 |
|
if (strcmp(s_op, "grid1_oninput") == 0) { |
|
1229 |
|
unsigned char i, k; |
|
1230 |
|
char *q; |
|
1231 |
|
|
|
1232 |
|
json_object_object_get_ex(j, "i", &text); |
|
1233 |
|
i = json_object_get_int(text); |
|
1234 |
|
|
|
1235 |
|
json_object_object_get_ex(j, "j", &text); |
|
1236 |
|
k = json_object_get_int(text); |
|
1237 |
|
|
|
1238 |
|
json_object_object_get_ex(j, "value", &text); |
|
1239 |
|
s_text = json_object_get_string(text); |
|
1240 |
|
|
|
1241 |
|
Log(10, "\tWe have a grid1 event [i=%hhu j=%hhu value=[%s]]!\n", |
|
1242 |
|
i, k, s_text); |
|
1243 |
|
|
|
1244 |
|
if ((i < 3) && (k < 3)) |
|
1245 |
|
snprintf(grid1_data[i][k], sizeof(grid1_data[i][k]), |
|
1246 |
|
"%s", s_text); |
|
1247 |
|
|
|
1248 |
|
q = s_text; |
|
1249 |
|
while (isalnum(*q)) |
|
1250 |
|
q++; |
|
1251 |
|
*q = '\0'; |
|
1252 |
|
|
|
1253 |
|
snprintf(code, sizeof(code), |
|
1254 |
|
"window.wsdemo.grid1_update(%hhu, %hhu, '%s');", |
|
1255 |
|
i, k, s_text); |
|
1256 |
|
push_code_broadcast(C, code); |
|
1257 |
|
|
|
1258 |
|
json_object_put(answer); |
1120 |
1259 |
return; |
return; |
1121 |
1260 |
} |
} |
1122 |
1261 |
|
|
|
... |
... |
int main(void) |
1222 |
1361 |
char *stats, path[256]; |
char *stats, path[256]; |
1223 |
1362 |
struct timeval now; |
struct timeval now; |
1224 |
1363 |
struct tm tm; |
struct tm tm; |
|
1364 |
|
unsigned int i, j; |
|
1365 |
|
|
|
1366 |
|
// Global data init |
|
1367 |
|
// Init grid1 data |
|
1368 |
|
for (i = 0; i < 3; i++) { |
|
1369 |
|
for (j = 0; j < 3; j++) { |
|
1370 |
|
snprintf(grid1_data[i][j], sizeof(grid1_data[i][j]), |
|
1371 |
|
"%ld", random() % 1000); |
|
1372 |
|
} |
|
1373 |
|
} |
1225 |
1374 |
|
|
1226 |
1375 |
gettimeofday(&now, NULL); |
gettimeofday(&now, NULL); |
1227 |
1376 |
localtime_r(&now.tv_sec, &tm); |
localtime_r(&now.tv_sec, &tm); |