]> icculus.org git repositories - crow/jumpnbump.git/blob - dos/interrpt.c
aa3f9340c99b1f7a95686507b4f14b0a81fb5069
[crow/jumpnbump.git] / dos / interrpt.c
1 /*
2  * interrpt.h
3  * Copyright (C) 1998 Brainchild Design - http://brainchilddesign.com/
4  * 
5  * Copyright (C) 2002 Florian Schulze - crow@icculus.org
6  *
7  * Portions of this code are from the MPEG software simulation group
8  * idct implementation. This code will be replaced with a new
9  * implementation soon.
10  *
11  * This file is part of Jump'n'Bump.
12  *
13  * Jump'n'Bump is free software; you can redistribute it and/or modify
14  * it under the terms of the GNU General Public License as published by
15  * the Free Software Foundation; either version 2 of the License, or
16  * (at your option) any later version.
17  *
18  * Jump'n'Bump is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software
25  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
26  */
27
28 #include "globals.h"
29
30
31 struct {
32         char enabled;
33 } keyb_handler_info;
34
35 volatile char keyb[256];
36 volatile char last_keys[50];
37
38 unsigned char scancode2ascii[256] = {
39         0, 0, 49, 50, 51, 52, 53, 54, 55, 56,           /* 0-9 */
40         57, 48, 45, 0, 0, 0, 113, 119, 101, 114,        /* 10-19 */
41         116, 121, 117, 105, 111, 112, 0, 0, 0, 0,       /* 20-29 */
42         97, 115, 100, 102, 103, 104, 106, 107, 108, 0,  /* 30-39 */
43         0, 0, 0, 0, 122, 120, 99, 118, 98, 110,         /* 40-49 */
44         109, 44, 46, 47, 0, 0, 0, 32, 0, 0,             /* 50-59 */
45         0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
46         0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
47         0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
48         0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
49         0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
50         0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
51         0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
52         0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
53         0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
54         0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
55         0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
56         0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
57         0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
58         0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
59         0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
60         0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
61         0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
62         0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
63         0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
64         0, 0, 0, 0, 0, 0
65 };
66
67 _go32_dpmi_seginfo old_keyb_handler_seginfo, new_keyb_handler_seginfo;
68
69
70 void keyb_handler()
71 {
72         unsigned char key;
73         static char extended;
74         int c1;
75
76         key = inportb(0x60);
77
78         if (key == 0xe0)
79                 extended = 1;
80         else {
81                 if (extended == 0) {
82                         if ((key & 0x80) == 0) {
83                                 keyb[key & 0x7f] = 1;
84                                 for (c1 = 48; c1 > 0; c1--)
85                                         last_keys[c1] = last_keys[c1 - 1];
86                                 last_keys[0] = scancode2ascii[key & 0x7f];
87                         } else
88                                 keyb[key & 0x7f] = 0;
89                 } else {
90                         if ((key & 0x80) == 0) {
91                                 keyb[(key & 0x7f) + 0x80] = 1;
92                                 for (c1 = 48; c1 > 0; c1--)
93                                         last_keys[c1] = last_keys[c1 - 1];
94                                 last_keys[0] = scancode2ascii[(key & 0x7f) + 0x80];
95                         } else
96                                 keyb[(key & 0x7f) + 0x80] = 0;
97                 }
98                 if (extended == 1)
99                         extended = 0;
100         }
101
102         outportb(0x20, 0x20);
103
104 }
105
106 void keyb_handler_end()
107 {
108 }
109
110
111 char hook_keyb_handler(void)
112 {
113
114         if (keyb_handler_info.enabled == 0) {
115                 _go32_dpmi_lock_data((char *) &keyb, sizeof(keyb));
116                 _go32_dpmi_lock_code(keyb_handler, (unsigned long) keyb_handler_end - (unsigned long) keyb_handler);
117                 _go32_dpmi_get_protected_mode_interrupt_vector(9, &old_keyb_handler_seginfo);
118                 new_keyb_handler_seginfo.pm_offset = (int) keyb_handler;
119                 if (_go32_dpmi_allocate_iret_wrapper(&new_keyb_handler_seginfo) != 0)
120                         return 1;
121                 if (_go32_dpmi_set_protected_mode_interrupt_vector(9, &new_keyb_handler_seginfo) != 0) {
122                         _go32_dpmi_free_iret_wrapper(&new_keyb_handler_seginfo);
123                         return 1;
124                 }
125                 keyb_handler_info.enabled = 1;
126                 memset(last_keys, 0, sizeof(last_keys));
127         }
128
129         return 0;
130
131 }
132
133
134 void remove_keyb_handler(void)
135 {
136
137         if (keyb_handler_info.enabled == 1) {
138                 _go32_dpmi_set_protected_mode_interrupt_vector(9, &old_keyb_handler_seginfo);
139                 _go32_dpmi_free_iret_wrapper(&new_keyb_handler_seginfo);
140                 keyb_handler_info.enabled = 0;
141         }
142
143 }
144
145
146 char key_pressed(unsigned char key)
147 {
148
149         return keyb[key];
150
151 }