| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749 |
- /*
- * Copyright (c) 2012-2016 Daniele Bartolini and individual contributors.
- * License: https://github.com/taylor001/crown/blob/master/LICENSE
- */
- #include "config.h"
- #if CROWN_PLATFORM_LINUX
- #include "bundle_compiler.h"
- #include "command_line.h"
- #include "console_server.h"
- #include "device.h"
- #include "display.h"
- #include "os_event_queue.h"
- #include "thread.h"
- #include "window.h"
- #include <stdlib.h>
- #include <X11/extensions/Xrandr.h>
- #include <X11/Xatom.h>
- #include <X11/XKBlib.h>
- #include <X11/Xlib.h>
- #include <X11/Xutil.h>
- #include <bgfx/bgfxplatform.h>
- namespace crown
- {
- static KeyboardButton::Enum x11_translate_key(KeySym x11_key)
- {
- switch (x11_key)
- {
- case XK_BackSpace: return KeyboardButton::BACKSPACE;
- case XK_Tab: return KeyboardButton::TAB;
- case XK_space: return KeyboardButton::SPACE;
- case XK_Escape: return KeyboardButton::ESCAPE;
- case XK_Return: return KeyboardButton::ENTER;
- case XK_F1: return KeyboardButton::F1;
- case XK_F2: return KeyboardButton::F2;
- case XK_F3: return KeyboardButton::F3;
- case XK_F4: return KeyboardButton::F4;
- case XK_F5: return KeyboardButton::F5;
- case XK_F6: return KeyboardButton::F6;
- case XK_F7: return KeyboardButton::F7;
- case XK_F8: return KeyboardButton::F8;
- case XK_F9: return KeyboardButton::F9;
- case XK_F10: return KeyboardButton::F10;
- case XK_F11: return KeyboardButton::F11;
- case XK_F12: return KeyboardButton::F12;
- case XK_Home: return KeyboardButton::HOME;
- case XK_Left: return KeyboardButton::LEFT;
- case XK_Up: return KeyboardButton::UP;
- case XK_Right: return KeyboardButton::RIGHT;
- case XK_Down: return KeyboardButton::DOWN;
- case XK_Page_Up: return KeyboardButton::PAGE_UP;
- case XK_Page_Down: return KeyboardButton::PAGE_DOWN;
- case XK_Insert: return KeyboardButton::INSERT;
- case XK_Delete: return KeyboardButton::DELETE;
- case XK_End: return KeyboardButton::END;
- case XK_Shift_L: return KeyboardButton::LEFT_SHIFT;
- case XK_Shift_R: return KeyboardButton::RIGHT_SHIFT;
- case XK_Control_L: return KeyboardButton::LEFT_CTRL;
- case XK_Control_R: return KeyboardButton::RIGHT_CTRL;
- case XK_Caps_Lock: return KeyboardButton::CAPS_LOCK;
- case XK_Alt_L: return KeyboardButton::LEFT_ALT;
- case XK_Alt_R: return KeyboardButton::RIGHT_ALT;
- case XK_Super_L: return KeyboardButton::LEFT_SUPER;
- case XK_Super_R: return KeyboardButton::RIGHT_SUPER;
- case XK_Num_Lock: return KeyboardButton::NUM_LOCK;
- case XK_KP_Enter: return KeyboardButton::NUMPAD_ENTER;
- case XK_KP_Delete: return KeyboardButton::NUMPAD_DELETE;
- case XK_KP_Multiply: return KeyboardButton::NUMPAD_MULTIPLY;
- case XK_KP_Add: return KeyboardButton::NUMPAD_ADD;
- case XK_KP_Subtract: return KeyboardButton::NUMPAD_SUBTRACT;
- case XK_KP_Divide: return KeyboardButton::NUMPAD_DIVIDE;
- case XK_KP_Insert:
- case XK_KP_0: return KeyboardButton::NUMPAD_0;
- case XK_KP_End:
- case XK_KP_1: return KeyboardButton::NUMPAD_1;
- case XK_KP_Down:
- case XK_KP_2: return KeyboardButton::NUMPAD_2;
- case XK_KP_Page_Down: // or XK_KP_Next
- case XK_KP_3: return KeyboardButton::NUMPAD_3;
- case XK_KP_Left:
- case XK_KP_4: return KeyboardButton::NUMPAD_4;
- case XK_KP_Begin:
- case XK_KP_5: return KeyboardButton::NUMPAD_5;
- case XK_KP_Right:
- case XK_KP_6: return KeyboardButton::NUMPAD_6;
- case XK_KP_Home:
- case XK_KP_7: return KeyboardButton::NUMPAD_7;
- case XK_KP_Up:
- case XK_KP_8: return KeyboardButton::NUMPAD_8;
- case XK_KP_Page_Up: // or XK_KP_Prior
- case XK_KP_9: return KeyboardButton::NUMPAD_9;
- case '0': return KeyboardButton::NUMBER_0;
- case '1': return KeyboardButton::NUMBER_1;
- case '2': return KeyboardButton::NUMBER_2;
- case '3': return KeyboardButton::NUMBER_3;
- case '4': return KeyboardButton::NUMBER_4;
- case '5': return KeyboardButton::NUMBER_5;
- case '6': return KeyboardButton::NUMBER_6;
- case '7': return KeyboardButton::NUMBER_7;
- case '8': return KeyboardButton::NUMBER_8;
- case '9': return KeyboardButton::NUMBER_9;
- case 'a': return KeyboardButton::A;
- case 'b': return KeyboardButton::B;
- case 'c': return KeyboardButton::C;
- case 'd': return KeyboardButton::D;
- case 'e': return KeyboardButton::E;
- case 'f': return KeyboardButton::F;
- case 'g': return KeyboardButton::G;
- case 'h': return KeyboardButton::H;
- case 'i': return KeyboardButton::I;
- case 'j': return KeyboardButton::J;
- case 'k': return KeyboardButton::K;
- case 'l': return KeyboardButton::L;
- case 'm': return KeyboardButton::M;
- case 'n': return KeyboardButton::N;
- case 'o': return KeyboardButton::O;
- case 'p': return KeyboardButton::P;
- case 'q': return KeyboardButton::Q;
- case 'r': return KeyboardButton::R;
- case 's': return KeyboardButton::S;
- case 't': return KeyboardButton::T;
- case 'u': return KeyboardButton::U;
- case 'v': return KeyboardButton::V;
- case 'w': return KeyboardButton::W;
- case 'x': return KeyboardButton::X;
- case 'y': return KeyboardButton::Y;
- case 'z': return KeyboardButton::Z;
- default: return KeyboardButton::COUNT;
- }
- }
- #define JS_EVENT_BUTTON 0x01 /* button pressed/released */
- #define JS_EVENT_AXIS 0x02 /* joystick moved */
- #define JS_EVENT_INIT 0x80 /* initial state of device */
- #define XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE 7849
- #define XINPUT_GAMEPAD_RIGHT_THUMB_DEADZONE 8689
- #define XINPUT_GAMEPAD_THRESHOLD 30
- static u8 s_button[] =
- {
- JoypadButton::A,
- JoypadButton::B,
- JoypadButton::X,
- JoypadButton::Y,
- JoypadButton::LEFT_SHOULDER,
- JoypadButton::RIGHT_SHOULDER,
- JoypadButton::BACK,
- JoypadButton::START,
- JoypadButton::GUIDE,
- JoypadButton::LEFT_THUMB,
- JoypadButton::RIGHT_THUMB,
- JoypadButton::UP, // FIXME (reported as axis...)
- JoypadButton::DOWN,
- JoypadButton::LEFT,
- JoypadButton::RIGHT
- };
- static u16 s_deadzone[] =
- {
- XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE,
- XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE,
- XINPUT_GAMEPAD_THRESHOLD,
- XINPUT_GAMEPAD_RIGHT_THUMB_DEADZONE,
- XINPUT_GAMEPAD_RIGHT_THUMB_DEADZONE,
- XINPUT_GAMEPAD_THRESHOLD
- };
- struct JoypadEvent
- {
- u32 time; /* event timestamp in milliseconds */
- s16 value; /* value */
- u8 type; /* event type */
- u8 number; /* axis/button number */
- };
- struct Joypad
- {
- void init()
- {
- char jspath[] = "/dev/input/jsX";
- char* num = strchr(jspath, 'X');
- for (u8 i = 0; i < CROWN_MAX_JOYPADS; ++i)
- {
- *num = '0' + i;
- _fd[i] = open(jspath, O_RDONLY | O_NONBLOCK);
- }
- memset(_connected, 0, sizeof(_connected));
- memset(_axis, 0, sizeof(_axis));
- }
- void shutdown()
- {
- for (u8 i = 0; i < CROWN_MAX_JOYPADS; ++i)
- {
- if (_fd[i] != -1)
- close(_fd[i]);
- }
- }
- void update(OsEventQueue& queue)
- {
- JoypadEvent ev;
- memset(&ev, 0, sizeof(ev));
- for (u8 i = 0; i < CROWN_MAX_JOYPADS; ++i)
- {
- const int fd = _fd[i];
- const bool connected = fd != -1;
- if (connected != _connected[i])
- queue.push_joypad_event(i, connected);
- _connected[i] = connected;
- if (!connected)
- continue;
- while(read(fd, &ev, sizeof(ev)) != -1)
- {
- const u8 num = ev.number;
- const s16 val = ev.value;
- switch (ev.type &= ~JS_EVENT_INIT)
- {
- case JS_EVENT_AXIS:
- {
- AxisData& axis = _axis[i];
- // Indices into axis.left/right respectively
- const u8 axis_idx[] = { 0, 1, 2, 0, 1, 2 };
- const s16 deadzone = s_deadzone[num];
- s16 value = val > deadzone || val < -deadzone ? val : 0;
- // Remap triggers to [0, INT16_MAX]
- if (num == 2 || num == 5)
- value = (value + INT16_MAX) >> 1;
- f32* values = num > 2 ? axis.right : axis.left;
- values[axis_idx[num]] = value != 0
- ? f32(value + (value < 0 ? deadzone : -deadzone)) / f32(INT16_MAX - deadzone)
- : 0.0f
- ;
- queue.push_joypad_event(i
- , num > 2 ? 1 : 0
- , values[0]
- , -values[1]
- , values[2]
- );
- break;
- }
- case JS_EVENT_BUTTON:
- {
- if (ev.number < CE_COUNTOF(s_button))
- {
- queue.push_joypad_event(i
- , s_button[ev.number]
- , val == 1
- );
- }
- break;
- }
- }
- }
- }
- }
- int _fd[CROWN_MAX_JOYPADS];
- bool _connected[CROWN_MAX_JOYPADS];
- struct AxisData
- {
- f32 left[3];
- f32 right[3];
- };
- AxisData _axis[CROWN_MAX_JOYPADS];
- };
- static bool s_exit = false;
- struct MainThreadArgs
- {
- DeviceOptions* opts;
- };
- s32 func(void* data)
- {
- MainThreadArgs* args = (MainThreadArgs*)data;
- crown::init(*args->opts);
- crown::update();
- crown::shutdown();
- s_exit = true;
- return EXIT_SUCCESS;
- }
- struct LinuxDevice
- {
- LinuxDevice()
- : _x11_display(NULL)
- , _screen_config(NULL)
- , _x11_detectable_autorepeat(false)
- {
- }
- int run(DeviceOptions* opts)
- {
- // http://tronche.com/gui/x/xlib/display/XInitThreads.html
- Status xs = XInitThreads();
- CE_ASSERT(xs != 0, "XInitThreads: error");
- CE_UNUSED(xs);
- _x11_display = XOpenDisplay(NULL);
- CE_ASSERT(_x11_display != NULL, "XOpenDisplay: error");
- ::Window root_window = RootWindow(_x11_display, DefaultScreen(_x11_display));
- // Do we have detectable autorepeat?
- Bool detectable;
- _x11_detectable_autorepeat = (bool)XkbSetDetectableAutoRepeat(_x11_display, true, &detectable);
- _wm_delete_message = XInternAtom(_x11_display, "WM_DELETE_WINDOW", False);
- // Save screen configuration
- _screen_config = XRRGetScreenInfo(_x11_display, root_window);
- Rotation rr_old_rot;
- const SizeID rr_old_sizeid = XRRConfigCurrentConfiguration(_screen_config, &rr_old_rot);
- // Start main thread
- MainThreadArgs mta;
- mta.opts = opts;
- Thread main_thread;
- main_thread.start(func, &mta);
- _joypad.init();
- while (!s_exit)
- {
- pump_events();
- }
- _joypad.shutdown();
- main_thread.stop();
- // Restore previous screen configuration
- Rotation rr_rot;
- const SizeID rr_sizeid = XRRConfigCurrentConfiguration(_screen_config, &rr_rot);
- if (rr_rot != rr_old_rot || rr_sizeid != rr_old_sizeid)
- {
- XRRSetScreenConfig(_x11_display
- , _screen_config
- , root_window
- , rr_old_sizeid
- , rr_old_rot
- , CurrentTime
- );
- }
- XRRFreeScreenConfigInfo(_screen_config);
- XCloseDisplay(_x11_display);
- return EXIT_SUCCESS;
- }
- void pump_events()
- {
- _joypad.update(_queue);
- while (XPending(_x11_display))
- {
- XEvent event;
- XNextEvent(_x11_display, &event);
- switch (event.type)
- {
- case EnterNotify:
- {
- _queue.push_mouse_event(event.xcrossing.x, event.xcrossing.y);
- break;
- }
- case ClientMessage:
- {
- if ((Atom)event.xclient.data.l[0] == _wm_delete_message)
- {
- _queue.push_exit_event(0);
- }
- break;
- }
- case ConfigureNotify:
- {
- _queue.push_metrics_event(event.xconfigure.x
- , event.xconfigure.y
- , event.xconfigure.width
- , event.xconfigure.height
- );
- break;
- }
- case ButtonPress:
- case ButtonRelease:
- {
- if (event.xbutton.button == Button4 || event.xbutton.button == Button5)
- {
- _queue.push_mouse_event(event.xbutton.x
- , event.xbutton.y
- , event.xbutton.button == Button4 ? 1.0f : -1.0f
- );
- break;
- }
- MouseButton::Enum mb;
- switch (event.xbutton.button)
- {
- case Button1: mb = MouseButton::LEFT; break;
- case Button2: mb = MouseButton::MIDDLE; break;
- case Button3: mb = MouseButton::RIGHT; break;
- default: mb = MouseButton::COUNT; break;
- }
- if (mb != MouseButton::COUNT)
- {
- _queue.push_mouse_event(event.xbutton.x
- , event.xbutton.y
- , mb
- , event.type == ButtonPress
- );
- }
- break;
- }
- case MotionNotify:
- {
- _queue.push_mouse_event(event.xmotion.x, event.xmotion.y);
- break;
- }
- case KeyPress:
- case KeyRelease:
- {
- KeySym keysym = XLookupKeysym(&event.xkey, 0);
- KeyboardButton::Enum kb = x11_translate_key(keysym);
- if (kb != KeyboardButton::COUNT)
- _queue.push_keyboard_event(kb, event.type == KeyPress);
- break;
- }
- case KeymapNotify:
- {
- XRefreshKeyboardMapping(&event.xmapping);
- break;
- }
- default:
- {
- break;
- }
- }
- }
- }
- public:
- ::Display* _x11_display;
- Atom _wm_delete_message;
- XRRScreenConfiguration* _screen_config;
- bool _x11_detectable_autorepeat;
- OsEventQueue _queue;
- Joypad _joypad;
- };
- static LinuxDevice s_ldvc;
- class WindowX11 : public Window
- {
- ::Display* _x11_display;
- ::Window _x11_window;
- Cursor _x11_hidden_cursor;
- Atom _wm_delete_message;
- public:
- WindowX11()
- : _x11_display(NULL)
- , _x11_window(None)
- , _x11_hidden_cursor(None)
- {
- _x11_display = s_ldvc._x11_display;
- }
- void open(u16 x, u16 y, u16 width, u16 height, u32 parent)
- {
- int screen = DefaultScreen(_x11_display);
- int depth = DefaultDepth(_x11_display, screen);
- Visual* visual = DefaultVisual(_x11_display, screen);
- ::Window root_window = RootWindow(_x11_display, screen);
- ::Window parent_window = (parent == 0) ? root_window : (::Window)parent;
- // Create main window
- XSetWindowAttributes win_attribs;
- win_attribs.background_pixmap = 0;
- win_attribs.border_pixel = 0;
- win_attribs.event_mask = FocusChangeMask
- | StructureNotifyMask
- ;
- if (!parent)
- {
- win_attribs.event_mask |= KeyPressMask
- | KeyReleaseMask
- | ButtonPressMask
- | ButtonReleaseMask
- | PointerMotionMask
- | EnterWindowMask
- ;
- }
- _x11_window = XCreateWindow(_x11_display
- , parent_window
- , x
- , y
- , width
- , height
- , 0
- , depth
- , InputOutput
- , visual
- , CWBorderPixel | CWEventMask
- , &win_attribs
- );
- CE_ASSERT(_x11_window != None, "XCreateWindow: error");
- // Build hidden cursor
- Pixmap bm_no;
- XColor black, dummy;
- Colormap colormap;
- static char no_data[] = { 0, 0, 0, 0, 0, 0, 0, 0 };
- colormap = XDefaultColormap(_x11_display, screen);
- XAllocNamedColor(_x11_display, colormap, "black", &black, &dummy);
- bm_no = XCreateBitmapFromData(_x11_display, _x11_window, no_data, 8, 8);
- _x11_hidden_cursor = XCreatePixmapCursor(_x11_display, bm_no, bm_no, &black, &black, 0, 0);
- _wm_delete_message = XInternAtom(_x11_display, "WM_DELETE_WINDOW", False);
- XSetWMProtocols(_x11_display, _x11_window, &_wm_delete_message, 1);
- XMapRaised(_x11_display, _x11_window);
- }
- void close()
- {
- XDestroyWindow(_x11_display, _x11_window);
- }
- void bgfx_setup()
- {
- bgfx::x11SetDisplayWindow(_x11_display, _x11_window);
- }
- void show()
- {
- XMapRaised(_x11_display, _x11_window);
- }
- void hide()
- {
- XUnmapWindow(_x11_display, _x11_window);
- }
- void resize(u16 width, u16 height)
- {
- XResizeWindow(_x11_display, _x11_window, width, height);
- }
- void move(u16 x, u16 y)
- {
- XMoveWindow(_x11_display, _x11_window, x, y);
- }
- void minimize()
- {
- }
- void restore()
- {
- }
- const char* title()
- {
- static char buf[512];
- memset(buf, 0, sizeof(buf));
- char* name;
- XFetchName(_x11_display, _x11_window, &name);
- strncpy(buf, name, sizeof(buf));
- XFree(name);
- return buf;
- }
- void set_title (const char* title)
- {
- XStoreName(_x11_display, _x11_window, title);
- }
- void* handle()
- {
- return (void*)(uintptr_t)_x11_window;
- }
- void show_cursor(bool show)
- {
- XDefineCursor(_x11_display
- , _x11_window
- , show ? None : _x11_hidden_cursor
- );
- }
- };
- Window* Window::create(Allocator& a)
- {
- return CE_NEW(a, WindowX11)();
- }
- void Window::destroy(Allocator& a, Window& w)
- {
- CE_DELETE(a, &w);
- }
- class DisplayXRandr : public Display
- {
- ::Display* _x11_display;
- XRRScreenConfiguration* _screen_config;
- public:
- DisplayXRandr()
- : _x11_display(NULL)
- , _screen_config(NULL)
- {
- _x11_display = s_ldvc._x11_display;
- _screen_config = s_ldvc._screen_config;
- }
- void modes(Array<DisplayMode>& modes)
- {
- int num = 0;
- XRRScreenSize* sizes = XRRConfigSizes(_screen_config, &num);
- if (!sizes)
- return;
- for (int i = 0; i < num; ++i)
- {
- DisplayMode dm;
- dm.id = (u32)i;
- dm.width = sizes[i].width;
- dm.height = sizes[i].height;
- array::push_back(modes, dm);
- }
- }
- void set_mode(u32 id)
- {
- int num = 0;
- XRRScreenSize* sizes = XRRConfigSizes(_screen_config, &num);
- if (!sizes || (int)id >= num)
- return;
- XRRSetScreenConfig(_x11_display
- , _screen_config
- , RootWindow(_x11_display, DefaultScreen(_x11_display))
- , (int)id
- , RR_Rotate_0
- , CurrentTime
- );
- }
- // void set_fullscreen(bool full)
- // {
- // XEvent e;
- // e.xclient.type = ClientMessage;
- // e.xclient.window = m_x11_window;
- // e.xclient.message_type = XInternAtom(_x11_display, "_NET_WM_STATE", False );
- // e.xclient.format = 32;
- // e.xclient.data.l[0] = full ? 1 : 0;
- // e.xclient.data.l[1] = XInternAtom(_x11_display, "_NET_WM_STATE_FULLSCREEN", False);
- // XSendEvent(_x11_display, DefaultRootWindow(_x11_display), False, SubstructureNotifyMask, &e);
- // }
- };
- Display* Display::create(Allocator& a)
- {
- return CE_NEW(a, DisplayXRandr)();
- }
- void Display::destroy(Allocator& a, Display& d)
- {
- CE_DELETE(a, &d);
- }
- bool next_event(OsEvent& ev)
- {
- return s_ldvc._queue.pop_event(ev);
- }
- } // namespace crown
- int main(int argc, char** argv)
- {
- using namespace crown;
- memory_globals::init();
- DeviceOptions opts(argc, argv);
- int exitcode = opts.parse();
- if (exitcode == EXIT_FAILURE)
- {
- return exitcode;
- }
- console_server_globals::init(opts.console_port(), opts.wait_console());
- bool do_continue = true;
- if (opts.do_compile())
- {
- bundle_compiler_globals::init(opts.source_dir(), opts.bundle_dir());
- do_continue = bundle_compiler::main(opts.do_compile(), opts.do_continue(), opts.platform());
- }
- if (do_continue)
- exitcode = crown::s_ldvc.run(&opts);
- if (opts.do_compile())
- bundle_compiler_globals::shutdown();
- console_server_globals::shutdown();
- memory_globals::shutdown();
- return exitcode;
- }
- #endif // CROWN_PLATFORM_LINUX
|