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