x.cpp 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  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. XUnmapWindow( xengine->m_display, m_window );
  210. XDestroyWindow( xengine->m_display, m_window );
  211. }
  212. slop::Rectangle::Rectangle( int x, int y, int width, int height, int border, int padding, float r, float g, float b ) {
  213. m_xoffset = 0;
  214. m_yoffset = 0;
  215. m_x = x;
  216. m_y = y;
  217. m_width = width;
  218. m_height = height;
  219. m_border = border;
  220. m_padding = padding;
  221. m_window = None;
  222. // Convert the width, height, x, and y to coordinates that don't have negative values.
  223. // (also adjust for padding and border size.)
  224. constrain( width, height );
  225. // If we don't have a border, we don't exist, so just die.
  226. if ( m_border == 0 ) {
  227. return;
  228. }
  229. // This sets up m_color
  230. int err = convertColor( r, g, b );
  231. if ( err ) {
  232. fprintf( stderr, "Couldn't allocate color of value %f,%f,%f!\n", r, g, b );
  233. }
  234. XSetWindowAttributes attributes;
  235. // Set up the window so it's our color
  236. attributes.background_pixmap = None;
  237. attributes.background_pixel = m_color.pixel;
  238. // Not actually sure what this does, but it keeps the window from bugging out :u.
  239. attributes.override_redirect = True;
  240. // We must use our color map, because that's where our color is allocated.
  241. attributes.colormap = xengine->m_colormap;
  242. unsigned long valueMask = CWBackPixmap | CWBackPixel | CWOverrideRedirect | CWColormap;
  243. // Create the window offset by our generated offsets (see constrain( float, float ))
  244. 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,
  245. 0, CopyFromParent, InputOutput,
  246. CopyFromParent, valueMask, &attributes );
  247. // Now punch a hole into it so it looks like a selection rectangle!
  248. XRectangle rect;
  249. rect.x = rect.y = m_border;
  250. rect.width = m_width;
  251. rect.height = m_height;
  252. XShapeCombineRectangles( xengine->m_display, m_window, ShapeBounding, 0, 0, &rect, 1, ShapeSubtract, 0);
  253. XMapWindow( xengine->m_display, m_window );
  254. }
  255. void slop::Rectangle::setPos( int x, int y ) {
  256. if ( m_x == x && m_y == y ) {
  257. return;
  258. }
  259. m_x = x;
  260. m_y = y;
  261. // If we don't have a border, we don't exist, so just die.
  262. if ( m_border == 0 ) {
  263. return;
  264. }
  265. XMoveWindow( xengine->m_display, m_window, m_x+m_xoffset-m_border, m_y+m_yoffset-m_border );
  266. }
  267. void slop::Rectangle::setDim( int w, int h ) {
  268. if ( m_width == w && m_height == h ) {
  269. return;
  270. }
  271. constrain( w, h );
  272. // If we don't have a border, we don't exist, so just die.
  273. if ( m_border == 0 ) {
  274. return;
  275. }
  276. // Change the window size and location to our generated offsets (see constrain( float, float ))
  277. XResizeWindow( xengine->m_display, m_window, m_width+m_border*2, m_height+m_border*2 );
  278. XMoveWindow( xengine->m_display, m_window, m_x+m_xoffset-m_border, m_y+m_yoffset-m_border );
  279. // Regenerate our hole
  280. XRectangle rect;
  281. rect.x = rect.y = 0;
  282. rect.width = m_width+m_border*2;
  283. rect.height = m_height+m_border*2;
  284. XShapeCombineRectangles( xengine->m_display, m_window, ShapeBounding, 0, 0, &rect, 1, ShapeSet, 0);
  285. // Then punch out another.
  286. rect.x = rect.y = m_border;
  287. rect.width = m_width;
  288. rect.height = m_height;
  289. XShapeCombineRectangles( xengine->m_display, m_window, ShapeBounding, 0, 0, &rect, 1, ShapeSubtract, 0);
  290. }
  291. void slop::XEngine::updateHoverWindow() {
  292. Window root, child;
  293. int mx, my;
  294. int wx, wy;
  295. unsigned int mask;
  296. // Query the pointer for the child window, the child window is basically the window we're hovering over.
  297. XQueryPointer( m_display, m_root, &root, &child, &mx, &my, &wx, &wy, &mask );
  298. // If we already know that we're hovering over it, do nothing.
  299. if ( m_hoverXWindow == child ) {
  300. return;
  301. }
  302. // Make sure we can't select one of our selection rectangles, that's just weird.
  303. for ( unsigned int i=0; i<m_rects.size(); i++ ) {
  304. if ( m_rects.at( i )->m_window == child ) {
  305. return;
  306. }
  307. }
  308. m_hoverXWindow = child;
  309. if ( child == None ) {
  310. return;
  311. }
  312. // Generate the geometry values so we can use them if needed.
  313. unsigned int depth;
  314. XGetGeometry( m_display, child, &root,
  315. &(m_hoverWindow.m_x), &(m_hoverWindow.m_y),
  316. &(m_hoverWindow.m_width), &(m_hoverWindow.m_height),
  317. &(m_hoverWindow.m_border), &depth );
  318. }
  319. void slop::XEngine::updateHoverWindow( Window child ) {
  320. // Same thing as updateHoverWindow but it uses the specified child.
  321. // It's used when we first grab the cursor so it's slightly more effecient
  322. // than calling XQueryPointer twice.
  323. if ( m_hoverXWindow == child ) {
  324. return;
  325. }
  326. for ( unsigned int i=0; i<m_rects.size(); i++ ) {
  327. if ( m_rects.at( i )->m_window == child ) {
  328. return;
  329. }
  330. }
  331. m_hoverXWindow = child;
  332. if ( child == None ) {
  333. return;
  334. }
  335. unsigned int depth;
  336. Window root;
  337. XGetGeometry( m_display, child, &root,
  338. &(m_hoverWindow.m_x), &(m_hoverWindow.m_y),
  339. &(m_hoverWindow.m_width), &(m_hoverWindow.m_height),
  340. &(m_hoverWindow.m_border), &depth );
  341. }
  342. // Keeps our rectangle's sizes all positive, so Xlib doesn't throw an exception.
  343. // It also keeps our values in absolute coordinates which is nice.
  344. void slop::Rectangle::constrain( int w, int h ) {
  345. int pad = m_padding;
  346. if ( pad < 0 && std::abs( w ) < std::abs( pad )*2 ) {
  347. pad = 0;
  348. }
  349. if ( w < 0 ) {
  350. m_flippedx = true;
  351. m_xoffset = w - pad;
  352. m_width = -w + pad*2;
  353. } else {
  354. m_flippedx = false;
  355. m_xoffset = -pad;
  356. m_width = w + pad*2;
  357. }
  358. pad = m_padding;
  359. if ( pad < 0 && std::abs( h ) < std::abs( pad )*2 ) {
  360. pad = 0;
  361. }
  362. if ( h < 0 ) {
  363. m_flippedy = true;
  364. m_yoffset = h - pad;
  365. m_height = -h + pad*2;
  366. } else {
  367. m_flippedy = false;
  368. m_yoffset = -pad;
  369. m_height = h + pad*2;
  370. }
  371. }
  372. int slop::Rectangle::convertColor( float r, float g, float b ) {
  373. // Convert float colors to shorts.
  374. short red = short( floor( r * 65535.f ) );
  375. short green = short( floor( g * 65535.f ) );
  376. short blue = short( floor( b * 65535.f ) );
  377. XColor color;
  378. color.red = red;
  379. color.green = green;
  380. color.blue = blue;
  381. // I don't deallocate this anywhere, I think X handles it ???
  382. int err = XAllocColor( xengine->m_display, xengine->m_colormap, &color );
  383. if ( err == BadColor ) {
  384. return err;
  385. }
  386. m_color = color;
  387. return 0;
  388. }