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
7 * This file is part of mdp.
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.
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.
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/>.
24 #include <locale.h> // setlocale
27 #include <string.h> // strchr
30 #include "include/viewer.h"
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 };
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 };
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 };
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};
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};
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};
64 int ncurses_display(deck_t *deck, int notrans, int nofade, int invert) {
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
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
83 slide_t *slide = deck->slide;
87 // set max_lines if line count exceeded
88 max_lines = (slide->lines > max_lines) ? slide->lines : max_lines;
91 // set max_cols if length exceeded
92 max_cols = (line->length > max_cols) ? line->length : max_cols;
98 // set locale to display UTF-8 correctly in ncurses
99 setlocale(LC_CTYPE, "");
104 if((max_cols > COLS) ||
105 (max_lines + bar_top + bar_bottom + 2 > LINES)) {
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);
116 // disable output of keyboard typing
119 // make getch() process one char at a time
126 if(has_colors() == TRUE) {
128 use_default_colors();
135 trans = 15; // white in 256 color mode
137 trans = 16; // black in 256 color mode
142 init_pair(CP_WHITE, 232, trans);
143 init_pair(CP_BLUE, 21, trans);
144 init_pair(CP_RED, 196, trans);
146 init_pair(CP_WHITE, 255, trans);
147 init_pair(CP_BLUE, 123, trans);
148 init_pair(CP_RED, 213, trans);
150 init_pair(CP_YELLOW, 208, trans);
152 // enable color fading
153 if(!nofade) fade = 1;
160 trans = 7; // white in 8 color mode
162 trans = 0; // black in 8 color mode
167 init_pair(CP_WHITE, 0, trans);
169 init_pair(CP_WHITE, 7, trans);
171 init_pair(CP_BLUE, 4, trans);
172 init_pair(CP_RED, 1, trans);
173 init_pair(CP_YELLOW, 3, trans);
179 // set background color of main window
181 wbkgd(stdscr, COLOR_PAIR(CP_YELLOW));
184 WINDOW *content = newwin(LINES - bar_top - bar_bottom, COLS, 0 + bar_top, 0);
186 wbkgd(content, COLOR_PAIR(CP_WHITE));
194 // always resize window in case terminal geometry has changed
195 wresize(content, LINES - bar_top - bar_bottom, COLS);
200 offset = next_blank(line->text, 0) + 1;
201 // add text to header
203 0, (COLS - line->length + offset) / 2,
204 "%s", &line->text->text[offset]);
208 if(deck->headers > 1) {
209 line = deck->header->next;
210 offset = next_blank(line->text, 0) + 1;
211 // add text to left footer
214 "%s", &line->text->text[offset]);
216 // add slide number to right footer
218 LINES - 1, COLS - int_length(deck->slides) - int_length(sc) - 6,
219 "%d / %d", sc, deck->slides);
221 // make header + fooder visible
229 add_line(content, l, (COLS - max_cols) / 2, line, max_cols, colors);
234 // make content visible
239 fade_in(content, trans, colors, invert);
241 // re-enable fading after any undefined key press
242 if(COLORS == 256 && !nofade) fade = 1;
244 // wait for user input
247 // evaluate user input
251 // show previous slide
254 case 8: // BACKSPACE (ascii)
255 case 127: // BACKSPACE (xterm)
256 case 263: // BACKSPACE (getty)
288 if(i <= deck->slides) {
305 // disable fading if slide n doesn't exist
318 for(i = sc; i <= deck->slides; i++) {
328 // do not fade out on exit
334 // disable fading on undefined key press
341 fade_out(content, trans, colors, invert);
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();
355 if(line->text->text) {
356 int offset = 0; // text offset
359 if(CHECK_BIT(line->bits, IS_CODE)) {
361 // set static offset for code
362 offset = CODE_INDENT;
364 // reverse color for code blocks
365 wattron(window, A_REVERSE);
370 "%s", &line->text->text[offset]);
375 if(CHECK_BIT(line->bits, IS_H1) || CHECK_BIT(line->bits, IS_H2)) {
377 // set headline color
379 wattron(window, COLOR_PAIR(CP_BLUE));
381 // enable underline for H1
382 if(CHECK_BIT(line->bits, IS_H1))
383 wattron(window, A_UNDERLINE);
386 while(line->text->text[offset] == '#')
387 offset = next_word(line->text, offset);
392 "%s", &line->text->text[offset]);
394 wattroff(window, A_UNDERLINE);
397 // move the cursor in position
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
410 if(line->text->text[offset] == ' ')
411 offset = next_word(line->text, offset);
415 // for each char in line
416 c = &line->text->text[offset];
419 // if char is in special char list
420 if(strchr(special, *c)) {
422 // closing special char (or second backslash)
423 if((stack->top)(stack, *c)) {
426 // print escaped backslash
428 wprintw(window, "%c", *c);
433 wattron(window, COLOR_PAIR(CP_WHITE));
437 wattroff(window, A_UNDERLINE);
441 // remove top special char from stack
444 // treat special as regular char
445 } else if((stack->top)(stack, '\\')) {
446 wprintw(window, "%c", *c);
448 // remove backslash from stack
451 // opening special char
457 wattron(window, COLOR_PAIR(CP_RED));
461 wattron(window, A_UNDERLINE);
463 // do nothing for backslashes
466 // push special char to stack
467 (stack->push)(stack, *c);
471 // print regular char
472 wprintw(window, "%c", *c);
478 // pop stack until empty to prevent formated trailing spaces
479 while(!(stack->empty)(stack)) {
480 switch((stack->pop)(stack)) {
484 wattron(window, COLOR_PAIR(CP_WHITE));
488 wattroff(window, A_UNDERLINE);
490 // do nothing for backslashes
496 // fill rest off line with spaces
497 for(i = getcurx(window) - x; i < max_cols; i++)
498 wprintw(window, "%s", " ");
500 // reset to default color
502 wattron(window, COLOR_PAIR(CP_WHITE));
503 wattroff(window, A_UNDERLINE);
504 wattroff(window, A_REVERSE);
507 (stack->delete)(stack);
510 void fade_out(WINDOW *window, int trans, int colors, int invert) {
513 for(i = 22; i >= 0; i--) {
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);
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);
526 // refresh window with new color
529 // delay for our eyes to recognize the change
535 void fade_in(WINDOW *window, int trans, int colors, int invert) {
538 for(i = 0; i <= 23; i++) {
540 // brighten color pairs
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);
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);
551 // refresh window with new color
554 // delay for our eyes to recognize the change
560 int int_length (int val) {