new doc page about slide breaks
[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                 } else {
263                     fade = 0;
264                 }
265                 break;
266
267             // show next slide
268             case KEY_DOWN:
269             case KEY_RIGHT:
270             case '\n': // ENTER
271             case ' ':  // SPACE
272             case 'j':
273             case 'l':
274                 if(slide->next) {
275                     slide = slide->next;
276                     sc++;
277                 } else {
278                     fade = 0;
279                 }
280                 break;
281
282             // show slide n
283             case '9': i++;
284             case '8': i++;
285             case '7': i++;
286             case '6': i++;
287             case '5': i++;
288             case '4': i++;
289             case '3': i++;
290             case '2': i++;
291             case '1': i++;
292                 if(i <= deck->slides) {
293                     while(sc != i) {
294                         // search forward
295                         if(sc < i) {
296                             if(slide->next) {
297                                 slide = slide->next;
298                                 sc++;
299                             }
300                         // search backward
301                         } else {
302                             if(slide->prev) {
303                                 slide = slide->prev;
304                                 sc--;
305                             }
306                         }
307                     }
308                 } else {
309                     // disable fading if slide n doesn't exist
310                     fade = 0;
311                 }
312                 break;
313
314             // show first slide
315             case KEY_HOME:
316                 slide = deck->slide;
317                 sc = 1;
318                 break;
319
320             // show last slide
321             case KEY_END:
322                 for(i = sc; i <= deck->slides; i++) {
323                     if(slide->next) {
324                             slide = slide->next;
325                             sc++;
326                     }
327                 }
328                 break;
329
330             // quit
331             case 'q':
332                 // do not fade out on exit
333                 fade = 0;
334                 slide = (void*)0;
335                 break;
336
337             default:
338                 // disable fading on undefined key press
339                 fade = 0;
340                 break;
341         }
342
343         // fade out
344         if(fade)
345             fade_out(content, trans, colors, invert);
346     }
347
348     endwin();
349
350     return(0);
351 }
352
353 void add_line(WINDOW *window, int y, int x, line_t *line, int max_cols, int colors) {
354     int i = 0; // increment
355     char *c; // char pointer for iteration
356     char *special = "\\*_"; // list of interpreted chars
357     cstack_t *stack = cstack_init();
358
359     if(line->text->text) {
360         int offset = 0; // text offset
361
362         // IS_CODE
363         if(CHECK_BIT(line->bits, IS_CODE)) {
364
365             // set static offset for code
366             offset = CODE_INDENT;
367
368             // reverse color for code blocks
369             wattron(window, A_REVERSE);
370
371             // print whole lines
372             mvwprintw(window,
373                       y, x,
374                       "%s", &line->text->text[offset]);
375
376         } else {
377
378             // IS_H1 || IS_H2
379             if(CHECK_BIT(line->bits, IS_H1) || CHECK_BIT(line->bits, IS_H2)) {
380
381                 // set headline color
382                 if(colors)
383                     wattron(window, COLOR_PAIR(CP_BLUE));
384
385                 // enable underline for H1
386                 if(CHECK_BIT(line->bits, IS_H1))
387                     wattron(window, A_UNDERLINE);
388
389                 // skip hashes
390                 while(line->text->text[offset] == '#')
391                     offset = next_word(line->text, offset);
392
393                 // print whole lines
394                 mvwprintw(window,
395                       y, x,
396                       "%s", &line->text->text[offset]);
397
398                 wattroff(window, A_UNDERLINE);
399
400             } else {
401                 // move the cursor in position
402                 wmove(window, y, x);
403
404                 // IS_QUOTE
405                 if(CHECK_BIT(line->bits, IS_QUOTE)) {
406                     while(line->text->text[offset] == '>') {
407                         // print a reverse color block
408                         wattron(window, A_REVERSE);
409                         wprintw(window, "%s", " ");
410                         wattroff(window, A_REVERSE);
411                         wprintw(window, "%s", " ");
412                         // find next quote or break
413                         offset++;
414                         if(line->text->text[offset] == ' ')
415                             offset = next_word(line->text, offset);
416                     }
417                 }
418
419                 // for each char in line
420                 c = &line->text->text[offset];
421                 while(*c) {
422
423                     // if char is in special char list
424                     if(strchr(special, *c)) {
425
426                         // closing special char (or second backslash)
427                         if((stack->top)(stack, *c)) {
428
429                             switch(*c) {
430                                 // print escaped backslash
431                                 case '\\':
432                                     wprintw(window, "%c", *c);
433                                     break;
434                                 // disable highlight
435                                 case '*':
436                                     if(colors)
437                                         wattron(window, COLOR_PAIR(CP_WHITE));
438                                     break;
439                                 // disable underline
440                                 case '_':
441                                     wattroff(window, A_UNDERLINE);
442                                     break;
443                             }
444
445                             // remove top special char from stack
446                             (stack->pop)(stack);
447
448                         // treat special as regular char
449                         } else if((stack->top)(stack, '\\')) {
450                             wprintw(window, "%c", *c);
451
452                             // remove backslash from stack
453                             (stack->pop)(stack);
454
455                         // opening special char
456                         } else {
457                             switch(*c) {
458                                 // enable highlight
459                                 case '*':
460                                     if(colors)
461                                         wattron(window, COLOR_PAIR(CP_RED));
462                                     break;
463                                 // enable underline
464                                 case '_':
465                                     wattron(window, A_UNDERLINE);
466                                     break;
467                                 // do nothing for backslashes
468                             }
469
470                             // push special char to stack
471                             (stack->push)(stack, *c);
472                         }
473
474                     } else {
475                         // remove backslash from stack
476                         if((stack->top)(stack, '\\'))
477                             (stack->pop)(stack);
478
479                         // print regular char
480                         wprintw(window, "%c", *c);
481                     }
482
483                     c++;
484                 }
485
486                 // pop stack until empty to prevent formated trailing spaces
487                 while(!(stack->empty)(stack)) {
488                     switch((stack->pop)(stack)) {
489                         // disable highlight
490                         case '*':
491                             if(colors)
492                                 wattron(window, COLOR_PAIR(CP_WHITE));
493                             break;
494                         // disable underline
495                         case '_':
496                             wattroff(window, A_UNDERLINE);
497                             break;
498                         // do nothing for backslashes
499                     }
500                 }
501             }
502         }
503
504         // fill rest off line with spaces
505         for(i = getcurx(window) - x; i < max_cols; i++)
506             wprintw(window, "%s", " ");
507
508         // reset to default color
509         if(colors)
510             wattron(window, COLOR_PAIR(CP_WHITE));
511         wattroff(window, A_UNDERLINE);
512         wattroff(window, A_REVERSE);
513     }
514
515     (stack->delete)(stack);
516 }
517
518 void fade_out(WINDOW *window, int trans, int colors, int invert) {
519     int i; // increment
520     if(colors) {
521         for(i = 22; i >= 0; i--) {
522
523             // dim color pairs
524             if(invert) {
525                 init_pair(CP_WHITE, white_ramp_invert[i], trans);
526                 init_pair(CP_BLUE, blue_ramp_invert[i], trans);
527                 init_pair(CP_RED, red_ramp_invert[i], trans);
528             } else {
529                 init_pair(CP_WHITE, white_ramp[i], trans);
530                 init_pair(CP_BLUE, blue_ramp[i], trans);
531                 init_pair(CP_RED, red_ramp[i], trans);
532             }
533
534             // refresh window with new color
535             wrefresh(window);
536
537             // delay for our eyes to recognize the change
538             usleep(FADE_DELAY);
539         }
540     }
541 }
542
543 void fade_in(WINDOW *window, int trans, int colors, int invert) {
544     int i; // increment
545     if(colors) {
546         for(i = 0; i <= 23; i++) {
547
548             // brighten color pairs
549             if(invert) {
550                 init_pair(CP_WHITE, white_ramp_invert[i], trans);
551                 init_pair(CP_BLUE, blue_ramp_invert[i], trans);
552                 init_pair(CP_RED, red_ramp_invert[i], trans);
553             } else {
554                 init_pair(CP_WHITE, white_ramp[i], trans);
555                 init_pair(CP_BLUE, blue_ramp[i], trans);
556                 init_pair(CP_RED, red_ramp[i], trans);
557             }
558
559             // refresh window with new color
560             wrefresh(window);
561
562             // delay for our eyes to recognize the change
563             usleep(FADE_DELAY);
564         }
565     }
566 }
567
568 int int_length (int val) {
569     int l = 1;
570     while(val > 9) {
571         l++;
572         val /= 10;
573     }
574     return l;
575 }