]> git.sesse.net Git - ccbs/blob - bigscreen/ccbs_bigscreen.cpp
Shape text using Pango and HarfBuzz; gives us nice ligatures and exotic scripts.
[ccbs] / bigscreen / ccbs_bigscreen.cpp
1 #include <cstdio>
2 #include <cstring>
3 #include <iconv.h>
4 #include <unistd.h>
5 #include <pqxx/pqxx>
6 #include <SDL.h>
7
8 #include "flagtrigger.h"
9 #include "widestring.h"
10 #include "fetch_current_tournament.h"
11 #include "fetch_list_of_active_groups.h"
12 #include "fetch_list_of_finished_groups.h"
13 #include "fetch_group.h"
14 #include "fetch_auxilliary_screens.h"
15 #include "fonts.h"
16 #include "theme.h"
17 #include "groupscreen.h"
18 #include "top10scorescreen.h"
19 #include "top5chosenscreen.h"
20 #include "splitscreen.h"
21 #include "rotatescreen.h"
22
23 SDL_Surface *screen = NULL;
24
25 Tournament active_tournament;
26 std::vector<SkeletonGroup> active_groups;
27 std::vector<GenericScreen *> screens;
28 GenericScreen *mainscreen = NULL;
29 unsigned char *framebuf, *screenbuf;
30 bool quit_requested = false;
31
32 void init(pqxx::connection &conn)
33 {
34         std::vector<widestring> aux_screens;
35         
36         if (screens.size() == 0 || mainscreen != screens[0])
37                 delete mainscreen;
38         
39         for (std::vector<GenericScreen *>::const_iterator i = screens.begin(); i != screens.end(); ++i) {
40                 delete *i;
41         }
42         screens.erase(screens.begin(), screens.end());
43
44 #if !USE_SPLITSCREEN
45         RotateScreen *rs = new RotateScreen();
46         mainscreen = rs;
47 #endif
48
49         conn.perform(FetchCurrentTournament(&active_tournament));
50         conn.perform(FetchListOfActiveGroups(&active_groups));
51
52         if (active_tournament.id == -1) {
53                 std::fprintf(stderr, "No active tournament\n");
54         } else {
55                 std::fprintf(stderr, "Current tournament is %d\n", active_tournament.id);
56
57                 for (std::vector<SkeletonGroup>::const_iterator i = active_groups.begin(); i != active_groups.end(); ++i) {
58                         std::fprintf(stderr, "tourn: %u  round: %u  parallel: %u  num_machines: %u\n",
59                                 i->tournament, i->round, i->parallel, i->num_machines);
60
61                         // memory leaks here?
62                         for (unsigned j = 0; j < i->num_machines; ++j) {
63 #if USE_SPLITSCREEN
64                                 RotateScreen *rs = new RotateScreen();
65                                 screens.push_back(rs);
66 #endif
67                                 rs->add_screen(new GroupScreen(conn, i->tournament, i->round, i->parallel, j, i->num_machines, i->players_per_machine));
68                         }
69                 }
70         }
71
72         bool show_only_main_screen = (USE_SPLITSCREEN && screens.size() == 1);
73
74         /*
75          * Show auxilliary screens except if we have too many already,
76          * or if we're in the special split-screen end-tournament mode,
77          * where there if only one.
78          */
79         RotateScreen *aux_screen = NULL;
80         if (screens.size() < 4 && !show_only_main_screen) {
81 #if USE_SPLITSCREEN
82                 RotateScreen *rs = new RotateScreen();
83                 screens.push_back(rs);
84 #endif
85                 aux_screen = rs;
86
87                 conn.perform(FetchAuxilliaryScreens(&aux_screens));
88                 for (std::vector<widestring>::const_iterator i = aux_screens.begin(); i != aux_screens.end(); ++i) {
89                         if (*i == widestring("top10scores")) {
90                                 rs->add_screen(new Top10ScoreScreen(conn, active_tournament.id));
91                                 continue;
92                         }
93                         if (*i == widestring("top5chosen")) {
94                                 rs->add_screen(new Top5ChosenScreen(conn, active_tournament.id));
95                                 continue;
96                         }
97                 }
98         }
99
100 #if USE_SPLITSCREEN
101         /*
102          * If we still have room, make yet another rotational screen with
103          * results from previous groups -- otherwise tack them onto the end
104          * of the auxilliary screens.
105          */
106         RotateScreen *finished_groups_screen;
107         if (show_only_main_screen) {
108                 finished_groups_screen = NULL;
109         } else if (screens.size() < 4) {
110                 finished_groups_screen = new RotateScreen();
111                 screens.push_back(finished_groups_screen);
112         } else {
113                 finished_groups_screen = aux_screen;
114         }
115         if (finished_groups_screen != NULL) {
116                 std::vector<SkeletonGroup> finished_groups;
117                 conn.perform(FetchListOfFinishedGroups(active_tournament.id, &finished_groups));
118
119                 for (std::vector<SkeletonGroup>::const_iterator i = finished_groups.begin(); i != finished_groups.end(); ++i) {
120                         finished_groups_screen->add_screen(new GroupScreen(conn, i->tournament, i->round, i->parallel, 0, 1, 1));
121                 }
122         }
123 #endif
124
125 #if USE_SPLITSCREEN
126         // hack
127         screens.push_back(NULL);
128         screens.push_back(NULL);
129         screens.push_back(NULL);
130         screens.push_back(NULL);
131
132         if (screens[1] == NULL) {
133                 mainscreen = screens[0];
134         } else {
135                 mainscreen = new SplitScreen(screens[0], screens[1], screens[2], screens[3]);
136         }
137 #endif
138 }
139
140 void main_loop(pqxx::connection &conn, unsigned screen_width, unsigned screen_height)
141 {
142         if (active_tournament.id == -1) {
143                 // No active tournament, sleep a second or so and exit
144                 conn.await_notification(1, 0);
145                 return;
146         }
147
148         if (mainscreen && mainscreen->check_invalidated()) {
149                 if (screen->pitch == screen_width * 4) {
150                         SDL_LockSurface(screen);
151                         mainscreen->draw((unsigned char *)screen->pixels, screen_width, screen_height);
152                         SDL_UnlockSurface(screen);
153                 } else {
154                         mainscreen->draw(framebuf, screen_width, screen_height);
155                         SDL_LockSurface(screen);
156                         for (unsigned y = 0; y < screen_height; ++y) {
157                                 unsigned char *sptr = framebuf + y * screen_width * 4;
158                                 unsigned char *dptr = (unsigned char *)screen->pixels + y * screen->pitch;
159                                 memcpy(dptr, sptr, screen_width * 4);
160                         }
161                         SDL_UnlockSurface(screen);
162                 }
163                 SDL_Flip(screen);
164                 conn.await_notification(0, 10000);
165         } else {
166                 SDL_Flip(screen);
167                 conn.await_notification(0, 200000);
168         }
169 }
170
171 void handle_events()
172 {
173         SDL_Event event;
174         while (SDL_PollEvent(&event)) {
175                 if (event.type == SDL_QUIT) {
176                         quit_requested = true;
177                 }
178                 if (event.type == SDL_KEYUP && event.key.keysym.sym == SDLK_ESCAPE) {
179                         quit_requested = true;
180                 }
181         }
182 }
183
184 int main(int argc, char **argv)
185 {
186         init_theme();
187         unsigned screen_width = atoi(get_theme_config("screen", "width").c_str());
188         unsigned screen_height = atoi(get_theme_config("screen", "height").c_str());
189         bool use_lcd = atoi(get_theme_config("screen", "fullscreen").c_str());
190
191         SDL_Init(SDL_INIT_VIDEO);
192         if (use_lcd) {
193                 screen = SDL_SetVideoMode(screen_width, screen_height, 32, SDL_DOUBLEBUF | SDL_FULLSCREEN);
194                 SDL_ShowCursor(SDL_DISABLE);
195         } else {
196                 screen = SDL_SetVideoMode(screen_width, screen_height, 32, SDL_DOUBLEBUF);
197         }
198         if (screen == NULL) {
199                 fprintf(stderr, "Video initialization failed: %s\n", SDL_GetError());
200                 exit(1);
201         }
202
203         framebuf = new unsigned char[screen_width * screen_height * 4];
204         screenbuf = new unsigned char[screen_width * screen_height * 4];
205         
206         try {
207                 init_freetype();
208                 set_screen_size(screen_width, screen_height);
209
210                 char connstr[1024];
211                 snprintf(connstr, sizeof(connstr), "dbname=%s host=%s user=%s password=%s",
212                          get_theme_config("db", "dbname").c_str(),
213                          get_theme_config("db", "host").c_str(),
214                          get_theme_config("db", "user").c_str(),
215                          get_theme_config("db", "password").c_str());
216                 pqxx::connection conn(connstr);
217                 FlagTrigger tournament_changed(conn, "active_tournament");
218                 FlagTrigger rounds_changed(conn, "active_groups");
219                 
220                 // when active_tournament or active_rounds is changed, we destroy everything and start from scratch
221                 // (at least currently)
222                 while (!quit_requested) {
223                         tournament_changed.reset_flag();
224                         rounds_changed.reset_flag();
225                         init(conn);
226                         do {
227                                 main_loop(conn, screen_width, screen_height);
228                                 conn.get_notifs();
229                                 handle_events();
230                         } while (!tournament_changed.get_flag() && !rounds_changed.get_flag() && !quit_requested);
231
232                         if (quit_requested) {
233                                 fprintf(stderr, "Quitting...\n");
234                         } else {
235                                 fprintf(stderr, "active_tournament or active_groups changed, resetting...\n");
236                         }
237                 }
238         } catch (const std::exception &e) {
239                 std::fprintf(stderr, "Exception: %s\n", e.what());
240                 exit(1);
241         }
242
243         SDL_Quit();
244         
245         return 0;
246 }