1 /* FreeBSD syscons video */
2
3 #ifdef FBSD_CONSOLE
4
5 #include <stdio.h>
6 #include <fcntl.h>
7 #include <sys/stat.h>
8 #include <sys/fbio.h>
9 #include <sys/kbio.h>
10 #include <sys/consio.h>
11 #include <termios.h>
12 #include <sys/mman.h>
13 #include <stdlib.h>
14 #include <unistd.h>
15 #include <stdarg.h>
16 #include <sys/time.h>
17 #include <sys/types.h>
18 #include <sys/socket.h>
19 #include <netinet/in.h>
20 #include <errno.h>
21 #include <signal.h>
22 #include "h2def.h"
23 #include "soundst.h"
24
25 #define SavePalette() SavRstPalette(FBIO_GETPALETTE)
26 #define RestorePalette() SavRstPalette(FBIO_SETPALETTE)
27
28 extern int usemouse;
29 int mouse_scale_factor;
30
31 static event_t event;
32
33 /* VGA state flags */
34
35 #define FB_MAPPED 1
36 #define CONS_ACTIVE 2
37 #define TTY_STARTED 4
38 #define KBD_RAWMODE 8
39 #define GRAPH_MODE 16
40 #define PALETTE_SAVED 32
41
42 struct vgainfo {
43 int fd; /* console file descriptor */
44 int oldmode; /* old video mode */
45 int oldkmode; /* old keyboard mode */
46 int consnum; /* current active console */
47 int ourcons; /* our console */
48 vtmode_t curmod; /* current console mode */
49 struct termios oti; /* old termios settings */
50 struct termios ti; /* our termios settings */
51 void *lfb; /* linear frame buffer */
52 int bufsize; /* frame buffer size */
53 int flags;
54 unsigned char red[256];
55 unsigned char green[256];
56 unsigned char blue[256];
57 };
58
59 void *VideoBuffer;
60
61 struct vgainfo VGAInfo;
62
63 /* Signals array */
64
65 int signals[]= {
66 SIGHUP,SIGINT,SIGQUIT,SIGILL,SIGTRAP,SIGEMT,SIGFPE,SIGKILL,
67 SIGBUS,SIGSEGV,SIGSYS,SIGPIPE,SIGALRM,SIGTERM,SIGURG,SIGSTOP,SIGTSTP,
68 SIGCONT,SIGCHLD,SIGTTIN,SIGTTOU,SIGIO,SIGXCPU,SIGXFSZ,SIGVTALRM,SIGPROF,
69 SIGWINCH,SIGINFO,SIGUSR1,SIGUSR2
70 };
71
ReadKey(void)72 unsigned char ReadKey(void) {
73 unsigned char c;
74 if (read(VGAInfo.fd,&c,1)<=0) return 0;
75 return c;
76 }
77
78 unsigned char keys0[]="1234567890-=\x08";
79 unsigned char keys1[]="\x09qwertyuiop[]";
80 unsigned char keys2[]="asdfghjkl;'";
81 unsigned char keys3[]="zxcvbnm,./";
82
GetKey(void)83 unsigned int GetKey(void) {
84 unsigned int c;
85 unsigned int r;
86 c=(unsigned int)ReadKey();
87 if (!c) return 0;
88 r=(c&0x80)?0x80000000:0;
89 c&=~0x80;
90 if (c>=2 && c<=11)
91 c=keys0[c-2];
92 else if (c>=15 && c<=27)
93 c=keys1[c-15];
94 else if (c>=30 && c<=40)
95 c=keys2[c-30];
96 else if (c>=44 && c<=53)
97 c=keys3[c-44];
98 else
99 switch (c) {
100 case 57: c=' '; break;
101 case 77: c=KEY_RIGHTARROW; break;
102 case 75: c=KEY_LEFTARROW; break;
103 case 72: c=KEY_UPARROW; break;
104 case 80: c=KEY_DOWNARROW; break;
105 case 1: c=KEY_ESCAPE; break;
106 case 28: c=KEY_ENTER; break;
107 case 9: c=KEY_TAB; break;
108 case 58: c=KEY_CAPSLOCK; break;
109 case 59: c=KEY_F1; break;
110 case 60: c=KEY_F2; break;
111 case 61: c=KEY_F3; break;
112 case 62: c=KEY_F4; break;
113 case 63: c=KEY_F5; break;
114 case 64: c=KEY_F6; break;
115 case 65: c=KEY_F7; break;
116 case 66: c=KEY_F8; break;
117 case 67: c=KEY_F9; break;
118 case 68: c=KEY_F10; break;
119 case 87: c=KEY_F11; break;
120 case 88: c=KEY_F12; break;
121 case 14: c=KEY_BACKSPACE; break;
122 case 69: c=KEY_PAUSE; break;
123 case 13: c=KEY_EQUALS; break;
124 case 12: c=KEY_MINUS; break;
125 case 42: c=KEY_RSHIFT; break;
126 case 29: c=KEY_RCTRL; break;
127 case 56: c=KEY_RALT; break;
128 case 0x35: c=KEY_FIVE; break;
129 case 0x36: c=KEY_SIX; break;
130 case 0x37: c=KEY_SEVEN; break;
131 // case 0x38: c=KEY_EIGHT; break;
132 // case 0x39: c=KEY_NINE; break;
133 case 0x30: c=KEY_ZERO; break;
134 case 0x5C: c=KEY_BACKSLASH; break;
135 case 71: c=KEY_HOME; break;
136 case 79: c=KEY_END; break;
137 case 73: c=KEY_PGUP; break;
138 case 81: c=KEY_PGDN; break;
139 }
140 return (c|r);
141 }
142
143 static int mousex, mousey, mousebutt;
144
145 struct mouse_info minfo;
146
FBSD_CheckMouse(void)147 void FBSD_CheckMouse(void) {
148
149 minfo.operation=MOUSE_GETINFO;
150
151 if (ioctl(VGAInfo.fd,CONS_MOUSECTL,&minfo)<0)
152 return;
153
154 if (mousebutt!=minfo.u.data.buttons ||
155 mousex!=minfo.u.data.x ||
156 mousey!=minfo.u.data.y) {
157 event.type=ev_mouse;
158 event.data2=((minfo.u.data.x-mousex)*mouse_scale_factor);
159 event.data3=((mousey-minfo.u.data.y)*mouse_scale_factor);
160 event.data1=mousebutt=minfo.u.data.buttons;
161 minfo.u.data.x=mousex;
162 minfo.u.data.y=mousey;
163 minfo.operation=MOUSE_MOVEABS;
164 if (ioctl(VGAInfo.fd,CONS_MOUSECTL,&minfo)<0)
165 return;
166 H2_PostEvent(&event);
167 }
168 }
169
SavRstPalette(int ioctlnum)170 int SavRstPalette(int ioctlnum) {
171 video_color_palette_t pal;
172 pal.index=0;
173 pal.count=256;
174 pal.red=VGAInfo.red;
175 pal.green=VGAInfo.green;
176 pal.blue=VGAInfo.blue;
177 pal.transparent=NULL;
178 return ioctl(VGAInfo.fd,ioctlnum,&pal);
179 }
180
181 void sighandler(int sig);
182
catchallsigs(void)183 void catchallsigs(void) {
184 int i;
185 for (i=0;i<sizeof(signals)/sizeof(int);i++)
186 signal(signals[i],sighandler);
187 }
188
freeallsigs(void)189 void freeallsigs(void) {
190 int i;
191 for (i=0;i<sizeof(signals)/sizeof(int);i++)
192 signal(signals[i],SIG_DFL);
193 }
194
195 int FBSD_ShutdownGraphics(void);
196
sighandler(int sig)197 void sighandler(int sig) {
198 if (sig==SIGWINCH) return;
199 printf("Fatal signal caught %d!! Dying!\n",sig);
200 freeallsigs();
201 FBSD_ShutdownGraphics();
202 abort();
203 }
204
FBSD_ShutdownGraphics(void)205 int FBSD_ShutdownGraphics(void) {
206
207 /* If StartupGraphics() fails, it leaves a big piece of shit */
208 /* Trying to take it away */
209
210 if (VGAInfo.flags & PALETTE_SAVED)
211 RestorePalette();
212
213 if (VGAInfo.flags & GRAPH_MODE)
214 ioctl(VGAInfo.fd,FBIO_SETMODE,&VGAInfo.oldmode);
215
216 if (VGAInfo.flags & KBD_RAWMODE) {
217 ioctl(VGAInfo.fd,KDSKBMODE,VGAInfo.oldkmode);
218 }
219
220 if (VGAInfo.flags & TTY_STARTED)
221 tcsetattr(VGAInfo.fd,TCSANOW,&VGAInfo.oti);
222
223 if (VGAInfo.flags & CONS_ACTIVE)
224 ioctl(VGAInfo.fd,VT_ACTIVATE,VGAInfo.consnum);
225
226 if (VGAInfo.flags & FB_MAPPED)
227 munmap(VGAInfo.lfb,0x10000);
228
229 close(VGAInfo.fd);
230
231 freeallsigs();
232
233 VGAInfo.flags=0;
234
235 return 0;
236 }
237
FBSD_StartupGraphics(void)238 int FBSD_StartupGraphics(void) {
239
240 int gmode;
241
242 VGAInfo.fd=0; /* STDIN */
243
244 VGAInfo.flags=0;
245
246 catchallsigs();
247
248 #define CHECKIT(xxx) if ((xxx)<0) goto fatal_error;
249
250 /* set noncanonical input mode */
251
252 if (tcgetattr(VGAInfo.fd,&VGAInfo.oti)) goto fatal_error;
253 VGAInfo.ti.c_lflag&=~(ICANON | ECHOKE | ECHOE | ECHONL | ECHOPRT | ECHOCTL | ECHO | ISIG);
254 VGAInfo.ti.c_cc[VMIN]=VGAInfo.ti.c_cc[VTIME]=0;
255 if (tcsetattr(VGAInfo.fd,TCSANOW,&VGAInfo.ti)) goto fatal_error;
256
257 VGAInfo.flags |= TTY_STARTED;
258
259 /* set raw keyboard mode */
260
261 CHECKIT(ioctl(VGAInfo.fd,KDGKBMODE,&VGAInfo.oldkmode));
262 CHECKIT(ioctl(VGAInfo.fd,KDSKBMODE,K_RAW));
263 CHECKIT(fcntl(VGAInfo.fd,F_SETFL,O_NONBLOCK));
264 VGAInfo.flags |= KBD_RAWMODE;
265
266 gmode=M_VGA13;
267 CHECKIT(ioctl(VGAInfo.fd,FBIO_GETMODE,&VGAInfo.oldmode));
268 CHECKIT(ioctl(VGAInfo.fd,FBIO_SETMODE,&gmode));
269 VGAInfo.flags |= GRAPH_MODE;
270
271 VGAInfo.bufsize=0x10000;
272 VGAInfo.lfb=mmap(0,0x10000,PROT_READ|PROT_WRITE,MAP_SHARED,VGAInfo.fd,0);
273 if (!VGAInfo.lfb) goto fatal_error;
274 VGAInfo.flags |= FB_MAPPED;
275
276 CHECKIT(SavePalette());
277 VGAInfo.flags |= PALETTE_SAVED;
278
279 VideoBuffer=VGAInfo.lfb;
280
281 /* All ok */
282 return 0;
283
284 fatal_error:
285
286 /* OOPS! Console can't set up graphics! */
287
288 FBSD_ShutdownGraphics();
289 return -1;
290 }
291
292
FBSD_GetEvent(void)293 void FBSD_GetEvent(void)
294 {
295 unsigned int k;
296
297 /* Check the keyboard */
298
299 k=GetKey();
300
301 if (k) {
302 if (k&0x80000000) {
303 event.type=ev_keyup;
304 event.data1=k&~0x80000000;
305 }
306 else {
307 event.type=ev_keydown;
308 event.data1=k;
309 }
310 H2_PostEvent(&event);
311 }
312 if (usemouse)
313 FBSD_CheckMouse();
314
315 }
316
317
FBSD_InitGraphics(void)318 int FBSD_InitGraphics(void)
319 {
320
321 if (FBSD_StartupGraphics()<0) {
322 fprintf(stderr,"syscons: Unable to initialize console: flags=%04X\n",VGAInfo.flags);
323 return 0;
324 };
325
326 minfo.operation=MOUSE_GETINFO;
327
328 if (ioctl(VGAInfo.fd,CONS_MOUSECTL,&minfo)>=0) {
329 mousex=minfo.u.data.x;
330 mousey=minfo.u.data.y;
331 mousebutt=minfo.u.data.buttons;
332 printf("syscons: Init mouse x=%d, y=%d, butt=%d\n",mousex,mousey,mousebutt);
333 }
334 else {
335 usemouse=0;
336 printf("syscons: Mouse init failed: %s\n",strerror(errno));
337 }
338 return 1;
339 }
340
341
FBSD_FinishUpdate(void)342 void FBSD_FinishUpdate(void)
343 {
344 memcpy(VGAInfo.lfb,screen,320*200);
345 }
346
FBSD_SetPalette(byte * palette)347 void FBSD_SetPalette(byte *palette)
348 {
349 int i,j;
350 byte red[256],green[256],blue[256];
351 video_color_palette_t pal;
352
353 // set the VGA colormap entries
354 for (i=0,j=0 ; i<768 ; i+=3,j++)
355 {
356 red[j] = gammatable[usegamma][palette[i]];
357 green[j] = gammatable[usegamma][palette[i+1]];
358 blue[j] = gammatable[usegamma][palette[i+2]];
359 }
360
361 pal.index=0;
362 pal.count=256;
363 pal.red=red;
364 pal.green=green;
365 pal.blue=blue;
366 pal.transparent=NULL;
367 if (ioctl(VGAInfo.fd,FBIO_SETPALETTE,&pal)<0) {
368 I_Error("syscons: cannot set palette!");
369 }
370
371 }
372
373 #endif
374
FBSD_Stub(void)375 void FBSD_Stub(void) {
376
377 }
378