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