123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. #include "x.hpp"
  2. is::XEngine* xengine = new is::XEngine();
  3. is::XEngine::XEngine() {
  4. m_display = NULL;
  5. m_visual = NULL;
  6. m_screen = NULL;
  7. m_good = false;
  8. m_mousex = -1;
  9. m_mousey = -1;
  10. m_hoverXWindow = None;
  11. }
  12. is::XEngine::~XEngine() {
  13. if ( !m_good ) {
  14. return;
  15. }
  16. for ( unsigned int i=0; i<m_cursors.size(); i++ ) {
  17. if ( m_cursors.at( i ) ) {
  18. XFreeCursor( m_display, m_cursors[i] );
  19. }
  20. }
  21. for ( unsigned int i=0; i<m_rects.size(); i++ ) {
  22. delete m_rects.at( i );
  23. }
  24. XCloseDisplay( m_display );
  25. }
  26. void is::XEngine::addRect( Rectangle* rect ) {
  27. m_rects.push_back( rect );
  28. }
  29. void is::XEngine::removeRect( Rectangle* rect ) {
  30. for ( unsigned int i=0; i<m_rects.size(); i++ ) {
  31. if ( m_rects.at( i ) == rect ) {
  32. m_rects.erase( m_rects.begin() + i );
  33. i--;
  34. delete rect;
  35. return;
  36. }
  37. }
  38. }
  39. bool is::XEngine::mouseDown( unsigned int button ) {
  40. if ( button >= m_mouse.size() ) {
  41. return false;
  42. }
  43. return m_mouse.at( button );
  44. }
  45. int is::XEngine::init( std::string display ) {
  46. // Initialize display
  47. m_display = XOpenDisplay( display.c_str() );
  48. if ( !m_display ) {
  49. printf( "Failed to open X display %s\n", display.c_str() );
  50. return 1;
  51. }
  52. m_screen = ScreenOfDisplay( m_display, DefaultScreen( m_display ) );
  53. m_visual = DefaultVisual ( m_display, XScreenNumberOfScreen( m_screen ) );
  54. m_colormap = DefaultColormap( m_display, XScreenNumberOfScreen( m_screen ) );
  55. m_root = RootWindow ( m_display, XScreenNumberOfScreen( m_screen ) );
  56. m_good = true;
  57. return 0;
  58. }
  59. int is::XEngine::grabCursor( is::CursorType type ) {
  60. if ( !m_good ) {
  61. return 1;
  62. }
  63. int xfontcursor = getCursor( type );
  64. int err = XGrabPointer( m_display, m_root, False,
  65. PointerMotionMask | ButtonPressMask | ButtonReleaseMask,
  66. GrabModeAsync, GrabModeAsync, m_root, xfontcursor, CurrentTime );
  67. if ( err != GrabSuccess ) {
  68. printf( "Failed to grab X cursor\n" );
  69. return 1;
  70. }
  71. // Quickly set the mouse position so we don't have to worry about x11 generating an event.
  72. Window root, child;
  73. int mx, my;
  74. int wx, wy;
  75. unsigned int mask;
  76. XQueryPointer( m_display, m_root, &root, &child, &mx, &my, &wx, &wy, &mask );
  77. m_mousex = mx;
  78. m_mousex = my;
  79. updateHoverWindow( child );
  80. return 0;
  81. }
  82. int is::XEngine::releaseCursor() {
  83. if ( !m_good ) {
  84. return 1;
  85. }
  86. XUngrabPointer( m_display, CurrentTime );
  87. return 0;
  88. }
  89. void is::XEngine::tick() {
  90. if ( !m_good ) {
  91. return;
  92. }
  93. XSync( m_display, false );
  94. XEvent event;
  95. while ( XPending( m_display ) ) {
  96. XNextEvent( m_display, &event );
  97. switch ( event.type ) {
  98. case MotionNotify: {
  99. m_mousex = event.xmotion.x;
  100. m_mousey = event.xmotion.y;
  101. break;
  102. }
  103. case ButtonPress: {
  104. if ( m_mouse.size() > event.xbutton.button ) {
  105. m_mouse.at( event.xbutton.button ) = true;
  106. } else {
  107. m_mouse.resize( event.xbutton.button+2, false );
  108. m_mouse.at( event.xbutton.button ) = true;
  109. }
  110. break;
  111. }
  112. case ButtonRelease: {
  113. if ( m_mouse.size() > event.xbutton.button ) {
  114. m_mouse.at( event.xbutton.button ) = false;
  115. } else {
  116. m_mouse.resize( event.xbutton.button+2, false );
  117. m_mouse.at( event.xbutton.button ) = false;
  118. }
  119. break;
  120. }
  121. default: break;
  122. }
  123. }
  124. // Since I couldn't get Xlib to send a EnterNotify or LeaveNotify events, we need to query the underlying window every frame.
  125. updateHoverWindow();
  126. }
  127. Cursor is::XEngine::getCursor( is::CursorType type ) {
  128. int xfontcursor;
  129. switch ( type ) {
  130. default:
  131. case Left: xfontcursor = XC_left_ptr; break;
  132. case Crosshair: xfontcursor = XC_crosshair; break;
  133. case Cross: xfontcursor = XC_cross; break;
  134. case UpperLeftCorner: xfontcursor = XC_ul_angle; break;
  135. case UpperRightCorner: xfontcursor = XC_ur_angle; break;
  136. case LowerLeftCorner: xfontcursor = XC_ll_angle; break;
  137. case LowerRightCorner: xfontcursor = XC_lr_angle; break;
  138. }
  139. Cursor newcursor = 0;
  140. if ( m_cursors.size() > xfontcursor ) {
  141. newcursor = m_cursors.at( xfontcursor );
  142. }
  143. if ( !newcursor ) {
  144. newcursor = XCreateFontCursor( m_display, xfontcursor );
  145. m_cursors.resize( xfontcursor+2, 0 );
  146. m_cursors.at( xfontcursor ) = newcursor;
  147. }
  148. return newcursor;
  149. }
  150. void is::XEngine::setCursor( is::CursorType type ) {
  151. if ( !m_good ) {
  152. return;
  153. }
  154. Cursor xfontcursor = getCursor( type );
  155. XChangeActivePointerGrab( m_display,
  156. PointerMotionMask | ButtonPressMask | ButtonReleaseMask,
  157. xfontcursor, CurrentTime );
  158. }
  159. is::Rectangle::~Rectangle() {
  160. //XFreeGC( xengine->m_display, m_gc );
  161. XUnmapWindow( xengine->m_display, m_window );
  162. XDestroyWindow( xengine->m_display, m_window );
  163. }
  164. is::Rectangle::Rectangle( int x, int y, int width, int height, int border, int padding ) {
  165. m_xoffset = 0;
  166. m_yoffset = 0;
  167. m_x = x;
  168. m_y = y;
  169. m_width = width;
  170. m_height = height;
  171. m_border = border;
  172. m_padding = padding;
  173. if ( m_width < 0 ) {
  174. m_xoffset += m_width;
  175. m_width = -m_width;
  176. }
  177. if ( m_height < 0 ) {
  178. m_yoffset += m_height;
  179. m_height = -m_height;
  180. }
  181. XAllocNamedColor( xengine->m_display, xengine->m_colormap, "black", &m_forground, &m_forgroundExact );
  182. XAllocNamedColor( xengine->m_display, xengine->m_colormap, "white", &m_background, &m_backgroundExact );
  183. XSetWindowAttributes attributes;
  184. attributes.background_pixmap = None;
  185. attributes.background_pixel = m_forground.pixel;
  186. attributes.save_under = True;
  187. attributes.override_redirect = True;
  188. attributes.colormap = xengine->m_colormap;
  189. unsigned long valueMask = CWBackPixmap | CWBackPixel |
  190. CWSaveUnder | CWOverrideRedirect |
  191. CWColormap;
  192. m_window = XCreateWindow( xengine->m_display, xengine->m_root, m_x-m_border+m_xoffset, m_y-m_border+m_yoffset, m_width+m_border*2, m_height+m_border*2,
  193. 0, CopyFromParent, InputOutput,
  194. CopyFromParent, valueMask, &attributes );
  195. XRectangle rect;
  196. rect.x = rect.y = m_border;
  197. rect.width = m_width;
  198. rect.height = m_height;
  199. XShapeCombineRectangles( xengine->m_display, m_window, ShapeBounding, 0, 0, &rect, 1, ShapeSubtract, 0);
  200. XMapWindow( xengine->m_display, m_window );
  201. draw();
  202. }
  203. void is::Rectangle::setPos( int x, int y ) {
  204. if ( m_x == x && m_y == y ) {
  205. return;
  206. }
  207. m_x = x;
  208. m_y = y;
  209. XMoveWindow( xengine->m_display, m_window, m_x-m_border+m_xoffset, m_y-m_border+m_yoffset );
  210. }
  211. void is::Rectangle::setDim( int w, int h ) {
  212. if ( m_width == w && m_height == h ) {
  213. return;
  214. }
  215. m_xoffset = 0;
  216. m_yoffset = 0;
  217. m_width = w;
  218. m_height = h;
  219. if ( w < 0 ) {
  220. m_xoffset += w;
  221. m_width = -w;
  222. }
  223. if ( h < 0 ) {
  224. m_yoffset += h;
  225. m_height = -h;
  226. }
  227. XResizeWindow( xengine->m_display, m_window, m_width+m_border*2, m_height+m_border*2 );
  228. XMoveWindow( xengine->m_display, m_window, m_x-m_border+m_xoffset, m_y-m_border+m_yoffset );
  229. // Now punch another hole in it.
  230. XRectangle rect;
  231. rect.x = rect.y = 0;
  232. rect.width = m_width+m_border*2;
  233. rect.height = m_height+m_border*2;
  234. XShapeCombineRectangles( xengine->m_display, m_window, ShapeBounding, 0, 0, &rect, 1, ShapeSet, 0);
  235. rect;
  236. rect.x = rect.y = m_border;
  237. rect.width = m_width;
  238. rect.height = m_height;
  239. XShapeCombineRectangles( xengine->m_display, m_window, ShapeBounding, 0, 0, &rect, 1, ShapeSubtract, 0);
  240. draw();
  241. }
  242. void is::Rectangle::draw() {
  243. }
  244. void is::XEngine::updateHoverWindow() {
  245. Window root, child;
  246. int mx, my;
  247. int wx, wy;
  248. unsigned int mask;
  249. XQueryPointer( m_display, m_root, &root, &child, &mx, &my, &wx, &wy, &mask );
  250. if ( m_hoverXWindow == child ) {
  251. return;
  252. }
  253. for ( unsigned int i=0; i<m_rects.size(); i++ ) {
  254. if ( m_rects.at( i )->m_window == child ) {
  255. return;
  256. }
  257. }
  258. m_hoverXWindow = child;
  259. if ( child == None ) {
  260. return;
  261. }
  262. unsigned int depth;
  263. XGetGeometry( m_display, child, &root,
  264. &(m_hoverWindow.m_x), &(m_hoverWindow.m_y),
  265. &(m_hoverWindow.m_width), &(m_hoverWindow.m_height),
  266. &(m_hoverWindow.m_border), &depth );
  267. }
  268. void is::XEngine::updateHoverWindow( Window child ) {
  269. if ( m_hoverXWindow == child ) {
  270. return;
  271. }
  272. for ( unsigned int i=0; i<m_rects.size(); i++ ) {
  273. if ( m_rects.at( i )->m_window == child ) {
  274. return;
  275. }
  276. }
  277. m_hoverXWindow = child;
  278. if ( child == None ) {
  279. return;
  280. }
  281. unsigned int depth;
  282. Window root;
  283. XGetGeometry( m_display, child, &root,
  284. &(m_hoverWindow.m_x), &(m_hoverWindow.m_y),
  285. &(m_hoverWindow.m_width), &(m_hoverWindow.m_height),
  286. &(m_hoverWindow.m_border), &depth );
  287. }