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