slop.cpp 6.7KB

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