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