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