entry_linux.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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. MainThreadEntry* self = (MainThreadEntry*)_userData;
  41. return _main_(self->m_argc, self->m_argv);
  42. }
  43. };
  44. struct Context
  45. {
  46. Context()
  47. : m_exit(false)
  48. {
  49. memset(s_translateKey, 0, sizeof(s_translateKey) );
  50. initTranslateKey(XK_Escape, Key::Esc);
  51. initTranslateKey(XK_Return, Key::Return);
  52. initTranslateKey(XK_Tab, Key::Tab);
  53. initTranslateKey(XK_BackSpace, Key::Backspace);
  54. initTranslateKey(XK_space, Key::Space);
  55. initTranslateKey(XK_Up, Key::Up);
  56. initTranslateKey(XK_Down, Key::Down);
  57. initTranslateKey(XK_Left, Key::Left);
  58. initTranslateKey(XK_Right, Key::Right);
  59. initTranslateKey(XK_Page_Up, Key::PageUp);
  60. initTranslateKey(XK_Page_Down, Key::PageUp);
  61. initTranslateKey(XK_Home, Key::Home);
  62. initTranslateKey(XK_KP_End, Key::End);
  63. initTranslateKey(XK_Print, Key::Print);
  64. initTranslateKey(XK_equal, Key::Plus);
  65. initTranslateKey(XK_minus, Key::Minus);
  66. initTranslateKey(XK_F1, Key::F1);
  67. initTranslateKey(XK_F2, Key::F2);
  68. initTranslateKey(XK_F3, Key::F3);
  69. initTranslateKey(XK_F4, Key::F4);
  70. initTranslateKey(XK_F5, Key::F5);
  71. initTranslateKey(XK_F6, Key::F6);
  72. initTranslateKey(XK_F7, Key::F7);
  73. initTranslateKey(XK_F8, Key::F8);
  74. initTranslateKey(XK_F9, Key::F9);
  75. initTranslateKey(XK_F10, Key::F10);
  76. initTranslateKey(XK_F11, Key::F11);
  77. initTranslateKey(XK_F12, Key::F12);
  78. initTranslateKey(XK_KP_Insert, Key::NumPad0);
  79. initTranslateKey(XK_KP_End, Key::NumPad1);
  80. initTranslateKey(XK_KP_Down, Key::NumPad2);
  81. initTranslateKey(XK_KP_Page_Down, Key::NumPad3);
  82. initTranslateKey(XK_KP_Left, Key::NumPad4);
  83. initTranslateKey(XK_KP_Begin, Key::NumPad5);
  84. initTranslateKey(XK_KP_Right, Key::NumPad6);
  85. initTranslateKey(XK_KP_Home, Key::NumPad7);
  86. initTranslateKey(XK_KP_Up, Key::NumPad8);
  87. initTranslateKey(XK_KP_Page_Up, Key::NumPad9);
  88. initTranslateKey('0', Key::Key0);
  89. initTranslateKey('1', Key::Key1);
  90. initTranslateKey('2', Key::Key2);
  91. initTranslateKey('3', Key::Key3);
  92. initTranslateKey('4', Key::Key4);
  93. initTranslateKey('5', Key::Key5);
  94. initTranslateKey('6', Key::Key6);
  95. initTranslateKey('7', Key::Key7);
  96. initTranslateKey('8', Key::Key8);
  97. initTranslateKey('9', Key::Key9);
  98. initTranslateKey('a', Key::KeyA);
  99. initTranslateKey('b', Key::KeyB);
  100. initTranslateKey('c', Key::KeyC);
  101. initTranslateKey('d', Key::KeyD);
  102. initTranslateKey('e', Key::KeyE);
  103. initTranslateKey('f', Key::KeyF);
  104. initTranslateKey('g', Key::KeyG);
  105. initTranslateKey('h', Key::KeyH);
  106. initTranslateKey('i', Key::KeyI);
  107. initTranslateKey('j', Key::KeyJ);
  108. initTranslateKey('k', Key::KeyK);
  109. initTranslateKey('l', Key::KeyL);
  110. initTranslateKey('m', Key::KeyM);
  111. initTranslateKey('n', Key::KeyN);
  112. initTranslateKey('o', Key::KeyO);
  113. initTranslateKey('p', Key::KeyP);
  114. initTranslateKey('q', Key::KeyQ);
  115. initTranslateKey('r', Key::KeyR);
  116. initTranslateKey('s', Key::KeyS);
  117. initTranslateKey('t', Key::KeyT);
  118. initTranslateKey('u', Key::KeyU);
  119. initTranslateKey('v', Key::KeyV);
  120. initTranslateKey('w', Key::KeyW);
  121. initTranslateKey('x', Key::KeyX);
  122. initTranslateKey('y', Key::KeyY);
  123. initTranslateKey('z', Key::KeyZ);
  124. }
  125. int32_t run(int _argc, char** _argv)
  126. {
  127. XInitThreads();
  128. m_display = XOpenDisplay(0);
  129. XLockDisplay(m_display);
  130. int32_t screen = DefaultScreen(m_display);
  131. int32_t depth = DefaultDepth(m_display, screen);
  132. Visual* visual = DefaultVisual(m_display, screen);
  133. Window root = RootWindow(m_display, screen);
  134. XSetWindowAttributes windowAttrs;
  135. windowAttrs.colormap =
  136. XCreateColormap(m_display
  137. , root
  138. , visual
  139. , AllocNone
  140. );
  141. windowAttrs.background_pixmap = 0;
  142. windowAttrs.border_pixel = 0;
  143. m_window = XCreateWindow(m_display
  144. , root
  145. , 0, 0
  146. , DEFAULT_WIDTH, DEFAULT_HEIGHT, 0, depth
  147. , InputOutput
  148. , visual
  149. , CWBorderPixel|CWColormap
  150. , &windowAttrs
  151. );
  152. XMapRaised(m_display, m_window);
  153. XFlush(m_display);
  154. XStoreName(m_display, m_window, "BGFX");
  155. XSelectInput(m_display
  156. , m_window
  157. , ExposureMask
  158. | KeyPressMask
  159. | KeyReleaseMask
  160. | PointerMotionMask
  161. | ButtonPressMask
  162. | ButtonReleaseMask
  163. | StructureNotifyMask
  164. );
  165. XUnlockDisplay(m_display);
  166. // XResizeWindow(s_display, s_window, _width, _height);
  167. bgfx::x11SetDisplayWindow(m_display, m_window);
  168. MainThreadEntry mte;
  169. mte.m_argc = _argc;
  170. mte.m_argv = _argv;
  171. bx::Thread thread;
  172. thread.init(mte.threadFunc, &mte);
  173. while (!m_exit)
  174. {
  175. if (XPending(m_display) )
  176. {
  177. XEvent event;
  178. XNextEvent(m_display, &event);
  179. switch (event.type)
  180. {
  181. case Expose:
  182. break;
  183. case ConfigureNotify:
  184. break;
  185. case ButtonPress:
  186. break;
  187. case ButtonRelease:
  188. break;
  189. case MotionNotify:
  190. break;
  191. case KeyPress:
  192. case KeyRelease:
  193. {
  194. KeySym xk = XLookupKeysym(&event.xkey, 0);
  195. Key::Enum key = fromXk(xk);
  196. if (Key::None != key)
  197. {
  198. m_eventQueue.postKeyEvent(key, 0, KeyPress == event.type);
  199. }
  200. }
  201. break;
  202. }
  203. }
  204. }
  205. thread.shutdown();
  206. return EXIT_SUCCESS;
  207. }
  208. Display* m_display;
  209. Window m_window;
  210. bool m_exit;
  211. EventQueue m_eventQueue;
  212. };
  213. static Context s_ctx;
  214. const Event* poll()
  215. {
  216. return s_ctx.m_eventQueue.poll();
  217. }
  218. void release(const Event* _event)
  219. {
  220. s_ctx.m_eventQueue.release(_event);
  221. }
  222. void setWindowSize(uint32_t _width, uint32_t _height)
  223. {
  224. }
  225. void toggleWindowFrame()
  226. {
  227. }
  228. } // namespace entry
  229. int main(int _argc, char** _argv)
  230. {
  231. using namespace entry;
  232. return s_ctx.run(_argc, _argv);
  233. }
  234. #endif // BX_PLATFORM_LINUX