13 // UCS-4 string with support for getting from UTF-8
14 class widestring : public std::wstring
17 void operator= (const char *from)
19 unsigned bytes = std::strlen(from);
20 char *from_buf = strdup(from);
21 wchar_t *to_buf = new wchar_t[bytes + 1];
23 char *inptr = from_buf, *outptr = reinterpret_cast<char *> (to_buf);
25 size_t in_left = bytes;
26 size_t out_left = bytes * sizeof(wchar_t);
28 size_t ret = iconv(ucs4_iconv, NULL, NULL, &outptr, &out_left);
29 if (ret == (size_t)(-1)) {
30 throw std::runtime_error("Error in iconv during initialization");
33 ret = iconv(ucs4_iconv, &inptr, &in_left, &outptr, &out_left);
34 if (ret == (size_t)(-1)) {
36 throw std::runtime_error("Error in iconv during conversion");
39 erase(begin(), end());
40 std::copy(to_buf, reinterpret_cast<wchar_t *> (outptr), std::back_inserter(*this));
48 void pqxx::from_string<widestring>(const char *from, widestring &to)
53 int my_draw_text(const widestring &str, unsigned char *buf, int xpos, int ypos, bool real_render, int r, int g, int b, FT_Face face, FT_Face symbolface);
61 Tournament active_tournament;
62 FT_Face font, symbolfont;
64 /* A trigger that sets a flag whenever it's trigged. */
65 class FlagTrigger : pqxx::trigger {
70 FlagTrigger(pqxx::connection_base &conn, const PGSTD::string &name)
71 : pqxx::trigger(conn, name), flag(false) {}
72 virtual ~FlagTrigger() throw () {}
74 virtual void operator() (int pid)
77 std::fprintf(stderr, "Received a flag trigger from pid %u\n", pid);
91 /* A transactor that fetches the current tournament and some information about it. */
92 class FetchCurrentTournament : public pqxx::transactor<> {
97 FetchCurrentTournament(Tournament *tourn) : tourn(tourn) {}
98 void operator() (pqxx::transaction<> &t)
100 pqxx::result res( t.exec("SELECT * FROM bigscreen.active_tournament NATURAL JOIN tournaments") );
102 pqxx::result::tuple tournament = res.at(0);
104 tourn->id = tournament["tournament"].as(tourn->id);
105 tourn->name = tournament["tournamentname"].as(tourn->name);
106 } catch (PGSTD::out_of_range &e) {
113 void init(pqxx::connection &conn)
115 conn.perform(FetchCurrentTournament(&active_tournament));
117 if (active_tournament.id == -1) {
118 std::fprintf(stderr, "No active tournament\n");
120 std::fprintf(stderr, "Current tournament is %d (name: '%s')\n",
121 active_tournament.id, active_tournament.name.c_str());
125 unsigned char framebuf[800 * 600 * 4];
127 void main_loop(pqxx::connection &conn)
129 if (active_tournament.id == -1) {
130 // No active tournament, sleep a second or so and exit
135 memset(framebuf, 0, 800*600*4);
137 pqxx::work t(conn, "trx");
140 pqxx::result res( t.exec("SELECT * FROM songs WHERE title LIKE 'M%'") );
142 for (pqxx::result::const_iterator i = res.begin(); i != res.end(); ++i) {
143 my_draw_text(i["title"].as(widestring()), framebuf, 0, y, 1, 255, 255, 255, font, symbolfont);
145 // std::fprintf(stderr, "%s\n", i["title"].c_str());
149 ptc_update(framebuf);
156 if (FT_Init_FreeType(&library))
157 throw std::runtime_error("FreeType init failed.");
158 if (FT_New_Face(library, "/usr/share/fonts/truetype/msttcorefonts/Georgia.ttf", 0, &font))
159 throw std::runtime_error("Face opening failed.");
160 if (FT_New_Face(library, "/usr/share/fonts/truetype/freefont/FreeSerif.ttf", 0, &symbolfont))
161 throw std::runtime_error("Face opening failed.");
162 if (FT_Set_Char_Size(font, 0, 12 * 64, 96, 96))
163 throw std::runtime_error("Size set failed.");
164 if (FT_Set_Char_Size(symbolfont, 0, 12 * 64, 96, 96))
165 throw std::runtime_error("Size set failed.");
168 int my_draw_text(const widestring &str, unsigned char *buf, int xpos, int ypos, bool real_render, int r, int g, int b, FT_Face face, FT_Face symbolface)
173 for (widestring::const_iterator i = str.begin(); i != str.end(); ++i) {
174 // try the normal font first, fall back if there's some special character
175 int glyph_index = FT_Get_Char_Index(face, *i);
176 if (glyph_index == 0) {
177 std::fprintf(stderr, "Couldn't find U+%x in primary face, falling back to symbol face\n",
179 glyph_index = FT_Get_Char_Index(symbolface, *i);
180 if (glyph_index == 0) {
181 std::fprintf(stderr, "Couldn't find U+%x in symbol face, ignoring\n", *i);
184 if (FT_Load_Glyph(symbolface, glyph_index, FT_LOAD_RENDER))
185 throw std::runtime_error("Couldn't load glyph from symbol face");
186 slot = symbolface->glyph;
188 if (FT_Load_Glyph(face, glyph_index, FT_LOAD_RENDER))
189 throw std::runtime_error("Couldn't load glyph from primary face");
195 FT_Bitmap *bm = &(slot->bitmap);
196 for (y = 0; y < bm->rows; y++) {
198 int dsty = ypos - slot->bitmap_top + y;
199 if (dsty < 0 || dsty > 599) continue;
201 unsigned char *dst = buf + dsty * 800*4 + (x + xpos + slot->bitmap_left)*4;
202 unsigned char *src = bm->buffer + y * bm->width;
203 for (xx = 0; xx < bm->width; xx++) {
204 *dst = (*dst * (256-*src) + r * *src) >> 8;
206 *dst = (*dst * (256-*src) + g * *src) >> 8;
208 *dst = (*dst * (256-*src) + b * *src) >> 8;
216 x += slot->advance.x >> 6;
223 int main(int argc, char **argv)
225 #if __BYTE_ORDER == __LITTLE_ENDIAN
226 ucs4_iconv = iconv_open("ucs-4le", "utf-8");
228 ucs4_iconv = iconv_open("ucs-4be", "utf-8");
231 ptc_open("CCBS bigscreen", 800, 600);
235 pqxx::connection conn("dbname=ccbs host=altersex.samfundet.no user=ccbs password=GeT|>>B_");
236 FlagTrigger tournament_changed(conn, "active_tournament");
238 // when active_tournament is changed, we destroy everything and start from scratch
240 tournament_changed.reset_flag();
245 } while (!tournament_changed.get_flag());
246 std::fprintf(stderr, "active_tournament changed, resetting...\n");
248 } catch (const std::exception &e) {
249 std::fprintf(stderr, "Exception: %s\n", e.what());