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 pqxx::work t(conn, "trx");
164 pqxx::result res( t.exec("SELECT * FROM songs") );
166 for (pqxx::result::const_iterator i = res.begin(); i != res.end(); ++i) {
167 my_draw_text(i["title"].as(widestring()), framebuf, 0, y, 1, 255, 255, 255, font);
169 std::fprintf(stderr, "%s\n", i["title"].c_str());
173 ptc_update(framebuf);
180 if (FT_Init_FreeType(&library))
181 throw std::logic_error("FreeType init failed.");
182 if (FT_New_Face(library, "/usr/share/fonts/truetype/msttcorefonts/Georgia.ttf", 0, &font))
183 throw std::logic_error("Face opening failed.");
184 if (FT_Set_Char_Size(font, 0, 12 * 64, 96, 96))
185 throw std::logic_error("Size set failed.");
188 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)
190 FT_GlyphSlot slot = face->glyph;
193 for (widestring::const_iterator i = str.begin(); i != str.end(); ++i) {
194 int glyph_index = FT_Get_Char_Index(face, *i);
195 if (FT_Load_Glyph(face, glyph_index, FT_LOAD_RENDER))
200 FT_Bitmap *bm = &(slot->bitmap);
201 for (y = 0; y < bm->rows; y++) {
203 int dsty = ypos - slot->bitmap_top + y;
204 if (dsty < 0 || dsty > 599) continue;
206 unsigned char *dst = buf + dsty * 800*4 + (x + xpos + slot->bitmap_left)*4;
207 unsigned char *src = bm->buffer + y * bm->width;
208 for (xx = 0; xx < bm->width; xx++) {
209 *dst = (*dst * (256-*src) + r * *src) >> 8;
211 *dst = (*dst * (256-*src) + g * *src) >> 8;
213 *dst = (*dst * (256-*src) + b * *src) >> 8;
221 x += slot->advance.x >> 6;
228 int main(int argc, char **argv)
230 ucs4_iconv = iconv_open("ucs-4le", "utf-8"); // FIXME: will be broken for big endian!
232 ptc_open("CCBS bigscreen", 800, 600);
236 pqxx::connection conn("dbname=ccbs host=altersex.samfundet.no user=ccbs password=GeT|>>B_");
237 FlagTrigger tournament_changed(conn, "active_tournament");
239 // when active_tournament is changed, we destroy everything and start from scratch
241 tournament_changed.reset_flag();
246 } while (!tournament_changed.get_flag());
247 std::fprintf(stderr, "active_tournament changed, resetting...\n");
249 } catch (const std::exception &e) {
250 std::fprintf(stderr, "Exception: %s\n", e.what());