entry_linux.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. /*
  2. * Copyright 2011-2013 Branimir Karadzic. All rights reserved.
  3. * License: http://www.opensource.org/licenses/BSD-2-Clause
  4. */
  5. #include <bx/bx.h>
  6. #if BX_PLATFORM_LINUX
  7. #include "bgfxplatform.h"
  8. #include <string.h>
  9. #include <stdlib.h>
  10. #define XK_MISCELLANY
  11. #define XK_LATIN1
  12. #include <X11/keysymdef.h>
  13. #include <bx/thread.h>
  14. #include <bx/os.h>
  15. #undef None
  16. #include "entry_p.h"
  17. #define DEFAULT_WIDTH 1280
  18. #define DEFAULT_HEIGHT 720
  19. extern int _main_(int _argc, char** _argv);
  20. namespace entry
  21. {
  22. static uint8_t s_translateKey[512];
  23. static void initTranslateKey(uint16_t _xk, Key::Enum _key)
  24. {
  25. _xk += 256;
  26. BX_CHECK(_xk < countof(s_translateKey), "Out of bounds %d.", _xk);
  27. s_translateKey[_xk&0x1ff] = (uint8_t)_key;
  28. }
  29. Key::Enum fromXk(uint16_t _xk)
  30. {
  31. _xk += 256;
  32. return 512 > _xk ? (Key::Enum)s_translateKey[_xk] : Key::None;
  33. }
  34. struct MainThreadEntry
  35. {
  36. int m_argc;
  37. char** m_argv;
  38. static int32_t threadFunc(void* _userData);
  39. };
  40. struct Context
  41. {
  42. Context()
  43. : m_exit(false)
  44. {
  45. memset(s_translateKey, 0, sizeof(s_translateKey) );
  46. initTranslateKey(XK_Escape, Key::Esc);
  47. initTranslateKey(XK_Return, Key::Return);
  48. initTranslateKey(XK_Tab, Key::Tab);
  49. initTranslateKey(XK_BackSpace, Key::Backspace);
  50. initTranslateKey(XK_space, Key::Space);
  51. initTranslateKey(XK_Up, Key::Up);
  52. initTranslateKey(XK_Down, Key::Down);
  53. initTranslateKey(XK_Left, Key::Left);
  54. initTranslateKey(XK_Right, Key::Right);
  55. initTranslateKey(XK_Page_Up, Key::PageUp);
  56. initTranslateKey(XK_Page_Down, Key::PageUp);
  57. initTranslateKey(XK_Home, Key::Home);
  58. initTranslateKey(XK_KP_End, Key::End);
  59. initTranslateKey(XK_Print, Key::Print);
  60. initTranslateKey(XK_equal, Key::Plus);
  61. initTranslateKey(XK_minus, Key::Minus);
  62. initTranslateKey(XK_F1, Key::F1);
  63. initTranslateKey(XK_F2, Key::F2);
  64. initTranslateKey(XK_F3, Key::F3);
  65. initTranslateKey(XK_F4, Key::F4);
  66. initTranslateKey(XK_F5, Key::F5);
  67. initTranslateKey(XK_F6, Key::F6);
  68. initTranslateKey(XK_F7, Key::F7);
  69. initTranslateKey(XK_F8, Key::F8);
  70. initTranslateKey(XK_F9, Key::F9);
  71. initTranslateKey(XK_F10, Key::F10);
  72. initTranslateKey(XK_F11, Key::F11);
  73. initTranslateKey(XK_F12, Key::F12);
  74. initTranslateKey(XK_KP_Insert, Key::NumPad0);
  75. initTranslateKey(XK_KP_End, Key::NumPad1);
  76. initTranslateKey(XK_KP_Down, Key::NumPad2);
  77. initTranslateKey(XK_KP_Page_Down, Key::NumPad3);
  78. initTranslateKey(XK_KP_Left, Key::NumPad4);
  79. initTranslateKey(XK_KP_Begin, Key::NumPad5);
  80. initTranslateKey(XK_KP_Right, Key::NumPad6);
  81. initTranslateKey(XK_KP_Home, Key::NumPad7);
  82. initTranslateKey(XK_KP_Up, Key::NumPad8);
  83. initTranslateKey(XK_KP_Page_Up, Key::NumPad9);
  84. initTranslateKey('0', Key::Key0);
  85. initTranslateKey('1', Key::Key1);
  86. initTranslateKey('2', Key::Key2);
  87. initTranslateKey('3', Key::Key3);
  88. initTranslateKey('4', Key::Key4);
  89. initTranslateKey('5', Key::Key5);
  90. initTranslateKey('6', Key::Key6);
  91. initTranslateKey('7', Key::Key7);
  92. initTranslateKey('8', Key::Key8);
  93. initTranslateKey('9', Key::Key9);
  94. initTranslateKey('a', Key::KeyA);
  95. initTranslateKey('b', Key::KeyB);
  96. initTranslateKey('c', Key::KeyC);
  97. initTranslateKey('d', Key::KeyD);
  98. initTranslateKey('e', Key::KeyE);
  99. initTranslateKey('f', Key::KeyF);
  100. initTranslateKey('g', Key::KeyG);
  101. initTranslateKey('h', Key::KeyH);
  102. initTranslateKey('i', Key::KeyI);
  103. initTranslateKey('j', Key::KeyJ);
  104. initTranslateKey('k', Key::KeyK);
  105. initTranslateKey('l', Key::KeyL);
  106. initTranslateKey('m', Key::KeyM);
  107. initTranslateKey('n', Key::KeyN);
  108. initTranslateKey('o', Key::KeyO);
  109. initTranslateKey('p', Key::KeyP);
  110. initTranslateKey('q', Key::KeyQ);
  111. initTranslateKey('r', Key::KeyR);
  112. initTranslateKey('s', Key::KeyS);
  113. initTranslateKey('t', Key::KeyT);
  114. initTranslateKey('u', Key::KeyU);
  115. initTranslateKey('v', Key::KeyV);
  116. initTranslateKey('w', Key::KeyW);
  117. initTranslateKey('x', Key::KeyX);
  118. initTranslateKey('y', Key::KeyY);
  119. initTranslateKey('z', Key::KeyZ);
  120. }
  121. int32_t run(int _argc, char** _argv)
  122. {
  123. XInitThreads();
  124. m_display = XOpenDisplay(0);
  125. int32_t screen = DefaultScreen(m_display);
  126. int32_t depth = DefaultDepth(m_display, screen);
  127. Visual* visual = DefaultVisual(m_display, screen);
  128. Window root = RootWindow(m_display, screen);
  129. XSetWindowAttributes windowAttrs;
  130. memset(&windowAttrs, 0, sizeof(windowAttrs) );
  131. windowAttrs.background_pixmap = 0;
  132. windowAttrs.border_pixel = 0;
  133. windowAttrs.event_mask = 0
  134. | ButtonPressMask
  135. | ButtonReleaseMask
  136. | ExposureMask
  137. | KeyPressMask
  138. | KeyReleaseMask
  139. | PointerMotionMask
  140. | ResizeRedirectMask
  141. | StructureNotifyMask
  142. ;
  143. m_window = XCreateWindow(m_display
  144. , root
  145. , 0, 0
  146. , DEFAULT_WIDTH, DEFAULT_HEIGHT, 0, depth
  147. , InputOutput
  148. , visual
  149. , CWBorderPixel|CWEventMask
  150. , &windowAttrs
  151. );
  152. XMapWindow(m_display, m_window);
  153. XStoreName(m_display, m_window, "BGFX");
  154. bgfx::x11SetDisplayWindow(m_display, m_window);
  155. MainThreadEntry mte;
  156. mte.m_argc = _argc;
  157. mte.m_argv = _argv;
  158. bx::Thread thread;
  159. thread.init(mte.threadFunc, &mte);
  160. while (!m_exit)
  161. {
  162. if (XPending(m_display) )
  163. {
  164. XEvent event;
  165. XNextEvent(m_display, &event);
  166. switch (event.type)
  167. {
  168. case Expose:
  169. break;
  170. case ConfigureNotify:
  171. break;
  172. case ButtonPress:
  173. case ButtonRelease:
  174. {
  175. const XButtonEvent& button = event.xbutton;
  176. MouseButton::Enum mb;
  177. switch (button.button)
  178. {
  179. case Button1: mb = MouseButton::Left; break;
  180. case Button2: mb = MouseButton::Middle; break;
  181. case Button3: mb = MouseButton::Right; break;
  182. default: mb = MouseButton::None; break;
  183. }
  184. if (MouseButton::None != mb)
  185. {
  186. m_eventQueue.postMouseEvent(button.x
  187. , button.y
  188. , mb
  189. , event.type == ButtonPress
  190. );
  191. }
  192. }
  193. break;
  194. case MotionNotify:
  195. {
  196. const XMotionEvent& motion = event.xmotion;
  197. m_eventQueue.postMouseEvent(motion.x
  198. , motion.y
  199. );
  200. }
  201. break;
  202. case KeyPress:
  203. case KeyRelease:
  204. {
  205. KeySym xk = XLookupKeysym(&event.xkey, 0);
  206. Key::Enum key = fromXk(xk);
  207. if (Key::None != key)
  208. {
  209. m_eventQueue.postKeyEvent(key, 0, KeyPress == event.type);
  210. }
  211. }
  212. break;
  213. case ResizeRequest:
  214. {
  215. const XResizeRequestEvent& resize = event.xresizerequest;
  216. XResizeWindow(m_display, m_window, resize.width, resize.height);
  217. }
  218. break;
  219. }
  220. }
  221. }
  222. thread.shutdown();
  223. XUnmapWindow(m_display, m_window);
  224. XDestroyWindow(m_display, m_window);
  225. return EXIT_SUCCESS;
  226. }
  227. Display* m_display;
  228. Window m_window;
  229. bool m_exit;
  230. EventQueue m_eventQueue;
  231. };
  232. static Context s_ctx;
  233. int32_t MainThreadEntry::threadFunc(void* _userData)
  234. {
  235. MainThreadEntry* self = (MainThreadEntry*)_userData;
  236. int32_t result = _main_(self->m_argc, self->m_argv);
  237. s_ctx.m_exit = true;
  238. return result;
  239. }
  240. const Event* poll()
  241. {
  242. return s_ctx.m_eventQueue.poll();
  243. }
  244. void release(const Event* _event)
  245. {
  246. s_ctx.m_eventQueue.release(_event);
  247. }
  248. void setWindowSize(uint32_t _width, uint32_t _height)
  249. {
  250. XResizeRequestEvent ev;
  251. ev.type = ResizeRequest;
  252. ev.serial = 0;
  253. ev.send_event = true;
  254. ev.display = s_ctx.m_display;
  255. ev.window = s_ctx.m_window;
  256. ev.width = (int)_width;
  257. ev.height = (int)_height;
  258. XSendEvent(s_ctx.m_display, s_ctx.m_window, false, ResizeRedirectMask, (XEvent*)&ev);
  259. }
  260. void toggleWindowFrame()
  261. {
  262. }
  263. void setMouseLock(bool _lock)
  264. {
  265. }
  266. } // namespace entry
  267. int main(int _argc, char** _argv)
  268. {
  269. using namespace entry;
  270. return s_ctx.run(_argc, _argv);
  271. }
  272. #endif // BX_PLATFORM_LINUX