main.cpp 23 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include "types.h"
#include "cfg/cfg.h"

#if HOST_OS==OS_LINUX
#include <poll.h>
#include <termios.h>
//#include <curses.h>
#include <fcntl.h>
#include <semaphore.h>
#include <stdarg.h>
#include <signal.h>
#include <sys/param.h>
#include <sys/mman.h>
#include <sys/time.h>
#include "hw/sh4/dyna/blockmanager.h"
#include <unistd.h>

18
#if defined(TARGET_EMSCRIPTEN)
19
  #include <emscripten.h>
20
#endif
21

22
23
24
25
#if defined(SUPPORT_X11)
  #include <X11/Xlib.h>
  #include <X11/Xatom.h>
  #include <X11/Xutil.h>
26

27
28
29
30
  #if !defined(GLES)
    #include <GL/gl.h>
    #include <GL/glx.h>
  #endif
31

32
33
  #include <map>
  map<int, int> x11_keymap;
34
35
#endif

36
#if !defined(ANDROID) && HOST_OS != OS_DARWIN && !defined(TARGET_EMSCRIPTEN)
37
38
  #include <sys/stat.h>
  #include <sys/types.h>
39
40
#endif

41
42
43
44
#if defined(USE_JOYSTICK)
  #include <linux/joystick.h>
#endif

45
#ifdef TARGET_PANDORA
46
47
48
49
  #include <signal.h>
  #include <execinfo.h>
  #include <sys/soundcard.h>
  #define WINDOW_WIDTH  800
50
#else
51
  #define WINDOW_WIDTH  640
52
#endif
53
54
55
56
#define WINDOW_HEIGHT 480

void* x11_win = 0;
void* x11_disp = 0;
57

58
59
60
void* libPvr_GetRenderTarget()
{
  return x11_win;
61
62
}

63
64
65
void* libPvr_GetRenderSurface()
{
  return x11_disp;
66
67
}

68
int msgboxf(const wchar* text, unsigned int type, ...)
69
{
70
  va_list args;
71

72
73
74
75
  wchar temp[2048];
  va_start(args, type);
  vsprintf(temp, text, args);
  va_end(args);
76

77
78
79
  //printf(NULL,temp,VER_SHORTNAME,type | MB_TASKMODAL);
  puts(temp);
  return MBX_OK;
80
81
82
83
}



84
85
86
u16 kcode[4] = {0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF};
u8 rt[4] = {0, 0, 0, 0};
u8 lt[4] = {0, 0, 0, 0};
87
u32 vks[4];
88
s8 joyx[4], joyy[4];
89

90
91
enum DCPad
{
92
93
94
95
96
97
98
  Btn_C       = 1,
  Btn_B       = 1<<1,
  Btn_A       = 1<<2,
  Btn_Start   = 1<<3,
  DPad_Up     = 1<<4,
  DPad_Down   = 1<<5,
  DPad_Left   = 1<<6,
99
  DPad_Right  = 1<<7,
100
101
102
103
104
  Btn_Z       = 1<<8,
  Btn_Y       = 1<<9,
  Btn_X       = 1<<10,
  Btn_D       = 1<<11,
  DPad2_Up    = 1<<12,
105
106
107
108
  DPad2_Down  = 1<<13,
  DPad2_Left  = 1<<14,
  DPad2_Right = 1<<15,

109
110
111
112
  Axis_LT = 0x10000,
  Axis_RT = 0x10001,
  Axis_X  = 0x20000,
  Axis_Y  = 0x20001,
113
114
115
116
117
};


void emit_WriteCodeCache();

118
/* evdev input */
119
static int evdev_fd = -1;
120

121
#if defined(USE_EVDEV)
122
123
124
125
126
127
128
  #define EVDEV_DEVICE_STRING "/dev/input/event%d"
  #ifdef TARGET_PANDORA
    #define EVDEV_DEFAULT_DEVICE_ID 4
  #else
    #define EVDEV_DEFAULT_DEVICE_ID 0
  #endif

129
130
131
  int input_evdev_init(const char* device)
  {
    char name[256] = "Unknown";
132

133
134
    printf("evdev: Trying to open device at '%s'", device);

135
    int fd = open(device, O_RDONLY);
136

137
    if (fd >= 0)
138
    {
139
140
141
142
143
144
      fcntl(fd, F_SETFL, O_NONBLOCK);
      if(ioctl(fd, EVIOCGNAME(sizeof(name)), name) < 0)
      {
        perror("evdev: ioctl");
      }
      printf("evdev: Found '%s' at '%s'\n", name, device);
145
    }
146
    else
147
    {
148
      perror("evdev: open");
149
    }
150

151
152
    return fd;
  }
153

154
155
156
  bool input_evdev_handle(int fd, u32 port)
  {
    if (fd < 0)
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
    {
      return false;
    }

    input_event ie;

    #if defined(TARGET_GCW0)
      #define KEY_A      0x1D
      #define KEY_B      0x38
      #define KEY_X      0x2A
      #define KEY_Y      0x39
      #define KEY_L      0xF
      #define KEY_R      0xE
      #define KEY_SELECT 0x1
      #define KEY_START  0x1C
      #define KEY_LEFT   0x69
      #define KEY_RIGHT  0x6A
      #define KEY_UP     0x67
      #define KEY_DOWN   0x6C
      #define KEY_LOCK   0x77    // Note that KEY_LOCK is a switch and remains pressed until it's switched back
177
    #endif
178
179

      static int keys[13];
180
      while(read(fd, &ie, sizeof(ie)) == sizeof(ie))
181
      {
182
183
        #if defined(TARGET_GCW0) || defined(TARGET_PANDORA)
          if (ie.type = EV_KEY)
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
            switch (ie.code)
            {
              case KEY_UP:     keys[ 1] = ie.value; break;
              case KEY_DOWN:   keys[ 2] = ie.value; break;
              case KEY_LEFT:   keys[ 3] = ie.value; break;
              case KEY_RIGHT:  keys[ 4] = ie.value; break;

              #if defined(TARGET_GCW0)
                case KEY_Y:      keys[ 5] = ie.value; break;
                case KEY_B:      keys[ 6] = ie.value; break;
                case KEY_A:      keys[ 7] = ie.value; break;
                case KEY_X:      keys[ 8] = ie.value; break;
                case KEY_SELECT: keys[ 9] = ie.value; break;
                case KEY_START:  keys[12] = ie.value; break;
              #elif  defined(TARGET_PANDORA)
                case KEY_SPACE:      keys[ 0] = ie.value; break;
                case KEY_PAGEUP:     keys[ 5] = ie.value; break;
                case KEY_PAGEDOWN:   keys[ 6] = ie.value; break;
                case KEY_END:        keys[ 7] = ie.value; break;
                case KEY_HOME:       keys[ 8] = ie.value; break;
                case KEY_MENU:       keys[ 9] = ie.value; break;
                case KEY_RIGHTSHIFT: keys[10] = ie.value; break;
                case KEY_RIGHTCTRL:  keys[11] = ie.value; break;
                case KEY_LEFTALT:    keys[12] = ie.value; break;
              #endif
            }
211
212
          }
        }
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
        if (keys[ 0]) { kcode[port] &= ~Btn_C; }
        if (keys[ 6]) { kcode[port] &= ~Btn_A; }
        if (keys[ 7]) { kcode[port] &= ~Btn_B; }
        if (keys[ 5]) { kcode[port] &= ~Btn_Y; }
        if (keys[ 8]) { kcode[port] &= ~Btn_X; }
        if (keys[ 1]) { kcode[port] &= ~DPad_Up; }
        if (keys[ 2]) { kcode[port] &= ~DPad_Down; }
        if (keys[ 3]) { kcode[port] &= ~DPad_Left; }
        if (keys[ 4]) { kcode[port] &= ~DPad_Right; }
        if (keys[12]) { kcode[port] &= ~Btn_Start; }
        if (keys[ 9]) { die("death by escape key"); }
        if (keys[10]) { rt[port] = 255; }
        if (keys[11]) { lt[port] = 255; }
        return true;
      #else
228
        printf("type %i key %i state %i\n", ie.type, ie.code, ie.value);
229
230
      #endif
    }
231

232
233
234
    return true;
  }
#endif
235

236
237
238
239
240

/* legacy joystick input */
static int joystick_fd = -1; // Joystick file descriptor

#if defined(USE_JOYSTICK)
241
242
  #define JOYSTICK_DEVICE_STRING "/dev/input/js%d"
  #define JOYSTICK_DEFAULT_DEVICE_ID 0
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
  #define JOYSTICK_MAP_SIZE 32

  const u32 joystick_map_btn_usb[JOYSTICK_MAP_SIZE]      = { Btn_Y, Btn_B, Btn_A, Btn_X, 0, 0, 0, 0, 0, Btn_Start };
  const u32 joystick_map_axis_usb[JOYSTICK_MAP_SIZE]     = { Axis_X, Axis_Y, 0, 0, 0, 0, 0, 0, 0, 0 };

  const u32 joystick_map_btn_xbox360[JOYSTICK_MAP_SIZE]  = { Btn_A, Btn_B, Btn_X, Btn_Y, 0, 0, 0, Btn_Start, 0, 0 };
  const u32 joystick_map_axis_xbox360[JOYSTICK_MAP_SIZE] = { Axis_X, Axis_Y, Axis_LT, 0, 0, Axis_RT, DPad_Left, DPad_Up, 0, 0 };

  const u32* joystick_map_btn = joystick_map_btn_usb;
  const u32* joystick_map_axis = joystick_map_axis_usb;

  int input_joystick_init(const char* device)
  {
    int axis_count = 0;
    int button_count = 0;
    char name[128] = "Unknown";

260
261
    printf("joystick: Trying to open device at '%s'", device);

262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
    int fd = open(device, O_RDONLY);

    if(fd >= 0)
    {
      fcntl(fd, F_SETFL, O_NONBLOCK);
      ioctl(fd, JSIOCGAXES, &axis_count);
      ioctl(fd, JSIOCGBUTTONS, &button_count);
      ioctl(fd, JSIOCGNAME(sizeof(name)), &name);

      printf("joystick: Found '%s' with %d axis and %d buttons at '%s'.\n", name, axis_count, button_count, device);

      if (strcmp(name, "Microsoft X-Box 360 pad") == 0)
      {
        joystick_map_btn = joystick_map_btn_xbox360;
        joystick_map_axis = joystick_map_axis_xbox360;
        printf("joystick: Using Xbox 360 map\n");
      }
    }
    else
    {
282
      perror("joystick open");
283
284
285
    }

    return fd;
286
287
  }

288
289
290
291
292
293
294
  bool input_joystick_handle(int fd, u32 port)
  {
    // Joystick must be connected
    if(fd < 0) {
      return false;
    }

295
    struct js_event JE;
296
297
    while(read(fd, &JE, sizeof(JE)) == sizeof(JE))
    if (JE.number < JOYSTICK_MAP_SIZE)
298
299
300
301
302
    {
      switch(JE.type & ~JS_EVENT_INIT)
      {
        case JS_EVENT_AXIS:
        {
303
304
          u32 mt = joystick_map_axis[JE.number] >> 16;
          u32 mo = joystick_map_axis[JE.number] & 0xFFFF;
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

          //printf("AXIS %d,%d\n",JE.number,JE.value);
          s8 v=(s8)(JE.value/256); //-127 ... + 127 range

          if (mt == 0)
          {
            kcode[port] |= mo;
            kcode[port] |= mo*2;
            if (v<-64)
            {
              kcode[port] &= ~mo;
            }
            else if (v>64)
            {
              kcode[port] &= ~(mo*2);
            }

           //printf("Mapped to %d %d %d\n",mo,kcode[port]&mo,kcode[port]&(mo*2));
          }
          else if (mt == 1)
          {
            if (v >= 0)
            {
              v++;  //up to 255
            }
            //printf("AXIS %d,%d Mapped to %d %d %d\n",JE.number,JE.value,mo,v,v+127);
            if (mo == 0)
            {
              lt[port] = (v + 127);
            }
            else if (mo == 1)
            {
              rt[port] = (v + 127);
            }
          }
          else if (mt == 2)
          {
            //  printf("AXIS %d,%d Mapped to %d %d [%d]",JE.number,JE.value,mo,v);
            if (mo == 0)
            {
              joyx[port] = v;
            }
            else if (mo == 1)
            {
              joyy[port] = v;
            }
          }
        }
        break;

        case JS_EVENT_BUTTON:
        {
357
358
          u32 mt = joystick_map_btn[JE.number] >> 16;
          u32 mo = joystick_map_btn[JE.number] & 0xFFFF;
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373

          // printf("BUTTON %d,%d\n",JE.number,JE.value);

          if (mt == 0)
          {
            // printf("Mapped to %d\n",mo);
            if (JE.value)
            {
              kcode[port] &= ~mo;
            }
            else
            {
              kcode[port] |= mo;
            }
          }
374
          else if (mt == 1)
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
          {
            // printf("Mapped to %d %d\n",mo,JE.value?255:0);
            if (mo==0)
            {
              lt[port] = JE.value ? 255 : 0;
            }
            else if (mo==1)
            {
              rt[port] = JE.value ? 255 : 0;
            }
          }
        }
        break;
      }
    }
390
391
392
393
394
395
396
397

    return true;
  }
#endif

void SetupInput()
{
  #if defined(USE_EVDEV)
398
399
400
401
402
403
404
405
406
407
408
409
    int evdev_device_id = cfgLoadInt("input", "evdev_device_id", EVDEV_DEFAULT_DEVICE_ID);
    if (evdev_device_id < 0) {
      puts("evdev input disabled by config.\n");
    }
    else
    {
      int evdev_device_length = snprintf(NULL, 0, EVDEV_DEVICE_STRING, evdev_device_id);
      char* evdev_device = (char*)malloc(evdev_device_length + 1);
      sprintf(evdev_device, EVDEV_DEVICE_STRING, evdev_device_id);
      evdev_fd = input_evdev_init(evdev_device);
      free(evdev_device);
    }
410
  #endif
411

412
  #if defined(USE_JOYSTICK)
413
414
415
416
417
418
419
420
421
422
423
424
    int joystick_device_id = cfgLoadInt("input", "joystick_device_id", JOYSTICK_DEFAULT_DEVICE_ID);
    if (joystick_device_id < 0) {
      puts("joystick input disabled by config.\n");
    }
    else
    {
      int joystick_device_length = snprintf(NULL, 0, JOYSTICK_DEVICE_STRING, joystick_device_id);
      char* joystick_device = (char*)malloc(joystick_device_length + 1);
      sprintf(joystick_device, JOYSTICK_DEVICE_STRING, joystick_device_id);
      joystick_fd = input_joystick_init(joystick_device);
      free(joystick_device);
    }
425
  #endif
426
427
428
429
}

extern bool KillTex;

430
#ifdef TARGET_PANDORA
431
432
433
434
435
436
437
438
439
440
  static Cursor CreateNullCursor(Display *display, Window root)
  {
    Pixmap cursormask;
    XGCValues xgc;
    GC gc;
    XColor dummycolour;
    Cursor cursor;

    cursormask = XCreatePixmap(display, root, 1, 1, 1/*depth*/);
    xgc.function = GXclear;
441
    gc = XCreateGC(display, cursormask, GCFunction, &xgc);
442
443
444
445
    XFillRectangle(display, cursormask, gc, 0, 0, 1, 1);
    dummycolour.pixel = 0;
    dummycolour.red = 0;
    dummycolour.flags = 04;
446
    cursor = XCreatePixmapCursor(display, cursormask, cursormask, &dummycolour,&dummycolour, 0,0);
447
448
449
450
    XFreePixmap(display,cursormask);
    XFreeGC(display,gc);
    return cursor;
  }
451
452
#endif

453
454
int x11_dc_buttons = 0xFFFF;

455
456
void UpdateInputState(u32 port)
{
457
  static char key = 0;
458

459
460
461
  kcode[port] = x11_dc_buttons;
  rt[port] = 0;
  lt[port] = 0;
462

463
464
465
  #if defined(TARGET_EMSCRIPTEN)
    return;
  #endif
466

467
468
469
470
471
472
473
  #if defined(USE_JOYSTICK)
    input_joystick_handle(joystick_fd, port);
  #endif

  #if defined(USE_EVDEV)
    input_evdev_handle(evdev_fd, port);
  #endif
474

475
476
477
  #if defined(TARGET_GCW0) || defined(TARGET_PANDORA)
    return;
  #endif
478

479
480
  bool done = false;
  while(!done)
481
482
483
484
  {
    key = 0;
    read(STDIN_FILENO, &key, 1);

485
486
487
488
489
490
491
492
    switch(key)
    {
      case 0:
      case EOF:
        done = true;
        break;
      case 'k': KillTex=true; break;
      case 'a': rt[port] = 255; break;
493
      case 's': lt[port] = 255; break; 
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
      //case 0x1b: die("death by escape key"); break; //this actually quits when i press left for some reason
      
      #ifdef TARGET_PANDORA
        case ' ': kcode[port] &= ~Btn_C; break;
        case '6': kcode[port] &= ~Btn_A; break;
        case 'O': kcode[port] &= ~Btn_B; break;
        case '5': kcode[port] &= ~Btn_Y; break;
        case 'H': kcode[port] &= ~Btn_X; break;
        case 'A': kcode[port] &= ~DPad_Up; break;
        case 'B': kcode[port] &= ~DPad_Down; break;
        case 'D': kcode[port] &= ~DPad_Left; break;
        case 'C': kcode[port] &= ~DPad_Right; break;
        case 'q': die("death by escape key"); break;
      #endif
      
      #if FEAT_SHREC != DYNAREC_NONE
        case 'b': emit_WriteCodeCache(); break;
        case 'n': bm_Reset(); break;
        case 'm': bm_Sort(); break;
        case ',':
          emit_WriteCodeCache();
          bm_Sort();
        break;
      #endif
    }
519
  }
520
521
522
523
}

void os_DoEvents()
{
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
  #if defined(SUPPORT_X11)
    if (x11_win)
    {
      //Handle X11
      XEvent e;

      if(XCheckWindowEvent((Display*)x11_disp, (Window)x11_win, KeyPressMask | KeyReleaseMask, &e))
      {
        switch(e.type)
        {
          case KeyPress:
          case KeyRelease:
          {
            int dc_key = x11_keymap[e.xkey.keycode];

            if (e.type == KeyPress)
            {
              x11_dc_buttons &= ~dc_key;
            }
            else
            {
              x11_dc_buttons |= dc_key;
            }

            //printf("KEY: %d -> %d: %d\n",e.xkey.keycode, dc_key, x11_dc_buttons );
          }
          break;


          {
            printf("KEYRELEASE\n");
          }
          break;

        }
      }
    }
  #endif
562
563
564
565
}

void os_SetWindowText(const char * text)
{
566
567
568
569
570
571
572
573
  if (0==x11_win || 0==x11_disp || 1)
  {
    printf("%s\n",text);
  }
  #if defined(SUPPORT_X11)
  else if (x11_win)
  {
    XChangeProperty((Display*)x11_disp, (Window)x11_win,
574
575
      XInternAtom((Display*)x11_disp, "WM_NAME", False),     //WM_NAME,
      XInternAtom((Display*)x11_disp, "UTF8_STRING", False), //UTF8_STRING,
576
577
578
      8, PropModeReplace, (const unsigned char *)text, strlen(text));
  }
  #endif
579
580
581
}


~skmp's avatar
~skmp committed
582
583
void* x11_glc;

584
585
586
int ndcid=0;
void os_CreateWindow()
{
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
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
  #if defined(SUPPORT_X11)
    if (cfgLoadInt("pvr", "nox11", 0) == 0)
    {
      XInitThreads();
      // X11 variables
      Window       x11Window = 0;
      Display*     x11Display = 0;
      long         x11Screen = 0;
      XVisualInfo* x11Visual = 0;
      Colormap     x11Colormap = 0;

      /*
      Step 0 - Create a NativeWindowType that we can use it for OpenGL ES output
      */
      Window sRootWindow;
      XSetWindowAttributes sWA;
      unsigned int ui32Mask;
      int i32Depth;

      // Initializes the display and screen
      x11Display = XOpenDisplay(0);
      if (!x11Display && !(x11Display = XOpenDisplay(":0")))
      {
        printf("Error: Unable to open X display\n");
        return;
      }
      x11Screen = XDefaultScreen(x11Display);

      // Gets the window parameters
      sRootWindow = RootWindow(x11Display, x11Screen);

      int depth = CopyFromParent;

      #if !defined(GLES)
        // Get a matching FB config
        static int visual_attribs[] =
        {
          GLX_X_RENDERABLE    , True,
          GLX_DRAWABLE_TYPE   , GLX_WINDOW_BIT,
          GLX_RENDER_TYPE     , GLX_RGBA_BIT,
          GLX_X_VISUAL_TYPE   , GLX_TRUE_COLOR,
          GLX_RED_SIZE        , 8,
          GLX_GREEN_SIZE      , 8,
          GLX_BLUE_SIZE       , 8,
          GLX_ALPHA_SIZE      , 8,
          GLX_DEPTH_SIZE      , 24,
          GLX_STENCIL_SIZE    , 8,
          GLX_DOUBLEBUFFER    , True,
          //GLX_SAMPLE_BUFFERS  , 1,
          //GLX_SAMPLES         , 4,
          None
        };

        int glx_major, glx_minor;

        // FBConfigs were added in GLX version 1.3.
        if (!glXQueryVersion(x11Display, &glx_major, &glx_minor) ||
            ((glx_major == 1) && (glx_minor < 3)) || (glx_major < 1))
        {
          printf("Invalid GLX version");
          exit(1);
        }

        int fbcount;
        GLXFBConfig* fbc = glXChooseFBConfig(x11Display, x11Screen, visual_attribs, &fbcount);
        if (!fbc)
        {
          printf("Failed to retrieve a framebuffer config\n");
          exit(1);
        }
        printf("Found %d matching FB configs.\n", fbcount);

        GLXFBConfig bestFbc = fbc[0];
        XFree(fbc);

        // Get a visual
        XVisualInfo *vi = glXGetVisualFromFBConfig(x11Display, bestFbc);
        printf("Chosen visual ID = 0x%x\n", vi->visualid);


        depth = vi->depth;
        x11Visual = vi;

        x11Colormap = XCreateColormap(x11Display, RootWindow(x11Display, x11Screen), vi->visual, AllocNone);
      #else
        i32Depth = DefaultDepth(x11Display, x11Screen);
        x11Visual = new XVisualInfo;
        XMatchVisualInfo(x11Display, x11Screen, i32Depth, TrueColor, x11Visual);
        if (!x11Visual)
        {
          printf("Error: Unable to acquire visual\n");
          return;
        }
        x11Colormap = XCreateColormap(x11Display, sRootWindow, x11Visual->visual, AllocNone);
      #endif

      sWA.colormap = x11Colormap;

      // Add to these for handling other events
      sWA.event_mask = StructureNotifyMask | ExposureMask | ButtonPressMask | ButtonReleaseMask | KeyPressMask | KeyReleaseMask;
      ui32Mask = CWBackPixel | CWBorderPixel | CWEventMask | CWColormap;

      #ifdef TARGET_PANDORA
        int width = 800;
        int height = 480;
      #else
        int width = cfgLoadInt("x11", "width", WINDOW_WIDTH);
        int height = cfgLoadInt("x11", "height", WINDOW_HEIGHT);
      #endif

      if (width == -1)
      {
        width = XDisplayWidth(x11Display, x11Screen);
        height = XDisplayHeight(x11Display, x11Screen);
      }

      // Creates the X11 window
      x11Window = XCreateWindow(x11Display, RootWindow(x11Display, x11Screen), (ndcid%3)*640, (ndcid/3)*480, width, height,
        0, depth, InputOutput, x11Visual->visual, ui32Mask, &sWA);

      #ifdef TARGET_PANDORA
        // fullscreen
        Atom wmState = XInternAtom(x11Display, "_NET_WM_STATE", False);
        Atom wmFullscreen = XInternAtom(x11Display, "_NET_WM_STATE_FULLSCREEN", False);
        XChangeProperty(x11Display, x11Window, wmState, XA_ATOM, 32, PropModeReplace, (unsigned char *)&wmFullscreen, 1);

        XMapRaised(x11Display, x11Window);
      #else
        XMapWindow(x11Display, x11Window);

        #if !defined(GLES)
          #define GLX_CONTEXT_MAJOR_VERSION_ARB       0x2091
          #define GLX_CONTEXT_MINOR_VERSION_ARB       0x2092
          typedef GLXContext (*glXCreateContextAttribsARBProc)(Display*, GLXFBConfig, GLXContext, Bool, const int*);

          glXCreateContextAttribsARBProc glXCreateContextAttribsARB = 0;
          glXCreateContextAttribsARB = (glXCreateContextAttribsARBProc)glXGetProcAddressARB((const GLubyte*)"glXCreateContextAttribsARB");
          verify(glXCreateContextAttribsARB != 0);
          int context_attribs[] =
          {
            GLX_CONTEXT_MAJOR_VERSION_ARB, 3,
            GLX_CONTEXT_MINOR_VERSION_ARB, 1,
            GLX_CONTEXT_FLAGS_ARB, GLX_CONTEXT_DEBUG_BIT_ARB,
            GLX_CONTEXT_PROFILE_MASK_ARB, GLX_CONTEXT_CORE_PROFILE_BIT_ARB,
            None
          };

          x11_glc = glXCreateContextAttribsARB(x11Display, bestFbc, 0, True, context_attribs);
          XSync(x11Display, False);

          if (!x11_glc)
          {
            die("Failed to create GL3.1 context\n");
          }
        #endif
      #endif

      XFlush(x11Display);

      //(EGLNativeDisplayType)x11Display;
      x11_disp = (void*)x11Display;
      x11_win = (void*)x11Window;
    }
    else
    {
      printf("Not creating X11 window ..\n");
    }
  #endif
755
756
757
758
759
760
}

termios tios, orig_tios;

int setup_curses()
{
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
  //initscr();
  //cbreak();
  //noecho();


  /* Get current terminal settings */
  if (tcgetattr(STDIN_FILENO, &orig_tios)) {
    printf("Error getting current terminal settings\n");
    return -1;
  }

  memcpy(&tios, &orig_tios, sizeof(struct termios));
  tios.c_lflag &= ~ICANON;    //(ECHO|ICANON);&= ~(ICANON | ECHO | ECHOE | ECHOK | ECHONL | ECHOPRT | ECHOKE | ICRNL);

  tios.c_cc[VTIME] = 0;
  tios.c_cc[VMIN]  = 0;

  if (tcsetattr(STDIN_FILENO, TCSANOW, &tios)) {
    printf("Error applying terminal settings\n");
    return -2;
  }

  if (tcgetattr(STDIN_FILENO, &tios)) {
    tcsetattr(0, TCSANOW, &orig_tios);
    printf("Error while asserting terminal settings\n");
    return -3;
  }

  if ((tios.c_lflag & ICANON) || !(tios.c_lflag & ECHO)) {
    tcsetattr(STDIN_FILENO, TCSANOW, &orig_tios);
    printf("Could not apply all terminal settings\n");
    return -4;
  }

  fcntl(STDIN_FILENO, F_SETFL, O_NONBLOCK);
  return 1;
}
798

799
800
801
void common_linux_setup();
int dc_init(int argc,wchar* argv[]);
void dc_run();
802

803
804
#ifdef TARGET_PANDORA
  void gl_term();
805

806
807
808
809
  void clean_exit(int sig_num)
  {
    void* array[10];
    size_t size;
810

811
812
    if (joystick_fd >= 0) { close(joystick_fd); }
    if (evdev_fd >= 0) { close(evdev_fd); }
813

814
815
816
817
    // Close EGL context ???
    if (sig_num!=0)
    {
      gl_term();
818
819
    }

820
821
822
823
824
    // close XWindow
    if (x11_win)
    {
      XDestroyWindow(x11_disp, x11_win);
      x11_win = 0;
825
    }
826
827
828
829
    if (x11_disp)
    {
      XCloseDisplay(x11_disp);
      x11_disp = 0;
830
831
    }

832
833
834
835
    // finish cleaning
    if (sig_num!=0)
    {
      write(2, "\nSignal received\n", sizeof("\nSignal received\n"));
836

837
838
839
840
841
      size = backtrace(array, 10);
      backtrace_symbols_fd(array, size, STDERR_FILENO);
      exit(1);
    }
  }
842
843
#endif

844
845
int main(int argc, wchar* argv[])
{
846
847
848
849
850
851
852
853
  if (setup_curses() < 0)
  {
    printf("failed to setup curses!\n");
  }
  #ifdef TARGET_PANDORA
    signal(SIGSEGV, clean_exit);
    signal(SIGKILL, clean_exit);
  #endif
854

855
  /* Set home dir */
856
  string home = ".";
857
  #if defined(USES_HOMEDIR)
858
859
860
861
862
    if(getenv("HOME") != NULL)
    {
      home = (string)getenv("HOME") + "/.reicast";
      mkdir(home.c_str(), 0755); // create the directory if missing
    }
863
  #endif
864
865
  SetHomeDir(home);
  printf("Home dir is: %s\n", GetPath("/").c_str());
866

867
868
869
  #if defined(SUPPORT_X11)
    x11_keymap[113] = DPad_Left;
    x11_keymap[114] = DPad_Right;
870

871
872
    x11_keymap[111] = DPad_Up;
    x11_keymap[116] = DPad_Down;
873

874
875
876
877
    x11_keymap[52] = Btn_Y;
    x11_keymap[53] = Btn_X;
    x11_keymap[54] = Btn_B;
    x11_keymap[55] = Btn_A;
878

879
880
881
882
883
    /*
      //TODO: Fix sliders
      x11_keymap[38] = DPad_Down;
      x11_keymap[39] = DPad_Down;
    */
884

885
886
    x11_keymap[36] = Btn_Start;
  #endif
887

888
  common_linux_setup();
889

890
  settings.profile.run_counts=0;
891

892
  dc_init(argc,argv);
893

894
895
  SetupInput();

896
897
898
899
900
901
902
903
904
905
906
907
908
  #if !defined(TARGET_EMSCRIPTEN)
    dc_run();
  #else
    emscripten_set_main_loop(&dc_run, 100, false);
  #endif


  #ifdef TARGET_PANDORA
    clean_exit(0);
  #endif

  return 0;
}
Jan Holthuis's avatar
Jan Holthuis committed
909
#endif
910
911
912

int get_mic_data(u8* buffer) { return 0; }
int push_vmu_screen(u8* buffer) { return 0; }
~skmp's avatar
~skmp committed
913
914
915

void os_DebugBreak()
{
916
  #if !defined(TARGET_EMSCRIPTEN)
~skmp's avatar
~skmp committed
917
    raise(SIGTRAP);
918
919
920
921
  #else
    printf("DEBUGBREAK!\n");
    exit(-1);
  #endif
~skmp's avatar
~skmp committed
922
}