10 #include "flagtrigger.h"
14 // UCS-4 string with support for getting from UTF-8
15 class widestring : public std::wstring
18 void operator= (const char *from)
20 unsigned bytes = std::strlen(from);
21 char *from_buf = strdup(from);
22 wchar_t *to_buf = new wchar_t[bytes + 1];
24 char *inptr = from_buf, *outptr = reinterpret_cast<char *> (to_buf);
26 size_t in_left = bytes;
27 size_t out_left = bytes * sizeof(wchar_t);
29 size_t ret = iconv(ucs4_iconv, NULL, NULL, &outptr, &out_left);
30 if (ret == (size_t)(-1)) {
31 throw std::runtime_error("Error in iconv during initialization");
34 ret = iconv(ucs4_iconv, &inptr, &in_left, &outptr, &out_left);
35 if (ret == (size_t)(-1)) {
37 throw std::runtime_error("Error in iconv during conversion");
40 erase(begin(), end());
41 std::copy(to_buf, reinterpret_cast<wchar_t *> (outptr), std::back_inserter(*this));
49 void pqxx::from_string<widestring>(const char *from, widestring &to)
54 int my_draw_text(const widestring &str, unsigned char *buf, int xpos, int ypos, bool real_render, int r, int g, int b, std::vector<FT_Face> &fontlist);
62 Tournament active_tournament;
63 std::vector<FT_Face> fonts;
65 /* A transactor that fetches the current tournament and some information about it. */
66 class FetchCurrentTournament : public pqxx::transactor<> {
71 FetchCurrentTournament(Tournament *tourn) : tourn(tourn) {}
72 void operator() (pqxx::transaction<> &t)
74 pqxx::result res( t.exec("SELECT * FROM bigscreen.active_tournament NATURAL JOIN tournaments") );
76 pqxx::result::tuple tournament = res.at(0);
78 tourn->id = tournament["tournament"].as(tourn->id);
79 tourn->name = tournament["tournamentname"].as(tourn->name);
80 } catch (PGSTD::out_of_range &e) {
87 void init(pqxx::connection &conn)
89 conn.perform(FetchCurrentTournament(&active_tournament));
91 if (active_tournament.id == -1) {
92 std::fprintf(stderr, "No active tournament\n");
94 std::fprintf(stderr, "Current tournament is %d (name: '%s')\n",
95 active_tournament.id, active_tournament.name.c_str());
99 unsigned char framebuf[800 * 600 * 4];
101 void main_loop(pqxx::connection &conn)
103 if (active_tournament.id == -1) {
104 // No active tournament, sleep a second or so and exit
109 memset(framebuf, 0, 800*600*4);
111 pqxx::work t(conn, "trx");
114 pqxx::result res( t.exec("SELECT * FROM songs WHERE title LIKE 'M%'") );
116 for (pqxx::result::const_iterator i = res.begin(); i != res.end(); ++i) {
117 my_draw_text(i["title"].as(widestring()), framebuf, 0, y, 1, 255, 255, 255, fonts);
119 // std::fprintf(stderr, "%s\n", i["title"].c_str());
123 ptc_update(framebuf);
131 if (FT_Init_FreeType(&library))
132 throw std::runtime_error("FreeType init failed.");
135 if (FT_New_Face(library, "/usr/share/fonts/truetype/msttcorefonts/Georgia.ttf", 0, &face))
136 throw std::runtime_error("Face opening failed.");
137 if (FT_Set_Char_Size(face, 0, 12 * 64, 96, 96))
138 throw std::runtime_error("Size set failed.");
139 fonts.push_back(face);
142 if (FT_New_Face(library, "/usr/share/fonts/truetype/freefont/FreeSerif.ttf", 0, &face)) {
143 std::fprintf(stderr, "Warning: Couldn't open FreeSerif, some glyphs might not be available\n");
145 if (FT_Set_Char_Size(face, 0, 12 * 64, 96, 96))
146 throw std::runtime_error("Size set failed.");
147 fonts.push_back(face);
151 if (FT_New_Face(library, "arialuni.ttf", 0, &face)) {
152 std::fprintf(stderr, "Warning: Couldn't open Arial Unicode MS, some glyphs might not be available\n");
154 if (FT_Set_Char_Size(face, 0, 12 * 64, 96, 96))
155 throw std::runtime_error("Size set failed.");
156 fonts.push_back(face);
160 int my_draw_text(const widestring &str, unsigned char *buf, int xpos, int ypos, bool real_render, int r, int g, int b, std::vector<FT_Face> &fontlist)
165 for (widestring::const_iterator i = str.begin(); i != str.end(); ++i) {
167 for (std::vector<FT_Face>::const_iterator j = fontlist.begin(); j != fontlist.end(); ++j) {
168 glyph_index = FT_Get_Char_Index(*j, *i);
169 if (glyph_index == 0)
172 if (FT_Load_Glyph(*j, glyph_index, FT_LOAD_RENDER))
173 throw std::runtime_error("Couldn't load glyph");
177 if (glyph_index == 0) {
178 std::fprintf(stderr, "Warning: Could not find a glyph in any font for U+%x, ignoring\n", *i);
184 FT_Bitmap *bm = &(slot->bitmap);
185 for (y = 0; y < bm->rows; y++) {
187 int dsty = ypos - slot->bitmap_top + y;
188 if (dsty < 0 || dsty > 599) continue;
190 unsigned char *dst = buf + dsty * 800*4 + (x + xpos + slot->bitmap_left)*4;
191 unsigned char *src = bm->buffer + y * bm->width;
192 for (xx = 0; xx < bm->width; xx++) {
193 *dst = (*dst * (256-*src) + r * *src) >> 8;
195 *dst = (*dst * (256-*src) + g * *src) >> 8;
197 *dst = (*dst * (256-*src) + b * *src) >> 8;
205 x += slot->advance.x >> 6;
212 int main(int argc, char **argv)
214 #if __BYTE_ORDER == __LITTLE_ENDIAN
215 ucs4_iconv = iconv_open("ucs-4le", "utf-8");
217 ucs4_iconv = iconv_open("ucs-4be", "utf-8");
220 ptc_open("CCBS bigscreen", 800, 600);
224 pqxx::connection conn("dbname=ccbs host=altersex.samfundet.no user=ccbs password=GeT|>>B_");
225 FlagTrigger tournament_changed(conn, "active_tournament");
226 FlagTrigger rounds_changed(conn, "active_groups");
228 // when active_tournament or active_rounds is changed, we destroy everything and start from scratch
229 // (at least currently)
231 tournament_changed.reset_flag();
232 rounds_changed.reset_flag();
237 } while (!tournament_changed.get_flag() && !rounds_changed.get_flag());
238 std::fprintf(stderr, "active_tournament or active_groups changed, resetting...\n");
240 } catch (const std::exception &e) {
241 std::fprintf(stderr, "Exception: %s\n", e.what());