slop.cpp 6.3KB

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