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