small readme changes
[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     // 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; // black in 8 color mode
120
121         if(COLORS == 256) {
122             if(notrans) trans = 16; // black in 256 color mode
123
124             // 256 color mode
125             init_pair(CP_WHITE, 255, trans);
126             init_pair(CP_BLUE, 123, trans);
127             init_pair(CP_RED, 213, trans);
128             init_pair(CP_YELLOW, 208, trans);
129
130             // enable color fading
131             if(!nofade) fade = 1;
132         } else {
133
134             // 8 color mode
135             init_pair(CP_WHITE, 7, trans);
136             init_pair(CP_BLUE, 4, trans);
137             init_pair(CP_RED, 1, trans);
138             init_pair(CP_YELLOW, 3, trans);
139         }
140
141         colors = 1;
142     }
143
144     // set background color of main window
145     if(colors)
146         wbkgd(stdscr, COLOR_PAIR(CP_YELLOW));
147
148     // setup main window
149     WINDOW *content = newwin(LINES - bar_top - bar_bottom, COLS, 0 + bar_top, 0);
150     if(colors)
151         wbkgd(content, COLOR_PAIR(CP_WHITE));
152
153     slide = deck->slide;
154     while(slide) {
155         // clear windows
156         werase(content);
157         werase(stdscr);
158
159         // always resize window in case terminal geometry has changed
160         wresize(content, LINES - bar_top - bar_bottom, COLS);
161
162         // setup header
163         if(bar_top) {
164             line = deck->header;
165             offset = next_blank(line->text, 0) + 1;
166             // add text to header
167             mvwprintw(stdscr,
168                       0, (COLS - line->length + offset) / 2,
169                       "%s", &line->text->text[offset]);
170         }
171
172         // setup footer
173         if(deck->headers > 1) {
174             line = deck->header->next;
175             offset = next_blank(line->text, 0) + 1;
176             // add text to left footer
177             mvwprintw(stdscr,
178                       LINES - 1, 3,
179                       "%s", &line->text->text[offset]);
180         }
181         // add slide number to right footer
182         mvwprintw(stdscr,
183                   LINES - 1, COLS - int_length(deck->slides) - int_length(sc) - 6,
184                   "%d / %d", sc, deck->slides);
185
186         // make header + fooder visible
187         wrefresh(stdscr);
188
189         line = slide->line;
190         l = 0;
191
192         // print lines
193         while(line) {
194             add_line(content, l, (COLS - max_cols) / 2, line, max_cols);
195             line = line->next;
196             l++;
197         }
198
199         // make content visible
200         wrefresh(content);
201
202         // fade in
203         if(fade)
204             fade_in(content, trans, colors);
205
206         // re-enable fading after any undefined key press
207         if(COLORS == 256 && !nofade) fade = 1;
208
209         // wait for user input
210         c = getch();
211
212         // evaluate user input
213         i = 0;
214         switch(c) {
215
216             // show previous slide
217             case KEY_UP:
218             case KEY_LEFT:
219             case 8:   // BACKSPACE (ascii)
220             case 127: // BACKSPACE (xterm)
221             case 263: // BACKSPACE (getty)
222             case 'h':
223             case 'k':
224                 if(slide->prev) {
225                     slide = slide->prev;
226                     sc--;
227                 }
228                 break;
229
230             // show next slide
231             case KEY_DOWN:
232             case KEY_RIGHT:
233             case '\n': // ENTER
234             case ' ':  // SPACE
235             case 'j':
236             case 'l':
237                 if(slide->next) {
238                     slide = slide->next;
239                     sc++;
240                 }
241                 break;
242
243             // show slide n
244             case '9': i++;
245             case '8': i++;
246             case '7': i++;
247             case '6': i++;
248             case '5': i++;
249             case '4': i++;
250             case '3': i++;
251             case '2': i++;
252             case '1': i++;
253                 if(i <= deck->slides) {
254                     while(sc != i) {
255                         // search forward
256                         if(sc < i) {
257                             if(slide->next) {
258                                 slide = slide->next;
259                                 sc++;
260                             }
261                         // search backward
262                         } else {
263                             if(slide->prev) {
264                                 slide = slide->prev;
265                                 sc--;
266                             }
267                         }
268                     }
269                 } else {
270                     // disable fading if slide n doesn't exist
271                     fade = 0;
272                 }
273                 break;
274
275             // show first slide
276             case KEY_HOME:
277                 slide = deck->slide;
278                 sc = 1;
279                 break;
280
281             // show last slide
282             case KEY_END:
283                 for(i = sc; i <= deck->slides; i++) {
284                     if(slide->next) {
285                             slide = slide->next;
286                             sc++;
287                     }
288                 }
289                 break;
290
291             // quit
292             case 'q':
293                 // do not fade out on exit
294                 fade = 0;
295                 slide = (void*)0;
296                 break;
297
298             default:
299                 // disable fading on undefined key press
300                 fade = 0;
301                 break;
302         }
303
304         // fade out
305         if(fade)
306             fade_out(content, trans, colors);
307     }
308
309     endwin();
310
311     return(0);
312 }
313
314 void add_line(WINDOW *window, int y, int x, line_t *line, int max_cols) {
315     int i = 0; // increment
316     char *c; // char pointer for iteration
317     char *special = "\\*_"; // list of interpreted chars
318     cstack_t *stack = cstack_init();
319
320     if(line->text->text) {
321         int offset = 0; // text offset
322
323         // IS_CODE
324         if(CHECK_BIT(line->bits, IS_CODE)) {
325
326             // set static offset for code
327             offset = CODE_INDENT;
328
329             // reverse color for code blocks
330             wattron(window, A_REVERSE);
331
332             // print whole lines
333             mvwprintw(window,
334                       y, x,
335                       "%s", &line->text->text[offset]);
336
337         } else {
338
339             // IS_H1 || IS_H2
340             if(CHECK_BIT(line->bits, IS_H1) || CHECK_BIT(line->bits, IS_H2)) {
341
342                 // set headline color
343                 wattron(window, COLOR_PAIR(CP_BLUE));
344
345                 // enable underline for H1
346                 if(CHECK_BIT(line->bits, IS_H1))
347                     wattron(window, A_UNDERLINE);
348
349                 // skip hashes
350                 while(line->text->text[offset] == '#')
351                     offset = next_word(line->text, offset);
352
353                 // print whole lines
354                 mvwprintw(window,
355                       y, x,
356                       "%s", &line->text->text[offset]);
357
358                 wattroff(window, A_UNDERLINE);
359
360             } else {
361                 // move the cursor in position
362                 wmove(window, y, x);
363
364                 // IS_QUOTE
365                 if(CHECK_BIT(line->bits, IS_QUOTE)) {
366                     while(line->text->text[offset] == '>') {
367                         // print a reverse color block
368                         wattron(window, A_REVERSE);
369                         wprintw(window, "%s", " ");
370                         wattroff(window, A_REVERSE);
371                         wprintw(window, "%s", " ");
372                         // find next quote or break
373                         offset++;
374                         if(line->text->text[offset] == ' ')
375                             offset = next_word(line->text, offset);
376                     }
377                 }
378
379                 // for each char in line
380                 c = &line->text->text[offset];
381                 while(*c) {
382
383                     // if char is in special char list
384                     if(strchr(special, *c)) {
385
386                         // closing special char (or second backslash)
387                         if((stack->top)(stack, *c)) {
388
389                             switch(*c) {
390                                 // print escaped backslash
391                                 case '\\':
392                                     wprintw(window, "%c", *c);
393                                     break;
394                                 // disable highlight
395                                 case '*':
396                                     wattron(window, COLOR_PAIR(CP_WHITE));
397                                     break;
398                                 // disable underline
399                                 case '_':
400                                     wattroff(window, A_UNDERLINE);
401                                     break;
402                             }
403
404                             // remove top special char from stack
405                             (stack->pop)(stack);
406
407                         // treat special as regular char
408                         } else if((stack->top)(stack, '\\')) {
409                             wprintw(window, "%c", *c);
410
411                             // remove backslash from stack
412                             (stack->pop)(stack);
413
414                         // opening special char
415                         } else {
416                             switch(*c) {
417                                 // enable highlight
418                                 case '*':
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                         case '\\':
444                             wprintw(window, "%c", '\\');
445                             break;
446                         // disable highlight
447                         case '*':
448                             wattron(window, COLOR_PAIR(CP_WHITE));
449                             break;
450                         // disable underline
451                         case '_':
452                             wattroff(window, A_UNDERLINE);
453                             break;
454                     }
455                 }
456             }
457         }
458
459         // fill rest off line with spaces
460         for(i = getcurx(window) - x; i < max_cols; i++)
461             wprintw(window, "%s", " ");
462
463         // reset to default color
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 }