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