]> git.sesse.net Git - ultimatescore/blob - client/mainwindow.cpp
Make the client talk to the UDP proxy.
[ultimatescore] / client / mainwindow.cpp
1 #include "mainwindow.h"
2 #include "post_to_main_thread.h"
3 #include "ui_mainwindow.h"
4
5 #include <arpa/inet.h>
6 #include <netinet/in.h>
7 #include <netinet/tcp.h>
8 #include <stdlib.h>
9 #include <sys/types.h>
10 #include <sys/socket.h>
11 #include <unistd.h>
12
13 using namespace std;
14
15 string escape_html(const string &str)
16 {
17         string s = "";
18         for (char ch : str) {
19                 if (ch == '<') {
20                         s += "&lt;";
21                 } else if (ch == '>') {
22                         s += "&gt;";
23                 } else if (ch == '&') {
24                         s += "&amp;";
25                 } else {
26                         s += ch;
27                 }
28         }
29         return s;
30 }
31
32 string escape_unicode(const string &str)
33 {
34         string s = "";
35         for (size_t pos = 0; pos < str.size(); ) {
36                 wchar_t wc;
37                 int len = mbtowc(&wc, str.data() + pos, str.size() - pos);
38                 if (len == -1) {
39                         wc = '?';
40                         len = 1;
41                 }
42                 pos += len;
43
44                 if (wc == '\\') {
45                         s += "\\\\";
46                 } else if (isprint(wc)) {
47                         s += wc;
48                 } else {
49                         char buf[16];
50                         snprintf(buf, sizeof(buf), "\\u%04x", wc);
51                         s += buf;
52                 }
53         }
54         return s;
55 }
56
57 string escape_quotes(const string &str)
58 {
59         string s = "";
60         for (char ch : str) {
61                 if (ch == '"') {
62                         s += '\\';
63                 }
64                 s += ch;
65         }
66         return s;
67 }
68
69 string serialize_as_json(const map<string, string> &param)
70 {
71         string s = "{";
72
73         bool first = true;
74         for (const auto &key_value : param) {
75                 if (!first) s += ", ";
76                 first = false;
77
78                 s += '"';
79                 s += escape_quotes(escape_unicode(key_value.first));
80                 s += "\": \"";
81                 s += escape_quotes(escape_unicode(key_value.second));
82                 s += '"';
83         }
84         s += "}";
85         return s;       
86 }
87
88 MainWindow::MainWindow(QWidget *parent) :
89     QMainWindow(parent),
90     ui(new Ui::MainWindow)
91 {
92         ui->setupUi(this);
93         ws = new WSServer("127.0.0.1", 5250);
94         ws->set_connection_callback([this](bool connected) {
95                 string msg = connected ? "Connected" : "Not connected";
96                 post_to_main_thread([this, msg]() {
97                         ui->ws_connected_label->setText(QString::fromStdString(msg));
98                 });
99         });
100
101         udp_thread = std::thread(&MainWindow::udp_thread_func, this, 6000);
102         udp_thread2 = std::thread(&MainWindow::udp_thread_func, this, 6001);
103
104         connect(ui->ws_disconnect_btn, &QPushButton::clicked, this, &MainWindow::ws_disconnect_clicked);
105         connect(ui->set_initials_btn, &QPushButton::clicked, this, &MainWindow::set_initials_clicked);
106         connect(ui->set_match_2_initials_btn, &QPushButton::clicked, this, &MainWindow::set_match_2_initials_clicked);
107         connect(ui->set_color_btn, &QPushButton::clicked, this, &MainWindow::set_color_clicked);
108         connect(ui->set_score_btn, &QPushButton::clicked, this, &MainWindow::set_score_clicked);
109         connect(ui->set_all_scorebug_btn, &QPushButton::clicked, this, &MainWindow::set_all_scorebug_clicked);
110         connect(ui->goal_1_btn, &QPushButton::clicked, this, [this]() { add_goal(ui->score_1_box, 1); });
111         connect(ui->ungoal_1_btn, &QPushButton::clicked, this, [this]() { add_goal(ui->score_1_box, -1); });
112         connect(ui->goal_2_btn, &QPushButton::clicked, this, [this]() { add_goal(ui->score_2_box, 1); });
113         connect(ui->ungoal_2_btn, &QPushButton::clicked, this, [this]() { add_goal(ui->score_2_box, -1); });
114
115         connect(ui->set_clock_btn, &QPushButton::clicked, this, &MainWindow::set_clock_clicked);
116         connect(ui->set_clock_limit_btn, &QPushButton::clicked, this, &MainWindow::set_clock_limit_clicked);
117         connect(ui->start_and_show_clock_btn, &QPushButton::clicked, this, &MainWindow::start_and_show_clock_clicked);
118         connect(ui->stop_clock_btn, &QPushButton::clicked, this, &MainWindow::stop_clock_clicked);
119         connect(ui->show_clock_btn, &QPushButton::clicked, this, &MainWindow::show_clock_clicked);
120         connect(ui->hide_clock_btn, &QPushButton::clicked, this, &MainWindow::hide_clock_clicked);
121         connect(ui->show_match_2_btn, &QPushButton::clicked, this, &MainWindow::show_match_2_clicked);
122         connect(ui->hide_match_2_btn, &QPushButton::clicked, this, &MainWindow::hide_match_2_clicked);
123
124         connect(ui->set_comment_btn, &QPushButton::clicked, this, &MainWindow::set_comment_clicked);
125         connect(ui->set_and_show_comment_btn, &QPushButton::clicked, this, &MainWindow::set_and_show_comment_clicked);
126         connect(ui->hide_comment_btn, &QPushButton::clicked, this, &MainWindow::hide_comment_clicked);
127         connect(ui->set_and_show_autocomment_btn, &QPushButton::clicked, this, &MainWindow::set_and_show_autocomment_clicked);
128         connect(ui->autoshow_autocomment, &QCheckBox::stateChanged, this, &MainWindow::autocomment_update);
129
130         connect(ui->show_lower_third_btn, &QPushButton::clicked, this, &MainWindow::show_lower_third_clicked);
131         connect(ui->hide_lower_third_btn, &QPushButton::clicked, this, &MainWindow::hide_lower_third_clicked);
132
133         connect(ui->quick_lower_third_edit, &QLineEdit::returnPressed, this, &MainWindow::quick_lower_third_activate);
134         connect(ui->show_quick_lower_third_btn, &QPushButton::clicked, this, &MainWindow::quick_lower_third_activate);
135
136         connect(ui->show_scorebug_btn, &QPushButton::clicked, this, &MainWindow::show_scorebug_clicked);
137         connect(ui->show_group_a_btn, &QPushButton::clicked, this, [this]() { show_group_clicked("Group A"); });
138         connect(ui->show_group_b_btn, &QPushButton::clicked, this, [this]() { show_group_clicked("Group B"); });
139         connect(ui->show_group_c_btn, &QPushButton::clicked, this, [this]() { show_group_clicked("Group C"); });
140         connect(ui->show_schedule_btn, &QPushButton::clicked, this, &MainWindow::show_schedule_clicked);
141         connect(ui->show_carousel_btn, &QPushButton::clicked, this, &MainWindow::show_carousel_clicked);
142         connect(ui->show_nothing_btn, &QPushButton::clicked, this, &MainWindow::show_nothing_clicked);
143         connect(ui->show_roster_1_btn, &QPushButton::clicked, this, [this]() { show_roster_clicked(ui->initials_1_edit->text().toStdString()); });
144         connect(ui->show_roster_2_btn, &QPushButton::clicked, this, [this]() { show_roster_clicked(ui->initials_2_edit->text().toStdString()); });
145         connect(ui->show_roster_carousel_btn, &QPushButton::clicked, this, &MainWindow::show_roster_carousel_clicked);
146
147         autocomment_update();
148
149         const set<pair<unsigned, unsigned>> usb{{ 0x0e8f, 0x0041 }};
150         event_device = new EventDevice(usb, ui->quick_lower_third_edit);
151         event_device->start_thread();
152 }
153
154 MainWindow::~MainWindow()
155 {
156         delete ui;
157 }
158
159 void MainWindow::ws_disconnect_clicked()
160 {
161         ws->change_port(stoi(ui->ws_port_box->text().toStdString()));
162 }
163
164 void MainWindow::set_initials_clicked()
165 {
166         map<string, string> param;
167         param["team1"] = escape_html(ui->initials_1_edit->text().toStdString());
168         param["team2"] = escape_html(ui->initials_2_edit->text().toStdString());
169         ws->send_command("update " + serialize_as_json(param));
170         ws->send_command("eval setteams()");
171 }
172
173 void MainWindow::set_match_2_initials_clicked()
174 {
175         map<string, string> param;
176         param["team1"] = escape_html(ui->match_2_initials_1_edit->text().toStdString());
177         param["team2"] = escape_html(ui->match_2_initials_2_edit->text().toStdString());
178         ws->send_command("update " + serialize_as_json(param));
179         ws->send_command("eval setteams2()");
180 }
181
182 void MainWindow::set_color_clicked()
183 {
184         map<string, string> param;
185         param["team1color"] = ui->color_1_edit->text().toStdString();  // Should maybe be escaped, but meh.
186         param["team2color"] = ui->color_2_edit->text().toStdString();
187         ws->send_command("update " + serialize_as_json(param));
188         ws->send_command("eval setcolors()");
189 }
190
191 void MainWindow::set_score_clicked()
192 {
193         map<string, string> param;
194         param["score1"] = to_string(ui->score_1_box->value());
195         param["score2"] = to_string(ui->score_2_box->value());
196         ws->send_command("update " + serialize_as_json(param));
197         ws->send_command("eval setscore()");
198         autocomment_update();
199 }
200
201 void MainWindow::set_all_scorebug_clicked()
202 {
203         set_initials_clicked();
204         set_color_clicked();
205         set_score_clicked();
206 }
207
208 void MainWindow::add_goal(QSpinBox *box, int delta)
209 {
210         box->setValue(box->value() + delta);
211         set_score_clicked();
212 }
213
214 void MainWindow::set_clock_clicked()
215 {
216         map<string, string> param;
217         param["clock_min"] = to_string(ui->clock_min_box->value());
218         param["clock_sec"] = to_string(ui->clock_sec_box->value());
219         ws->send_command("update " + serialize_as_json(param));
220         ws->send_command("eval setclockfromstate()");
221 }
222
223 void MainWindow::set_clock_limit_clicked()
224 {
225         map<string, string> param;
226         param["clock_limit_min"] = to_string(ui->clock_limit_min_box->value());
227         param["clock_limit_sec"] = to_string(ui->clock_limit_sec_box->value());
228         ws->send_command("update " + serialize_as_json(param));
229         ws->send_command("eval setclocklimitfromstate()");
230 }
231
232 void MainWindow::start_and_show_clock_clicked()
233 {
234         ws->send_command("eval startclock()");  // Also shows.
235 }
236
237 void MainWindow::stop_clock_clicked()
238 {
239         ws->send_command("eval stopclock()");
240 }
241
242 void MainWindow::show_clock_clicked()
243 {
244         ws->send_command("eval showclock()");
245 }
246
247 void MainWindow::hide_clock_clicked()
248 {
249         ws->send_command("eval hideclock()");
250 }
251
252 void MainWindow::show_match_2_clicked()
253 {
254         ws->send_command("eval showmatch2()");
255 }
256
257 void MainWindow::hide_match_2_clicked()
258 {
259         ws->send_command("eval hidematch2()");
260 }
261
262 void MainWindow::set_comment_clicked()
263 {
264         map<string, string> param;
265         param["comment"] = ui->comment_edit->text().toStdString();
266         ws->send_command("update " + serialize_as_json(param));
267         ws->send_command("eval setcomment()");
268 }
269
270 void MainWindow::set_and_show_comment_clicked()
271 {
272         set_comment_clicked();
273         ws->send_command("eval showcomment()");
274 }
275
276 void MainWindow::set_and_show_autocomment_clicked()
277 {
278         ui->comment_edit->setText(ui->autocomment_edit->text());
279         set_and_show_comment_clicked();
280 }
281
282 void MainWindow::hide_comment_clicked()
283 {
284         ws->send_command("eval hidecomment()");
285 }
286
287 void MainWindow::show_lower_third_clicked()
288 {
289         map<string, string> param;
290         param["text1"] = ui->lowerthird_heading_edit->text().toStdString();
291         param["text2"] = ui->lowerthird_subheading_edit->text().toStdString();
292         ws->send_command("update " + serialize_as_json(param));
293         ws->send_command("eval setandshowlowerthird()");
294 }
295
296 void MainWindow::hide_lower_third_clicked()
297 {
298         ws->send_command("eval hidelowerthird()");
299 }
300
301 void MainWindow::quick_lower_third_activate()
302 {
303         string code = ui->quick_lower_third_edit->text().toUpper().toStdString();
304         if (code == "A") {
305                 add_goal(ui->score_1_box, 1);
306         } else if (code == "B") {
307                 add_goal(ui->score_2_box, 1);
308         } else if (code == "C") {
309                 ws->send_command("eval hidelowerthird()");
310         } else {
311                 map<string, string> param;
312                 param["code"] = code;
313                 ws->send_command("update " + serialize_as_json(param));
314                 ws->send_command("eval quicklowerthird()");
315         }
316         ui->quick_lower_third_edit->clear();
317 }
318
319 void MainWindow::autocomment_update()
320 {
321         int score1 = ui->score_1_box->value();
322         int score2 = ui->score_2_box->value();
323         string msg;
324         if (abs(score1 - score2) >= 3) {
325                 msg = "Game ends after this point";
326         } else {
327                 int cap = max(score1, score2) + 1;
328                 if (score1 == score2) ++cap;
329
330                 if (cap >= 13) {
331                         msg = "Point cap: First to 13";
332                 } else {
333                         char buf[32];
334                         snprintf(buf, sizeof(buf), "Pagacap: First to %d", cap);
335                         msg = buf;
336                 }
337         }
338         ui->autocomment_edit->setText(QString::fromStdString(msg));
339
340         map<string, string> param;
341         param["autocomment_on_clock_limit"] = ui->autoshow_autocomment->isChecked() ? "1" : "0";
342         param["autocomment"] = msg;
343         ws->send_command("update " + serialize_as_json(param));
344 }
345
346 void MainWindow::show_scorebug_clicked()
347 {
348         ws->send_command("eval stopcarousel()");
349         ws->send_command("eval hidetable()");
350         ws->send_command("eval showscorebug()");
351 }
352
353 void MainWindow::show_group_clicked(const std::string &group_name)
354 {
355         map<string, string> param;
356         param["group_name"] = group_name;
357         ws->send_command("eval stopcarousel()");
358         ws->send_command("update " + serialize_as_json(param));
359         ws->send_command("eval showgroup_from_state()");
360 }
361
362 void MainWindow::show_roster_clicked(const std::string &team_code)
363 {
364         map<string, string> param;
365         param["team_code"] = team_code;
366         ws->send_command("eval stopcarousel()");
367         ws->send_command("update " + serialize_as_json(param));
368         ws->send_command("eval showroster_from_state()");
369 }
370
371 void MainWindow::show_schedule_clicked()
372 {
373         ws->send_command("eval stopcarousel()");
374         ws->send_command("eval showschedule()");
375 }
376
377 void MainWindow::show_carousel_clicked()
378 {
379         ws->send_command("eval stopcarousel()");
380         ws->send_command("eval showcarousel()");
381 }
382
383 void MainWindow::show_roster_carousel_clicked()
384 {
385         map<string, string> param;
386         param["team1"] = escape_html(ui->initials_1_edit->text().toStdString());
387         param["team2"] = escape_html(ui->initials_2_edit->text().toStdString());
388         ws->send_command("eval stopcarousel()");
389         ws->send_command("update " + serialize_as_json(param));
390         ws->send_command("eval showrostercarousel_from_state()");
391 }
392
393 void MainWindow::show_nothing_clicked()
394 {
395         ws->send_command("eval hidescorebug()");
396         ws->send_command("eval stopcarousel()");
397         ws->send_command("eval hidetable()");
398 }
399
400 void udp_thread_nat_func(int sock, int port)
401 {
402         sockaddr_in6 saddr6;
403         memset(&saddr6, 0, sizeof(saddr6));
404         saddr6.sin6_family = AF_INET6;
405         inet_pton(AF_INET6, "::ffff:193.35.52.50", &saddr6.sin6_addr);
406         saddr6.sin6_port = htons(port);
407
408         for ( ;; ) {
409                 char buf[] = "ping";
410                 sendto(sock, buf, 4, 0, (sockaddr *)&saddr6, sizeof(saddr6));
411                 sleep(1);
412         }
413 }
414
415 void MainWindow::udp_thread_func(int port)
416 {
417         int sock = socket(PF_INET6, SOCK_DGRAM, IPPROTO_UDP);
418         if (sock == -1) {
419                 perror("socket");
420                 exit(1);
421         }
422
423         int one = 1;
424         if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) == -1) {
425                 perror("setsockopt");
426                 exit(1);
427         }
428
429         sockaddr_in6 saddr6;
430         memset(&saddr6, 0, sizeof(saddr6));
431         saddr6.sin6_family = AF_INET6;
432         inet_pton(AF_INET6, "::", &saddr6.sin6_addr);
433         saddr6.sin6_port = htons(port);
434         if (bind(sock, (sockaddr *)&saddr6, sizeof(saddr6)) == -1) {
435                 perror("bind");
436                 exit(1);
437         }
438
439         std::thread(&udp_thread_nat_func, sock, port + 1000).detach();
440
441         for ( ;; ) {
442                 char buf[4096];
443                 int err = recv(sock, buf, sizeof(buf), 0);
444                 if (err == -1) {
445                         perror("recv");
446                         exit(1);
447                 }
448
449                 post_to_main_thread([buf, err, port, this] {
450                         bt6000_message_received(string(buf, err), port);
451                 });
452         }
453 }
454
455 int parse_digit(char ch)
456 {
457         if (ch >= '0' && ch <= '9') {
458                 return ch - '0';
459         }
460         return 0;
461 }
462
463 int parse_clock(char ch1, char ch2)
464 {
465         int s1 = parse_digit(ch1);
466         int s2 = parse_digit(ch2);
467         return s1 * 10 + s2;
468 }
469
470 int parse_score(char ch1, char ch2, char ch3)
471 {
472         int s1 = parse_digit(ch1);
473         int s2 = parse_digit(ch2);
474         int s3 = parse_digit(ch3);
475         return s1 * 100 + s2 * 10 + s3;
476 }
477
478 void MainWindow::bt6000_message_received(const string &msg, int port)
479 {
480         fprintf(stderr, "BT6000 message: '%s' (port %d)\n", msg.c_str(), port);
481         if (port == 6001) {
482                 if (!ui->bt6000_2_enable->isChecked()) {
483                         return;
484                 }
485         } else {
486                 if (!ui->bt6000_enable->isChecked()) {
487                         return;
488                 }
489         }
490
491         if (msg.size() >= 9 && msg[0] == 'G' && msg[1] == '0' && msg[2] == '1') {
492                 // G01: Game clock, period number, and number of time-outs.
493                 bool clock_running = !(msg[3] & 0x02);
494 //              bool klaxon = (msg[3] & 0x04);
495                 int minutes = parse_clock(msg[5], msg[6]);
496                 int seconds = parse_clock(msg[7], msg[8]);
497
498                 map<string, string> param;
499                 param["clock_min"] = to_string(minutes);
500                 param["clock_sec"] = to_string(seconds);
501                 ws->send_command("update " + serialize_as_json(param));
502
503                 if (port == 6001) {
504                         ws->send_command("eval adjustclock2fromstate()");
505                         if (clock_running) {
506                                 ws->send_command("eval startclock2()");
507                         } else {
508                                 ws->send_command("eval stopclock2()");
509                         }
510                 } else {
511                         ws->send_command("eval adjustclockfromstate()");
512                         if (clock_running) {
513                                 ws->send_command("eval startclock()");
514                         } else {
515                                 ws->send_command("eval stopclock()");
516                         }
517                 }
518         }
519         if (msg.size() >= 10 && msg[0] == 'G' && msg[1] == '0' && msg[2] == '2') {
520                 int score1 = parse_score(msg[4], msg[5], msg[6]);
521                 int score2 = parse_score(msg[7], msg[8], msg[9]);
522                 if (port == 6001) {
523                         map<string, string> param;
524                         param["score1"] = to_string(score1);
525                         param["score2"] = to_string(score2);
526                         ws->send_command("update " + serialize_as_json(param));
527                         ws->send_command("eval setscore2()");
528                 } else {
529                         ui->score_1_box->setValue(score1);
530                         ui->score_2_box->setValue(score2);
531                         set_score_clicked();
532                 }
533         }
534
535         // Ignore type 3 (penalties) and type 4 (timeouts).
536 }