|
|
1.1 root 1: #include <termios.h>
2: #include <sys/ioctl.h>
3: #include <sys/stat.h>
4: #include <sys/vt.h>
5: #include <stdarg.h>
6: #include <stdio.h>
7: #include <signal.h>
8: #include "/usr/local/include/vga.h"
9: #include "/usr/local/include/vgakeyboard.h"
10: #include "/usr/local/include/vgamouse.h"
11:
12: #include "quakedef.h"
13: #include "vid_256.h"
14:
15: #define stringify(m) { #m, m }
16:
17: viddef_t vid;
18:
19: int num_modes;
20: vga_modeinfo *modes;
21: int current_mode;
22:
23: int num_shades=32;
24:
25: struct
26: {
27: char *name;
28: int num;
29: } mice[] =
30: {
31: stringify(MOUSE_MICROSOFT),
32: stringify(MOUSE_MOUSESYSTEMS),
33: stringify(MOUSE_MMSERIES),
34: stringify(MOUSE_LOGITECH),
35: stringify(MOUSE_BUSMOUSE),
36: stringify(MOUSE_PS2),
37: };
38:
39: static unsigned char scantokey[128];
40:
41: int num_mice = sizeof (mice) / sizeof(mice[0]);
42:
43: int d_con_indirect = 0;
44:
45: int svgalib_inited=0;
46: int UseMouse = 1;
47: int UseDisplay = 1;
48: int UseKeyboard = 1;
49:
50: int mouserate = MOUSE_DEFAULTSAMPLERATE;
51:
52: char *framebuffer_ptr;
53:
54: cvar_t mouse_button_commands[3] =
55: {
56: {"mouse1","+attack"},
57: {"mouse2","+strafe"},
58: {"mouse3","+forward"},
59: };
60: cvar_t mouse_sensitivity = {"sensitivity","5"};
61:
62: int mouse_buttons;
63: int mouse_oldbuttonstate;
64: int mouse_x, mouse_y;
65:
66: /*
67: =================
68: VID_Gamma_f
69:
70: Keybinding command
71: =================
72: */
73: void VID_Gamma_f (void)
74: {
75: float gamma, f, inf;
76: unsigned char palette[768];
77: int i;
78:
79: if (Cmd_Argc () == 2)
80: {
81: gamma = Q_atof (Cmd_Argv(1));
82:
83: for (i=0 ; i<768 ; i++)
84: {
85: f = pow ( (host_basepal[i]+1)/256.0 , gamma );
86: inf = f*255 + 0.5;
87: if (inf < 0)
88: inf = 0;
89: if (inf > 255)
90: inf = 255;
91: palette[i] = inf;
92: }
93:
94: VID_SetPalette (palette);
95:
96: vid.recalc_refdef = 1; // force a surface cache flush
97: }
98: }
99:
100:
101: void VID_InitModes(void)
102: {
103:
104: int i;
105:
106: // get complete information on all modes
107:
108: num_modes = vga_lastmodenumber()+1;
109: I_Printf("num_modes = %d\n", num_modes);
110: modes = Z_Malloc(num_modes * sizeof(vga_modeinfo));
111: for (i=0 ; i<num_modes ; i++)
112: {
113: if (vga_hasmode(i))
114: Q_memcpy(&modes[i], vga_getmodeinfo(i), sizeof (vga_modeinfo));
115: else
116: modes[i].width = 0; // means not available
117: }
118:
119: // filter for modes i don't support
120:
121: for (i=0 ; i<num_modes ; i++)
122: {
123: if (modes[i].bytesperpixel != 1) modes[i].width = 0;
124: }
125:
126: }
127:
128: int get_mode(char *name, int width, int height, int depth)
129: {
130:
131: int i;
132: int ok, match;
133:
134: match = (!!width) + (!!height)*2 + (!!depth)*4;
135:
136: if (name)
137: {
138: i = vga_getmodenumber(name);
139: if (!modes[i].width)
140: {
141: I_Printf("Mode [%s] not supported\n", name);
142: i = G320x200x256;
143: }
144: }
145: else
146: {
147: for (i=0 ; i<num_modes ; i++)
148: if (modes[i].width)
149: {
150: ok = (modes[i].width == width)
151: + (modes[i].height == height)*2
152: + (modes[i].bytesperpixel == depth/8)*4;
153: if ((ok & match) == ok)
154: break;
155: }
156: if (i==num_modes)
157: {
158: I_Printf("Mode %dx%d (%d bits) not supported\n",
159: width, height, depth);
160: i = G320x200x256;
161: }
162: }
163:
164: return i;
165:
166: }
167:
168: int matchmouse(int mouse, char *name)
169: {
170: int i;
171: for (i=0 ; i<num_mice ; i++)
172: if (!strcmp(mice[i].name, name))
173: return i;
174: return mouse;
175: }
176:
177: #if 0
178:
179: void vtswitch(int newconsole)
180: {
181:
182: int fd;
183: struct vt_stat x;
184:
185: // switch consoles and wait until reactivated
186: fd = open("/dev/console", O_RDONLY);
187: ioctl(fd, VT_GETSTATE, &x);
188: ioctl(fd, VT_ACTIVATE, newconsole);
189: ioctl(fd, VT_WAITACTIVE, x.v_active);
190: close(fd);
191:
192: }
193:
194: #endif
195:
196: void keyhandler(int scancode, int state)
197: {
198:
199: int sc;
200:
201: sc = scancode & 0x7f;
202: // Con_Printf("scancode=%x (%d%s)\n", scancode, sc, scancode&0x80?"+128":"");
203: Key_Event(scantokey[sc], state == KEY_EVENTPRESS);
204:
205: }
206:
207: void VID_Shutdown(void)
208: {
209:
210: if (!svgalib_inited) return;
211:
212: // printf("shutdown graphics called\n");
213: if (UseKeyboard)
214: keyboard_close();
215: if (UseDisplay)
216: vga_setmode(TEXT);
217: // printf("shutdown graphics finished\n");
218:
219: svgalib_inited = 0;
220:
221: }
222:
223: void VID_SetPalette(byte *palette)
224: {
225:
226: static int tmppal[256*3];
227: int *tp;
228: int i;
229:
230: if (!svgalib_inited)
231: return;
232:
233: if (vga_getcolors() == 256)
234: {
235:
236: tp = tmppal;
237: for (i=256*3 ; i ; i--)
238: *(tp++) = *(palette++) >> 2;
239:
240: if (UseDisplay)
241: vga_setpalvec(0, 256, tmppal);
242:
243: }
244:
245: }
246:
247: void VID_Init(unsigned char *palette)
248: {
249:
250: int i;
251: int w, h, d;
252:
253: if (svgalib_inited)
254: return;
255:
256: Cmd_AddCommand ("gamma", VID_Gamma_f);
257:
258: /*
259: signal(SIGHUP, (void (*)(int)) I_Quit);
260: signal(SIGINT, (void (*)(int)) I_Quit);
261: signal(SIGKILL, (void (*)(int)) I_Quit);
262: signal(SIGTERM, (void (*)(int)) I_Quit);
263: signal(SIGSTOP, (void (*)(int)) I_Quit);
264: signal(SIGQUIT, (void (*)(int)) I_Quit);
265: */
266:
267: if (UseDisplay)
268: {
269: vga_init();
270:
271: VID_InitModes();
272:
273: // interpret command-line params
274:
275: w = h = d = 0;
276: if (getenv("GSVGAMODE"))
277: current_mode = get_mode(getenv("GSVGAMODE"), w, h, d);
278: else if (COM_CheckParm("-mode"))
279: current_mode = get_mode(com_argv[COM_CheckParm("-mode")+1], w, h, d);
280: else if (COM_CheckParm("-w") || COM_CheckParm("-h")
281: || COM_CheckParm("-d"))
282: {
283: if (COM_CheckParm("-w"))
284: w = Q_atoi(com_argv[COM_CheckParm("-w")+1]);
285: if (COM_CheckParm("-h"))
286: h = Q_atoi(com_argv[COM_CheckParm("-h")+1]);
287: if (COM_CheckParm("-d"))
288: d = Q_atoi(com_argv[COM_CheckParm("-d")+1]);
289: current_mode = get_mode(0, w, h, d);
290: }
291: else
292: current_mode = G320x200x256;
293:
294: // set vid parameters
295:
296: vid.width = modes[current_mode].width;
297: vid.rowbytes = modes[current_mode].linewidth;
298: vid.height = modes[current_mode].height;
299: vid.aspect = 1.0;
300: vid.colormap = (pixel_t *) colormap256;
301: vid.fullbright = 256 - LittleLong (*((int *)vid.colormap + 2048));
302: vid.buffer = (pixel_t *) Hunk_HighAlloc(vid.rowbytes * vid.height);
303:
304: // get goin'
305:
306: vga_setmode(current_mode);
307: framebuffer_ptr = (char *) vga_getgraphmem();
308: // if (vga_setlinearaddressing()>0)
309: // framebuffer_ptr = (char *) vga_getgraphmem();
310: if (!framebuffer_ptr)
311: I_Error("This mode isn't hapnin'\n");
312:
313: vga_setpage(0);
314:
315: svgalib_inited=1;
316:
317: VID_SetPalette(palette);
318:
319: }
320:
321: if (COM_CheckParm("-nokybd")) UseKeyboard = 0;
322:
323: if (UseKeyboard)
324: {
325: for (i=0 ; i<128 ; i++)
326: scantokey[i] = ' ';
327:
328: scantokey[42] = K_SHIFT;
329: scantokey[54] = K_SHIFT;
330: scantokey[72] = K_UPARROW;
331: scantokey[103] = K_UPARROW;
332: scantokey[80] = K_DOWNARROW;
333: scantokey[108] = K_DOWNARROW;
334: scantokey[75] = K_LEFTARROW;
335: scantokey[105] = K_LEFTARROW;
336: scantokey[77] = K_RIGHTARROW;
337: scantokey[106] = K_RIGHTARROW;
338: scantokey[29] = K_CTRL;
339: scantokey[97] = K_CTRL;
340: scantokey[56] = K_ALT;
341: scantokey[100] = K_ALT;
342: // scantokey[58] = JK_CAPS;
343: // scantokey[69] = JK_NUM_LOCK;
344: // scantokey[71] = JK_HOME;
345: // scantokey[79] = JK_END;
346: // scantokey[83] = JK_DEL;
347: scantokey[1 ] = K_ESCAPE;
348: scantokey[28] = K_ENTER;
349: scantokey[15] = K_TAB;
350: scantokey[14] = K_BACKSPACE;
351: scantokey[119] = K_PAUSE;
352: scantokey[57] = ' ';
353:
354: scantokey[2] = '1';
355: scantokey[3] = '2';
356: scantokey[4] = '3';
357: scantokey[5] = '4';
358: scantokey[6] = '5';
359: scantokey[7] = '6';
360: scantokey[8] = '7';
361: scantokey[9] = '8';
362: scantokey[10] = '9';
363: scantokey[11] = '0';
364: scantokey[12] = '-';
365: scantokey[13] = '=';
366: scantokey[41] = '`';
367: scantokey[26] = '[';
368: scantokey[27] = ']';
369: scantokey[39] = ';';
370: scantokey[40] = '\'';
371: scantokey[51] = ',';
372: scantokey[52] = '.';
373: scantokey[53] = '/';
374: scantokey[43] = '\\';
375:
376: scantokey[59] = K_F1;
377: scantokey[60] = K_F2;
378: scantokey[61] = K_F3;
379: scantokey[62] = K_F4;
380: scantokey[63] = K_F5;
381: scantokey[64] = K_F6;
382: scantokey[65] = K_F7;
383: scantokey[66] = K_F8;
384: scantokey[67] = K_F9;
385: scantokey[68] = K_F10;
386: scantokey[87] = K_F11;
387: scantokey[88] = K_F12;
388: scantokey[30] = 'a';
389: scantokey[48] = 'b';
390: scantokey[46] = 'c';
391: scantokey[32] = 'd';
392: scantokey[18] = 'e';
393: scantokey[33] = 'f';
394: scantokey[34] = 'g';
395: scantokey[35] = 'h';
396: scantokey[23] = 'i';
397: scantokey[36] = 'j';
398: scantokey[37] = 'k';
399: scantokey[38] = 'l';
400: scantokey[50] = 'm';
401: scantokey[49] = 'n';
402: scantokey[24] = 'o';
403: scantokey[25] = 'p';
404: scantokey[16] = 'q';
405: scantokey[19] = 'r';
406: scantokey[31] = 's';
407: scantokey[20] = 't';
408: scantokey[22] = 'u';
409: scantokey[47] = 'v';
410: scantokey[17] = 'w';
411: scantokey[45] = 'x';
412: scantokey[21] = 'y';
413: scantokey[44] = 'z';
414:
415: if (keyboard_init())
416: I_Error("keyboard_init() failed");
417: keyboard_seteventhandler(keyhandler);
418: }
419:
420: }
421:
422: void VID_Update(vrect_t *rects)
423: {
424:
425: int ycount;
426: int offset;
427:
428: if (!svgalib_inited)
429: return;
430:
431: while (rects)
432: {
433: ycount = rects->height;
434: offset = rects->y * vid.rowbytes + rects->x;
435: while (ycount--)
436: {
437: memcpy(framebuffer_ptr + offset, vid.buffer + offset, rects->width);
438: offset += vid.rowbytes;
439: }
440:
441: rects = rects->pnext;
442: }
443:
444: }
445:
446: static int dither;
447:
448: void VID_DitherOn(void)
449: {
450: if (dither == 0)
451: {
452: R_ViewChanged ();
453: dither = 1;
454: }
455: }
456:
457: void VID_DitherOff(void)
458: {
459: if (dither)
460: {
461: R_ViewChanged ();
462: dither = 0;
463: }
464: }
465:
466: int I_OpenWindow(void)
467: {
468: return 0;
469: }
470:
471: void I_EraseWindow(int window)
472: {
473: }
474:
475: void I_DrawCircle(int window, int x, int y, int r)
476: {
477: }
478:
479: void I_DisplayWindow(int window)
480: {
481: }
482:
483: void Sys_SendKeyEvents(void)
484: {
485: if (!svgalib_inited)
486: return;
487:
488: if (UseKeyboard)
489: while (keyboard_update());
490: }
491:
492: char *Sys_ConsoleInput (void)
493: {
494: return 0;
495: }
496:
497: void mousehandler(int mouse_buttonstate, int dx, int dy)
498: {
499:
500: int i;
501:
502: for (i=0 ; i<mouse_buttons ; i++)
503: {
504: if ( (mouse_buttonstate & (1<<i)) && !(mouse_oldbuttonstate & (1<<i)) )
505: Cbuf_AddText (mouse_button_commands[i].string);
506:
507: if ( !(mouse_buttonstate & (1<<i)) && (mouse_oldbuttonstate & (1<<i)) )
508: {
509: if (mouse_button_commands[i].string[0] == '+')
510: {
511: mouse_button_commands[i].string[0] = '-';
512: Cbuf_AddText (mouse_button_commands[i].string);
513: mouse_button_commands[i].string[0] = '+';
514: }
515: }
516: }
517:
518: mouse_oldbuttonstate = mouse_buttonstate;
519:
520: mouse_x = dx*3;
521: mouse_y = dy*3;
522:
523: }
524:
525: void IN_Init(void)
526: {
527:
528: int mtype;
529: char *mousedev;
530: int mouserate;
531:
532: if (UseMouse)
533: {
534:
535: Cvar_RegisterVariable (&mouse_button_commands[0]);
536: Cvar_RegisterVariable (&mouse_button_commands[1]);
537: Cvar_RegisterVariable (&mouse_button_commands[2]);
538:
539: Cvar_RegisterVariable (&mouse_sensitivity);
540:
541: mouse_buttons = 3;
542:
543: mtype = vga_getmousetype();
544:
545: mousedev = "/dev/mouse";
546: if (getenv("MOUSEDEV")) mousedev = getenv("MOUSEDEV");
547: if (COM_CheckParm("-mdev"))
548: mousedev = com_argv[COM_CheckParm("-mdev")+1];
549:
550: mouserate = 1200;
551: if (getenv("MOUSERATE")) mouserate = atoi(getenv("MOUSERATE"));
552: if (COM_CheckParm("-mrate"))
553: mouserate = atoi(com_argv[COM_CheckParm("-mrate")+1]);
554:
555: printf("Mouse: dev=%s,type=%s,speed=%d\n",
556: mousedev, mice[mtype].name, mouserate);
557: if (mouse_init(mousedev, mtype, mouserate))
558: {
559: Con_Printf("No mouse found\n");
560: UseMouse = 0;
561: }
562: else
563: mouse_seteventhandler(mousehandler);
564:
565: }
566:
567: }
568:
569: void IN_Shutdown(void)
570: {
571: if (UseMouse)
572: mouse_close();
573: }
574:
575: void IN_Commands (void)
576: {
577: if (UseMouse)
578: while (mouse_update());
579: }
580:
581: int CL_KeyState (int *key);
582: extern int in_strafe;
583: void IN_Move (usercmd_t *cmd)
584: {
585:
586: if (!UseMouse)
587: return;
588:
589: // add mouse X/Y movement to cmd
590: if (in_strafe)
591: {
592: cmd->sidemove += 0.8*mouse_sensitivity.value * mouse_x;
593: cmd->forwardmove -= 1.0*mouse_sensitivity.value * mouse_y;
594: }
595: else
596: {
597: cl_viewangles[YAW] -= 4.0/180*mouse_sensitivity.value * mouse_x;
598: cl_viewangles[PITCH] -= 4.0/180*mouse_sensitivity.value * mouse_y;
599: }
600:
601: if (cl_viewangles[PITCH] > 80)
602: cl_viewangles[PITCH] = 80;
603: if (cl_viewangles[PITCH] < -70)
604: cl_viewangles[PITCH] = -70;
605:
606: if (cl_viewangles[ROLL] > 50)
607: cl_viewangles[ROLL] = 50;
608: if (cl_viewangles[ROLL] < -50)
609: cl_viewangles[ROLL] = -50;
610:
611: /*
612: if (!UseMouse)
613: return;
614:
615: // add mouse X/Y movement to cmd
616: if (CL_KeyState (&in_strafe))
617: {
618: cmd->sidemove += 0.8*mouse_sensitivity.value * mouse_x;
619: cmd->forwardmove -= 1.0*mouse_sensitivity.value * mouse_y;
620: }
621: else
622: {
623: cmd->rightturn += 4.0/180*mouse_sensitivity.value * mouse_x;
624: cmd->upturn -= 4.0/180*mouse_sensitivity.value * mouse_y;
625: }
626:
627: */
628:
629: }
630:
631:
632:
633: /*
634: ================
635: VID_NumModes
636: ================
637: */
638: int VID_NumModes ()
639: {
640: return (1);
641: }
642:
643:
644: /*
645: ================
646: VID_ModeInfo
647: ================
648: */
649: char *VID_ModeInfo (int modenum)
650: {
651: static char *badmodestr = "Bad mode number";
652: static char modestr[40];
653:
654: if (modenum == 0)
655: {
656: sprintf (modestr, "%d x %d, %d bpp",
657: vid.width, vid.height, modes[current_mode].bytesperpixel*8);
658: return (modestr);
659: }
660: else
661: {
662: return (badmodestr);
663: }
664: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.