Repository navigation
Expand file tree
/
Copy pathx11.cpp
More file actions
533 lines (464 loc) · 13.1 KB
/
8000
Copy pathx11.cpp
File metadata and controls
533 lines (464 loc) · 13.1 KB
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
/*
This file is part of libswirl
*/
#include "license/bsd"
#if defined(SUPPORT_X11)
#include <imgui/imgui.h>
#include <map>
#include <memory>
#include <X11/Xlib.h>
#include <X11/Xatom.h>
#include <X11/Xutil.h>
#include "types.h"
#include "libswirl.h"
#include "cfg/cfg.h"
#include "linux-dist/x11.h"
#include "linux-dist/main.h"
// #include "gui/gui.h"
// #include "gui/gui_renderer.h"
// #include "gui/gui_partials.h"
#include "input/gamepad.h"
#if FEAT_HAS_NIXPROF
#include "profiler/profiler.h"
#endif
#include "x11_keyboard.h"
#include "utils/glinit/glx/glx.h"
#if defined(TARGET_PANDORA)
#define DEFAULT_FULLSCREEN true
#define DEFAULT_WINDOW_WIDTH 800
#else
#define DEFAULT_FULLSCREEN false
#define DEFAULT_WINDOW_WIDTH 640
#endif
#define DEFAULT_WINDOW_HEIGHT 480
class MouseInputMapping : public InputMapping
{
public:
MouseInputMapping()
{
name = "X11 Mouse";
set_button(DC_BTN_A, Button1);
set_button(DC_BTN_B, Button3);
set_button(DC_BTN_START, Button2);
dirty = false;
}
};
class X11MouseGamepadDevice : public GamepadDevice
{
public:
X11MouseGamepadDevice(int maple_port) : GamepadDevice(maple_port, "X11")
{
_name = "Mouse";
_unique_id = "x11_mouse";
if (!find_mapping())
input_mapper = new MouseInputMapping();
}
bool gamepad_btn_input(u32 code, bool pressed) override
{
if (g_GUI && g_GUI->IsOpen())
// Don't register mouse clicks as gamepad presses when gui is open
// This makes the gamepad presses to be handled first and the mouse position to be ignored
// TODO Make this generic
return false;
else
return GamepadDevice::gamepad_btn_input(code, pressed);
}
};
int x11_keyboard_input = 0;
static std::shared_ptr<X11KeyboardDevice> x11_keyboard;
static std::shared_ptr<X11KbGamepadDevice> kb_gamepad;
static std::shared_ptr<X11MouseGamepadDevice> mouse_gamepad;
int x11_width;
int x11_height;
int ndcid = 0;
bool x11_fullscreen = false;
Atom wmDeleteMessage;
void* x11_vis;
extern bool dump_frame_switch;
enum
{
_NET_WM_STATE_REMOVE =0,
_NET_WM_STATE_ADD = 1,
_NET_WM_STATE_TOGGLE =2
};
void x11_window_set_fullscreen(bool fullscreen)
{
XEvent xev;
xev.xclient.type = ClientMessage;
xev.xclient.window = (Window)x11_win;
xev.xclient.message_type = XInternAtom((Display*)x11_disp, "_NET_WM_STATE", False);
xev.xclient.format = 32;
xev.xclient.data.l[0] = 2; // _NET_WM_STATE_TOGGLE
xev.xclient.data.l[1] = XInternAtom((Display*)x11_disp, "_NET_WM_STATE_FULLSCREEN", True);
xev.xclient.data.l[2] = 0; // no second property to toggle
xev.xclient.data.l[3] = 1;
xev.xclient.data.l[4] = 0;
printf("x11: setting fullscreen to %d\n", fullscreen);
XSendEvent((Display*)x11_disp, DefaultRootWindow((Display*)x11_disp), False, SubstructureNotifyMask, &xev);
}
void event_x11_handle()
{
XEvent event;
while(XPending((Display *)x11_disp))
{
XNextEvent((Display *)x11_disp, &event);
if
10BC8
(event.type == ClientMessage &&
event.xclient.data.l[0] == wmDeleteMessage)
{
if (virtualDreamcast && sh4_cpu->IsRunning()) {
virtualDreamcast->Stop([] {
g_GUIRenderer->Stop();
});
}
else
{
g_GUIRenderer->Stop();
}
}
else if (event.type == ConfigureNotify)
{
x11_width = event.xconfigure.width;
x11_height = event.xconfigure.height;
}
}
}
extern u32 mo_buttons;
extern f32 mo_x_delta;
extern f32 mo_y_delta;
extern f32 mo_wheel_delta;
extern s32 mo_x_abs;
extern s32 mo_y_abs;
static bool capturing_mouse;
static Cursor empty_cursor = None;
static Cursor create_empty_cursor()
{
if (empty_cursor == None)
{
Display *display = (Display*)x11_disp;
char data[] = { 0 };
XColor color;
color.red = color.green = color.blue = 0;
Pixmap pixmap = XCreateBitmapFromData(display, DefaultRootWindow(display),
data, 1, 1);
if (pixmap)
{
empty_cursor = XCreatePixmapCursor(display, pixmap, pixmap, &color, &color, 0, 0);
XFreePixmap(display, pixmap);
}
}
return empty_cursor;
}
static void destroy_empty_cursor()
{
if (empty_cursor != None)
{
XFreeCursor((Display*)x11_disp, empty_cursor);
empty_cursor = None;
}
}
static void x11_capture_mouse()
{
x11_window_set_text("Reicast - mouse capture");
capturing_mouse = true;
Cursor cursor = create_empty_cursor();
Display *display = (Display*)x11_disp;
Window window = (Window)x11_win;
XDefineCursor(display, window, cursor);
XGrabPointer(display, window, False,
ButtonPressMask | ButtonReleaseMask | PointerMotionMask | FocusChangeMask,
GrabModeAsync, GrabModeAsync, None, None, CurrentTime);
}
static void x11_uncapture_mouse()
{
x11_window_set_text("Reicast");
capturing_mouse = false;
Display *display = (Display*)x11_disp;
Window window = (Window)x11_win;
XUndefineCursor(display, window);
XUngrabPointer(display, CurrentTime);
}
void input_x11_handle()
{
//Handle X11
static int prev_x = -1;
static int prev_y = -1;
bool mouse_moved = false;
XEvent e;
Display *display = (Display*)x11_disp;
while (XCheckWindowEvent(display, (Window)x11_win,
KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask
| PointerMotionMask | FocusChangeMask,
&e))
{
switch(e.type)
{
case KeyPress:
{
char buf[2];
KeySym keysym_return;
int len = XLookupString(&e.xkey, buf, 1, &keysym_return, NULL);
if (len > 0)
x11_keyboard->keyboard_character(buf[0]);
}
/* no break */
case KeyRelease:
{
if (e.type == KeyRelease && XEventsQueued(display, QueuedAfterReading))
{
XEvent nev;
XPeekEvent(display, &nev);
if (nev.type == KeyPress && nev.xkey.time == e.xkey.time &&
nev.xkey.keycode == e.xkey.keycode)
// Key wasn’t actually released: auto repeat
continue;
}
// Dreamcast keyboard emulation
x11_keyboard->keyboard_input(e.xkey.keycode, e.type == KeyPress);
// keyboard-based emulated gamepad
kb_gamepad->gamepad_btn_input(e.xkey.keycode, e.type == KeyPress);
// Start/stop mouse capture with Left Ctrl + Left Alt
if (e.type == KeyPress
&& ((e.xkey.keycode == KEY_LALT && (e.xkey.state & ControlMask))
|| (e.xkey.keycode == KEY_LCTRL && (e.xkey.state & Mod1Mask))))
{
capturing_mouse = !capturing_mouse;
if (capturing_mouse)
x11_capture_mouse();
else
x11_uncapture_mouse();
}
// TODO Move this to bindable keys or in the gui menu
if (x11_keyboard_input)
{
#if 1
if (e.xkey.keycode == KEY_F10)
{
// Dump the next frame into a file
dump_frame_switch = e.type == KeyPress;
}
else
#elif FEAT_HAS_NIXPROF
if (e.type == KeyRelease && e.xkey.keycode == KEY_F10)
{
if (sample_Switch(3000)) {
printf("Starting profiling\n");
} else {
printf("Stopping profiling\n");
}
}
else
#endif
if (e.type == KeyRelease && e.xkey.keycode == KEY_F11)
{
x11_fullscreen = !x11_fullscreen;
x11_window_set_fullscreen(x11_fullscreen);
}
if (e.type == KeyRelease && e.xkey.keycode == KEY_F10) {
printf("calling refsw_dump\n");
void refsw_dump();
refsw_dump();
}
}
}
break;
case FocusOut:
{
if (capturing_mouse)
x11_uncapture_mouse();
capturing_mouse = false;
}
break;
case ButtonPress:
case ButtonRelease:
mouse_gamepad->gamepad_btn_input(e.xbutton.button, e.type == ButtonPress);
{
u32 button_mask = 0;
switch (e.xbutton.button)
{
case Button1: // Left button
button_mask = 1 << 2;
break;
case Button2: // Middle button
button_mask = 1 << 3;
break;
case Button3: // Right button
button_mask = 1 << 1;
break;
case Button4: // Mouse wheel up
mo_wheel_delta -= 16;
break;
case Button5: // Mouse wheel down
mo_wheel_delta += 16;
break;
default:
break;
}
if (button_mask)
{
if (e.type == ButtonPress)
mo_buttons &= ~button_mask;
else
mo_buttons |= button_mask;
}
}
/* no break */
case MotionNotify:
// For Light gun
mo_x_abs = (e.xmotion.x - (x11_width - x11_height * 640 / 480) / 2) * 480 / x11_height;
mo_y_abs = e.xmotion.y * 480 / x11_height;
// For mouse
mouse_moved = true;
if (prev_x != -1)
mo_x_delta += (f32)(e.xmotion.x - prev_x) * settings.input.MouseSensitivity / 100.f;
if (prev_y != -1)
mo_y_delta += (f32)(e.xmotion.y - prev_y) * settings.input.MouseSensitivity / 100.f;
prev_x = e.xmotion.x;
prev_y = e.xmotion.y;
break;
}
}
if (capturing_mouse && mouse_moved)
{
prev_x = x11_width / 2;
prev_y = x11_height / 2;
XWarpPointer(display, None, (Window)x11_win, 0, 0, 0, 0,
prev_x, prev_y);
XSync(display, true);
}
}
void input_x11_init()
{
printf("F10: dump vram\n");
x11_keyboard = std::make_shared<X11KeyboardDevice>(0);
kb_gamepad = std::make_shared<X11KbGamepadDevice>(0);
GamepadDevice::Register(kb_gamepad);
mouse_gamepad = std::make_shared<X11MouseGamepadDevice>(0);
GamepadDevice::Register(mouse_gamepad);
x11_keyboard_input = (cfgLoadInt("input", "enable_x11_keyboard", 1) >= 1);
if (!x11_keyboard_input)
printf("X11 Keyboard input disabled by config.\n");
}
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(NULL);
if (!x11Display && !(x11Display = XOpenDisplay(":0")))
{
printf("Error: Unable to open X display\n");
return;
}
x11Screen = XDefaultScreen(x11Display);
float xdpi = (float)DisplayWidth(x11Display, x11Screen) / DisplayWidthMM(x11Display, x11Screen) * 25.4;
float ydpi = (float)DisplayHeight(x11Display, x11Screen) / DisplayHeightMM(x11Display, x11Screen) * 25.4;
screen_dpi = max(xdpi, ydpi);
// Gets the window parameters
sRootWindow = RootWindow(x11Display, x11Screen);
int depth = CopyFromParent;
#if SUPPORT_GLX
if (!glx_ChooseVisual(x11Display, x11Screen, &x11Visual, &depth))
{
printf("Error: Unable to acquire visual\n");
return;
}
#else
i32Depth = DefaultDepth(x11Display, x11Screen);
x11Visual = new XVisualInfo;
XMatchVisualInfo(x11Display, x11Screen, i32Depth, TrueColor, x11Visual);
if (!x11Visual)
{
printf("Error: Unable to acquire visual\n");
return;
}
#endif
x11Colormap = XCreateColormap(x11Display, sRootWindow, x11Visual->visual, AllocNone);
sWA.colormap = x11Colormap;
// Add to these for handling other events
sWA.event_mask = StructureNotifyMask | ExposureMask | ButtonPressMask | ButtonReleaseMask | KeyPressMask | KeyReleaseMask;
sWA.event_mask |= PointerMotionMask | FocusChangeMask;
ui32Mask = CWBackPixel | CWBorderPixel | CWEventMask | CWColormap;
x11_width = cfgLoadInt("x11", "width", DEFAULT_WINDOW_WIDTH);
x11_height = cfgLoadInt("x11", "height", DEFAULT_WINDOW_HEIGHT);
x11_fullscreen = cfgLoadBool("x11", "fullscreen", DEFAULT_FULLSCREEN);
if (x11_width < 0 || x11_height < 0)
{
x11_width = XDisplayWidth(x11Display, x11Screen);
x11_height = XDisplayHeight(x11Display, x11Screen);
}
// Creates the X11 window
x11Window = XCreateWindow(x11Display, RootWindow(x11Display, x11Screen), (ndcid%3)*640, (ndcid/3)*480, x11_width, x11_height,
0, depth, InputOutput, x11Visual->visual, ui32Mask, &sWA);
// Capture the close window event
wmDeleteMessage = XInternAtom(x11Display, "WM_DELETE_WINDOW", False);
XSetWMProtocols(x11Display, x11Window, &wmDeleteMessage, 1);
if(x11_fullscreen)
{
// 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);
}
XFlush(x11Display);
//(EGLNativeDisplayType)x11Display;
x11_disp = (void*)x11Display;
x11_win = (void*)x11Window;
x11_vis = (void*)x11Visual->visual;
x11_window_set_text("Reicast");
}
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()
{
destroy_empty_cursor();
// close XWindow
if (x11_win)
{
if (!x11_fullscreen)
{
cfgSaveInt("x11", "width", x11_width);
cfgSaveInt("x11", "height", x11_height);
}
cfgSaveBool("x11", "fullscreen", x11_fullscreen);
XDestroyWindow((Display*)x11_disp, (Window)x11_win);
x11_win = NULL;
}
if (x11_disp)
{
XCloseDisplay((Display*)x11_disp);
x11_disp = NULL;
}
}
#endif