slop.cpp 6.7KB

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