1 #include "mainwindow.h"
2 #include "post_to_main_thread.h"
3 #include "ui_mainwindow.h"
6 #include <netinet/in.h>
7 #include <netinet/tcp.h>
10 #include <sys/socket.h>
14 string escape_html(const string &str)
20 } else if (ch == '>') {
22 } else if (ch == '&') {
31 string escape_unicode(const string &str)
34 for (size_t pos = 0; pos < str.size(); ) {
36 int len = mbtowc(&wc, str.data() + pos, str.size() - pos);
45 } else if (isprint(wc)) {
49 snprintf(buf, sizeof(buf), "\\u%04x", wc);
56 string escape_quotes(const string &str)
68 string serialize_as_json(const map<string, string> ¶m)
73 for (const auto &key_value : param) {
74 if (!first) s += ", ";
78 s += escape_quotes(escape_unicode(key_value.first));
80 s += escape_quotes(escape_unicode(key_value.second));
87 MainWindow::MainWindow(QWidget *parent) :
89 ui(new Ui::MainWindow)
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));
100 udp_thread = std::thread(&MainWindow::udp_thread_func, this, 6000);
101 udp_thread2 = std::thread(&MainWindow::udp_thread_func, this, 6001);
103 connect(ui->ws_disconnect_btn, &QPushButton::clicked, this, &MainWindow::ws_disconnect_clicked);
104 connect(ui->set_initials_btn, &QPushButton::clicked, this, &MainWindow::set_initials_clicked);
105 connect(ui->set_match_2_initials_btn, &QPushButton::clicked, this, &MainWindow::set_match_2_initials_clicked);
106 connect(ui->set_color_btn, &QPushButton::clicked, this, &MainWindow::set_color_clicked);
107 connect(ui->set_score_btn, &QPushButton::clicked, this, &MainWindow::set_score_clicked);
108 connect(ui->set_all_scorebug_btn, &QPushButton::clicked, this, &MainWindow::set_all_scorebug_clicked);
109 connect(ui->goal_1_btn, &QPushButton::clicked, this, [this]() { add_goal(ui->score_1_box, 1); });
110 connect(ui->ungoal_1_btn, &QPushButton::clicked, this, [this]() { add_goal(ui->score_1_box, -1); });
111 connect(ui->goal_2_btn, &QPushButton::clicked, this, [this]() { add_goal(ui->score_2_box, 1); });
112 connect(ui->ungoal_2_btn, &QPushButton::clicked, this, [this]() { add_goal(ui->score_2_box, -1); });
114 connect(ui->set_clock_btn, &QPushButton::clicked, this, &MainWindow::set_clock_clicked);
115 connect(ui->set_clock_limit_btn, &QPushButton::clicked, this, &MainWindow::set_clock_limit_clicked);
116 connect(ui->start_and_show_clock_btn, &QPushButton::clicked, this, &MainWindow::start_and_show_clock_clicked);
117 connect(ui->stop_clock_btn, &QPushButton::clicked, this, &MainWindow::stop_clock_clicked);
118 connect(ui->show_clock_btn, &QPushButton::clicked, this, &MainWindow::show_clock_clicked);
119 connect(ui->hide_clock_btn, &QPushButton::clicked, this, &MainWindow::hide_clock_clicked);
120 connect(ui->show_match_2_btn, &QPushButton::clicked, this, &MainWindow::show_match_2_clicked);
121 connect(ui->hide_match_2_btn, &QPushButton::clicked, this, &MainWindow::hide_match_2_clicked);
123 connect(ui->set_comment_btn, &QPushButton::clicked, this, &MainWindow::set_comment_clicked);
124 connect(ui->set_and_show_comment_btn, &QPushButton::clicked, this, &MainWindow::set_and_show_comment_clicked);
125 connect(ui->hide_comment_btn, &QPushButton::clicked, this, &MainWindow::hide_comment_clicked);
126 connect(ui->set_and_show_autocomment_btn, &QPushButton::clicked, this, &MainWindow::set_and_show_autocomment_clicked);
128 connect(ui->show_lower_third_btn, &QPushButton::clicked, this, &MainWindow::show_lower_third_clicked);
129 connect(ui->hide_lower_third_btn, &QPushButton::clicked, this, &MainWindow::hide_lower_third_clicked);
131 connect(ui->quick_lower_third_edit, &QLineEdit::returnPressed, this, &MainWindow::quick_lower_third_activate);
132 connect(ui->show_quick_lower_third_btn, &QPushButton::clicked, this, &MainWindow::quick_lower_third_activate);
134 connect(ui->show_scorebug_btn, &QPushButton::clicked, this, &MainWindow::show_scorebug_clicked);
135 connect(ui->show_group_a_btn, &QPushButton::clicked, this, [this]() { show_group_clicked("Group A"); });
136 connect(ui->show_group_b_btn, &QPushButton::clicked, this, [this]() { show_group_clicked("Group B"); });
137 connect(ui->show_schedule_btn, &QPushButton::clicked, this, &MainWindow::show_schedule_clicked);
138 connect(ui->show_carousel_btn, &QPushButton::clicked, this, &MainWindow::show_carousel_clicked);
139 connect(ui->show_nothing_btn, &QPushButton::clicked, this, &MainWindow::show_nothing_clicked);
140 connect(ui->show_roster_1_btn, &QPushButton::clicked, this, [this]() { show_roster_clicked(ui->initials_1_edit->text().toStdString()); });
141 connect(ui->show_roster_2_btn, &QPushButton::clicked, this, [this]() { show_roster_clicked(ui->initials_2_edit->text().toStdString()); });
142 connect(ui->show_roster_carousel_btn, &QPushButton::clicked, this, &MainWindow::show_roster_carousel_clicked);
144 autocomment_update();
146 const set<pair<unsigned, unsigned>> usb{{ 0x0e8f, 0x0041 }};
147 event_device = new EventDevice(usb, ui->quick_lower_third_edit);
148 event_device->start_thread();
151 MainWindow::~MainWindow()
156 void MainWindow::ws_disconnect_clicked()
158 ws->change_port(stoi(ui->ws_port_box->text().toStdString()));
161 void MainWindow::set_initials_clicked()
163 map<string, string> param;
164 param["team1"] = escape_html(ui->initials_1_edit->text().toStdString());
165 param["team2"] = escape_html(ui->initials_2_edit->text().toStdString());
166 ws->send_command("update " + serialize_as_json(param));
167 ws->send_command("eval setteams()");
170 void MainWindow::set_match_2_initials_clicked()
172 map<string, string> param;
173 param["team1"] = escape_html(ui->match_2_initials_1_edit->text().toStdString());
174 param["team2"] = escape_html(ui->match_2_initials_2_edit->text().toStdString());
175 ws->send_command("update " + serialize_as_json(param));
176 ws->send_command("eval setteams2()");
179 void MainWindow::set_color_clicked()
181 map<string, string> param;
182 param["team1color"] = ui->color_1_edit->text().toStdString(); // Should maybe be escaped, but meh.
183 param["team2color"] = ui->color_2_edit->text().toStdString();
184 ws->send_command("update " + serialize_as_json(param));
185 ws->send_command("eval setcolors()");
188 void MainWindow::set_score_clicked()
190 map<string, string> param;
191 param["score1"] = to_string(ui->score_1_box->value());
192 param["score2"] = to_string(ui->score_2_box->value());
193 ws->send_command("update " + serialize_as_json(param));
194 ws->send_command("eval setscore()");
195 autocomment_update();
198 void MainWindow::set_all_scorebug_clicked()
200 set_initials_clicked();
205 void MainWindow::add_goal(QSpinBox *box, int delta)
207 box->setValue(box->value() + delta);
211 void MainWindow::set_clock_clicked()
213 map<string, string> param;
214 param["clock_min"] = to_string(ui->clock_min_box->value());
215 param["clock_sec"] = to_string(ui->clock_sec_box->value());
216 ws->send_command("update " + serialize_as_json(param));
217 ws->send_command("eval setclockfromstate()");
220 void MainWindow::set_clock_limit_clicked()
222 map<string, string> param;
223 param["clock_limit_min"] = to_string(ui->clock_limit_min_box->value());
224 param["clock_limit_sec"] = to_string(ui->clock_limit_sec_box->value());
225 ws->send_command("update " + serialize_as_json(param));
226 ws->send_command("eval setclocklimitfromstate()");
229 void MainWindow::start_and_show_clock_clicked()
231 ws->send_command("eval startclock()"); // Also shows.
234 void MainWindow::stop_clock_clicked()
236 ws->send_command("eval stopclock()");
239 void MainWindow::show_clock_clicked()
241 ws->send_command("eval showclock()");
244 void MainWindow::hide_clock_clicked()
246 ws->send_command("eval hideclock()");
249 void MainWindow::show_match_2_clicked()
251 ws->send_command("eval showmatch2()");
254 void MainWindow::hide_match_2_clicked()
256 ws->send_command("eval hidematch2()");
259 void MainWindow::set_comment_clicked()
261 map<string, string> param;
262 param["comment"] = ui->comment_edit->text().toStdString();
263 ws->send_command("update " + serialize_as_json(param));
264 ws->send_command("eval setcomment()");
267 void MainWindow::set_and_show_comment_clicked()
269 set_comment_clicked();
270 ws->send_command("eval showcomment()");
273 void MainWindow::set_and_show_autocomment_clicked()
275 ui->comment_edit->setText(ui->autocomment_edit->text());
276 set_and_show_comment_clicked();
279 void MainWindow::hide_comment_clicked()
281 ws->send_command("eval hidecomment()");
284 void MainWindow::show_lower_third_clicked()
286 map<string, string> param;
287 param["text1"] = ui->lowerthird_heading_edit->text().toStdString();
288 param["text2"] = ui->lowerthird_subheading_edit->text().toStdString();
289 ws->send_command("update " + serialize_as_json(param));
290 ws->send_command("eval setandshowlowerthird()");
293 void MainWindow::hide_lower_third_clicked()
295 ws->send_command("eval hidelowerthird()");
298 void MainWindow::quick_lower_third_activate()
300 string code = ui->quick_lower_third_edit->text().toUpper().toStdString();
302 add_goal(ui->score_1_box, 1);
303 } else if (code == "B") {
304 add_goal(ui->score_2_box, 1);
305 } else if (code == "C") {
306 ws->send_command("eval hidelowerthird()");
308 map<string, string> param;
309 param["code"] = code;
310 ws->send_command("update " + serialize_as_json(param));
311 ws->send_command("eval quicklowerthird()");
313 ui->quick_lower_third_edit->clear();
316 void MainWindow::autocomment_update()
318 int score1 = ui->score_1_box->value();
319 int score2 = ui->score_2_box->value();
321 if (abs(score1 - score2) >= 3) {
322 msg = "Game ends after this point";
324 int cap = max(score1, score2) + 1;
325 if (score1 == score2) ++cap;
328 msg = "Point cap: First to 13";
331 snprintf(buf, sizeof(buf), "Pagacap: First to %d", cap);
335 ui->autocomment_edit->setText(QString::fromStdString(msg));
338 void MainWindow::show_scorebug_clicked()
340 ws->send_command("eval stopcarousel()");
341 ws->send_command("eval hidetable()");
342 ws->send_command("eval showscorebug()");
345 void MainWindow::show_group_clicked(const std::string &group_name)
347 map<string, string> param;
348 param["group_name"] = group_name;
349 ws->send_command("eval stopcarousel()");
350 ws->send_command("update " + serialize_as_json(param));
351 ws->send_command("eval showgroup_from_state()");
354 void MainWindow::show_roster_clicked(const std::string &team_code)
356 map<string, string> param;
357 param["team_code"] = team_code;
358 ws->send_command("eval stopcarousel()");
359 ws->send_command("update " + serialize_as_json(param));
360 ws->send_command("eval showroster_from_state()");
363 void MainWindow::show_schedule_clicked()
365 ws->send_command("eval stopcarousel()");
366 ws->send_command("eval showschedule()");
369 void MainWindow::show_carousel_clicked()
371 ws->send_command("eval stopcarousel()");
372 ws->send_command("eval showcarousel()");
375 void MainWindow::show_roster_carousel_clicked()
377 map<string, string> param;
378 param["team1"] = escape_html(ui->initials_1_edit->text().toStdString());
379 param["team2"] = escape_html(ui->initials_2_edit->text().toStdString());
380 ws->send_command("eval stopcarousel()");
381 ws->send_command("update " + serialize_as_json(param));
382 ws->send_command("eval showrostercarousel_from_state()");
385 void MainWindow::show_nothing_clicked()
387 ws->send_command("eval hidescorebug()");
388 ws->send_command("eval stopcarousel()");
389 ws->send_command("eval hidetable()");
392 void MainWindow::udp_thread_func(int port)
394 int sock = socket(PF_INET6, SOCK_DGRAM, IPPROTO_UDP);
401 if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) == -1) {
402 perror("setsockopt");
407 memset(&saddr6, 0, sizeof(saddr6));
408 saddr6.sin6_family = AF_INET6;
409 inet_pton(AF_INET6, "::", &saddr6.sin6_addr);
410 saddr6.sin6_port = htons(port);
411 if (bind(sock, (sockaddr *)&saddr6, sizeof(saddr6)) == -1) {
418 int err = recv(sock, buf, sizeof(buf), 0);
424 post_to_main_thread([buf, err, port, this] {
425 bt6000_message_received(string(buf, err), port);
430 int parse_digit(char ch)
432 if (ch >= '0' && ch <= '9') {
438 int parse_clock(char ch1, char ch2)
440 int s1 = parse_digit(ch1);
441 int s2 = parse_digit(ch2);
445 int parse_score(char ch1, char ch2, char ch3)
447 int s1 = parse_digit(ch1);
448 int s2 = parse_digit(ch2);
449 int s3 = parse_digit(ch3);
450 return s1 * 100 + s2 * 10 + s3;
453 void MainWindow::bt6000_message_received(const string &msg, int port)
455 fprintf(stderr, "BT6000 message: '%s' (port %d)\n", msg.c_str(), port);
457 if (!ui->bt6000_2_enable->isChecked()) {
461 if (!ui->bt6000_enable->isChecked()) {
466 if (msg.size() >= 9 && msg[0] == 'G' && msg[1] == '0' && msg[2] == '1') {
467 // G01: Game clock, period number, and number of time-outs.
468 bool clock_running = !(msg[3] & 0x02);
469 // bool klaxon = (msg[3] & 0x04);
470 int minutes = parse_clock(msg[5], msg[6]);
471 int seconds = parse_clock(msg[7], msg[8]);
473 map<string, string> param;
474 param["clock_min"] = to_string(minutes);
475 param["clock_sec"] = to_string(seconds);
476 ws->send_command("update " + serialize_as_json(param));
479 ws->send_command("eval adjustclock2fromstate()");
481 ws->send_command("eval startclock2()");
483 ws->send_command("eval stopclock2()");
486 ws->send_command("eval adjustclockfromstate()");
488 ws->send_command("eval startclock()");
490 ws->send_command("eval stopclock()");
494 if (msg.size() >= 10 && msg[0] == 'G' && msg[1] == '0' && msg[2] == '2') {
495 int score1 = parse_score(msg[4], msg[5], msg[6]);
496 int score2 = parse_score(msg[7], msg[8], msg[9]);
498 map<string, string> param;
499 param["score1"] = to_string(score1);
500 param["score2"] = to_string(score2);
501 ws->send_command("update " + serialize_as_json(param));
502 ws->send_command("eval setscore2()");
504 ui->score_1_box->setValue(score1);
505 ui->score_2_box->setValue(score2);
510 // Ignore type 3 (penalties) and type 4 (timeouts).