x11.cpp 8.26 KB
Newer Older
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
#if defined(SUPPORT_X11)
#include <map>
#include <X11/Xlib.h>
#include <X11/Xatom.h>
#include <X11/Xutil.h>

#if !defined(GLES)
  #include <GL/gl.h>
  #include <GL/glx.h>
#endif

#include "types.h"
#include "cfg/cfg.h"
#include "linux-dist/x11.h"
#include "linux-dist/main.h"

#if defined(TARGET_PANDORA)
  #define WINDOW_WIDTH  800
#else
  #define WINDOW_WIDTH  640
#endif
#define WINDOW_HEIGHT 480

map<int, int> x11_keymap;
int x11_dc_buttons = 0xFFFF;

int ndcid = 0;
void* x11_glc;

#ifdef TARGET_PANDORA
  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;
    gc = XCreateGC(display, cursormask, GCFunction, &xgc);
    XFillRectangle(display, cursormask, gc, 0, 0, 1, 1);
    dummycolour.pixel = 0;
    dummycolour.red = 0;
    dummycolour.flags = 04;
    cursor = XCreatePixmapCursor(display, cursormask, cursormask, &dummycolour,&dummycolour, 0,0);
    XFreePixmap(display,cursormask);
    XFreeGC(display,gc);
    return cursor;
  }
#endif

void input_x11_handle() {
    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)
            {
              kcode[0] &= ~dc_key;
            }
            else
            {
              kcode[0] |= dc_key;
            }

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


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

        }
      }
    }
}

void input_x11_init()
{
    x11_keymap[113] = DC_DPAD_LEFT;
    x11_keymap[114] = DC_DPAD_RIGHT;

    x11_keymap[111] = DC_DPAD_UP;
    x11_keymap[116] = DC_DPAD_DOWN;

    x11_keymap[53] = DC_BTN_X;
    x11_keymap[54] = DC_BTN_B;
    x11_keymap[55] = DC_BTN_A;

    /*
      //TODO: Fix sliders
      x11_keymap[38] = DPad_Down;
      x11_keymap[39] = DPad_Down;
    */

    x11_keymap[36] = DC_BTN_START;
}

void x11_window_create()
{
    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");
    }
}

void x11_window_set_text(const char* text)
{
  if (x11_win)
  {
    XChangeProperty((Display*)x11_disp, (Window)x11_win,
      XInternAtom((Display*)x11_disp, "WM_NAME", False),     //WM_NAME,
      XInternAtom((Display*)x11_disp, "UTF8_STRING", False), //UTF8_STRING,
      8, PropModeReplace, (const unsigned char *)text, strlen(text));
  }
}

void x11_window_destroy()
{
    // close XWindow
    if (x11_win)
    {
      XDestroyWindow(x11_disp, x11_win);
      x11_win = 0;
    }
    if (x11_disp)
    {
      XCloseDisplay(x11_disp);
      x11_disp = 0;
    }
}
#endif