13 class widestring : public std::basic_string<unsigned>
16 void operator= (const char *from)
18 unsigned bytes = std::strlen(from);
19 char *from_buf = strdup(from);
20 unsigned *to_buf = new unsigned[bytes + 1];
22 char *inptr = from_buf, *outptr = reinterpret_cast<char *> (to_buf);
24 size_t in_left = bytes;
25 size_t out_left = bytes * sizeof(unsigned);
27 size_t ret = iconv(ucs4_iconv, NULL, NULL, &outptr, &out_left);
28 if (ret == (size_t)(-1)) {
29 throw std::runtime_error("Error in iconv during initialization");
32 ret = iconv(ucs4_iconv, &inptr, &in_left, &outptr, &out_left);
33 if (ret == (size_t)(-1)) {
35 throw std::runtime_error("Error in iconv during conversion");
38 erase(begin(), end());
39 std::copy(to_buf, reinterpret_cast<unsigned *> (outptr), std::back_inserter(*this));
47 void std::char_traits<unsigned>::assign(unsigned &to, unsigned const &from)
53 unsigned *std::char_traits<unsigned>::copy(unsigned *to, unsigned const *from, unsigned n)
55 return static_cast<unsigned *>(memcpy(to, from, n * sizeof(unsigned)));
59 unsigned *std::char_traits<unsigned>::move(unsigned *to, unsigned const *from, unsigned n)
61 return static_cast<unsigned *>(memmove(to, from, n * sizeof(unsigned)));
65 unsigned *std::char_traits<unsigned>::assign(unsigned *to, size_t n, unsigned a)
67 for (unsigned i = 0; i < n; ++i)
74 void pqxx::from_string<widestring>(const char *from, widestring &to)
79 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);
87 Tournament active_tournament;
90 /* A trigger that sets a flag whenever it's trigged. */
91 class FlagTrigger : pqxx::trigger {
96 FlagTrigger(pqxx::connection_base &conn, const PGSTD::string &name)
97 : pqxx::trigger(conn, name), flag(false) {}
98 virtual ~FlagTrigger() throw () {}
100 virtual void operator() (int pid)
103 std::fprintf(stderr, "Received a flag trigger from pid %u\n", pid);
106 bool get_flag() const
117 /* A transactor that fetches the current tournament and some information about it. */
118 class FetchCurrentTournament : public pqxx::transactor<> {
123 FetchCurrentTournament(Tournament *tourn) : tourn(tourn) {}
124 void operator() (pqxx::transaction<> &t)
126 pqxx::result res( t.exec("SELECT * FROM bigscreen.active_tournament NATURAL JOIN tournaments") );
128 pqxx::result::tuple tournament = res.at(0);
130 tourn->id = tournament["tournament"].as(tourn->id);
131 tourn->name = tournament["tournamentname"].as(tourn->name);
132 } catch (PGSTD::out_of_range &e) {
139 void init(pqxx::connection &conn)
141 conn.perform(FetchCurrentTournament(&active_tournament));
143 if (active_tournament.id == -1) {
144 std::fprintf(stderr, "No active tournament\n");
146 std::fprintf(stderr, "Current tournament is %d (name: '%s')\n",
147 active_tournament.id, active_tournament.name.c_str());
151 unsigned char framebuf[800 * 600 * 4];
153 void main_loop(pqxx::connection &conn)
155 if (active_tournament.id == -1) {
156 // No active tournament, sleep a second or so and exit
161 memset(framebuf, 0, 800*600*4);
163 pqxx::work t(conn, "trx");
166 pqxx::result res( t.exec("SELECT * FROM songs WHERE title LIKE 'M%'") );
168 for (pqxx::result::const_iterator i = res.begin(); i != res.end(); ++i) {
169 my_draw_text(i["title"].as(widestring()), framebuf, 0, y, 1, 255, 255, 255, font);
171 // std::fprintf(stderr, "%s\n", i["title"].c_str());
175 ptc_update(framebuf);
182 if (FT_Init_FreeType(&library))
183 throw std::logic_error("FreeType init failed.");
184 if (FT_New_Face(library, "/usr/share/fonts/truetype/msttcorefonts/Georgia.ttf", 0, &font))
185 throw std::logic_error("Face opening failed.");
186 if (FT_Set_Char_Size(font, 0, 12 * 64, 96, 96))
187 throw std::logic_error("Size set failed.");
190 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)
192 FT_GlyphSlot slot = face->glyph;
195 for (widestring::const_iterator i = str.begin(); i != str.end(); ++i) {
196 int glyph_index = FT_Get_Char_Index(face, *i);
198 printf("Loading U+%x\n", *i);
200 if (FT_Load_Glyph(face, glyph_index, FT_LOAD_RENDER))
205 FT_Bitmap *bm = &(slot->bitmap);
206 for (y = 0; y < bm->rows; y++) {
208 int dsty = ypos - slot->bitmap_top + y;
209 if (dsty < 0 || dsty > 599) continue;
211 unsigned char *dst = buf + dsty * 800*4 + (x + xpos + slot->bitmap_left)*4;
212 unsigned char *src = bm->buffer + y * bm->width;
213 for (xx = 0; xx < bm->width; xx++) {
214 *dst = (*dst * (256-*src) + r * *src) >> 8;
216 *dst = (*dst * (256-*src) + g * *src) >> 8;
218 *dst = (*dst * (256-*src) + b * *src) >> 8;
226 x += slot->advance.x >> 6;
233 int main(int argc, char **argv)
235 ucs4_iconv = iconv_open("ucs-4le", "utf-8"); // FIXME: will be broken for big endian!
237 ptc_open("CCBS bigscreen", 800, 600);
241 pqxx::connection conn("dbname=ccbs host=altersex.samfundet.no user=ccbs password=GeT|>>B_");
242 FlagTrigger tournament_changed(conn, "active_tournament");
244 // when active_tournament is changed, we destroy everything and start from scratch
246 tournament_changed.reset_flag();
251 } while (!tournament_changed.get_flag());
252 std::fprintf(stderr, "active_tournament changed, resetting...\n");
254 } catch (const std::exception &e) {
255 std::fprintf(stderr, "Exception: %s\n", e.what());