]> git.sesse.net Git - ccbs/blob - bigscreen/ccbs_bigscreen.cpp
Make the bigscreen application support running groups on multiple machines.
[ccbs] / bigscreen / ccbs_bigscreen.cpp
1 #include <cstdio>
2 #include <cstring>
3 #include <iconv.h>
4 #include <unistd.h>
5 #include <pqxx/pqxx>
6 #include <tinyptc.h>
7 #include "flagtrigger.h"
8 #include "widestring.h"
9 #include "fetch_current_tournament.h"
10 #include "fetch_list_of_active_groups.h"
11 #include "fetch_group.h"
12 #include "fonts.h"
13 #include "groupscreen.h"
14 #include "splitscreen.h"
15 #include "rotatescreen.h"
16
17 Tournament active_tournament;
18 std::vector<SkeletonGroup> active_groups;
19 std::vector<GenericScreen *> screens;
20 GenericScreen *mainscreen = NULL;
21 unsigned char framebuf[800 * 600 * 4], screenbuf[800 * 600 * 4];
22
23 void init(pqxx::connection &conn)
24 {
25         if (screens.size() == 0 || mainscreen != screens[0])
26                 delete mainscreen;
27         
28         for (std::vector<GenericScreen *>::const_iterator i = screens.begin(); i != screens.end(); ++i) {
29                 delete *i;
30         }
31         screens.erase(screens.begin(), screens.end());
32
33         conn.perform(FetchCurrentTournament(&active_tournament));
34         conn.perform(FetchListOfActiveGroups(&active_groups));
35
36         if (active_tournament.id == -1) {
37                 std::fprintf(stderr, "No active tournament\n");
38         } else {
39                 std::fprintf(stderr, "Current tournament is %d\n", active_tournament.id);
40
41                 for (std::vector<SkeletonGroup>::const_iterator i = active_groups.begin(); i != active_groups.end(); ++i) {
42                         std::fprintf(stderr, "tourn: %u  round: %u  parallel: %u  num_machines: %u\n",
43                                 i->tournament, i->round, i->parallel, i->num_machines);
44
45                         // memory leaks here?
46                         for (unsigned j = 0; j < i->num_machines; ++j) {
47                                 RotateScreen *rs = new RotateScreen();
48                                 screens.push_back(rs);
49                                 rs->add_screen(new GroupScreen(conn, i->tournament, i->round, i->parallel, j, i->num_machines));
50                         }
51                 }
52         }
53
54         // hack
55         screens.push_back(NULL);
56         screens.push_back(NULL);
57         screens.push_back(NULL);
58         screens.push_back(NULL);
59
60         if (screens[1] == NULL) {
61                 mainscreen = screens[0];
62         } else {
63                 mainscreen = new SplitScreen(screens[0], screens[1], screens[2], screens[3]);
64         }
65 }
66
67 void main_loop(pqxx::connection &conn)
68 {
69         if (active_tournament.id == -1) {
70                 // No active tournament, sleep a second or so and exit
71                 conn.await_notification(1, 0);
72                 return;
73         }
74
75         if (mainscreen && mainscreen->check_invalidated()) {
76                 mainscreen->draw(framebuf);
77                 ptc_update(framebuf);
78                 conn.await_notification(0, 10000);
79         } else {
80                 ptc_update(framebuf);
81                 conn.await_notification(0, 200000);
82         }
83 }
84
85 int main(int argc, char **argv)
86 {
87         ptc_open("CCBS bigscreen", 800, 600);
88         
89         try {
90                 init_freetype();
91                 pqxx::connection conn("dbname=ccbs host=altersex.samfundet.no user=ccbs password=GeT|>>B_");
92                 FlagTrigger tournament_changed(conn, "active_tournament");
93                 FlagTrigger rounds_changed(conn, "active_groups");
94                 
95                 // when active_tournament or active_rounds is changed, we destroy everything and start from scratch
96                 // (at least currently)
97                 for ( ;; ) {
98                         tournament_changed.reset_flag();
99                         rounds_changed.reset_flag();
100                         init(conn);
101                         do {
102                                 main_loop(conn);
103                                 conn.get_notifs();
104                         } while (!tournament_changed.get_flag() && !rounds_changed.get_flag());
105                         std::fprintf(stderr, "active_tournament or active_groups changed, resetting...\n");
106                 }
107         } catch (const std::exception &e) {
108                 std::fprintf(stderr, "Exception: %s\n", e.what());
109                 exit(1);
110         }
111         
112         return 0;
113 }