7f1fb851677c32b8f80ff78e7192f45cfeb761fc
[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 <locale.h> // setlocale
25 #include <ncurses.h>
26 #include <stdlib.h>
27 #include <string.h> // strchr
28 #include <unistd.h>
29
30 #include "include/viewer.h"
31
32 static short white_ramp[24] = { 16, 232, 233, 234, 235, 236,
33                                237, 238, 239, 240, 241, 242,
34                                244, 245, 246, 247, 248, 249,
35                                250, 251, 252, 253, 254, 255 };
36
37 static short blue_ramp[24]  = { 16,  17,  17,  18,  18,  19,
38                                 19,  20,  20,  21,  27,  32,
39                                 33,  38,  39,  44,  45,  45,
40                                 81,  81,  51,  51, 123, 123 };
41
42 static short red_ramp[24]   = { 16,  52,  52,  53,  53,  89,
43                                 89,  90,  90, 126, 127, 127,
44                                163, 163, 164, 164, 200, 200,
45                                201, 201, 207, 207, 213, 213 };
46
47 int ncurses_display(deck_t *deck, int notrans, int nofade) {
48
49     int c = 0;          // char
50     int i = 0;          // iterate
51     int l = 0;          // line number
52     int sc = 1;         // slide count
53     int colors = 0;     // amount of colors supported
54     int fade = 0;       // disable color fading by default
55     int trans = -1;     // enable transparency if term supports it
56     int max_lines = 0;  // max lines per slide
57     int max_cols = 0;   // max columns per line
58     int offset;         // text offset
59
60     // header line 1 is displayed at the top
61     int bar_top = (deck->headers > 0) ? 1 : 0;
62     // header line 2 is displayed at the bottom
63     // anyway we display the slide number at the bottom
64     int bar_bottom = 1;
65
66     slide_t *slide = deck->slide;
67     line_t *line;
68
69     while(slide) {
70         // set max_lines if line count exceeded
71         max_lines = (slide->lines > max_lines) ? slide->lines : max_lines;
72         line = slide->line;
73         while(line) {
74             // set max_cols if length exceeded
75             max_cols = (line->length > max_cols) ? line->length : max_cols;
76             line = line->next;
77         }
78         slide = slide->next;
79     }
80
81     // set locale to display UTF-8 correctly in ncurses
82     setlocale(LC_CTYPE, "");
83
84     // replace stdin with current tty if markdown input was piped
85     freopen("/dev/tty", "rw", stdin);
86
87     // init ncurses
88     initscr();
89
90     if((max_cols > COLS) ||
91        (max_lines + bar_top + bar_bottom + 2 > LINES)) {
92
93         fprintf(stderr, "Error: Terminal size %ix%i to small. Need at least %ix%i.\n",
94             COLS, LINES, max_cols, max_lines + bar_top + bar_bottom + 2);
95         endwin();
96         return(1);
97     }
98
99     // replace stdin with current tty if markdown input was piped
100     freopen("/dev/tty", "rw", stdin);
101
102     // disable cursor
103     curs_set(0);
104
105     // disable output of keyboard typing
106     noecho();
107
108     // make getch() process one char at a time
109     cbreak();
110
111     // enable arrow keys
112     keypad(stdscr,TRUE);
113
114     // set colors
115     if(has_colors() == TRUE) {
116         start_color();
117         use_default_colors();
118
119         if(notrans) trans = 0; // 0 is black
120
121         if(COLORS == 256) {
122             // 256 color mode
123             init_pair(CP_WHITE, 255, trans);
124             init_pair(CP_BLUE, 123, trans);
125             init_pair(CP_RED, 213, trans);
126             init_pair(CP_YELLOW, 208, trans);
127
128             // enable color fading
129             if(!nofade) fade = 1;
130         } else {
131
132             // 8 color mode
133             init_pair(CP_WHITE, 7, trans);
134             init_pair(CP_BLUE, 4, trans);
135             init_pair(CP_RED, 1, trans);
136             init_pair(CP_YELLOW, 3, trans);
137         }
138
139         colors = 1;
140     }
141
142     // set background color of main window
143     if(colors)
144         wbkgd(stdscr, COLOR_PAIR(CP_YELLOW));
145
146     // setup main window
147     WINDOW *content = newwin(LINES - bar_top - bar_bottom, COLS, 0 + bar_top, 0);
148     if(colors)
149         wbkgd(content, COLOR_PAIR(CP_WHITE));
150
151     slide = deck->slide;
152     while(slide) {
153         // clear windows
154         werase(content);
155         werase(stdscr);
156
157         // always resize window in case terminal geometry has changed
158         wresize(content, LINES - bar_top - bar_bottom, COLS);
159
160         // setup header
161         if(bar_top) {
162             line = deck->header;
163             offset = next_blank(line->text, 0) + 1;
164             // add text to header
165             mvwprintw(stdscr,
166                       0, (COLS - line->length + offset) / 2,
167                       "%s", &line->text->text[offset]);
168         }
169
170         // setup footer
171         if(deck->headers > 1) {
172             line = deck->header->next;
173             offset = next_blank(line->text, 0) + 1;
174             // add text to left footer
175             mvwprintw(stdscr,
176                       LINES - 1, 3,
177                       "%s", &line->text->text[offset]);
178         }
179         // add slide number to right footer
180         mvwprintw(stdscr,
181                   LINES - 1, COLS - int_length(deck->slides) - int_length(sc) - 6,
182                   "%d / %d", sc, deck->slides);
183
184         // make header + fooder visible
185         wrefresh(stdscr);
186
187         line = slide->line;
188         l = 0;
189
190         // print lines
191         while(line) {
192             add_line(content, l, (COLS - max_cols) / 2, line, max_cols);
193             line = line->next;
194             l++;
195         }
196
197         // make content visible
198         wrefresh(content);
199
200         // fade in
201         if(fade)
202             fade_in(content, trans, colors);
203
204         // re-enable fading after any undefined key press
205         if(COLORS == 256 && !nofade) fade = 1;
206
207         // wait for user input
208         c = getch();
209
210         // evaluate user input
211         i = 0;
212         switch(c) {
213
214             // show previous slide
215             case KEY_UP:
216             case KEY_LEFT:
217             case 8:   // BACKSPACE (ascii)
218             case 127: // BACKSPACE (xterm)
219             case 263: // BACKSPACE (getty)
220             case 'h':
221             case 'k':
222                 if(slide->prev) {
223                     slide = slide->prev;
224                     sc--;
225                 }
226                 break;
227
228             // show next slide
229             case KEY_DOWN:
230             case KEY_RIGHT:
231             case '\n': // ENTER
232             case ' ':  // SPACE
233             case 'j':
234             case 'l':
235                 if(slide->next) {
236                     slide = slide->next;
237                     sc++;
238                 }
239                 break;
240
241             // show slide n
242             case '9': i++;
243             case '8': i++;
244             case '7': i++;
245             case '6': i++;
246             case '5': i++;
247             case '4': i++;
248             case '3': i++;
249             case '2': i++;
250             case '1': i++;
251                 if(i <= deck->slides) {
252                     while(sc != i) {
253                         // search forward
254                         if(sc < i) {
255                             if(slide->next) {
256                                 slide = slide->next;
257                                 sc++;
258                             }
259                         // search backward
260                         } else {
261                             if(slide->prev) {
262                                 slide = slide->prev;
263                                 sc--;
264                             }
265                         }
266                     }
267                 } else {
268                     // disable fading if slide n doesn't exist
269                     fade = 0;
270                 }
271                 break;
272
273             // show first slide
274             case KEY_HOME:
275                 slide = deck->slide;
276                 sc = 1;
277                 break;
278
279             // show last slide
280             case KEY_END:
281                 for(i = sc; i <= deck->slides; i++) {
282                     if(slide->next) {
283                             slide = slide->next;
284                             sc++;
285                     }
286                 }
287                 break;
288
289             // quit
290             case 'q':
291                 // do not fade out on exit
292                 fade = 0;
293                 slide = (void*)0;
294                 break;
295
296             default:
297                 // disable fading on undefined key press
298                 fade = 0;
299                 break;
300         }
301
302         // fade out
303         if(fade)
304             fade_out(content, trans, colors);
305     }
306
307     endwin();
308
309     return(0);
310 }
311
312 void add_line(WINDOW *window, int y, int x, line_t *line, int max_cols) {
313     int i = 0; // increment
314     char *c; // char pointer for iteration
315     char *special = "\\*_"; // list of interpreted chars
316     cstack_t *stack = cstack_init();
317
318     if(line->text->text) {
319         int offset = 0; // text offset
320         offset = next_nonblank(line->text, 0);
321
322         // IS_CODE
323         if(CHECK_BIT(line->bits, IS_CODE)) {
324
325             // set static offset for code
326             offset = CODE_INDENT;
327
328             // reverse color for code blocks
329             wattron(window, A_REVERSE);
330
331             // print whole lines
332             mvwprintw(window,
333                       y, x,
334                       "%s", &line->text->text[offset]);
335
336         } else {
337
338             // IS_H1 || IS_H2
339             if(CHECK_BIT(line->bits, IS_H1) || CHECK_BIT(line->bits, IS_H2)) {
340
341                 // set headline color
342                 wattron(window, COLOR_PAIR(CP_BLUE));
343
344                 // enable underline for H1
345                 if(CHECK_BIT(line->bits, IS_H1))
346                     wattron(window, A_UNDERLINE);
347
348                 // skip hashes
349                 while(line->text->text[offset] == '#')
350                     offset = next_word(line->text, offset);
351
352                 // print whole lines
353                 mvwprintw(window,
354                       y, x,
355                       "%s", &line->text->text[offset]);
356
357                 wattroff(window, A_UNDERLINE);
358
359             } else {
360                 // move the cursor in position
361                 wmove(window, y, x);
362
363                 // IS_QUOTE
364                 if(CHECK_BIT(line->bits, IS_QUOTE)) {
365                     while(line->text->text[offset] == '>') {
366                         // print a reverse color block
367                         wattron(window, A_REVERSE);
368                         wprintw(window, "%s", " ");
369                         wattroff(window, A_REVERSE);
370                         wprintw(window, "%s", " ");
371                         // find next quote or break
372                         offset++;
373                         if(line->text->text[offset] == ' ')
374                             offset = next_word(line->text, offset);
375                     }
376                 }
377
378                 // for each char in line
379                 c = &line->text->text[offset];
380                 while(*c) {
381
382                     // if char is in special char list
383                     if(strchr(special, *c)) {
384
385                         // closing special char (or second backslash)
386                         if((stack->top)(stack, *c)) {
387
388                             switch(*c) {
389                                 // print escaped backslash
390                                 case '\\':
391                                     wprintw(window, "%c", *c);
392                                     break;
393                                 // disable highlight
394                                 case '*':
395                                     wattron(window, COLOR_PAIR(CP_WHITE));
396                                     break;
397                                 // disable underline
398                                 case '_':
399                                     wattroff(window, A_UNDERLINE);
400                                     break;
401                             }
402
403                             // remove top special char from stack
404                             (stack->pop)(stack);
405
406                         // treat special as regular char
407                         } else if((stack->top)(stack, '\\')) {
408                             wprintw(window, "%c", *c);
409
410                             // remove backslash from stack
411                             (stack->pop)(stack);
412
413                         // opening special char
414                         } else {
415                             switch(*c) {
416                                 // enable highlight
417                                 case '*':
418                                     wattron(window, COLOR_PAIR(CP_RED));
419                                     break;
420                                 // enable underline
421                                 case '_':
422                                     wattron(window, A_UNDERLINE);
423                                     break;
424                                 // do nothing for backslashes
425                             }
426
427                             // push special char to stack
428                             (stack->push)(stack, *c);
429                         }
430
431                     } else {
432                         // print regular char
433                         wprintw(window, "%c", *c);
434                     }
435
436                     c++;
437                 }
438
439                 // pop stack until empty to prevent formated trailing spaces
440                 while(!(stack->empty)(stack)) {
441                     switch((stack->pop)(stack)) {
442                         case '\\':
443                             wprintw(window, "%c", '\\');
444                             break;
445                         // disable highlight
446                         case '*':
447                             wattron(window, COLOR_PAIR(CP_WHITE));
448                             break;
449                         // disable underline
450                         case '_':
451                             wattroff(window, A_UNDERLINE);
452                             break;
453                     }
454                 }
455             }
456         }
457
458         // fill rest off line with spaces
459         for(i = getcurx(window) - x; i < max_cols; i++)
460             wprintw(window, "%s", " ");
461
462         // reset to default color
463         wattron(window, COLOR_PAIR(CP_WHITE));
464         wattroff(window, A_UNDERLINE);
465         wattroff(window, A_REVERSE);
466     }
467
468     (stack->delete)(stack);
469 }
470
471 void fade_out(WINDOW *window, int trans, int colors) {
472     int i; // increment
473     if(colors) {
474         for(i = 22; i >= 0; i--) {
475             // darken color pairs
476             init_pair(CP_WHITE, white_ramp[i], trans);
477             init_pair(CP_BLUE, blue_ramp[i], trans);
478             init_pair(CP_RED, red_ramp[i], trans);
479             // refresh window with new color
480             wrefresh(window);
481             // delay for our eyes to recognize the change
482             usleep(FADE_DELAY);
483         }
484     }
485 }
486
487 void fade_in(WINDOW *window, int trans, int colors) {
488     int i; // increment
489     if(colors) {
490         for(i = 0; i <= 23; i++) {
491             // lighten color pairs
492             init_pair(CP_WHITE, white_ramp[i], trans);
493             init_pair(CP_BLUE, blue_ramp[i], trans);
494             init_pair(CP_RED, red_ramp[i], trans);
495             // refresh window with new color
496             wrefresh(window);
497             // delay for our eyes to recognize the change
498             usleep(FADE_DELAY);
499         }
500     }
501 }
502
503 int int_length (int val) {
504     int l = 1;
505     while(val > 9) {
506         l++;
507         val /= 10;
508     }
509     return l;
510 }