WIP X11 key/mouse support.

This commit is contained in:
bkaradzic 2013-01-17 22:38:13 -08:00
parent 135846717b
commit a2057d65aa
5 changed files with 183 additions and 23 deletions

View File

@ -40,7 +40,7 @@ namespace entry
{
enum Enum
{
Unknown = 0,
None = 0,
Esc,
Return,
Tab,

View File

@ -8,13 +8,18 @@
#if BX_PLATFORM_LINUX
#include "bgfxplatform.h"
#include <string.h>
#include <stdlib.h>
#define XK_MISCELLANY
#define XK_LATIN1
#include <X11/keysymdef.h>
#include <bx/thread.h>
#include <bx/os.h>
#undef None
#include "entry.h"
#include "entry_p.h"
#define DEFAULT_WIDTH 1280
#define DEFAULT_HEIGHT 720
@ -23,6 +28,21 @@ extern int _main_(int _argc, char** _argv);
namespace entry
{
static uint8_t s_translateKey[512];
static void initTranslateKey(uint16_t _xk, Key::Enum _key)
{
_xk += 256;
BX_CHECK(_xk < countof(s_translateKey), "Out of bounds %d.", _xk);
s_translateKey[_xk&0x1ff] = (uint8_t)_key;
}
Key::Enum fromXk(uint16_t _xk)
{
_xk += 256;
return 512 > _xk ? (Key::Enum)s_translateKey[_xk] : Key::None;
}
struct MainThreadEntry
{
int m_argc;
@ -37,8 +57,89 @@ namespace entry
struct Context
{
Context()
: m_exit(false)
{
memset(s_translateKey, 0, sizeof(s_translateKey) );
initTranslateKey(XK_Escape, Key::Esc);
initTranslateKey(XK_Return, Key::Return);
initTranslateKey(XK_Tab, Key::Tab);
initTranslateKey(XK_BackSpace, Key::Backspace);
initTranslateKey(XK_space, Key::Space);
initTranslateKey(XK_Up, Key::Up);
initTranslateKey(XK_Down, Key::Down);
initTranslateKey(XK_Left, Key::Left);
initTranslateKey(XK_Right, Key::Right);
initTranslateKey(XK_Page_Up, Key::PageUp);
initTranslateKey(XK_Page_Down, Key::PageUp);
initTranslateKey(XK_Home, Key::Home);
initTranslateKey(XK_KP_End, Key::End);
initTranslateKey(XK_Print, Key::Print);
initTranslateKey(XK_equal, Key::Plus);
initTranslateKey(XK_minus, Key::Minus);
initTranslateKey(XK_F1, Key::F1);
initTranslateKey(XK_F2, Key::F2);
initTranslateKey(XK_F3, Key::F3);
initTranslateKey(XK_F4, Key::F4);
initTranslateKey(XK_F5, Key::F5);
initTranslateKey(XK_F6, Key::F6);
initTranslateKey(XK_F7, Key::F7);
initTranslateKey(XK_F8, Key::F8);
initTranslateKey(XK_F9, Key::F9);
initTranslateKey(XK_F10, Key::F10);
initTranslateKey(XK_F11, Key::F11);
initTranslateKey(XK_F12, Key::F12);
initTranslateKey(XK_KP_Insert, Key::NumPad0);
initTranslateKey(XK_KP_End, Key::NumPad1);
initTranslateKey(XK_KP_Down, Key::NumPad2);
initTranslateKey(XK_KP_Page_Down, Key::NumPad3);
initTranslateKey(XK_KP_Left, Key::NumPad4);
initTranslateKey(XK_KP_Begin, Key::NumPad5);
initTranslateKey(XK_KP_Right, Key::NumPad6);
initTranslateKey(XK_KP_Home, Key::NumPad7);
initTranslateKey(XK_KP_Up, Key::NumPad8);
initTranslateKey(XK_KP_Page_Up, Key::NumPad9);
initTranslateKey('0', Key::Key0);
initTranslateKey('1', Key::Key1);
initTranslateKey('2', Key::Key2);
initTranslateKey('3', Key::Key3);
initTranslateKey('4', Key::Key4);
initTranslateKey('5', Key::Key5);
initTranslateKey('6', Key::Key6);
initTranslateKey('7', Key::Key7);
initTranslateKey('8', Key::Key8);
initTranslateKey('9', Key::Key9);
initTranslateKey('a', Key::KeyA);
initTranslateKey('b', Key::KeyB);
initTranslateKey('c', Key::KeyC);
initTranslateKey('d', Key::KeyD);
initTranslateKey('e', Key::KeyE);
initTranslateKey('f', Key::KeyF);
initTranslateKey('g', Key::KeyG);
initTranslateKey('h', Key::KeyH);
initTranslateKey('i', Key::KeyI);
initTranslateKey('j', Key::KeyJ);
initTranslateKey('k', Key::KeyK);
initTranslateKey('l', Key::KeyL);
initTranslateKey('m', Key::KeyM);
initTranslateKey('n', Key::KeyN);
initTranslateKey('o', Key::KeyO);
initTranslateKey('p', Key::KeyP);
initTranslateKey('q', Key::KeyQ);
initTranslateKey('r', Key::KeyR);
initTranslateKey('s', Key::KeyS);
initTranslateKey('t', Key::KeyT);
initTranslateKey('u', Key::KeyU);
initTranslateKey('v', Key::KeyV);
initTranslateKey('w', Key::KeyW);
initTranslateKey('x', Key::KeyX);
initTranslateKey('y', Key::KeyY);
initTranslateKey('z', Key::KeyZ);
}
int32_t run(int _argc, char** _argv)
{
XInitThreads();
m_display = XOpenDisplay(0);
XLockDisplay(m_display);
@ -71,6 +172,19 @@ namespace entry
XMapRaised(m_display, m_window);
XFlush(m_display);
XStoreName(m_display, m_window, "BGFX");
XSelectInput(m_display
, m_window
, ExposureMask
| KeyPressMask
| KeyReleaseMask
| PointerMotionMask
| ButtonPressMask
| ButtonReleaseMask
| StructureNotifyMask
);
XUnlockDisplay(m_display);
// XResizeWindow(s_display, s_window, _width, _height);
@ -84,6 +198,45 @@ namespace entry
bx::Thread thread;
thread.init(mte.threadFunc, &mte);
while (!m_exit)
{
if (XPending(m_display) )
{
XEvent event;
XNextEvent(m_display, &event);
switch (event.type)
{
case Expose:
break;
case ConfigureNotify:
break;
case ButtonPress:
break;
case ButtonRelease:
break;
case MotionNotify:
break;
case KeyPress:
case KeyRelease:
{
KeySym xk = XLookupKeysym(&event.xkey, 0);
Key::Enum key = fromXk(xk);
if (Key::None != key)
{
m_eventQueue.postKeyEvent(key, 0, KeyPress == event.type);
}
}
break;
}
}
}
thread.shutdown();
return EXIT_SUCCESS;
@ -91,25 +244,29 @@ namespace entry
Display* m_display;
Window m_window;
bool m_exit;
EventQueue m_eventQueue;
};
static Context s_ctx;
const Event* poll()
{
return NULL;
return s_ctx.m_eventQueue.poll();
}
void release(const Event* _event)
{
s_ctx.m_eventQueue.release(_event);
}
void setWindowSize(uint32_t _width, uint32_t _height)
{
void setWindowSize(uint32_t _width, uint32_t _height)
{
}
void toggleWindowFrame()
{
{
}
} // namespace entry

View File

@ -1,19 +1,19 @@
/*
* Copyright 2011-2013 Branimir Karadzic. All rights reserved.
* License: http://www.opensource.org/licenses/BSD-2-Clause
*/
#ifndef __ENTRY_PRIVATE_H__
#define __ENTRY_PRIVATE_H__
/*
* Copyright 2011-2013 Branimir Karadzic. All rights reserved.
* License: http://www.opensource.org/licenses/BSD-2-Clause
*/
#ifndef __ENTRY_PRIVATE_H__
#define __ENTRY_PRIVATE_H__
#include <bgfxplatform.h>
#include <bx/spscqueue.h>
#include "dbg.h"
#include "entry.h"
namespace entry
{
namespace entry
{
class EventQueue
{
public:
@ -67,7 +67,7 @@ namespace entry
private:
bx::SpScUnboundedQueue<Event> m_queue;
};
} // namespace entry
#endif // __ENTRY_PRIVATE_H__
} // namespace entry
#endif // __ENTRY_PRIVATE_H__

View File

@ -138,6 +138,7 @@ namespace entry
s_translateKey['K'] = Key::KeyK;
s_translateKey['L'] = Key::KeyL;
s_translateKey['M'] = Key::KeyM;
s_translateKey['N'] = Key::KeyN;
s_translateKey['O'] = Key::KeyO;
s_translateKey['P'] = Key::KeyP;
s_translateKey['Q'] = Key::KeyQ;

View File

@ -133,12 +133,11 @@ namespace bgfx
}
}
glXMakeCurrent(s_display, s_window, m_context);
XUnlockDisplay(s_display);
import();
glXMakeCurrent(s_display, s_window, m_context);
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
glXSwapBuffers(s_display, s_window);
@ -146,6 +145,8 @@ namespace bgfx
void GlContext::destroy()
{
glXMakeCurrent(s_display, 0, 0);
glXDestroyContext(s_display, m_context);
}
void GlContext::resize(uint32_t _width, uint32_t _height)
@ -154,6 +155,7 @@ namespace bgfx
void GlContext::swap()
{
glXMakeCurrent(s_display, s_window, m_context);
glXSwapBuffers(s_display, s_window);
}