6e182d98 |
1 | #include <stdio.h> |
2 | #include <stdlib.h> |
3 | #include <string.h> |
4 | #include <ctype.h> |
5 | #include <assert.h> |
6 | |
7 | #include <curses.h> |
8 | |
9 | #include "tweak.h" |
10 | |
11 | int display_rows, display_cols; |
12 | |
13 | void display_beep(void) |
14 | { |
15 | beep(); |
16 | } |
17 | |
18 | static void get_screen_size (void) { |
19 | getmaxyx(stdscr, display_rows, display_cols); |
20 | } |
21 | |
22 | void display_setup(void) |
23 | { |
24 | initscr(); |
25 | noecho(); |
26 | keypad(stdscr, 0); |
27 | raw(); |
28 | move(0,0); |
29 | refresh(); |
30 | get_screen_size(); |
1610a3c6 |
31 | if (has_colors()) { |
6e182d98 |
32 | start_color(); |
1610a3c6 |
33 | use_default_colors(); |
34 | } |
6e182d98 |
35 | } |
36 | |
37 | void display_cleanup(void) |
38 | { |
39 | endwin(); |
40 | } |
41 | |
42 | void display_moveto(int y, int x) |
43 | { |
44 | wmove(stdscr, y, x); |
45 | } |
46 | |
47 | void display_refresh(void) |
48 | { |
49 | refresh(); |
50 | } |
51 | |
52 | void display_write_str(char *str) |
53 | { |
54 | waddstr(stdscr, str); |
55 | } |
56 | |
57 | void display_write_chars(char *str, int len) |
58 | { |
59 | waddnstr(stdscr, str, len); |
60 | } |
61 | |
62 | #define MAXCOLOURS 32 |
63 | int attrs[MAXCOLOURS]; |
64 | |
65 | void display_define_colour(int colour, int fg, int bg) |
66 | { |
67 | static int colours[8] = { |
68 | COLOR_BLACK, |
69 | COLOR_RED, |
70 | COLOR_GREEN, |
71 | COLOR_YELLOW, |
72 | COLOR_BLUE, |
73 | COLOR_MAGENTA, |
74 | COLOR_CYAN, |
75 | COLOR_WHITE, |
76 | }; |
77 | |
1610a3c6 |
78 | if (fg < 0 && bg < 0) { |
79 | attrs[colour] = 0; |
80 | } else { |
81 | assert(colour >= 0 && colour < MAXCOLOURS && colour < COLOR_PAIRS-2); |
82 | assert(!(bg & ~7)); /* bold backgrounds are nonportable */ |
83 | init_pair(colour+1, colours[fg & 7], colours[bg]); |
84 | attrs[colour] = (fg & 8 ? A_BOLD : 0) | COLOR_PAIR(colour+1); |
85 | } |
6e182d98 |
86 | } |
87 | |
88 | void display_set_colour(int colour) |
89 | { |
90 | wattrset(stdscr, attrs[colour]); |
91 | } |
92 | |
93 | void display_clear_to_eol(void) |
94 | { |
95 | wclrtoeol(stdscr); |
96 | } |
97 | |
98 | int last_getch = ERR; |
99 | |
100 | int display_getkey(void) |
101 | { |
102 | int ret; |
103 | extern void schedule_update(void); |
104 | |
105 | if (last_getch != ERR) { |
106 | int ret = last_getch; |
107 | last_getch = ERR; |
108 | return ret; |
109 | } |
110 | while (1) { |
111 | ret = getch(); |
112 | if (ret == KEY_RESIZE) { |
113 | schedule_update(); |
114 | continue; |
115 | } |
116 | return ret; |
117 | } |
118 | } |
119 | |
120 | int display_input_to_flush(void) |
121 | { |
122 | int ret; |
123 | if (last_getch != ERR) |
124 | return TRUE; |
125 | |
126 | nodelay(stdscr, 1); |
127 | ret = getch(); |
128 | nodelay(stdscr, 0); |
129 | if (ret == ERR) |
130 | return FALSE; |
131 | |
132 | last_getch = ret; |
133 | return TRUE; |
134 | } |
135 | |
136 | void display_post_error(void) |
137 | { |
138 | /* I don't _think_ we need do anything here */ |
139 | } |
140 | |
141 | void display_recheck_size(void) |
142 | { |
143 | get_screen_size (); |
144 | } |