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