Unify the coding style for `return`.
[smdp.git] / src / 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 <stdlib.h>
26 #include <string.h> // strchr
27 #include <unistd.h>
28
29 #include "viewer.h"
30
31 // color ramp for fading from black to color
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,  33,
39                                 32,  39,  38,  45,  44,  44,
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 // color ramp for fading from white to color
48 static short white_ramp_invert[24] = { 15, 255, 254, 254, 252, 251,
49                                       250, 249, 248, 247, 246, 245,
50                                       243, 242, 241, 240, 239, 238,
51                                       237, 236, 235, 234, 233, 232};
52
53 static short blue_ramp_invert[24]  = { 15, 231, 231, 195, 195, 159,
54                                       159, 123, 123,  87,  51,  44,
55                                        45,  38,  39,  32,  33,  33,
56                                        26,  26,  27,  27,  21,  21};
57
58 static short red_ramp_invert[24]   = { 15, 231, 231, 224, 224, 225,
59                                       225, 218, 218, 219, 212, 213,
60                                       206, 207, 201, 200, 199, 199,
61                                       198, 198, 197, 197, 196, 196};
62
63 int ncurses_display(deck_t *deck, int notrans, int nofade, int invert) {
64
65     int c = 0;          // char
66     int i = 0;          // iterate
67     int l = 0;          // line number
68     int lc = 0;         // line count
69     int sc = 1;         // slide count
70     int colors = 0;     // amount of colors supported
71     int fade = 0;       // disable color fading by default
72     int trans = -1;     // enable transparency if term supports it
73     int max_lines = 0;  // max lines per slide
74     int max_cols = 0;   // max columns per line
75     int offset;         // text offset
76
77     // header line 1 is displayed at the top
78     int bar_top = (deck->headers > 0) ? 1 : 0;
79     // header line 2 is displayed at the bottom
80     // anyway we display the slide number at the bottom
81     int bar_bottom = 1;
82
83     slide_t *slide = deck->slide;
84     line_t *line;
85
86     // set locale to display UTF-8 correctly in ncurses
87     setlocale(LC_CTYPE, "");
88
89     // init ncurses
90     initscr();
91
92     while(slide) {
93         lc = 0;
94         line = slide->line;
95
96         while(line) {
97             if(line->length > COLS) {
98                 i = line->length;
99                 offset = 0;
100                 while(i > COLS) {
101
102                     i = prev_blank(line->text, offset + COLS) - offset;
103
104                     // single word is > COLS
105                     if(!i) {
106                         // calculate min_width
107                         i = next_blank(line->text, offset + COLS) - offset;
108
109                         // disable ncurses
110                         endwin();
111
112                         // print error
113                         fprintf(stderr, "Error: Terminal width (%i columns) too small. Need at least %i columns.\n", COLS, i);
114                         fprintf(stderr, "You may need to shorten some lines by inserting line breaks.\n");
115
116                         return 1;
117                     }
118
119                     // set max_cols
120                     max_cols = MAX(i, max_cols);
121
122                     // iterate to next line
123                     offset = prev_blank(line->text, offset + COLS);
124                     i = line->length - offset;
125                     lc++;
126                 }
127                 // set max_cols one last time
128                 max_cols = MAX(i, max_cols);
129             } else {
130                 // set max_cols
131                 max_cols = MAX(line->length, max_cols);
132             }
133             lc++;
134             line = line->next;
135         }
136
137         max_lines = MAX(lc, max_lines);
138
139         slide = slide->next;
140     }
141
142     // not enough lines
143     if(max_lines + bar_top + bar_bottom > LINES) {
144
145         // disable ncurses
146         endwin();
147
148         // print error
149         fprintf(stderr, "Error: Terminal heigth (%i lines) too small. Need at least %i lines.\n", LINES, max_lines + bar_top + bar_bottom);
150         fprintf(stderr, "You may need to add additional horizontal rules ('***') to split your file in shorter slides.\n");
151
152         return 1;
153     }
154
155     // disable cursor
156     curs_set(0);
157
158     // disable output of keyboard typing
159     noecho();
160
161     // make getch() process one char at a time
162     cbreak();
163
164     // enable arrow keys
165     keypad(stdscr,TRUE);
166
167     // set colors
168     if(has_colors() == TRUE) {
169         start_color();
170         use_default_colors();
171
172         // 256 color mode
173         if(COLORS == 256) {
174
175             if(notrans) {
176                 if(invert) {
177                     trans = 15; // white in 256 color mode
178                 } else {
179                     trans = 16; // black in 256 color mode
180                 }
181             }
182
183             if(invert) {
184                 init_pair(CP_WHITE, 232, trans);
185                 init_pair(CP_BLUE, 21, trans);
186                 init_pair(CP_RED, 196, trans);
187                 init_pair(CP_BLACK, 15, 232);
188             } else {
189                 init_pair(CP_WHITE, 255, trans);
190                 init_pair(CP_BLUE, 123, trans);
191                 init_pair(CP_RED, 213, trans);
192                 init_pair(CP_BLACK, 16, 255);
193             }
194             init_pair(CP_YELLOW, 208, trans);
195
196             // enable color fading
197             if(!nofade) fade = 1;
198
199         // 8 color mode
200         } else {
201
202             if(notrans) {
203                 if(invert) {
204                     trans = 7; // white in 8 color mode
205                 } else {
206                     trans = 0; // black in 8 color mode
207                 }
208             }
209
210             if(invert) {
211                 init_pair(CP_WHITE, 0, trans);
212                 init_pair(CP_BLACK, 7, 0);
213             } else {
214                 init_pair(CP_WHITE, 7, trans);
215                 init_pair(CP_BLACK, 0, 7);
216             }
217             init_pair(CP_BLUE, 4, trans);
218             init_pair(CP_RED, 1, trans);
219             init_pair(CP_YELLOW, 3, trans);
220         }
221
222         colors = 1;
223     }
224
225     // set background color of main window
226     if(colors)
227         wbkgd(stdscr, COLOR_PAIR(CP_YELLOW));
228
229     // setup main window
230     WINDOW *content = newwin(LINES - bar_top - bar_bottom, COLS, 0 + bar_top, 0);
231     if(colors)
232         wbkgd(content, COLOR_PAIR(CP_WHITE));
233
234     slide = deck->slide;
235     while(slide) {
236         // clear windows
237         werase(content);
238         werase(stdscr);
239
240         // always resize window in case terminal geometry has changed
241         wresize(content, LINES - bar_top - bar_bottom, COLS);
242
243         // setup header
244         if(bar_top) {
245             line = deck->header;
246             offset = next_blank(line->text, 0) + 1;
247             // add text to header
248             mvwprintw(stdscr,
249                       0, (COLS - line->length + offset) / 2,
250                       "%s", &line->text->text[offset]);
251         }
252
253         // setup footer
254         if(deck->headers > 1) {
255             line = deck->header->next;
256             offset = next_blank(line->text, 0) + 1;
257             // add text to left footer
258             mvwprintw(stdscr,
259                       LINES - 1, 3,
260                       "%s", &line->text->text[offset]);
261         }
262         // add slide number to right footer
263         mvwprintw(stdscr,
264                   LINES - 1, COLS - int_length(deck->slides) - int_length(sc) - 6,
265                   "%d / %d", sc, deck->slides);
266
267         // make header + fooder visible
268         wrefresh(stdscr);
269
270         line = slide->line;
271         l = 0;
272
273         // print lines
274         while(line) {
275             add_line(content, l, (COLS - max_cols) / 2, line, max_cols, colors);
276             line = line->next;
277             l++;
278         }
279
280         // make content visible
281         wrefresh(content);
282
283         // fade in
284         if(fade)
285             fade_in(content, trans, colors, invert);
286
287         // re-enable fading after any undefined key press
288         if(COLORS == 256 && !nofade) fade = 1;
289
290         // wait for user input
291         c = getch();
292
293         // evaluate user input
294         i = 0;
295         switch(c) {
296
297             // show previous slide
298             case KEY_UP:
299             case KEY_LEFT:
300             case KEY_PPAGE:
301             case 8:   // BACKSPACE (ascii)
302             case 127: // BACKSPACE (xterm)
303             case 263: // BACKSPACE (getty)
304             case 'h':
305             case 'k':
306                 if(slide->prev) {
307                     slide = slide->prev;
308                     sc--;
309                 } else {
310                     fade = 0;
311                 }
312                 break;
313
314             // show next slide
315             case KEY_DOWN:
316             case KEY_RIGHT:
317             case KEY_NPAGE:
318             case '\n': // ENTER
319             case ' ':  // SPACE
320             case 'j':
321             case 'l':
322                 if(slide->next) {
323                     slide = slide->next;
324                     sc++;
325                 } else {
326                     fade = 0;
327                 }
328                 break;
329
330             // show slide n
331             case '9': i++;
332             case '8': i++;
333             case '7': i++;
334             case '6': i++;
335             case '5': i++;
336             case '4': i++;
337             case '3': i++;
338             case '2': i++;
339             case '1': i++;
340                 if(i <= deck->slides) {
341                     while(sc != i) {
342                         // search forward
343                         if(sc < i) {
344                             if(slide->next) {
345                                 slide = slide->next;
346                                 sc++;
347                             }
348                         // search backward
349                         } else {
350                             if(slide->prev) {
351                                 slide = slide->prev;
352                                 sc--;
353                             }
354                         }
355                     }
356                 } else {
357                     // disable fading if slide n doesn't exist
358                     fade = 0;
359                 }
360                 break;
361
362             // show first slide
363             case KEY_HOME:
364                 slide = deck->slide;
365                 sc = 1;
366                 break;
367
368             // show last slide
369             case KEY_END:
370                 for(i = sc; i <= deck->slides; i++) {
371                     if(slide->next) {
372                             slide = slide->next;
373                             sc++;
374                     }
375                 }
376                 break;
377
378             // quit
379             case 'q':
380                 // do not fade out on exit
381                 fade = 0;
382                 slide = NULL;
383                 break;
384
385             default:
386                 // disable fading on undefined key press
387                 fade = 0;
388                 break;
389         }
390
391         // fade out
392         if(fade)
393             fade_out(content, trans, colors, invert);
394     }
395
396     endwin();
397
398     return 0;
399 }
400
401 void add_line(WINDOW *window, int y, int x, line_t *line, int max_cols, int colors) {
402
403     if(!line->text->text) {
404         return;
405     }
406
407     int i; // increment
408     int offset = 0; // text offset
409
410     // move the cursor in position
411     wmove(window, y, x);
412
413     // IS_UNORDERED_LIST_3
414     if(CHECK_BIT(line->bits, IS_UNORDERED_LIST_3)) {
415         offset = next_nonblank(line->text, 0);
416         char prompt[10];
417         strcpy(&prompt[0], CHECK_BIT(line->bits, IS_UNORDERED_LIST_1)? "|  " : "   ");
418         strcpy(&prompt[3], CHECK_BIT(line->bits, IS_UNORDERED_LIST_2)? "|  " : "   ");
419         strcpy(&prompt[6], line->next && CHECK_BIT(line->next->bits, IS_UNORDERED_LIST_3)? "+- " : "`- ");
420         wprintw(window,
421                 "%s", prompt);
422
423         inline_display(window, &line->text->text[offset + 2], colors);
424
425     // IS_UNORDERED_LIST_2
426     } else if(CHECK_BIT(line->bits, IS_UNORDERED_LIST_2)) {
427         offset = next_nonblank(line->text, 0);
428         char prompt[7];
429         strcpy(&prompt[0], CHECK_BIT(line->bits, IS_UNORDERED_LIST_1)? "|  " : "   ");
430         strcpy(&prompt[3], line->next && CHECK_BIT(line->next->bits, IS_UNORDERED_LIST_2)? "+- " : "`- ");
431         wprintw(window,
432                 "%s", prompt);
433
434         inline_display(window, &line->text->text[offset + 2], colors);
435
436     // IS_UNORDERED_LIST_1
437     } else if(CHECK_BIT(line->bits, IS_UNORDERED_LIST_1)) {
438         offset = next_nonblank(line->text, 0);
439         char prompt[4];
440         strcpy(&prompt[0], line->next && CHECK_BIT(line->next->bits, IS_UNORDERED_LIST_1)? "+- " : "`- ");
441         wprintw(window,
442                 "%s", prompt);
443
444         inline_display(window, &line->text->text[offset + 2], colors);
445
446     // IS_CODE
447     } else if(CHECK_BIT(line->bits, IS_CODE)) {
448
449         // set static offset for code
450         offset = CODE_INDENT;
451
452         // reverse color for code blocks
453         if(colors)
454             wattron(window, COLOR_PAIR(CP_BLACK));
455
456         // print whole lines
457         wprintw(window,
458                 "%s", &line->text->text[offset]);
459
460     // IS_H1 || IS_H2
461     } else if(CHECK_BIT(line->bits, IS_H1) || CHECK_BIT(line->bits, IS_H2)) {
462
463         // set headline color
464         if(colors)
465             wattron(window, COLOR_PAIR(CP_BLUE));
466
467         // enable underline for H1
468         if(CHECK_BIT(line->bits, IS_H1))
469             wattron(window, A_UNDERLINE);
470
471         // skip hashes
472         while(line->text->text[offset] == '#')
473             offset = next_word(line->text, offset);
474
475         // print whole lines
476         wprintw(window,
477                 "%s", &line->text->text[offset]);
478
479         wattroff(window, A_UNDERLINE);
480
481     } else {
482
483         // IS_QUOTE
484         if(CHECK_BIT(line->bits, IS_QUOTE)) {
485             while(line->text->text[offset] == '>') {
486                 // print a reverse color block
487                 if(colors) {
488                     wattron(window, COLOR_PAIR(CP_BLACK));
489                     wprintw(window, "%s", " ");
490                     wattron(window, COLOR_PAIR(CP_WHITE));
491                     wprintw(window, "%s", " ");
492                 } else {
493                     wprintw(window, "%s", ">");
494                 }
495
496                 // find next quote or break
497                 offset++;
498                 if(line->text->text[offset] == ' ')
499                     offset = next_word(line->text, offset);
500             }
501         }
502
503         inline_display(window, &line->text->text[offset], colors);
504     }
505
506     // fill rest off line with spaces
507     for(i = getcurx(window) - x; i < max_cols; i++)
508         wprintw(window, "%s", " ");
509
510     // reset to default color
511     if(colors)
512         wattron(window, COLOR_PAIR(CP_WHITE));
513     wattroff(window, A_UNDERLINE);
514 }
515
516 void inline_display(WINDOW *window, const char *c, const int colors) {
517     const static char *special = "\\*_`"; // list of interpreted chars
518     cstack_t *stack = cstack_init();
519
520     // for each char in line
521     for(; *c; c++) {
522
523         // if char is in special char list
524         if(strchr(special, *c)) {
525
526             // closing special char (or second backslash)
527             if((stack->top)(stack, *c)) {
528
529                 switch(*c) {
530                     // print escaped backslash
531                     case '\\':
532                         wprintw(window, "%c", *c);
533                         break;
534                     // disable highlight
535                     case '*':
536                         if(colors)
537                             wattron(window, COLOR_PAIR(CP_WHITE));
538                         break;
539                     // disable underline
540                     case '_':
541                         wattroff(window, A_UNDERLINE);
542                         break;
543                     // disable inline code
544                     case '`':
545                         if(colors)
546                             wattron(window, COLOR_PAIR(CP_WHITE));
547                         break;
548                 }
549
550                 // remove top special char from stack
551                 (stack->pop)(stack);
552
553             // treat special as regular char
554             } else if((stack->top)(stack, '\\')) {
555                 wprintw(window, "%c", *c);
556
557                 // remove backslash from stack
558                 (stack->pop)(stack);
559
560             // opening special char
561             } else {
562                 switch(*c) {
563                     // enable highlight
564                     case '*':
565                         if(colors)
566                             wattron(window, COLOR_PAIR(CP_RED));
567                         break;
568                     // enable underline
569                     case '_':
570                         wattron(window, A_UNDERLINE);
571                         break;
572                     // enable inline code
573                     case '`':
574                         if(colors)
575                             wattron(window, COLOR_PAIR(CP_BLACK));
576                         break;
577                     // do nothing for backslashes
578                 }
579
580                 // push special char to stack
581                 (stack->push)(stack, *c);
582             }
583
584         } else {
585             // remove backslash from stack
586             if((stack->top)(stack, '\\'))
587                 (stack->pop)(stack);
588
589             // print regular char
590             wprintw(window, "%c", *c);
591         }
592     }
593
594     // pop stack until empty to prevent formated trailing spaces
595     while(!(stack->empty)(stack)) {
596         switch((stack->pop)(stack)) {
597             // disable highlight
598             case '*':
599                 if(colors)
600                     wattron(window, COLOR_PAIR(CP_WHITE));
601                 break;
602             // disable underline
603             case '_':
604                 wattroff(window, A_UNDERLINE);
605                 break;
606             // disable inline code
607             case '`':
608                 if(colors)
609                     wattron(window, COLOR_PAIR(CP_WHITE));
610                 break;
611             // do nothing for backslashes
612         }
613     }
614
615     (stack->delete)(stack);
616 }
617
618 void fade_out(WINDOW *window, int trans, int colors, int invert) {
619     int i; // increment
620     if(colors && COLORS == 256) {
621         for(i = 22; i >= 0; i--) {
622
623             // dim color pairs
624             if(invert) {
625                 init_pair(CP_WHITE, white_ramp_invert[i], trans);
626                 init_pair(CP_BLUE, blue_ramp_invert[i], trans);
627                 init_pair(CP_RED, red_ramp_invert[i], trans);
628                 init_pair(CP_BLACK, 15, white_ramp_invert[i]);
629             } else {
630                 init_pair(CP_WHITE, white_ramp[i], trans);
631                 init_pair(CP_BLUE, blue_ramp[i], trans);
632                 init_pair(CP_RED, red_ramp[i], trans);
633                 init_pair(CP_BLACK, 16, white_ramp[i]);
634             }
635
636             // refresh window with new color
637             wrefresh(window);
638
639             // delay for our eyes to recognize the change
640             usleep(FADE_DELAY);
641         }
642     }
643 }
644
645 void fade_in(WINDOW *window, int trans, int colors, int invert) {
646     int i; // increment
647     if(colors && COLORS == 256) {
648         for(i = 0; i <= 23; i++) {
649
650             // brighten color pairs
651             if(invert) {
652                 init_pair(CP_WHITE, white_ramp_invert[i], trans);
653                 init_pair(CP_BLUE, blue_ramp_invert[i], trans);
654                 init_pair(CP_RED, red_ramp_invert[i], trans);
655                 init_pair(CP_BLACK, 15, white_ramp_invert[i]);
656             } else {
657                 init_pair(CP_WHITE, white_ramp[i], trans);
658                 init_pair(CP_BLUE, blue_ramp[i], trans);
659                 init_pair(CP_RED, red_ramp[i], trans);
660                 init_pair(CP_BLACK, 16, white_ramp[i]);
661             }
662
663             // refresh window with new color
664             wrefresh(window);
665
666             // delay for our eyes to recognize the change
667             usleep(FADE_DELAY);
668         }
669     }
670 }
671
672 int int_length (int val) {
673     int l = 1;
674     while(val > 9) {
675         l++;
676         val /= 10;
677     }
678     return l;
679 }