rewrote Makefile + copyright and license infos
[smdp.git] / viewer.c
1 /*
2  * Functions necessary to display a deck of slides in different color modes
3  * using ncurses. Only white, red, and blue are supported, as they can be
4  * faded in 256 color mode.
5  * Copyright (C) 2014 Michael Goehler
6  *
7  * This file is part of mpd.
8  *
9  * This program is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation, either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program. If not, see <http://www.gnu.org/licenses/>.
21  *
22  */
23
24 #include <ncurses.h>
25 #include <stdlib.h>
26 #include <string.h> // strchr
27 #include <unistd.h>
28
29 #include "include/viewer.h"
30
31 static short white_ramp[24] = { 16, 232, 233, 234, 235, 236,
32                                237, 238, 239, 240, 241, 242,
33                                244, 245, 246, 247, 248, 249,
34                                250, 251, 252, 253, 254, 255 };
35
36 static short blue_ramp[24]  = { 16,  17,  17,  18,  18,  19,
37                                 19,  20,  20,  21,  27,  32,
38                                 33,  38,  39,  44,  45,  45,
39                                 81,  81,  51,  51, 123, 123 };
40
41 static short red_ramp[24]   = { 16,  52,  52,  53,  53,  89,
42                                 89,  90,  90, 126, 127, 127,
43                                163, 163, 164, 164, 200, 200,
44                                201, 201, 207, 207, 213, 213 };
45
46 int ncurses_display(deck_t *deck, int notrans, int nofade) {
47
48     int c = 0;          // char
49     int l = 0;          // line number
50     int colors = 0;     // amount of colors supported
51     int fade = 0;       // disable color fading by default
52     int trans = -1;     // enable transparency if term supports it
53     int max_lines = 0;  // max lines per slide
54     int max_cols = 0;   // max columns per line
55     int offset;         // text offset
56
57     // header line 1 is displayed at the top
58     int bar_top = (deck->headers > 0) ? 1 : 0;
59     // header line 2 and 3 are displayed at the bottom
60     int bar_bottom = (deck->headers > 1) ? 1 : 0;
61
62     slide_t *slide = deck->slide;
63     line_t *line;
64
65     while(slide) {
66         // set max_lines if line count exceeded
67         max_lines = (slide->lines > max_lines) ? slide->lines : max_lines;
68         line = slide->line;
69         while(line) {
70             // set max_cols if length exceeded
71             max_cols = (line->length > max_cols) ? line->length : max_cols;
72             line = line->next;
73         }
74         slide = slide->next;
75     }
76
77     // replace stdin with current tty if markdown input was piped
78     freopen("/dev/tty", "rw", stdin);
79
80     // init ncurses
81     initscr();
82
83     if((max_cols > COLS) ||
84        (max_lines + bar_top + bar_bottom + 2 > LINES)) {
85
86         fprintf(stderr, "Error: Terminal size %ix%i to small. Need at least %ix%i.\n",
87             COLS, LINES, max_cols, max_lines + bar_top + bar_bottom + 2);
88         endwin();
89         return(1);
90     }
91
92     // replace stdin with current tty if markdown input was piped
93     freopen("/dev/tty", "rw", stdin);
94
95     // disable cursor
96     curs_set(0);
97
98     // disable output of keyboard typing
99     noecho();
100
101     // make getch() process one char at a time
102     cbreak();
103
104     // enable arrow keys
105     keypad(stdscr,TRUE);
106
107     // set colors
108     if(has_colors() == TRUE) {
109         start_color();
110         use_default_colors();
111
112         if(notrans) trans = 0; // 0 is black
113
114         if(COLORS == 256) {
115             // 256 color mode
116             init_pair(CP_WHITE, 255, trans);
117             init_pair(CP_BLUE, 123, trans);
118             init_pair(CP_RED, 213, trans);
119             init_pair(CP_YELLOW, 208, trans);
120
121             // enable color fading
122             if(!nofade) fade = 1;
123         } else {
124
125             // 8 color mode
126             init_pair(CP_WHITE, 7, trans);
127             init_pair(CP_BLUE, 4, trans);
128             init_pair(CP_RED, 1, trans);
129             init_pair(CP_YELLOW, 3, trans);
130         }
131
132         colors = 1;
133     }
134
135     // set background color of main window
136     if(colors)
137         wbkgd(stdscr, COLOR_PAIR(CP_YELLOW));
138
139     // setup header
140     if(bar_top) {
141         line = deck->header;
142         offset = next_blank(line->text, 0) + 1;
143         // add text to header
144         mvwprintw(stdscr,
145                   0, (COLS - line->length + offset) / 2,
146                   "%s", &line->text->text[offset]);
147     }
148
149     // setup footer
150     if(bar_bottom) {
151         line = deck->header->next;
152         offset = next_blank(line->text, 0) + 1;
153         // add text to left footer
154         mvwprintw(stdscr,
155                   LINES - 1, 3,
156                   "%s", &line->text->text[offset]);
157
158         if(deck->headers > 2) {
159             line = deck->header->next->next;
160             offset = next_blank(line->text, 0) + 1;
161             // add text to right footer
162             mvwprintw(stdscr,
163                       LINES - 1, COLS - line->length + offset - 3,
164                       "%s", &line->text->text[offset]);
165         }
166     }
167
168     // make header + fooder visible
169     wrefresh(stdscr);
170
171     // setup main window
172     WINDOW *content = newwin(LINES - bar_top - bar_bottom, COLS, 0 + bar_top, 0);
173     if(colors)
174         wbkgd(content, COLOR_PAIR(CP_WHITE));
175
176     slide = deck->slide;
177     while(slide) {
178         // clear main window
179         werase(content);
180
181         line = slide->line;
182         l = 0;
183
184         // print lines
185         while(line) {
186             add_line(content, l, (COLS - max_cols) / 2, line, max_cols);
187             line = line->next;
188             l++;
189         }
190
191         // make content visible
192         wrefresh(content);
193
194         // fade in
195         if(fade)
196             fade_in(content, trans, colors);
197
198         // re-enable fading after any undefined key press
199         if(COLORS == 256 && !nofade) fade = 1;
200
201         // wait for user input
202         c = getch();
203
204         // evaluate user input
205         switch(c) {
206
207             // show previous slide
208             case KEY_UP:
209             case KEY_LEFT:
210             case 8:   // BACKSPACE (ascii)
211             case 127: // BACKSPACE (xterm)
212             case 263: // BACKSPACE (getty)
213             case 'h':
214             case 'k':
215                 if(slide->prev)
216                     slide = slide->prev;
217                 break;
218
219             // show next slide
220             case KEY_DOWN:
221             case KEY_RIGHT:
222             case '\n': // ENTER
223             case ' ':  // SPACE
224             case 'j':
225             case 'l':
226                 if(slide->next)
227                     slide = slide->next;
228                 break;
229
230             // quit
231             case 'q':
232                 // do not fade out on exit
233                 fade = 0;
234                 slide = (void*)0;
235                 break;
236
237             default:
238                 // disable fading on undefined key press
239                 fade = 0;
240                 break;
241         }
242
243         // fade out
244         if(fade)
245             fade_out(content, trans, colors);
246     }
247
248     endwin();
249
250     return(0);
251 }
252
253 void add_line(WINDOW *window, int y, int x, line_t *line, int max_cols) {
254     int i = 0; // increment
255     char *c; // char pointer for iteration
256     char *special = "\\*_"; // list of interpreted chars
257     cstack_t *stack = cstack_init();
258
259     if(line->text->text) {
260         int offset = 0; // text offset
261         offset = next_nonblank(line->text, 0);
262
263         // IS_CODE
264         if(CHECK_BIT(line->bits, IS_CODE)) {
265
266             // set static offset for code
267             offset = CODE_INDENT;
268
269             // reverse color for code blocks
270             wattron(window, A_REVERSE);
271
272             // print whole lines
273             mvwprintw(window,
274                       y, x,
275                       "%s", &line->text->text[offset]);
276
277         // IS_QUOTE
278         } else if(CHECK_BIT(line->bits, IS_QUOTE)) {
279             //TODO replace greater sign with color block
280
281             //FIXME remove dummy print code
282             mvwprintw(window,
283                       y, x,
284                       "%s", &line->text->text[offset]);
285
286         } else {
287
288             // IF_H1 || IF_H2
289             if(CHECK_BIT(line->bits, IS_H1) || CHECK_BIT(line->bits, IS_H2)) {
290
291                 // set headline color
292                 wattron(window, COLOR_PAIR(CP_BLUE));
293
294                 // enable underline for H1
295                 if(CHECK_BIT(line->bits, IS_H1))
296                     wattron(window, A_UNDERLINE);
297
298                 // skip hashes
299                 while(line->text->text[offset] == '#')
300                     offset = next_word(line->text, offset);
301
302                 // print whole lines
303                 mvwprintw(window,
304                       y, x,
305                       "%s", &line->text->text[offset]);
306
307                 wattroff(window, A_UNDERLINE);
308
309             } else {
310                 // move the cursor in position
311                 wmove(window, y, x);
312
313                 // for each char in line
314                 c = line->text->text;
315                 while(*c) {
316
317                     // if char is in special char list
318                     if(strchr(special, *c)) {
319
320                         // closing special char (or second backslash)
321                         if((stack->top)(stack, *c)) {
322
323                             switch(*c) {
324                                 // print escaped backslash
325                                 case '\\':
326                                     wprintw(window, "%c", *c);
327                                     break;
328                                 // disable highlight
329                                 case '*':
330                                     wattron(window, COLOR_PAIR(CP_WHITE));
331                                     break;
332                                 // disable underline
333                                 case '_':
334                                     wattroff(window, A_UNDERLINE);
335                                     break;
336                             }
337
338                             // remove top special char from stack
339                             (stack->pop)(stack);
340
341                         // treat special as regular char
342                         } else if((stack->top)(stack, '\\')) {
343                             wprintw(window, "%c", *c);
344
345                             // remove backslash from stack
346                             (stack->pop)(stack);
347
348                         // opening special char
349                         } else {
350                             switch(*c) {
351                                 // enable highlight
352                                 case '*':
353                                     wattron(window, COLOR_PAIR(CP_RED));
354                                     break;
355                                 // enable underline
356                                 case '_':
357                                     wattron(window, A_UNDERLINE);
358                                     break;
359                                 // do nothing for backslashes
360                             }
361
362                             // push special char to stack
363                             (stack->push)(stack, *c);
364                         }
365
366                     } else {
367                         // print regular char
368                         wprintw(window, "%c", *c);
369                     }
370
371                     c++;
372                 }
373
374                 //TODO pop stack until empty
375             }
376         }
377
378         // fill rest off line with spaces
379         for(i = getcurx(window) - x; i < max_cols; i++)
380             wprintw(window, "%s", " ");
381
382         // reset to default color
383         wattron(window, COLOR_PAIR(CP_WHITE));
384         wattroff(window, A_UNDERLINE);
385         wattroff(window, A_REVERSE);
386     }
387
388     (stack->delete)(stack);
389 }
390
391 void fade_out(WINDOW *window, int trans, int colors) {
392     int i; // increment
393     if(colors) {
394         for(i = 22; i >= 0; i--) {
395             // darken color pairs
396             init_pair(CP_WHITE, white_ramp[i], trans);
397             init_pair(CP_BLUE, blue_ramp[i], trans);
398             init_pair(CP_RED, red_ramp[i], trans);
399             // refresh window with new color
400             wrefresh(window);
401             // delay for our eyes to recognize the change
402             usleep(FADE_DELAY);
403         }
404     }
405 }
406
407 void fade_in(WINDOW *window, int trans, int colors) {
408     int i; // increment
409     if(colors) {
410         for(i = 0; i <= 23; i++) {
411             // lighten color pairs
412             init_pair(CP_WHITE, white_ramp[i], trans);
413             init_pair(CP_BLUE, blue_ramp[i], trans);
414             init_pair(CP_RED, red_ramp[i], trans);
415             // refresh window with new color
416             wrefresh(window);
417             // delay for our eyes to recognize the change
418             usleep(FADE_DELAY);
419         }
420     }
421 }
422