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