Re-add OpenGL window, we commented it out and forgot it that way.
[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 "glwindow.h"
7
8 iconv_t ucs4_iconv;
9
10 // UCS-4 string
11 class widestring : public std::basic_string<unsigned>
12 {
13 public:
14         void operator= (const char *from)
15         {
16                 unsigned bytes = std::strlen(from);
17                 char *from_buf = strdup(from);
18                 unsigned *to_buf = new unsigned[bytes + 1];
19
20                 char *inptr = from_buf, *outptr = reinterpret_cast<char *> (to_buf);
21
22                 size_t in_left = bytes;
23                 size_t out_left = bytes * sizeof(unsigned);
24
25                 size_t ret = iconv(ucs4_iconv, NULL, NULL, &outptr, &out_left);
26                 if (ret == (size_t)(-1)) {
27                         throw std::runtime_error("Error in iconv during initialization");
28                 }
29
30                 ret = iconv(ucs4_iconv, &inptr, &in_left, &outptr, &out_left);
31                 if (ret == (size_t)(-1)) {
32                         perror("iconv");
33                         throw std::runtime_error("Error in iconv during conversion");
34                 }
35
36                 erase(begin(), end());
37                 std::copy(to_buf, reinterpret_cast<unsigned *> (outptr), std::back_inserter(*this));
38
39                 free(from_buf);
40                 delete[] to_buf;
41         }
42 };
43
44 template<>
45 void std::char_traits<unsigned>::assign(unsigned &to, unsigned const &from)
46 {
47         to = from;
48 }
49
50 template<>
51 unsigned *std::char_traits<unsigned>::copy(unsigned *to, unsigned const *from, unsigned n)
52 {
53         return static_cast<unsigned *>(memcpy(to, from, n * sizeof(unsigned)));
54 }
55
56 template<>
57 unsigned *std::char_traits<unsigned>::move(unsigned *to, unsigned const *from, unsigned n)
58 {
59         return static_cast<unsigned *>(memmove(to, from, n * sizeof(unsigned)));
60 }
61
62 template<>
63 unsigned *std::char_traits<unsigned>::assign(unsigned *to, size_t n, unsigned a)
64 {
65         for (unsigned i = 0; i < n; ++i)
66                 *to++ = a;
67         return to;
68 }
69
70
71 template<>
72 void pqxx::from_string<widestring>(const char *from, widestring &to)
73 {
74         to = from;
75 }
76
77 class Tournament {
78 public:
79         int id;
80         widestring name;
81 };
82
83 Tournament active_tournament;
84
85 /* A trigger that sets a flag whenever it's trigged. */
86 class FlagTrigger : pqxx::trigger {
87 private:
88         bool flag;
89         
90 public:
91         FlagTrigger(pqxx::connection_base &conn, const PGSTD::string &name)
92                 : pqxx::trigger(conn, name), flag(false) {}
93         virtual ~FlagTrigger() throw () {}
94         
95         virtual void operator() (int pid)
96         {
97                 flag = true;
98                 std::fprintf(stderr, "Received a flag trigger from pid %u\n", pid);
99         }
100
101         bool get_flag() const
102         {
103                 return flag;
104         }
105
106         void reset_flag()
107         {
108                 flag = false;
109         }
110 };
111
112 /* A transactor that fetches the current tournament and some information about it. */
113 class FetchCurrentTournament : public pqxx::transactor<> {
114 private:
115         Tournament *tourn;
116
117 public:
118         FetchCurrentTournament(Tournament *tourn) : tourn(tourn) {}
119         void operator() (pqxx::transaction<> &t)
120         {
121                 pqxx::result res( t.exec("SELECT * FROM bigscreen.active_tournament NATURAL JOIN tournaments") );
122                 try {
123                         pqxx::result::tuple tournament = res.at(0);
124
125                         tourn->id = tournament["tournament"].as(tourn->id);
126                         tourn->name = tournament["tournamentname"].as(tourn->name);
127                 } catch (PGSTD::out_of_range &e) {
128                         tourn->id = -1;
129                         tourn->name = "";
130                 }
131         }
132 };
133
134 void init(pqxx::connection &conn)
135 {
136         conn.perform(FetchCurrentTournament(&active_tournament));
137
138         if (active_tournament.id == -1) {
139                 std::fprintf(stderr, "No active tournament\n");
140         } else {
141                 std::fprintf(stderr, "Current tournament is %d (name: '%s')\n",
142                         active_tournament.id, active_tournament.name.c_str());
143         }
144 }
145
146 void main_loop(pqxx::connection &conn)
147 {
148         if (active_tournament.id == -1) {
149                 // No active tournament, sleep a second or so and exit
150                 sleep(1);
151                 return;
152         }
153         
154         pqxx::work t(conn, "trx");
155
156         // fetch all songs
157         pqxx::result res( t.exec("SELECT * FROM songs") );
158         for (pqxx::result::const_iterator i = res.begin(); i != res.end(); ++i) {
159                 std::fprintf(stderr, "%s\n", i["title"].c_str());
160         }
161         t.commit();
162         
163         sleep(1);
164 }
165
166 int main(int argc, char **argv)
167 {
168         ucs4_iconv = iconv_open("ucs-4", "utf-8");
169         
170         GLWindow glw("CCBS bigscreen", 800, 600, 32, false, 16, -1);
171         try {
172                 pqxx::connection conn("dbname=ccbs host=altersex.samfundet.no user=ccbs password=GeT|>>B_");
173                 FlagTrigger tournament_changed(conn, "active_tournament");
174                 
175                 // when active_tournament is changed, we destroy everything and start from scratch
176                 for ( ;; ) {
177                         tournament_changed.reset_flag();
178                         init(conn);
179                         do {
180                                 main_loop(conn);
181                                 conn.get_notifs();
182                         } while (!tournament_changed.get_flag());
183                         std::fprintf(stderr, "active_tournament changed, resetting...\n");
184                 }
185         } catch (const std::exception &e) {
186                 std::fprintf(stderr, "Exception: %s\n", e.what());
187                 exit(1);
188         }
189         
190         return 0;
191 }