Fetch the list of current active rounds and display it.
[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 "fonts.h"
12
13 Tournament active_tournament;
14 std::vector<Group> active_groups;
15 unsigned char framebuf[800 * 600 * 4];
16
17 void init(pqxx::connection &conn)
18 {
19         conn.perform(FetchCurrentTournament(&active_tournament));
20         conn.perform(FetchListOfActiveGroups(&active_groups));
21
22         if (active_tournament.id == -1) {
23                 std::fprintf(stderr, "No active tournament\n");
24         } else {
25                 std::fprintf(stderr, "Current tournament is %d\n", active_tournament.id);
26
27                 for (std::vector<Group>::const_iterator i = active_groups.begin(); i != active_groups.end(); ++i) {
28                         std::fprintf(stderr, "tourn: %u  round: %u   parallel: %u\n",
29                                 i->tournament, i->round, i->parallel);
30                 }
31         }
32 }
33
34 void main_loop(pqxx::connection &conn)
35 {
36         if (active_tournament.id == -1) {
37                 // No active tournament, sleep a second or so and exit
38                 sleep(1);
39                 return;
40         }
41
42         memset(framebuf, 0, 800*600*4);
43         
44         pqxx::work t(conn, "trx");
45
46         // fetch all songs
47         pqxx::result res( t.exec("SELECT * FROM songs WHERE title LIKE 'M%'") );
48         unsigned y = 0;
49         for (pqxx::result::const_iterator i = res.begin(); i != res.end(); ++i) {
50                 my_draw_text(i["title"].as(widestring()), framebuf, 0, y, 1, 255, 255, 255);
51                 y += 20;
52 //              std::fprintf(stderr, "%s\n", i["title"].c_str());
53         }
54         t.commit();
55
56         ptc_update(framebuf);
57         sleep(1);
58 }
59
60 int main(int argc, char **argv)
61 {
62         ptc_open("CCBS bigscreen", 800, 600);
63         
64         try {
65                 init_freetype();
66                 pqxx::connection conn("dbname=ccbs host=altersex.samfundet.no user=ccbs password=GeT|>>B_");
67                 FlagTrigger tournament_changed(conn, "active_tournament");
68                 FlagTrigger rounds_changed(conn, "active_groups");
69                 
70                 // when active_tournament or active_rounds is changed, we destroy everything and start from scratch
71                 // (at least currently)
72                 for ( ;; ) {
73                         tournament_changed.reset_flag();
74                         rounds_changed.reset_flag();
75                         init(conn);
76                         do {
77                                 main_loop(conn);
78                                 conn.get_notifs();
79                         } while (!tournament_changed.get_flag() && !rounds_changed.get_flag());
80                         std::fprintf(stderr, "active_tournament or active_groups changed, resetting...\n");
81                 }
82         } catch (const std::exception &e) {
83                 std::fprintf(stderr, "Exception: %s\n", e.what());
84                 exit(1);
85         }
86         
87         return 0;
88 }