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