slop.cpp 7.2KB

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