slop.cpp 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. #include "slop.hpp"
  2. X11* x11;
  3. Mouse* mouse;
  4. Keyboard* keyboard;
  5. Resource* resource;
  6. // Defaults!
  7. SlopOptions::SlopOptions() {
  8. borderSize = 1;
  9. nodecorations = false;
  10. tolerance = 2;
  11. padding = 0;
  12. shader = "textured";
  13. highlight = false;
  14. xdisplay = ":0";
  15. r = 0.5;
  16. g = 0.5;
  17. b = 0.5;
  18. a = 1;
  19. }
  20. SlopSelection::SlopSelection( float x, float y, float w, float h, int id ) {
  21. this->x = x;
  22. this->y = y;
  23. this->w = w;
  24. this->h = h;
  25. this->id = id;
  26. }
  27. SlopSelection GLSlopSelect( SlopOptions* options, bool* cancelled, SlopWindow* window );
  28. SlopSelection XShapeSlopSelect( SlopOptions* options, bool* cancelled);
  29. SlopSelection SlopSelect( SlopOptions* options, bool* cancelled ) {
  30. SlopSelection returnval(0,0,0,0,0);
  31. bool deleteOptions = false;
  32. if ( !options ) {
  33. deleteOptions = true;
  34. options = new SlopOptions();
  35. }
  36. resource = new Resource();
  37. // Set up x11 temporarily
  38. x11 = new X11(options->xdisplay);
  39. keyboard = new Keyboard( x11 );
  40. bool success = false;
  41. SlopWindow* window;
  42. // First we check if we have a compositor available
  43. if ( x11->hasCompositor() ) {
  44. // If we have a compositor, we try using OpenGL
  45. try {
  46. window = new SlopWindow();
  47. success = true;
  48. } catch (...) {
  49. success = false;
  50. }
  51. } else {
  52. std::cerr << "Failed to detect a compositor, OpenGL hardware-accelleration disabled...\n";
  53. }
  54. if ( !success ) {
  55. // If we fail, we launch the XShape version of slop.
  56. std::cerr << "Failed to launch OpenGL context, --shader parameter will be ignored.\n";
  57. returnval = XShapeSlopSelect( options, cancelled );
  58. } else {
  59. returnval = GLSlopSelect( options, cancelled, window );
  60. }
  61. delete x11;
  62. delete resource;
  63. if ( deleteOptions ) {
  64. delete options;
  65. }
  66. return returnval;
  67. }
  68. SlopSelection XShapeSlopSelect( SlopOptions* options, bool* cancelled ) {
  69. // Init our little state machine, memory is a tad of a misnomer
  70. SlopMemory memory( options, new XShapeRectangle(glm::vec2(0,0), glm::vec2(0,0), options->borderSize, options->padding, glm::vec4( options->r, options->g, options->b, options->a ), options->highlight) );
  71. mouse = new Mouse( x11, options->nodecorations, ((XShapeRectangle*)memory.rectangle)->window );
  72. // We have no GL context, so the matrix is useless...
  73. glm::mat4 fake;
  74. // This is where we'll run through all of our stuffs
  75. while( memory.running ) {
  76. mouse->update();
  77. keyboard->update();
  78. // We move our statemachine forward.
  79. memory.update( 1 );
  80. // We don't actually draw anything, but the state machine uses
  81. // this to know when to spawn the window.
  82. memory.draw( fake );
  83. // X11 explodes if we update as fast as possible, here's a tiny sleep.
  84. XFlush(x11->display);
  85. std::this_thread::sleep_for(std::chrono::milliseconds(10));
  86. // Then we draw the framebuffer to the screen
  87. if ( keyboard->anyKeyDown() || mouse->getButton( 3 ) ) {
  88. memory.running = false;
  89. if ( cancelled ) {
  90. *cancelled = true;
  91. }
  92. } else {
  93. *cancelled = false;
  94. }
  95. }
  96. // Now we should have a selection! We parse everything we know about it here.
  97. glm::vec4 output = memory.rectangle->getRect();
  98. // Lets now clear both front and back buffers before closing.
  99. // hopefully it'll be completely transparent while closing!
  100. // Then we clean up.
  101. delete mouse;
  102. // Finally return the data.
  103. return SlopSelection( output.x, output.y, output.z, output.w, memory.selectedWindow );
  104. }
  105. SlopSelection GLSlopSelect( SlopOptions* options, bool* cancelled, SlopWindow* window ) {
  106. mouse = new Mouse( x11, options->nodecorations, window->window );
  107. if ( options->shader != "textured" ) {
  108. window->framebuffer->setShader( options->shader );
  109. }
  110. // Init our little state machine, memory is a tad of a misnomer
  111. SlopMemory memory( options, new GLRectangle(glm::vec2(0,0), glm::vec2(0,0), options->borderSize, options->padding, glm::vec4( options->r, options->g, options->b, options->a ), options->highlight) );
  112. // This is where we'll run through all of our stuffs
  113. while( memory.running ) {
  114. mouse->update();
  115. keyboard->update();
  116. // We move our statemachine forward.
  117. memory.update( 1 );
  118. // Then we draw our junk to a framebuffer.
  119. window->framebuffer->bind();
  120. glClearColor (0.0, 0.0, 0.0, 0.0);
  121. glClear (GL_COLOR_BUFFER_BIT);
  122. memory.draw( window->camera );
  123. window->framebuffer->unbind();
  124. // Then we draw the framebuffer to the screen
  125. window->framebuffer->draw();
  126. window->display();
  127. GLenum err = glGetError();
  128. if ( err != GL_NO_ERROR ) {
  129. throw err;
  130. }
  131. if ( keyboard->anyKeyDown() || mouse->getButton( 3 ) ) {
  132. memory.running = false;
  133. if ( cancelled ) {
  134. *cancelled = true;
  135. }
  136. } else {
  137. *cancelled = false;
  138. }
  139. }
  140. // Now we should have a selection! We parse everything we know about it here.
  141. glm::vec4 output = memory.rectangle->getRect();
  142. // Lets now clear both front and back buffers before closing.
  143. // hopefully it'll be completely transparent while closing!
  144. glClearColor (0.0, 0.0, 0.0, 0.0);
  145. glClear (GL_COLOR_BUFFER_BIT);
  146. window->display();
  147. glClear (GL_COLOR_BUFFER_BIT);
  148. window->display();
  149. // Then we clean up.
  150. delete window;
  151. delete mouse;
  152. // Finally return the data.
  153. return SlopSelection( output.x, output.y, output.z, output.w, memory.selectedWindow );
  154. }