slop.cpp 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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. shaders.push_back("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. if ( !options->nokeyboard ) {
  64. XErrorHandler ph = XSetErrorHandler(slop::TmpXError);
  65. keyboard = new Keyboard( x11 );
  66. XSetErrorHandler(ph);
  67. }
  68. bool success = false;
  69. std::string errorstring = "";
  70. SlopWindow* window;
  71. // First we check if we have a compositor available
  72. if ( x11->hasCompositor() && !options->noopengl ) {
  73. // If we have a compositor, we try using OpenGL
  74. try {
  75. window = new SlopWindow();
  76. success = true;
  77. } catch( std::exception* e ) {
  78. errorstring += std::string(e->what()) + "\n";
  79. success = false;
  80. } catch (...) {
  81. success = false;
  82. }
  83. } else {
  84. errorstring += "Failed to detect a compositor, OpenGL hardware-accelleration disabled...\n";
  85. }
  86. if ( !success ) {
  87. // If we fail, we launch the XShape version of slop.
  88. if ( !quiet && !options->noopengl ) {
  89. if ( errorstring.length() <= 0 ) {
  90. errorstring += "Failed to launch OpenGL context, --shader parameter will be ignored.\n";
  91. std::cerr << errorstring;
  92. } else {
  93. std::cerr << errorstring;
  94. }
  95. }
  96. returnval = slop::XShapeSlopSelect( options, cancelled );
  97. } else {
  98. returnval = slop::GLSlopSelect( options, cancelled, window );
  99. }
  100. delete x11;
  101. delete slop::resource;
  102. if ( deleteOptions ) {
  103. delete options;
  104. }
  105. return returnval;
  106. }
  107. slop::SlopSelection slop::XShapeSlopSelect( slop::SlopOptions* options, bool* cancelled ) {
  108. // Init our little state machine, memory is a tad of a misnomer
  109. slop::SlopMemory* memory = new slop::SlopMemory( 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) );
  110. slop::mouse = new slop::Mouse( x11, options->nodecorations, ((XShapeRectangle*)memory->rectangle)->window );
  111. // We have no GL context, so the matrix is useless...
  112. glm::mat4 fake;
  113. // This is where we'll run through all of our stuffs
  114. auto last = std::chrono::high_resolution_clock::now();
  115. while( memory->running ) {
  116. slop::mouse->update();
  117. if ( !options->nokeyboard ) {
  118. slop::keyboard->update();
  119. }
  120. // We move our statemachine forward.
  121. auto current = std::chrono::high_resolution_clock::now();
  122. std::chrono::duration<double, std::milli> frametime = current-last;
  123. last = current;
  124. memory->update( frametime.count()/1000.f );
  125. // We don't actually draw anything, but the state machine uses
  126. // this to know when to spawn the window.
  127. memory->draw( fake );
  128. // X11 explodes if we update as fast as possible, here's a tiny sleep.
  129. XFlush(x11->display);
  130. std::this_thread::sleep_for(std::chrono::milliseconds(10));
  131. // Then we draw the framebuffer to the screen
  132. if ( (!options->nokeyboard && slop::keyboard->anyKeyDown()) || slop::mouse->getButton( 3 ) ) {
  133. memory->running = false;
  134. if ( cancelled ) {
  135. *cancelled = true;
  136. }
  137. } else {
  138. *cancelled = false;
  139. }
  140. }
  141. // Now we should have a selection! We parse everything we know about it here.
  142. glm::vec4 output = memory->rectangle->getRect();
  143. // Lets now clear both front and back buffers before closing.
  144. // hopefully it'll be completely transparent while closing!
  145. // Then we clean up.
  146. delete slop::mouse;
  147. Window selectedWindow = memory->selectedWindow;
  148. delete memory;
  149. // Try to detect the window dying.
  150. int tries = 0;
  151. while( tries < 50 ) {
  152. XEvent event;
  153. if ( XCheckTypedEvent( x11->display, UnmapNotify, &event ) ) { break; }
  154. if ( XCheckTypedEvent( x11->display, DestroyNotify, &event ) ) { break; }
  155. std::this_thread::sleep_for(std::chrono::milliseconds(10));
  156. tries++;
  157. }
  158. // Finally return the data.
  159. return slop::SlopSelection( output.x, output.y, output.z, output.w, selectedWindow );
  160. }
  161. slop::SlopSelection slop::GLSlopSelect( slop::SlopOptions* options, bool* cancelled, SlopWindow* window ) {
  162. slop::mouse = new slop::Mouse( x11, options->nodecorations, window->window );
  163. std::string vert = "#version 120\nattribute vec2 position;\nattribute vec2 uv;\nvarying vec2 uvCoord;\nvoid main()\n{\nuvCoord = uv;\ngl_Position = vec4(position,0,1);\n}\n";
  164. std::string frag = "#version 120\nuniform sampler2D texture;\nvarying vec2 uvCoord;\nvoid main()\n {\ngl_FragColor = texture2D( texture, uvCoord );\n}\n";
  165. slop::Shader* textured = new slop::Shader( vert, frag, false );
  166. std::vector<slop::Shader*> shaders;
  167. for( int i=0;i<options->shaders.size();i++ ) {
  168. std::string sn = options->shaders[i];
  169. if ( sn != "textured" ) {
  170. shaders.push_back( new slop::Shader( sn + ".vert", sn + ".frag" ) );
  171. } else {
  172. shaders.push_back( textured );
  173. }
  174. }
  175. // Init our little state machine, memory is a tad of a misnomer
  176. slop::SlopMemory* memory = new slop::SlopMemory( 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) );
  177. slop::Framebuffer* pingpong = new slop::Framebuffer(WidthOfScreen(x11->screen), HeightOfScreen(x11->screen));
  178. // This is where we'll run through all of our stuffs
  179. auto start = std::chrono::high_resolution_clock::now();
  180. auto last = start;
  181. while( memory->running ) {
  182. slop::mouse->update();
  183. if ( !options->nokeyboard ) {
  184. slop::keyboard->update();
  185. }
  186. // We move our statemachine forward.
  187. auto current = std::chrono::high_resolution_clock::now();
  188. std::chrono::duration<double, std::milli> frametime = current-last;
  189. last = current;
  190. memory->update( frametime.count()/1000.f );
  191. // Then we draw our junk to a framebuffer.
  192. window->framebuffer->setShader( textured );
  193. window->framebuffer->bind();
  194. glClearColor (0.0, 0.0, 0.0, 0.0);
  195. glClear (GL_COLOR_BUFFER_BIT);
  196. memory->draw( window->camera );
  197. window->framebuffer->unbind();
  198. std::chrono::duration<double, std::milli> elapsed = current-start;
  199. int i;
  200. // We have our clean buffer, now to slather it with some juicy shader chains.
  201. glEnable( GL_BLEND );
  202. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  203. for (i=0;i<=(int)shaders.size()-2;i+=2) {
  204. pingpong->bind();
  205. glClearColor (0.0, 0.0, 0.0, 0.0);
  206. glClear (GL_COLOR_BUFFER_BIT);
  207. window->framebuffer->setShader( shaders[i] );
  208. window->framebuffer->draw(slop::mouse->getMousePos(), elapsed.count()/1000.f, glm::vec4( options->r, options->g, options->b, options->a ) );
  209. pingpong->unbind();
  210. window->framebuffer->bind();
  211. glClearColor (0.0, 0.0, 0.0, 0.0);
  212. glClear (GL_COLOR_BUFFER_BIT);
  213. pingpong->setShader( shaders[i+1] );
  214. pingpong->draw(slop::mouse->getMousePos(), elapsed.count()/1000.f, glm::vec4( options->r, options->g, options->b, options->a ) );
  215. window->framebuffer->unbind();
  216. }
  217. for (;i<shaders.size();i++) {
  218. pingpong->bind();
  219. glClearColor (0.0, 0.0, 0.0, 0.0);
  220. glClear (GL_COLOR_BUFFER_BIT);
  221. window->framebuffer->setShader( shaders[i] );
  222. window->framebuffer->draw(slop::mouse->getMousePos(), elapsed.count()/1000.f, glm::vec4( options->r, options->g, options->b, options->a ) );
  223. pingpong->unbind();
  224. }
  225. glDisable( GL_BLEND );
  226. if ( i%2 != 0 ) {
  227. window->framebuffer->draw(slop::mouse->getMousePos(), elapsed.count()/1000.f, glm::vec4( options->r, options->g, options->b, options->a ) );
  228. } else {
  229. pingpong->draw(slop::mouse->getMousePos(), elapsed.count()/1000.f, glm::vec4( options->r, options->g, options->b, options->a ) );
  230. }
  231. window->display();
  232. // Here we sleep just to prevent our CPU usage from going to 100%.
  233. std::this_thread::sleep_for(std::chrono::milliseconds(10));
  234. GLenum err = glGetError();
  235. if ( err != GL_NO_ERROR ) {
  236. std::string error;
  237. switch(err) {
  238. case GL_INVALID_OPERATION: error="INVALID_OPERATION"; break;
  239. case GL_INVALID_ENUM: error="INVALID_ENUM"; break;
  240. case GL_INVALID_VALUE: error="INVALID_VALUE"; break;
  241. case GL_OUT_OF_MEMORY: error="OUT_OF_MEMORY"; break;
  242. case GL_INVALID_FRAMEBUFFER_OPERATION: error="INVALID_FRAMEBUFFER_OPERATION"; break;
  243. }
  244. throw new std::runtime_error( "OpenGL threw an error: " + error );
  245. }
  246. if ( (!options->nokeyboard && slop::keyboard->anyKeyDown()) || slop::mouse->getButton( 3 ) ) {
  247. memory->running = false;
  248. if ( cancelled ) {
  249. *cancelled = true;
  250. }
  251. } else {
  252. *cancelled = false;
  253. }
  254. }
  255. // Now we should have a selection! We parse everything we know about it here.
  256. glm::vec4 output = memory->rectangle->getRect();
  257. // Lets now clear both front and back buffers before closing.
  258. // hopefully it'll be completely transparent while closing!
  259. glClearColor (0.0, 0.0, 0.0, 0.0);
  260. glClear (GL_COLOR_BUFFER_BIT);
  261. window->display();
  262. glClear (GL_COLOR_BUFFER_BIT);
  263. window->display();
  264. // Then we clean up.
  265. delete window;
  266. delete slop::mouse;
  267. Window selectedWindow = memory->selectedWindow;
  268. delete memory;
  269. // Try to detect the window dying.
  270. int tries = 0;
  271. while( tries < 50 ) {
  272. XEvent event;
  273. if ( XCheckTypedEvent( x11->display, UnmapNotify, &event ) ) { break; }
  274. if ( XCheckTypedEvent( x11->display, DestroyNotify, &event ) ) { break; }
  275. std::this_thread::sleep_for(std::chrono::milliseconds(10));
  276. tries++;
  277. }
  278. // Finally return the data.
  279. return slop::SlopSelection( output.x, output.y, output.z, output.w, selectedWindow );
  280. }