123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. #include "x.hpp"
  2. slop::XEngine* xengine = new slop::XEngine();
  3. slop::XEngine::XEngine() {
  4. m_keypressed = false;
  5. m_display = NULL;
  6. m_visual = NULL;
  7. m_screen = NULL;
  8. m_good = false;
  9. m_mousex = -1;
  10. m_mousey = -1;
  11. m_hoverXWindow = None;
  12. }
  13. slop::XEngine::~XEngine() {
  14. if ( !m_good ) {
  15. return;
  16. }
  17. for ( unsigned int i=0; i<m_cursors.size(); i++ ) {
  18. if ( m_cursors.at( i ) ) {
  19. XFreeCursor( m_display, m_cursors[i] );
  20. }
  21. }
  22. for ( unsigned int i=0; i<m_rects.size(); i++ ) {
  23. delete m_rects.at( i );
  24. }
  25. XCloseDisplay( m_display );
  26. }
  27. // We need to keep track of the rectangle windows, so that they don't override our "focus"d windows.
  28. void slop::XEngine::addRect( Rectangle* rect ) {
  29. m_rects.push_back( rect );
  30. }
  31. void slop::XEngine::removeRect( Rectangle* rect ) {
  32. for ( unsigned int i=0; i<m_rects.size(); i++ ) {
  33. if ( m_rects.at( i ) == rect ) {
  34. m_rects.erase( m_rects.begin() + i );
  35. i--;
  36. delete rect;
  37. return;
  38. }
  39. }
  40. }
  41. bool slop::XEngine::mouseDown( unsigned int button ) {
  42. if ( button >= m_mouse.size() ) {
  43. return false;
  44. }
  45. return m_mouse.at( button );
  46. }
  47. int slop::XEngine::init( std::string display ) {
  48. // Initialize display
  49. m_display = XOpenDisplay( display.c_str() );
  50. if ( !m_display ) {
  51. fprintf( stderr, "Error: Failed to open X display %s\n", display.c_str() );
  52. return 1;
  53. }
  54. m_screen = ScreenOfDisplay( m_display, DefaultScreen( m_display ) );
  55. m_visual = DefaultVisual ( m_display, XScreenNumberOfScreen( m_screen ) );
  56. m_colormap = DefaultColormap( m_display, XScreenNumberOfScreen( m_screen ) );
  57. m_root = RootWindow ( m_display, XScreenNumberOfScreen( m_screen ) );
  58. m_good = true;
  59. return 0;
  60. }
  61. int slop::XEngine::grabKeyboard() {
  62. if ( !m_good ) {
  63. return 1;
  64. }
  65. int err = XGrabKeyboard( m_display, m_root, False,
  66. GrabModeAsync, GrabModeAsync, CurrentTime );
  67. if ( err != GrabSuccess ) {
  68. fprintf( stderr, "Error: Failed to grab X keyboard.\n" );
  69. fprintf( stderr, "This can be caused by launching slop incorrectly.\n" );
  70. fprintf( stderr, "gnome-session launches it fine from keyboard binds.\n" );
  71. return 1;
  72. }
  73. }
  74. int slop::XEngine::releaseKeyboard() {
  75. if ( !m_good ) {
  76. return 1;
  77. }
  78. XUngrabKeyboard( m_display, CurrentTime );
  79. return 0;
  80. }
  81. // Grabs the cursor, be wary that setCursor changes the mouse masks.
  82. int slop::XEngine::grabCursor( slop::CursorType type ) {
  83. if ( !m_good ) {
  84. return 1;
  85. }
  86. int xfontcursor = getCursor( type );
  87. int err = XGrabPointer( m_display, m_root, False,
  88. PointerMotionMask | ButtonPressMask | ButtonReleaseMask,
  89. GrabModeAsync, GrabModeAsync, m_root, xfontcursor, CurrentTime );
  90. if ( err != GrabSuccess ) {
  91. fprintf( stderr, "Error: Failed to grab X cursor.\n" );
  92. fprintf( stderr, "This can be caused by launching slop incorrectly.\n" );
  93. fprintf( stderr, "gnome-session launches it fine from keyboard binds.\n" );
  94. return 1;
  95. }
  96. // Quickly set the mouse position so we don't have to worry about x11 generating an event.
  97. Window root, child;
  98. int mx, my;
  99. int wx, wy;
  100. unsigned int mask;
  101. XQueryPointer( m_display, m_root, &root, &child, &mx, &my, &wx, &wy, &mask );
  102. m_mousex = mx;
  103. m_mousey = my;
  104. // Oh and while we're at it, make sure we set the window we're hoving over as well.
  105. updateHoverWindow( child );
  106. return 0;
  107. }
  108. int slop::XEngine::releaseCursor() {
  109. if ( !m_good ) {
  110. return 1;
  111. }
  112. XUngrabPointer( m_display, CurrentTime );
  113. return 0;
  114. }
  115. void slop::XEngine::tick() {
  116. if ( !m_good ) {
  117. return;
  118. }
  119. XFlush( m_display );
  120. XEvent event;
  121. while ( XPending( m_display ) ) {
  122. XNextEvent( m_display, &event );
  123. switch ( event.type ) {
  124. case MotionNotify: {
  125. m_mousex = event.xmotion.x;
  126. m_mousey = event.xmotion.y;
  127. break;
  128. }
  129. case ButtonPress: {
  130. // Our pitiful mouse manager--
  131. if ( m_mouse.size() > event.xbutton.button ) {
  132. m_mouse.at( event.xbutton.button ) = true;
  133. } else {
  134. m_mouse.resize( event.xbutton.button+2, false );
  135. m_mouse.at( event.xbutton.button ) = true;
  136. }
  137. break;
  138. }
  139. case ButtonRelease: {
  140. if ( m_mouse.size() > event.xbutton.button ) {
  141. m_mouse.at( event.xbutton.button ) = false;
  142. } else {
  143. m_mouse.resize( event.xbutton.button+2, false );
  144. m_mouse.at( event.xbutton.button ) = false;
  145. }
  146. break;
  147. }
  148. // For this particular utility, we only care if a key is pressed.
  149. // I'm too lazy to implement an actual keyhandler for that.
  150. case KeyPress: {
  151. m_keypressed = true;
  152. break;
  153. }
  154. // Since we also don't care if it's released, do nothing! yay
  155. case KeyRelease: {
  156. //m_keypressed = false;
  157. break;
  158. }
  159. default: break;
  160. }
  161. }
  162. // Since I couldn't get Xlib to send EnterNotify or LeaveNotify events, we need to query the underlying window every frame.
  163. updateHoverWindow();
  164. }
  165. // This converts an enum into a preallocated cursor, the cursor will automatically deallocate itself on ~XEngine
  166. Cursor slop::XEngine::getCursor( slop::CursorType type ) {
  167. int xfontcursor;
  168. switch ( type ) {
  169. default:
  170. case Left: xfontcursor = XC_left_ptr; break;
  171. case Crosshair: xfontcursor = XC_crosshair; break;
  172. case Cross: xfontcursor = XC_cross; break;
  173. case UpperLeftCorner: xfontcursor = XC_ul_angle; break;
  174. case UpperRightCorner: xfontcursor = XC_ur_angle; break;
  175. case LowerLeftCorner: xfontcursor = XC_ll_angle; break;
  176. case LowerRightCorner: xfontcursor = XC_lr_angle; break;
  177. }
  178. Cursor newcursor = 0;
  179. if ( m_cursors.size() > xfontcursor ) {
  180. newcursor = m_cursors.at( xfontcursor );
  181. }
  182. if ( !newcursor ) {
  183. newcursor = XCreateFontCursor( m_display, xfontcursor );
  184. m_cursors.resize( xfontcursor+2, 0 );
  185. m_cursors.at( xfontcursor ) = newcursor;
  186. }
  187. return newcursor;
  188. }
  189. // Swaps out the current cursor, bewary that XChangeActivePointerGrab also resets masks, so if you change the mouse masks on grab you need to change them here too.
  190. void slop::XEngine::setCursor( slop::CursorType type ) {
  191. if ( !m_good ) {
  192. return;
  193. }
  194. Cursor xfontcursor = getCursor( type );
  195. XChangeActivePointerGrab( m_display,
  196. PointerMotionMask | ButtonPressMask | ButtonReleaseMask,
  197. xfontcursor, CurrentTime );
  198. }
  199. slop::Rectangle::~Rectangle() {
  200. //XFreeGC( xengine->m_display, m_gc );
  201. if ( m_window == None ) {
  202. return;
  203. }
  204. //XFreeColors( xengine->m_display, xengine->m_colormap, m_color.pixel, 1,
  205. XUnmapWindow( xengine->m_display, m_window );
  206. XDestroyWindow( xengine->m_display, m_window );
  207. }
  208. slop::Rectangle::Rectangle( int x, int y, int width, int height, int border, int padding, float r, float g, float b ) {
  209. m_xoffset = 0;
  210. m_yoffset = 0;
  211. m_x = x;
  212. m_y = y;
  213. m_width = width;
  214. m_height = height;
  215. m_border = border;
  216. m_padding = padding;
  217. m_window = None;
  218. // Convert the width, height, x, and y to coordinates that don't have negative values.
  219. // (also adjust for padding and border size.)
  220. constrain( width, height );
  221. // If we don't have a border, we don't exist, so just die.
  222. if ( m_border == 0 ) {
  223. return;
  224. }
  225. // This sets up m_color
  226. int err = convertColor( r, g, b );
  227. if ( err ) {
  228. fprintf( stderr, "Couldn't allocate color of value %f,%f,%f!\n", r, g, b );
  229. }
  230. XSetWindowAttributes attributes;
  231. // Set up the window so it's our color
  232. attributes.background_pixmap = None;
  233. attributes.background_pixel = m_color.pixel;
  234. // Not actually sure what this does, but it keeps the window from bugging out :u.
  235. attributes.override_redirect = True;
  236. // We must use our color map, because that's where our color is allocated.
  237. attributes.colormap = xengine->m_colormap;
  238. unsigned long valueMask = CWBackPixmap | CWBackPixel | CWOverrideRedirect | CWColormap;
  239. // Create the window offset by our generated offsets (see constrain( float, float ))
  240. m_window = XCreateWindow( xengine->m_display, xengine->m_root, m_x+m_xoffset, m_y+m_yoffset, m_width+m_border*2, m_height+m_border*2,
  241. 0, CopyFromParent, InputOutput,
  242. CopyFromParent, valueMask, &attributes );
  243. // Now punch a hole into it so it looks like a selection rectangle!
  244. XRectangle rect;
  245. rect.x = rect.y = m_border;
  246. rect.width = m_width;
  247. rect.height = m_height;
  248. XShapeCombineRectangles( xengine->m_display, m_window, ShapeBounding, 0, 0, &rect, 1, ShapeSubtract, 0);
  249. XMapWindow( xengine->m_display, m_window );
  250. }
  251. void slop::Rectangle::setPos( int x, int y ) {
  252. if ( m_x == x && m_y == y ) {
  253. return;
  254. }
  255. m_x = x;
  256. m_y = y;
  257. // If we don't have a border, we don't exist, so just die.
  258. if ( m_border == 0 ) {
  259. return;
  260. }
  261. XMoveWindow( xengine->m_display, m_window, m_x+m_xoffset, m_y+m_yoffset );
  262. }
  263. void slop::Rectangle::setDim( int w, int h ) {
  264. if ( m_width == w && m_height == h ) {
  265. return;
  266. }
  267. constrain( w, h );
  268. // If we don't have a border, we don't exist, so just die.
  269. if ( m_border == 0 ) {
  270. return;
  271. }
  272. // Change the window size and location to our generated offsets (see constrain( float, float ))
  273. XResizeWindow( xengine->m_display, m_window, m_width+m_border*2, m_height+m_border*2 );
  274. XMoveWindow( xengine->m_display, m_window, m_x+m_xoffset, m_y+m_yoffset );
  275. // Regenerate our hole
  276. XRectangle rect;
  277. rect.x = rect.y = 0;
  278. rect.width = m_width+m_border*2;
  279. rect.height = m_height+m_border*2;
  280. XShapeCombineRectangles( xengine->m_display, m_window, ShapeBounding, 0, 0, &rect, 1, ShapeSet, 0);
  281. // Then punch out another.
  282. rect.x = rect.y = m_border;
  283. rect.width = m_width;
  284. rect.height = m_height;
  285. XShapeCombineRectangles( xengine->m_display, m_window, ShapeBounding, 0, 0, &rect, 1, ShapeSubtract, 0);
  286. }
  287. void slop::XEngine::updateHoverWindow() {
  288. Window root, child;
  289. int mx, my;
  290. int wx, wy;
  291. unsigned int mask;
  292. // Query the pointer for the child window, the child window is basically the window we're hovering over.
  293. XQueryPointer( m_display, m_root, &root, &child, &mx, &my, &wx, &wy, &mask );
  294. // If we already know that we're hovering over it, do nothing.
  295. if ( m_hoverXWindow == child ) {
  296. return;
  297. }
  298. // Make sure we can't select one of our selection rectangles, that's just weird.
  299. for ( unsigned int i=0; i<m_rects.size(); i++ ) {
  300. if ( m_rects.at( i )->m_window == child ) {
  301. return;
  302. }
  303. }
  304. m_hoverXWindow = child;
  305. if ( child == None ) {
  306. return;
  307. }
  308. // Generate the geometry values so we can use them if needed.
  309. unsigned int depth;
  310. XGetGeometry( m_display, child, &root,
  311. &(m_hoverWindow.m_x), &(m_hoverWindow.m_y),
  312. &(m_hoverWindow.m_width), &(m_hoverWindow.m_height),
  313. &(m_hoverWindow.m_border), &depth );
  314. }
  315. void slop::XEngine::updateHoverWindow( Window child ) {
  316. // Same thing as updateHoverWindow but it uses the specified child.
  317. // It's used when we first grab the cursor so it's slightly more effecient
  318. // than calling XQueryPointer twice.
  319. if ( m_hoverXWindow == child ) {
  320. return;
  321. }
  322. for ( unsigned int i=0; i<m_rects.size(); i++ ) {
  323. if ( m_rects.at( i )->m_window == child ) {
  324. return;
  325. }
  326. }
  327. m_hoverXWindow = child;
  328. if ( child == None ) {
  329. return;
  330. }
  331. unsigned int depth;
  332. Window root;
  333. XGetGeometry( m_display, child, &root,
  334. &(m_hoverWindow.m_x), &(m_hoverWindow.m_y),
  335. &(m_hoverWindow.m_width), &(m_hoverWindow.m_height),
  336. &(m_hoverWindow.m_border), &depth );
  337. }
  338. // Keeps our rectangle's sizes all positive, so Xlib doesn't throw an exception.
  339. // It also keeps our values in absolute coordinates which is nice.
  340. void slop::Rectangle::constrain( int w, int h ) {
  341. int pad = m_padding;
  342. if ( pad < 0 && std::abs( w ) < std::abs( pad )*2 ) {
  343. pad = 0;
  344. }
  345. if ( w < 0 ) {
  346. m_flippedx = true;
  347. m_xoffset = w - pad - m_border;
  348. m_width = -w + pad*2;
  349. } else {
  350. m_flippedx = false;
  351. m_xoffset = -pad - m_border;
  352. m_width = w + pad*2;
  353. }
  354. pad = m_padding;
  355. if ( pad < 0 && std::abs( h ) < std::abs( pad )*2 ) {
  356. pad = 0;
  357. }
  358. if ( h < 0 ) {
  359. m_flippedy = true;
  360. m_yoffset = h - pad - m_border;
  361. m_height = -h + pad*2;
  362. } else {
  363. m_flippedy = false;
  364. m_yoffset = -pad - m_border;
  365. m_height = h + pad*2;
  366. }
  367. }
  368. int slop::Rectangle::convertColor( float r, float g, float b ) {
  369. // Convert float colors to shorts.
  370. short red = short( floor( r * 65535.f ) );
  371. short green = short( floor( g * 65535.f ) );
  372. short blue = short( floor( b * 65535.f ) );
  373. XColor color;
  374. color.red = red;
  375. color.green = green;
  376. color.blue = blue;
  377. // I don't deallocate this anywhere, I think X handles it ???
  378. int err = XAllocColor( xengine->m_display, xengine->m_colormap, &color );
  379. if ( err == BadColor ) {
  380. return err;
  381. }
  382. m_color = color;
  383. return 0;
  384. }