File: events.cpp

package info (click to toggle)
asc 2.1.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 59,052 kB
  • ctags: 25,676
  • sloc: cpp: 145,189; sh: 8,705; ansic: 5,564; makefile: 551; perl: 150
file content (715 lines) | stat: -rw-r--r-- 16,422 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
/***************************************************************************
                          mousesdl.cpp  -  description
                             -------------------
    begin                : Sun Dec 19 1999
    copyright            : (C) 1999 by Martin Bickel
    email                : bickel@asc-hq.org
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/



#include <queue>
#include <SDL.h>
#include <SDL_thread.h>

#include "ctype.h"

#include "../events.h"
#include "../stack.h"
#include "../basegfx.h"
#include "../global.h"
// #include "keysymbols.h"
#include "../errors.h"
#include "../messaginghub.h"
#include "graphicsqueue.h"

volatile tmousesettings mouseparams;

SDL_mutex* keyboardmutex = NULL;
SDL_mutex* eventHandlingMutex = NULL;
SDL_mutex* eventQueueMutex = NULL;
SDL_mutex* graphicsQueueMutex = NULL;

queue<tkey>   keybuffer_sym;
queue<Uint32> keybuffer_prnt;
queue<SDL_Event> eventQueue;
bool _queueEvents = true;
bool _fillLegacyEventStructures = false;

bool eventThreadRunning = false;


std::list<GraphicsQueueOperation*> graphicsQueue;

int exitprogram = 0;


/***************************************************************************
 *                                                                         *
 *   Mouse handling routines                                               *
 *                                                                         *
 ***************************************************************************/


bool redrawScreen = false;

const bool trueflag = true;

const bool* mouseUpdateFlag = &trueflag;

void setMouseUpdateFlag( const bool* flag )
{
   mouseUpdateFlag = flag;
}


void mousevisible( int an)
{}


int getmousestatus ()
{
   return 2;
}

int mouseTranslate ( int m)
{

   const int mousetranslate[3] = {
      0, 2,1
   } ;  // in DOS  right button is 1 and center is 2

   if ( m >= 3 )
      return m;
   else
      return mousetranslate[m];
}



void setmouseposition ( int x, int y )
{}



void setnewmousepointer ( void* picture, int hotspotx, int hotspoty )
{}


int mouseinrect ( int x1, int y1, int x2, int y2 )
{
   if ( mouseparams.x >= x1  && mouseparams.y >= y1  && mouseparams.x <= x2 && mouseparams.y <= y2 )
      return 1;
   else
      return 0;
}

int mouseinrect ( const tmouserect* rect )
{
   if ( mouseparams.x >= rect->x1  && mouseparams.y >= rect->y1  && mouseparams.x <= rect->x2 && mouseparams.y <= rect->y2 )
      return 1;
   else
      return 0;
}



tmouserect tmouserect :: operator+ ( const tmouserect& b ) const
{
   tmouserect c;
   c.x1 = x1 + b.x1;
   c.y1 = y1 + b.y1;
   c.x2 = x2 + b.x2;
   c.y2 = y2 + b.y2;
   return c;
}



/***************************************************************************
 *                                                                         *
 *   Keyboard handling routines                                            *
 *                                                                         *
 ***************************************************************************/



int keypress( void )
{
   int result = 0;
   int r = SDL_mutexP ( keyboardmutex );
   if ( !r ) {
      result = !keybuffer_sym.empty ( );
      r = SDL_mutexV ( keyboardmutex );
   }
   return result;
}


tkey r_key(void)
{
   int found = 0;
   tkey key;
   do {
      int r = SDL_mutexP ( keyboardmutex );
      if ( !r ) {
         if ( !keybuffer_sym.empty() ) {
            key = keybuffer_sym.front();
            keybuffer_sym.pop();
            keybuffer_prnt.pop();
            found++;
         }
         r = SDL_mutexV ( keyboardmutex );
      }
      if (!found ) {
         int t = ticker;
         while ( t + 5 > ticker )
            releasetimeslice();
      }
   } while ( !found );
   return key;
}

int rp_key(void)
{
   int found = 0;
   tkey key;
   do {
      int r = SDL_mutexP ( keyboardmutex );
      if ( !r ) {
         if ( !keybuffer_prnt.empty() ) {
            key = keybuffer_prnt.front();
            keybuffer_sym.pop();
            keybuffer_prnt.pop();
            found++;
         }
         r = SDL_mutexV ( keyboardmutex );
      }
      if (!found ) {
         int t = ticker;
         while ( t + 5 > ticker )
            releasetimeslice();
      }
   } while ( !found );
   return key;
}

void getkeysyms ( tkey* keysym, int* keyprnt )
{
   int found = 0;
   do {
      int r = SDL_mutexP ( keyboardmutex );
      if ( !r ) {
         if ( !keybuffer_prnt.empty() ) {
            *keysym = keybuffer_sym.front();
            *keyprnt = keybuffer_prnt.front();
            keybuffer_sym.pop();
            keybuffer_prnt.pop();
            found++;
         }
         r = SDL_mutexV ( keyboardmutex );
      }
      if (!found ) {
         int t = ticker;
         while ( t + 5 > ticker )
            releasetimeslice();
      }
   } while ( !found );
}


char  skeypress(tkey keynr)
{
   Uint8 *keystate = SDL_GetKeyState ( NULL );
   return keystate[ keynr ];
}




void wait(void)
{}



tkey char2key(int c )
{
   if ( c < 128 )
      return tolower(c);
   else
      return ct_invvalue;
}


int  releasetimeslice( void )
{
   SDL_Delay(10);
   if ( redrawScreen ) {
      redrawScreen = false;
      copy2screen();
   }
   return 0;
}








/***************************************************************************
 *                                                                         *
 *   Timer routines                                                        *
 *                                                                         *
 ***************************************************************************/



volatile int  ticker = 0; // was static, but I think this needs to be global somewhere

void ndelay(int time)
{
   long l;

   l = ticker;
   do {
      releasetimeslice();
   }  while (ticker - l > time);
}


int tticker = 0;

void starttimer(void)
{
   tticker = ticker;
}

char time_elapsed(int time)
{
   if (tticker + time <= ticker) return 1;
   else return 0;
}



/***************************************************************************
 *                                                                         *
 *   Event handling routines                                               *
 *                                                                         *
 ***************************************************************************/

#include <iostream>

int closeEventThread = 0;

const int keyTranslationNum = 7;
int keyTranslation[keyTranslationNum][2] = { { 228, 132 }, //   "a
                                             { 246, 148 }, //   "o
                                             { 252, 129 }, //   "u
                                             { 196, 142 }, //   "A
                                             { 214, 153 }, //   "O
                                             { 220, 154 }, //   "U
                                             { 223, 225 } }; // sz

int processEvents ( )
{
   SDL_mutexP ( eventHandlingMutex );

   SDL_Event event;
   int result;
   if ( SDL_PollEvent ( &event ) == 1) {
      if ( _fillLegacyEventStructures ) {
        switch ( event.type ) {
         case SDL_MOUSEBUTTONUP:
         case SDL_MOUSEBUTTONDOWN:
            {
               int taste = mouseTranslate(event.button.button - 1);
               int state = event.button.type == SDL_MOUSEBUTTONDOWN;
               if ( state )
                  mouseparams.taste |= (1 << taste);
               else
                  mouseparams.taste &= ~(1 << taste);
               mouseparams.x = event.button.x;
               mouseparams.y = event.button.y;
            }
            break;

         case SDL_MOUSEMOTION:
            {
               mouseparams.x = event.motion.x;
               mouseparams.y = event.motion.y;
               mouseparams.x1 = event.motion.x;
               mouseparams.y1 = event.motion.y;
               mouseparams.taste = 0;
               for ( int i = 0; i < 3; i++ )
                  if ( event.motion.state & (1 << i) )
                     mouseparams.taste |= 1 << mouseTranslate(i);
            }
            break;
         case SDL_KEYDOWN:
            {
               int r = SDL_mutexP ( keyboardmutex );
               if ( !r ) {
                  tkey key = event.key.keysym.sym;
                  if ( event.key.keysym.mod & KMOD_ALT )
                     key |= ct_altp;
                  if ( event.key.keysym.mod & KMOD_CTRL )
                     key |= ct_stp;
                  if ( event.key.keysym.mod & KMOD_SHIFT )
                     key |= ct_shp;
                  keybuffer_sym.push ( key );

                  int newsym = event.key.keysym.unicode;
                  for ( int i = 0; i < keyTranslationNum; i++ )
                     if ( event.key.keysym.unicode == keyTranslation[i][0] )
                        newsym = keyTranslation[i][1];
                  keybuffer_prnt.push ( newsym );
                  r = SDL_mutexV ( keyboardmutex );
               }
            }
            break;
         case SDL_KEYUP:
         {}
            break;

         case SDL_QUIT:
            exitprogram = 1;
            break;
#ifdef _WIN32_
         case SDL_ACTIVEEVENT: 
            redrawScreen = true;
            break;
#endif
        } 
      } else {
#ifdef _WIN32_
         if ( event.type  == SDL_ACTIVEEVENT ) {
            queueOperation( new UpdateRectOp( SDL_GetVideoSurface(), 0,0,0,0), false, true );
         }
#endif

      }
      result = 1;
      if ( _queueEvents ) {
         SDL_mutexP( eventQueueMutex );
         eventQueue.push ( event );
         SDL_mutexV( eventQueueMutex );
      }


   } else
      result = 0;

   SDL_mutexV( eventHandlingMutex );
   return result;
}

#if defined(_WIN32_) | defined(__APPLE__)
#define FirstThreadEvents 1
#endif

bool syncGraphics = true;


SigC::Signal1<void,const SDL_Surface*> postScreenUpdate;

void queueOperation( GraphicsQueueOperation* gqo, bool wait, bool forceAsync )
{
   if ( !eventThreadRunning ) {
      gqo->execute();
      delete gqo;
      return;
   }

   SDL_mutexP( graphicsQueueMutex );
   graphicsQueue.push_back( gqo );
   SDL_mutexV( graphicsQueueMutex );

   if ( forceAsync )
      return;

   if ( syncGraphics || wait ) {
      bool finished = false;
      do {
         SDL_Delay(10);
         SDL_mutexP( graphicsQueueMutex );
         finished = graphicsQueue.empty();
         SDL_mutexV( graphicsQueueMutex );
      } while (!finished);
   }
}


void UpdateRectOp::execute()
{ 
   if ( *mouseUpdateFlag )
      SDL_ShowCursor( 0 );

   SDL_UpdateRect( screen, x,y,w,h); 
   postScreenUpdate( screen );

   if ( *mouseUpdateFlag )
      SDL_ShowCursor( 1 );
};


UpdateRectsOp::UpdateRectsOp( SDL_Surface *screen, int numrects, SDL_Rect *rects) 
{
   this->numrects = numrects;
   this->rects = new SDL_Rect[numrects];
   for ( int i = 0; i< numrects; ++i )
      this->rects[i] = rects[i];
   this->screen = screen;
};

UpdateRectsOp::~UpdateRectsOp()
{
   delete[] rects;
}

void UpdateRectsOp::execute() 
{ 
   if ( *mouseUpdateFlag )
      SDL_ShowCursor( 0 );

   SDL_UpdateRects( screen, numrects, rects); 
   postScreenUpdate( screen );

   if ( *mouseUpdateFlag )
      SDL_ShowCursor( 1 );
}


void InitScreenOp::execute() 
{ 
   if ( *mouseUpdateFlag )
      SDL_ShowCursor( 0 );

   SDL_Surface* screen = SDL_SetVideoMode(x, y, depth, flags);
   if (screen == NULL) 
      screen = SDL_SetVideoMode(x, y, depth, flags & ~SDL_FULLSCREEN );

   srf( screen );
   initASCGraphicSubsystem( screen );

   if ( *mouseUpdateFlag )
      SDL_ShowCursor( 1 );
};


bool processGraphicsQueue()
{
#ifdef FirstThreadEvents
   GraphicsQueueOperation* gqo = NULL;
   SDL_mutexP( graphicsQueueMutex );
   if ( !graphicsQueue.empty() ) {
      gqo = graphicsQueue.front();
      SDL_mutexV( graphicsQueueMutex );
      if ( gqo ) {
         gqo->execute();

         SDL_mutexP( graphicsQueueMutex );
         graphicsQueue.pop_front();
         SDL_mutexV( graphicsQueueMutex );
         delete gqo;
         return true;
      } else
         return false;
   } else {
      SDL_mutexV( graphicsQueueMutex );
      return false;   
   }
#else
   return false;
#endif
}

int eventthread ( void* nothing )
{
#ifdef FirstThreadEvents
   eventThreadRunning = true;
#endif
   while ( !closeEventThread ) {
      if ( !processEvents() )
         SDL_Delay(10);
      processGraphicsQueue();
      ticker = getTicker();
   }
#ifdef FirstThreadEvents
   eventThreadRunning = false;
   while ( processGraphicsQueue() ) 
      SDL_Delay(10);
#endif
   return closeEventThread;
}


int getTicker()
{
   return SDL_GetTicks() / 10;
}

#ifdef FirstThreadEvents 
int (*_gamethread)(void *);

int gameThreadWrapper ( void* data )
{
   try {
      int res = _gamethread ( data );
      closeEventThread = 1;
      return res;
   }
#ifndef WIN32
   catch ( ... ) {
      fatalError ("An unhandled exception occured. Quitting application");
   }
#else
   catch ( ASCexception ) {
      fatalError ("An unhandled exception occured. Quitting application");
   }
#endif
   closeEventThread = -1;
   return -1;
}
#endif


//! The handle for the second thread; depending on platform this could be the event handling thread or the game thread
SDL_Thread* secondThreadHandle = NULL;



int initializeEventHandling ( int (*gamethread)(void *) , void *data )
{

   mouseparams.xsize = 10;
   mouseparams.ysize = 10;

   keyboardmutex = SDL_CreateMutex ();
   if ( !keyboardmutex ) {
      printf("creating keyboard mutex failed\n" );
      exit(1);
   }

   eventHandlingMutex = SDL_CreateMutex ();
   if ( !eventHandlingMutex ) {
      printf("creating eventHandling mutex failed\n" );
      exit(1);
   }


   eventQueueMutex = SDL_CreateMutex ();
   if ( !eventQueueMutex ) {
      printf("creating eventQueueMutex failed\n" );
      exit(1);
   }

   graphicsQueueMutex = SDL_CreateMutex();
   if ( !graphicsQueueMutex ) {
      printf("creating graphicsQueueMutex failed\n" );
      exit(1);
   }

   SDL_EnableUNICODE ( 1 );
   SDL_EnableKeyRepeat ( 250, 30 );

   
#ifdef FirstThreadEvents 
   _gamethread = gamethread;
   secondThreadHandle = SDL_CreateThread ( gameThreadWrapper, data );
   int res = eventthread( NULL );
#else
   secondThreadHandle = SDL_CreateThread ( eventthread, NULL );
   int res = gamethread( data );
   closeEventThread = 1;
#endif


   SDL_WaitThread ( secondThreadHandle, NULL );
   return res;
}



void exit_asc( int returnresult )
{
#ifndef FirstThreadEvents 
   if ( secondThreadHandle ) {
      closeEventThread = 1;
      SDL_WaitThread ( secondThreadHandle, NULL );
   }   
   exit( returnresult );   
#else
   if ( secondThreadHandle ) {
      throw ThreadExitException();
   }
   exit( returnresult );   


#endif

   
}


bool setEventRouting( bool queue, bool legacy )
{
  bool prev = _queueEvents;
  
  _fillLegacyEventStructures = legacy;
  
  _queueEvents = queue;
  if ( !queue ) {
      // clear all waiting events in the queue
      SDL_mutexP( eventQueueMutex );
      while ( !eventQueue.empty())
         eventQueue.pop();
      SDL_mutexV( eventQueueMutex );
   }
   
   return prev;
}

bool legacyEventSystemActive()
{
   return _fillLegacyEventStructures;   
}


bool getQueuedEvent ( SDL_Event& event )
{
   SDL_mutexP( eventQueueMutex );
   if ( !eventQueue.empty() ) {
      event = eventQueue.front();
      eventQueue.pop();
      SDL_mutexV( eventQueueMutex );
      return true;
   }
   SDL_mutexV( eventQueueMutex );
   return false;
}
    
bool peekEvent ( SDL_Event& event )
{
   SDL_mutexP( eventQueueMutex );
   if ( !eventQueue.empty() ) {
      event = eventQueue.front();
      SDL_mutexV( eventQueueMutex );
      return true;
   }
   SDL_mutexV( eventQueueMutex );
   return false;
}