Fix compiler warnings (missing #includes etc) thrown up by RedHat.
[sgt/tweak] / curses.c
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();
31 if (has_colors())
32 start_color();
33 }
34
35 void display_cleanup(void)
36 {
37 endwin();
38 }
39
40 void display_moveto(int y, int x)
41 {
42 wmove(stdscr, y, x);
43 }
44
45 void display_refresh(void)
46 {
47 refresh();
48 }
49
50 void display_write_str(char *str)
51 {
52 waddstr(stdscr, str);
53 }
54
55 void display_write_chars(char *str, int len)
56 {
57 waddnstr(stdscr, str, len);
58 }
59
60 #define MAXCOLOURS 32
61 int attrs[MAXCOLOURS];
62
63 void display_define_colour(int colour, int fg, int bg)
64 {
65 static int colours[8] = {
66 COLOR_BLACK,
67 COLOR_RED,
68 COLOR_GREEN,
69 COLOR_YELLOW,
70 COLOR_BLUE,
71 COLOR_MAGENTA,
72 COLOR_CYAN,
73 COLOR_WHITE,
74 };
75
76 assert(colour >= 0 && colour < MAXCOLOURS && colour < COLOR_PAIRS-2);
77
78 assert(!(bg & ~7)); /* bold backgrounds are nonportable */
79 init_pair(colour+1, colours[fg & 7], colours[bg]);
80 attrs[colour] = (fg & 8 ? A_BOLD : 0) | COLOR_PAIR(colour+1);
81 }
82
83 void display_set_colour(int colour)
84 {
85 wattrset(stdscr, attrs[colour]);
86 }
87
88 void display_clear_to_eol(void)
89 {
90 wclrtoeol(stdscr);
91 }
92
93 int last_getch = ERR;
94
95 int display_getkey(void)
96 {
97 int ret;
98 extern void schedule_update(void);
99
100 if (last_getch != ERR) {
101 int ret = last_getch;
102 last_getch = ERR;
103 return ret;
104 }
105 while (1) {
106 ret = getch();
107 if (ret == KEY_RESIZE) {
108 schedule_update();
109 continue;
110 }
111 return ret;
112 }
113 }
114
115 int display_input_to_flush(void)
116 {
117 int ret;
118 if (last_getch != ERR)
119 return TRUE;
120
121 nodelay(stdscr, 1);
122 ret = getch();
123 nodelay(stdscr, 0);
124 if (ret == ERR)
125 return FALSE;
126
127 last_getch = ret;
128 return TRUE;
129 }
130
131 void display_post_error(void)
132 {
133 /* I don't _think_ we need do anything here */
134 }
135
136 void display_recheck_size(void)
137 {
138 get_screen_size ();
139 }