WIP key/mouse support.

This commit is contained in:
bkaradzic 2013-01-18 21:12:04 -08:00
parent e764548220
commit 165e98c60b
3 changed files with 155 additions and 116 deletions

View file

@ -48,11 +48,7 @@ namespace entry
int m_argc;
char** m_argv;
static int32_t threadFunc(void* _userData)
{
MainThreadEntry* self = (MainThreadEntry*)_userData;
return _main_(self->m_argc, self->m_argv);
}
static int32_t threadFunc(void* _userData);
};
struct Context
@ -142,22 +138,25 @@ namespace entry
XInitThreads();
m_display = XOpenDisplay(0);
XLockDisplay(m_display);
int32_t screen = DefaultScreen(m_display);
int32_t depth = DefaultDepth(m_display, screen);
Visual* visual = DefaultVisual(m_display, screen);
Window root = RootWindow(m_display, screen);
XSetWindowAttributes windowAttrs;
windowAttrs.colormap =
XCreateColormap(m_display
, root
, visual
, AllocNone
);
memset(&windowAttrs, 0, sizeof(windowAttrs) );
windowAttrs.background_pixmap = 0;
windowAttrs.border_pixel = 0;
windowAttrs.event_mask = 0
| ButtonPressMask
| ButtonReleaseMask
| ExposureMask
| KeyPressMask
| KeyReleaseMask
| PointerMotionMask
| ResizeRedirectMask
| StructureNotifyMask
;
m_window = XCreateWindow(m_display
, root
@ -165,30 +164,13 @@ namespace entry
, DEFAULT_WIDTH, DEFAULT_HEIGHT, 0, depth
, InputOutput
, visual
, CWBorderPixel|CWColormap
, CWBorderPixel|CWEventMask
, &windowAttrs
);
XMapRaised(m_display, m_window);
XFlush(m_display);
XMapWindow(m_display, m_window);
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);
bgfx::x11SetDisplayWindow(m_display, m_window);
MainThreadEntry mte;
@ -214,12 +196,36 @@ namespace entry
break;
case ButtonPress:
break;
case ButtonRelease:
{
const XButtonEvent& button = event.xbutton;
MouseButton::Enum mb;
switch (button.button)
{
case Button1: mb = MouseButton::Left; break;
case Button2: mb = MouseButton::Middle; break;
case Button3: mb = MouseButton::Right; break;
default: mb = MouseButton::None; break;
}
if (MouseButton::None != mb)
{
m_eventQueue.postMouseEvent(button.x
, button.y
, mb
, event.type == ButtonPress
);
}
}
break;
case MotionNotify:
{
const XMotionEvent& motion = event.xmotion;
m_eventQueue.postMouseEvent(motion.x
, motion.y
);
}
break;
case KeyPress:
@ -233,12 +239,22 @@ namespace entry
}
}
break;
case ResizeRequest:
{
const XResizeRequestEvent& resize = event.xresizerequest;
XResizeWindow(m_display, m_window, resize.width, resize.height);
}
break;
}
}
}
thread.shutdown();
XUnmapWindow(m_display, m_window);
XDestroyWindow(m_display, m_window);
return EXIT_SUCCESS;
}
@ -251,6 +267,14 @@ namespace entry
static Context s_ctx;
int32_t MainThreadEntry::threadFunc(void* _userData)
{
MainThreadEntry* self = (MainThreadEntry*)_userData;
int32_t result = _main_(self->m_argc, self->m_argv);
s_ctx.m_exit = true;
return result;
}
const Event* poll()
{
return s_ctx.m_eventQueue.poll();
@ -263,6 +287,15 @@ namespace entry
void setWindowSize(uint32_t _width, uint32_t _height)
{
XResizeRequestEvent ev;
ev.type = ResizeRequest;
ev.serial = 0;
ev.send_event = true;
ev.display = s_ctx.m_display;
ev.window = s_ctx.m_window;
ev.width = (int)_width;
ev.height = (int)_height;
XSendEvent(s_ctx.m_display, s_ctx.m_window, false, ResizeRedirectMask, (XEvent*)&ev);
}
void toggleWindowFrame()

View file

@ -358,7 +358,7 @@ namespace entry
{
int32_t mx = GET_X_LPARAM(_lparam);
int32_t my = GET_Y_LPARAM(_lparam);
m_eventQueue.postMouseEvent(mx, my, MouseButton::Middle, _id == WM_LBUTTONDOWN);
m_eventQueue.postMouseEvent(mx, my, MouseButton::Left, _id == WM_LBUTTONDOWN);
}
break;
@ -378,7 +378,7 @@ namespace entry
{
int32_t mx = GET_X_LPARAM(_lparam);
int32_t my = GET_Y_LPARAM(_lparam);
m_eventQueue.postMouseEvent(mx, my, MouseButton::Middle, _id == WM_RBUTTONDOWN);
m_eventQueue.postMouseEvent(mx, my, MouseButton::Right, _id == WM_RBUTTONDOWN);
}
break;

View file

@ -1,80 +1,86 @@
/*
* Copyright 2011-2013 Branimir Karadzic. All rights reserved.
* License: http://www.opensource.org/licenses/BSD-2-Clause
*/
#ifndef __PROCESS_EVENTS_H__
#define __PROCESS_EVENTS_H__
inline bool processEvents(uint32_t& _width, uint32_t& _height, uint32_t& _debug)
{
using namespace entry;
const Event* ev;
do
{
struct SE { const Event* m_ev; SE() : m_ev(poll() ) {} ~SE() { if (NULL != m_ev) { release(m_ev); } } } scopeEvent;
ev = scopeEvent.m_ev;
if (NULL != ev)
{
switch (ev->m_type)
{
case Event::Exit:
return true;
case Event::Mouse:
{
// const MouseEvent* mouse = static_cast<const MouseEvent*>(ev);
// if (mouse->m_move)
// {
// }
}
break;
case Event::Key:
{
const KeyEvent* key = static_cast<const KeyEvent*>(ev);
if ( (key->m_key == Key::KeyQ && (key->m_modifiers & (Modifier::LeftCtrl|Modifier::RightCtrl) ) )
|| ( (key->m_key == Key::Esc) ) )
{
return true;
}
else if (key->m_key == Key::F1 && key->m_down)
{
_debug ^= BGFX_DEBUG_STATS;
bgfx::setDebug(_debug);
return false;
}
else if (key->m_key == Key::F10 && key->m_down)
{
setWindowSize(1280, 720);
_width = 1280;
_height = 720;
}
else if (key->m_key == Key::F11 && key->m_down)
{
toggleWindowFrame();
}
}
break;
case Event::Size:
{
const SizeEvent* size = static_cast<const SizeEvent*>(ev);
bgfx::reset(size->m_width, size->m_height);
_width = size->m_width;
_height = size->m_height;
}
break;
default:
break;
}
}
} while (NULL != ev);
return false;
}
#endif // __PROCESS_EVENTS_H__
/*
* Copyright 2011-2013 Branimir Karadzic. All rights reserved.
* License: http://www.opensource.org/licenses/BSD-2-Clause
*/
#ifndef __PROCESS_EVENTS_H__
#define __PROCESS_EVENTS_H__
inline bool processEvents(uint32_t& _width, uint32_t& _height, uint32_t& _debug)
{
using namespace entry;
const Event* ev;
do
{
struct SE { const Event* m_ev; SE() : m_ev(poll() ) {} ~SE() { if (NULL != m_ev) { release(m_ev); } } } scopeEvent;
ev = scopeEvent.m_ev;
if (NULL != ev)
{
switch (ev->m_type)
{
case Event::Exit:
return true;
case Event::Mouse:
{
// const MouseEvent* mouse = static_cast<const MouseEvent*>(ev);
// if (mouse->m_move)
// {
// }
}
break;
case Event::Key:
{
const KeyEvent* key = static_cast<const KeyEvent*>(ev);
if ( (key->m_key == Key::KeyQ && (key->m_modifiers & (Modifier::LeftCtrl|Modifier::RightCtrl) ) )
|| ( (key->m_key == Key::Esc) ) )
{
return true;
}
else if (key->m_key == Key::F1 && key->m_down)
{
_debug ^= BGFX_DEBUG_STATS;
bgfx::setDebug(_debug);
return false;
}
else if (key->m_key == Key::F9 && key->m_down)
{
setWindowSize(640, 480);
_width = 640;
_height = 480;
}
else if (key->m_key == Key::F10 && key->m_down)
{
setWindowSize(1280, 720);
_width = 1280;
_height = 720;
}
else if (key->m_key == Key::F11 && key->m_down)
{
toggleWindowFrame();
}
}
break;
case Event::Size:
{
const SizeEvent* size = static_cast<const SizeEvent*>(ev);
bgfx::reset(size->m_width, size->m_height);
_width = size->m_width;
_height = size->m_height;
}
break;
default:
break;
}
}
} while (NULL != ev);
return false;
}
#endif // __PROCESS_EVENTS_H__