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