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