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