xslop.cpp 2.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. #include "slop.hpp"
  2. #include "slopstates.hpp"
  3. #include "mouse.hpp"
  4. #include "resource.hpp"
  5. #include "keyboard.hpp"
  6. #include <chrono>
  7. #include <thread>
  8. #include "xshaperectangle.hpp"
  9. X11* x11;
  10. Mouse* mouse;
  11. Keyboard* keyboard;
  12. Resource* resource;
  13. // Defaults!
  14. SlopOptions::SlopOptions() {
  15. borderSize = 1;
  16. nodecorations = false;
  17. tolerance = 2;
  18. padding = 0;
  19. highlight = false;
  20. xdisplay = ":0";
  21. r = 0.5;
  22. g = 0.5;
  23. b = 0.5;
  24. a = 1;
  25. }
  26. SlopSelection::SlopSelection( float x, float y, float w, float h, int id ) {
  27. this->x = x;
  28. this->y = y;
  29. this->w = w;
  30. this->h = h;
  31. this->id = id;
  32. }
  33. SlopSelection SlopSelect( SlopOptions* options, bool* cancelled ) {
  34. bool deleteOptions = false;
  35. if ( !options ) {
  36. deleteOptions = true;
  37. options = new SlopOptions();
  38. }
  39. resource = new Resource();
  40. // Set up x11 temporarily
  41. x11 = new X11(options->xdisplay);
  42. keyboard = new Keyboard( x11 );
  43. // Init our little state machine, memory is a tad of a misnomer
  44. SlopMemory memory( options );
  45. mouse = new Mouse( x11, options->nodecorations, memory.rectangle->window );
  46. glm::mat4 fake;
  47. // This is where we'll run through all of our stuffs
  48. while( memory.running ) {
  49. mouse->update();
  50. keyboard->update();
  51. // We move our statemachine forward.
  52. memory.update( 1 );
  53. // We don't actually draw anything, but the state machine uses
  54. // this to know when to spawn the window.
  55. memory.draw( fake );
  56. // X11 explodes if we update as fast as possible, here's a tiny sleep.
  57. XFlush(x11->display);
  58. std::this_thread::sleep_for(std::chrono::milliseconds(10));
  59. // Then we draw the framebuffer to the screen
  60. if ( keyboard->anyKeyDown() || mouse->getButton( 3 ) ) {
  61. memory.running = false;
  62. if ( cancelled ) {
  63. *cancelled = true;
  64. }
  65. } else {
  66. *cancelled = false;
  67. }
  68. }
  69. // Now we should have a selection! We parse everything we know about it here.
  70. glm::vec4 output = memory.rectangle->getRect();
  71. // Lets now clear both front and back buffers before closing.
  72. // hopefully it'll be completely transparent while closing!
  73. // Then we clean up.
  74. delete mouse;
  75. delete x11;
  76. delete resource;
  77. if ( deleteOptions ) {
  78. delete options;
  79. }
  80. // Finally return the data.
  81. return SlopSelection( output.x, output.y, output.z, output.w, memory.selectedWindow );
  82. }