1 #include "tweak.h"
2
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <string.h>
6 #include <ctype.h>
7 #include <assert.h>
8
9 #if defined(unix) && !defined(GO32)
10 #include <signal.h>
11 #include <sys/ioctl.h>
12 #endif
13
14 #include <slang.h>
15
16 #if defined(unix) && !defined(GO32)
sigwinch(int sigtype)17 static int sigwinch (int sigtype)
18 {
19 extern void schedule_update(void);
20 schedule_update();
21 signal (SIGWINCH, (void *) sigwinch);
22 return 0;
23 }
24 #endif
25
26 int display_rows, display_cols;
27
display_beep(void)28 void display_beep(void)
29 {
30 SLtt_beep();
31 }
32
get_screen_size(void)33 static void get_screen_size (void) {
34 int r = 0, c = 0;
35
36 #ifdef TIOCGWINSZ
37 struct winsize wind_struct;
38
39 if ((ioctl(1,TIOCGWINSZ,&wind_struct) == 0)
40 || (ioctl(0, TIOCGWINSZ, &wind_struct) == 0)
41 || (ioctl(2, TIOCGWINSZ, &wind_struct) == 0)) {
42 c = (int) wind_struct.ws_col;
43 r = (int) wind_struct.ws_row;
44 }
45 #elif defined(MSDOS)
46 union REGS regs;
47
48 regs.h.ah = 0x0F;
49 int86 (0x10, ®s, ®s);
50 c = regs.h.ah;
51
52 regs.x.ax = 0x1130, regs.h.bh = 0;
53 int86 (0x10, ®s, ®s);
54 r = regs.h.dl + 1;
55 #endif
56
57 if ((r <= 0) || (r > 200)) r = 24;
58 if ((c <= 0) || (c > 250)) c = 80;
59 display_rows = SLtt_Screen_Rows = r;
60 display_cols = SLtt_Screen_Cols = c;
61 }
62
display_setup(void)63 void display_setup(void)
64 {
65 SLtt_get_terminfo();
66
67 if (SLang_init_tty (ABORT, 1, 0) == -1) {
68 fprintf(stderr, "tweak: SLang_init_tty: returned error code\n");
69 exit (1);
70 }
71 SLang_set_abort_signal (NULL);
72 SLtt_Use_Ansi_Colors = TRUE;
73
74 get_screen_size ();
75 if (SLsmg_init_smg () < 0) {
76 fprintf(stderr, "tweak: SLsmg_init_smg: returned error code\n");
77 SLang_reset_tty ();
78 exit (1);
79 }
80
81 #if defined(unix) && !defined(GO32)
82 signal (SIGWINCH, (void *) sigwinch);
83 #endif
84 }
85
display_cleanup(void)86 void display_cleanup(void)
87 {
88 SLsmg_reset_smg ();
89 SLang_reset_tty ();
90 }
91
display_moveto(int y,int x)92 void display_moveto(int y, int x)
93 {
94 SLsmg_gotorc(y, x);
95 }
96
display_refresh(void)97 void display_refresh(void)
98 {
99 SLsmg_refresh();
100 }
101
display_write_str(char * str)102 void display_write_str(char *str)
103 {
104 SLsmg_write_nchars(str, strlen(str));
105 }
106
display_write_chars(char * str,int len)107 void display_write_chars(char *str, int len)
108 {
109 SLsmg_write_nchars(str, len);
110 }
111
display_define_colour(int colour,int fg,int bg,int reverse)112 void display_define_colour(int colour, int fg, int bg, int reverse)
113 {
114 static char *colours[16] = {
115 "black", "red", "green", "brown",
116 "blue", "magenta", "cyan", "lightgray",
117 "gray", "brightred", "brightgreen", "yellow",
118 "brightblue", "brightmagenta", "brightcyan", "white",
119 };
120 char cname[40];
121
122 if (fg < 0 && bg < 0) {
123 /* FIXME: not sure how to support terminal default fg+bg */
124 fg = 7;
125 bg = 0;
126 }
127
128 sprintf(cname, "colour%d", colour);
129
130 SLtt_set_color(colour, cname, colours[fg], colours[bg]);
131 }
132
display_set_colour(int colour)133 void display_set_colour(int colour)
134 {
135 SLsmg_set_color(colour);
136 }
137
display_clear_to_eol(void)138 void display_clear_to_eol(void)
139 {
140 SLsmg_erase_eol();
141 }
142
display_getkey(void)143 int display_getkey(void)
144 {
145 return SLang_getkey();
146 }
147
display_input_to_flush(void)148 int display_input_to_flush(void)
149 {
150 return SLang_input_pending(0);
151 }
152
display_post_error(void)153 void display_post_error(void)
154 {
155 SLKeyBoard_Quit = 0;
156 SLang_Error = 0;
157 }
158
display_recheck_size(void)159 void display_recheck_size(void)
160 {
161 SLsmg_reset_smg ();
162 get_screen_size ();
163 SLsmg_init_smg ();
164 }
165