source: abuse/tags/pd/macabuse/imlib/port/dos4gw/event.c @ 49

Last change on this file since 49 was 49, checked in by Sam Hocevar, 11 years ago
  • Imported original public domain release, for future reference.
  • Property svn:keywords set to Id
File size: 8.9 KB
Line 
1#include "system.h"
2#include "image.hpp"
3#include "palette.hpp"
4#include "mdlread.hpp"
5#include "video.hpp"
6#include "dos.h"
7#include "macs.hpp"
8#include "mouse.hpp"
9#include "conio.h"
10#include "event.hpp"
11#include "sprite.hpp"
12#include "monoprnt.hpp"
13#include <ctype.h>
14#include <conio.h>
15#include <dos.h>
16
17#define __EMU_MOUSE__
18
19void event_handler::flush_screen()
20{
21#ifdef __EMU_MOUSE__
22  mouse->mouse_sprite()->x=mouse->drawx();
23  mouse->mouse_sprite()->y=mouse->drawy();
24  mouse->mouse_sprite()->get_background();
25  mouse->mouse_sprite()->draw();
26#endif
27  update_dirty(screen);
28#ifdef __EMU_MOUSE__
29  mouse->mouse_sprite()->restore_background();
30#endif
31}
32
33unsigned char shift_pressed;
34
35int key_translate(unsigned char ch)
36{
37  switch (ch)
38  {
39    case 30 : return shift_pressed ? 'A' : 'a'; break;
40    case 48 : return shift_pressed ? 'B' : 'b'; break;
41    case 46 : return shift_pressed ? 'C' : 'c'; break;
42    case 32 : return shift_pressed ? 'D' : 'd'; break;
43    case 18 : return shift_pressed ? 'E' : 'e'; break;
44    case 33 : return shift_pressed ? 'F' : 'f'; break;
45    case 34 : return shift_pressed ? 'G' : 'g'; break;
46    case 35 : return shift_pressed ? 'H' : 'h'; break;
47    case 23 : return shift_pressed ? 'I' : 'i'; break;
48    case 36 : return shift_pressed ? 'J' : 'j'; break;
49    case 37 : return shift_pressed ? 'K' : 'k'; break;
50    case 38 : return shift_pressed ? 'L' : 'l'; break;
51    case 50 : return shift_pressed ? 'M' : 'm'; break;
52    case 49 : return shift_pressed ? 'N' : 'n'; break;
53    case 24 : return shift_pressed ? 'O' : 'o'; break;
54    case 25 : return shift_pressed ? 'P' : 'p'; break;
55    case 16 : return shift_pressed ? 'Q' : 'q'; break;
56    case 19 : return shift_pressed ? 'R' : 'r'; break;
57    case 31 : return shift_pressed ? 'S' : 's'; break;
58    case 20 : return shift_pressed ? 'T' : 't'; break;
59    case 22 : return shift_pressed ? 'U' : 'u'; break;
60    case 47 : return shift_pressed ? 'V' : 'v'; break;
61    case 17 : return shift_pressed ? 'W' : 'w'; break;
62    case 45 : return shift_pressed ? 'X' : 'x'; break;
63    case 21 : return shift_pressed ? 'Y' : 'y'; break;
64    case 44 : return shift_pressed ? 'Z' : 'z'; break;
65    case 51 : return shift_pressed ? '<' : ','; break;
66    case 52 : return shift_pressed ? '>' : '.'; break;
67    case 53 : return shift_pressed ? '?' : '/'; break;     
68    case 39 : return shift_pressed ? ':' : ';'; break;
69    case 40 : return shift_pressed ? '"' : '\''; break;
70    case 26 : return shift_pressed ? '{' : '['; break;
71    case 27 : return shift_pressed ? '}' : ']'; break;
72    case 43 : return shift_pressed ? '|' : '\\'; break;
73    case 13 : return shift_pressed ? '+' : '='; break;
74    case 12 : return shift_pressed ? '_' : '-'; break;     
75    case 11 : return shift_pressed ? ')' : '0'; break;
76    case 10 : return shift_pressed ? '(' : '9'; break;
77    case  9 : return shift_pressed ? '*' : '8'; break;
78    case  8 : return shift_pressed ? '&' : '7'; break;
79    case  7 : return shift_pressed ? '^' : '6'; break;
80    case  6 : return shift_pressed ? '%' : '5'; break;
81    case  5 : return shift_pressed ? '$' : '4'; break;
82    case  4 : return shift_pressed ? '#' : '3'; break;
83    case  3 : return shift_pressed ? '@' : '2'; break;
84    case  2 : return shift_pressed ? '!' : '1'; break;
85    case 41 : return shift_pressed ? '~' : '`'; break;
86     
87    case 14 : return JK_BACKSPACE; break;
88    case 15 : return JK_TAB; break;
89    case 28 : return JK_ENTER; break;
90    case 1 : return JK_ESC; break;
91    case 57 : return JK_SPACE; break;
92     
93    case 72 : return JK_UP; break;
94    case 80 : return JK_DOWN; break;
95    case 75 : return JK_LEFT; break;
96    case 77 : return JK_RIGHT; break;       
97    case 29 : return JK_CTRL_L; break;     
98    case 56 : return JK_ALT_L; break;
99    case 42 : return JK_SHIFT_L; break;     
100    case 54 : return JK_SHIFT_R; break;     
101    case 58 : return JK_CAPS; break;     
102    case 69 : return JK_NUM_LOCK; break;         
103    case 71 : return JK_HOME; break;     
104    case 79 : return JK_END; break;     
105    case 83 : return JK_DEL; break;     
106    case 82 : return JK_INSERT; break;
107
108    case 59 : return JK_F1; break;     
109    case 60 : return JK_F2; break;     
110    case 61 : return JK_F3; break;     
111    case 62 : return JK_F4; break;     
112    case 63 : return JK_F5; break;     
113    case 64 : return JK_F6; break;     
114    case 65 : return JK_F7; break;     
115    case 66 : return JK_F8; break;     
116    case 67 : return JK_F9; break;     
117    case 68 : return JK_F10; break;       
118    case 73 : return JK_PAGEUP; break;
119    case 81 : return JK_PAGEUP; break;
120    default :
121      return 0;     
122  }
123
124
125
126void (__interrupt __far *old_key_intr)();
127unsigned char key_que[256],key_que_head=0,key_que_tail=0,
128                alt_flag=0,ctrl_flag=0,shift_flag=0,
129                nice_key_mode=0;   // chain to old interrupt ?
130             
131void __interrupt keyboard_intr_handler()
132
133  unsigned char k=inp(0x60);                // read the key code
134  if (k!=224)                               // 224 is extended keyboard info
135                                            // we can just throw it away
136    key_que[key_que_head++]=k;                // stick it in the que 
137  outp(0x20,0x20);                          // notify serviced int
138 
139  // allow the user to uninstall the key board handler
140  // by pressing CTRL-ALT-SHIFT-C  :)  (don't want people to do it!)
141 
142  switch (k&127)
143  {
144    case 56 : alt_flag=!(k&128);     break;
145    case 29 : ctrl_flag=!(k&128);    break;
146    case 54 :
147    case 42 : shift_flag=!(k&128);  break;
148    case 46 : if (alt_flag & ctrl_flag & shift_flag)
149                _dos_setvect(9,old_key_intr); break;
150  } 
151  if (nice_key_mode)
152    _chain_intr(old_key_intr);
153}
154
155
156int event_handler::get_key_flags()
157{
158  return 0;
159}
160
161
162
163void keyboard_init()
164{
165  old_key_intr=_dos_getvect(9);
166  _dos_setvect(0x8009,keyboard_intr_handler);
167}
168
169 
170
171
172void keyboard_uninit()          // install the old int 9 handler
173
174  _dos_setvect(9,old_key_intr);
175}
176
177
178char *key_names[]= {"Up Arrow","Down Arrow","Left Arrow","Right Arrow",
179                    "Left Ctrl","Right Ctrl","Left Alt","Right Alt",
180                    "Left Shift","Right Shift","Caps Lock","Num Lock",
181                    "Home","End","Del","F1","F2","F3","F4","F5","F6",
182                    "F7","F8","F9","F10"};
183
184
185char *jkey_name(int key)
186{
187  static char sing[2];
188 if (key>255)
189    return key_names[key-256];
190  else if (key==JK_BACKSPACE)
191    return "Backspace";
192  else if (key==JK_TAB)
193    return "Tab";
194  else if (key==JK_ENTER)
195    return "Enter";
196  else if (key==JK_ESC)
197    return"Esc";
198  else if (key==JK_SPACE)
199    return "Space";
200  else if (isprint(key))
201  {
202    sing[0]=key;
203    sing[1]=0;
204    return sing;
205  }
206  return NULL;
207}
208
209int event_handler::event_waiting()
210{
211  int ch;
212  event *evt;
213 
214  while (key_que_head!=key_que_tail)
215  {
216    int up=key_que[key_que_tail]&128,
217        key=key_que[key_que_tail]&127;
218     
219    evt=new event;
220   
221    evt->key=key_translate(key);
222    if (up)
223    {
224      evt->type=EV_KEYRELEASE;
225    }
226    else 
227    {
228      evt->type=EV_KEY;
229    }
230   
231    if (evt->key==JK_SHIFT_L || evt->key==JK_SHIFT_R)
232      shift_pressed=!up;   
233    events.add_end((linked_node *)evt);     
234    ewaiting=1;     
235    key_que_tail++;
236  }   
237  if (mhere)
238  {
239    mouse->update();
240    if (mouse->lastx()!=mouse->x() || mouse->lasty()!=mouse->y()
241       || mouse->last_button()!=mouse->button())
242      ewaiting=1;
243  }
244  return ewaiting;
245}
246
247event_handler::event_handler(image *screen, palette *pal)
248{
249  CHECK(screen && pal);
250  mouse=new JCMouse(screen,pal);
251  mhere=mouse->exsist();
252  last_keystat=get_key_flags();
253  ewaiting=0;
254  keyboard_init(); 
255}
256
257
258void event_handler::get_event(event &ev)
259{
260  int kf,kf_change=0,ch;
261  event *evt;
262  event *ep;
263  while (!ewaiting)
264    event_waiting();
265
266  ep=(event *)events.first();
267  if (ep)
268  { ev=*ep;
269    events.unlink((linked_node *)ep);
270    delete ep;
271    ewaiting=events.first()!=NULL;
272  }
273  else
274  {
275    kf=get_key_flags();
276    if (kf!=last_keystat)
277    { last_keystat=kf;
278      kf_change=1;
279      ev.type=EV_KEY_SPECIAL;
280      ev.key_special.shift=kf&2;
281      ev.key_special.ctrl=kf&4;
282      ev.key_special.alt=kf&8;
283    }
284    else if (mhere && (mouse->last_button()!=mouse->button()))
285      ev.type=EV_MOUSE_BUTTON;
286    else if (mhere && (mouse->lastx()!=mouse->x() ||
287             mouse->lasty()!=mouse->y()))
288      ev.type=EV_MOUSE_MOVE;
289    else ev.type=EV_SPURIOUS;
290
291    if (ev.type==EV_MOUSE_MOVE)
292    {
293      mouse->mouse_sprite()->x=mouse->x();
294      mouse->mouse_sprite()->y=mouse->y();
295    }
296    ewaiting=0;
297  }
298  ev.mouse_move.x=mouse->x();
299  ev.mouse_move.y=mouse->y();
300  ev.mouse_button=mouse->button();
301
302}
303
304
305void event_handler::add_redraw(int X1, int Y1, int X2, int Y2, void *Start)
306{
307  event *ev;
308  ev=new event;
309  ev->type=EV_REDRAW;
310  ev->redraw.x1=X1; ev->redraw.x2=X2;
311  ev->redraw.y1=Y1; ev->redraw.y2=Y2; ev->redraw.start=Start;
312  events.add_end((linked_node *)ev);
313}
314
315event_handler::~event_handler()
316{
317  delete mouse;
318  keyboard_uninit();
319}
320
321
322
Note: See TracBrowser for help on using the repository browser.