slop.cpp 5.9KB

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