Changes to make Tweak compile on Mac OS X (still as a Unix/curses
[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 use_default_colors();
34 }
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, int reverse)
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
78 if (fg < 0 && bg < 0) {
79 attrs[colour] = 0;
80 } else {
81 assert(colour >= 0 && colour < MAXCOLOURS);
82 assert(!(bg & ~7)); /* bold backgrounds are nonportable */
83 if (colour < COLOR_PAIRS-2) {
84 init_pair(colour+1, colours[fg & 7], colours[bg]);
85 attrs[colour] = (fg & 8 ? A_BOLD : 0) | COLOR_PAIR(colour+1);
86 } else {
87 /* can't allocate a colour pair, so we just use b&w attrs */
88 attrs[colour] = (fg & 8 ? A_BOLD : 0) | (reverse ? A_REVERSE : 0);
89 }
90 }
91 }
92
93 void display_set_colour(int colour)
94 {
95 wattrset(stdscr, attrs[colour]);
96 }
97
98 void display_clear_to_eol(void)
99 {
100 wclrtoeol(stdscr);
101 }
102
103 int last_getch = ERR;
104
105 int display_getkey(void)
106 {
107 int ret;
108 extern void schedule_update(void);
109
110 if (last_getch != ERR) {
111 int ret = last_getch;
112 last_getch = ERR;
113 return ret;
114 }
115 while (1) {
116 ret = getch();
117 if (ret == KEY_RESIZE) {
118 schedule_update();
119 continue;
120 }
121 return ret;
122 }
123 }
124
125 int display_input_to_flush(void)
126 {
127 int ret;
128 if (last_getch != ERR)
129 return TRUE;
130
131 nodelay(stdscr, 1);
132 ret = getch();
133 nodelay(stdscr, 0);
134 if (ret == ERR)
135 return FALSE;
136
137 last_getch = ret;
138 return TRUE;
139 }
140
141 void display_post_error(void)
142 {
143 /* I don't _think_ we need do anything here */
144 }
145
146 void display_recheck_size(void)
147 {
148 get_screen_size ();
149 }