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