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, std::vector<FT_Face> &fontlist);
61 Tournament active_tournament;
62 std::vector<FT_Face> fonts;
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, fonts);
145 // std::fprintf(stderr, "%s\n", i["title"].c_str());
149 ptc_update(framebuf);
157 if (FT_Init_FreeType(&library))
158 throw std::runtime_error("FreeType init failed.");
161 if (FT_New_Face(library, "/usr/share/fonts/truetype/msttcorefonts/Georgia.ttf", 0, &face))
162 throw std::runtime_error("Face opening failed.");
163 if (FT_Set_Char_Size(face, 0, 12 * 64, 96, 96))
164 throw std::runtime_error("Size set failed.");
165 fonts.push_back(face);
168 if (FT_New_Face(library, "/usr/share/fonts/truetype/freefont/FreeSerif.ttf", 0, &face)) {
169 std::fprintf(stderr, "Warning: Couldn't open FreeSerif, some glyphs might not be available\n");
171 if (FT_Set_Char_Size(face, 0, 12 * 64, 96, 96))
172 throw std::runtime_error("Size set failed.");
173 fonts.push_back(face);
177 if (FT_New_Face(library, "arialuni.ttf", 0, &face)) {
178 std::fprintf(stderr, "Warning: Couldn't open Arial Unicode MS, some glyphs might not be available\n");
180 if (FT_Set_Char_Size(face, 0, 12 * 64, 96, 96))
181 throw std::runtime_error("Size set failed.");
182 fonts.push_back(face);
186 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)
191 for (widestring::const_iterator i = str.begin(); i != str.end(); ++i) {
193 for (std::vector<FT_Face>::const_iterator j = fontlist.begin(); j != fontlist.end(); ++j) {
194 glyph_index = FT_Get_Char_Index(*j, *i);
195 if (glyph_index == 0)
198 if (FT_Load_Glyph(*j, glyph_index, FT_LOAD_RENDER))
199 throw std::runtime_error("Couldn't load glyph");
203 if (glyph_index == 0) {
204 std::fprintf(stderr, "Warning: Could not find a glyph in any font for U+%x, ignoring\n", *i);
210 FT_Bitmap *bm = &(slot->bitmap);
211 for (y = 0; y < bm->rows; y++) {
213 int dsty = ypos - slot->bitmap_top + y;
214 if (dsty < 0 || dsty > 599) continue;
216 unsigned char *dst = buf + dsty * 800*4 + (x + xpos + slot->bitmap_left)*4;
217 unsigned char *src = bm->buffer + y * bm->width;
218 for (xx = 0; xx < bm->width; xx++) {
219 *dst = (*dst * (256-*src) + r * *src) >> 8;
221 *dst = (*dst * (256-*src) + g * *src) >> 8;
223 *dst = (*dst * (256-*src) + b * *src) >> 8;
231 x += slot->advance.x >> 6;
238 int main(int argc, char **argv)
240 #if __BYTE_ORDER == __LITTLE_ENDIAN
241 ucs4_iconv = iconv_open("ucs-4le", "utf-8");
243 ucs4_iconv = iconv_open("ucs-4be", "utf-8");
246 ptc_open("CCBS bigscreen", 800, 600);
250 pqxx::connection conn("dbname=ccbs host=altersex.samfundet.no user=ccbs password=GeT|>>B_");
251 FlagTrigger tournament_changed(conn, "active_tournament");
253 // when active_tournament is changed, we destroy everything and start from scratch
255 tournament_changed.reset_flag();
260 } while (!tournament_changed.get_flag());
261 std::fprintf(stderr, "active_tournament changed, resetting...\n");
263 } catch (const std::exception &e) {
264 std::fprintf(stderr, "Exception: %s\n", e.what());