slop.cpp 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. #include <GL/glew.h>
  2. #include <GL/gl.h>
  3. #include <chrono>
  4. #include <thread>
  5. #include <string>
  6. #include <vector>
  7. #include <stdlib.h>
  8. #include "slopstates.hpp"
  9. #include "mouse.hpp"
  10. #include "resource.hpp"
  11. #include "keyboard.hpp"
  12. #include "window.hpp"
  13. #include "shader.hpp"
  14. #include "framebuffer.hpp"
  15. #include "glrectangle.hpp"
  16. #include "xshaperectangle.hpp"
  17. #include "slop.hpp"
  18. namespace slop {
  19. X11* x11;
  20. Mouse* mouse;
  21. Keyboard* keyboard;
  22. Resource* resource;
  23. SlopSelection GLSlopSelect( slop::SlopOptions* options, slop::SlopWindow* window );
  24. SlopSelection XShapeSlopSelect( slop::SlopOptions* options );
  25. static int TmpXError(Display * d, XErrorEvent * ev) {
  26. return 0;
  27. }
  28. }
  29. template<typename Out>
  30. static void split(const std::string &s, char delim, Out result) {
  31. std::stringstream ss;
  32. ss.str(s);
  33. std::string item;
  34. while (std::getline(ss, item, delim)) {
  35. *(result++) = item;
  36. }
  37. }
  38. static std::vector<std::string> split(const std::string &s, char delim) {
  39. std::vector<std::string> elems;
  40. split(s, delim, std::back_inserter(elems));
  41. return elems;
  42. }
  43. using namespace slop;
  44. char slop_default_xdisplay[] = ":0";
  45. char slop_default_shaders[] = "textured";
  46. // Defaults!
  47. slop::SlopOptions::SlopOptions() {
  48. border = 1;
  49. nokeyboard = false;
  50. noopengl = false;
  51. nodecorations = false;
  52. tolerance = 2;
  53. padding = 0;
  54. shaders = slop_default_shaders;
  55. highlight = false;
  56. r = 0.5;
  57. g = 0.5;
  58. b = 0.5;
  59. a = 1;
  60. quiet = false;
  61. char* envdisplay = getenv("DISPLAY");
  62. if (envdisplay == NULL) {
  63. xdisplay = slop_default_xdisplay;
  64. } else {
  65. xdisplay = envdisplay;
  66. }
  67. }
  68. slop::SlopSelection::SlopSelection( float x, float y, float w, float h, int id, bool cancelled ) {
  69. this->x = x;
  70. this->y = y;
  71. this->w = w;
  72. this->h = h;
  73. this->id = id;
  74. this->cancelled = cancelled;
  75. }
  76. slop::SlopSelection slop::SlopSelect( slop::SlopOptions* options ) {
  77. slop::SlopSelection returnval(0,0,0,0,0,true);
  78. bool deleteOptions = false;
  79. if ( !options ) {
  80. deleteOptions = true;
  81. options = new slop::SlopOptions();
  82. }
  83. resource = new Resource();
  84. // Set up x11 temporarily
  85. x11 = new X11(options->xdisplay);
  86. if ( !options->nokeyboard ) {
  87. XErrorHandler ph = XSetErrorHandler(slop::TmpXError);
  88. keyboard = new Keyboard( x11 );
  89. XSetErrorHandler(ph);
  90. }
  91. bool success = false;
  92. std::string errorstring = "";
  93. SlopWindow* window;
  94. // First we check if we have a compositor available
  95. if ( x11->hasCompositor() && !options->noopengl ) {
  96. // If we have a compositor, we try using OpenGL
  97. try {
  98. window = new SlopWindow();
  99. if (!GLEW_VERSION_3_0) {
  100. delete window;
  101. throw new std::runtime_error( "OpenGL version is not high enough, slop requires OpenGL 3.0!\nOpenGL accelleration is disabled. Use -o or -q to suppress this message." );
  102. }
  103. success = true;
  104. } catch( std::exception* e ) {
  105. errorstring += std::string(e->what()) + "\n";
  106. success = false;
  107. } catch (...) {
  108. success = false;
  109. }
  110. } else {
  111. errorstring += "Failed to detect a compositor, OpenGL hardware-accelleration disabled...\n";
  112. }
  113. if ( !success ) {
  114. // If we fail, we launch the XShape version of slop.
  115. if ( !options->quiet && !options->noopengl ) {
  116. if ( errorstring.length() <= 0 ) {
  117. errorstring += "Failed to launch OpenGL context, --shader parameter will be ignored.\n";
  118. std::cerr << errorstring;
  119. } else {
  120. std::cerr << errorstring;
  121. }
  122. }
  123. returnval = slop::XShapeSlopSelect( options );
  124. } else {
  125. returnval = slop::GLSlopSelect( options, window );
  126. }
  127. delete x11;
  128. delete slop::resource;
  129. if ( deleteOptions ) {
  130. delete options;
  131. }
  132. return returnval;
  133. }
  134. slop::SlopSelection slop::XShapeSlopSelect( slop::SlopOptions* options ) {
  135. // Init our little state machine, memory is a tad of a misnomer
  136. bool cancelled = false;
  137. slop::SlopMemory* memory = new slop::SlopMemory( options, new XShapeRectangle(glm::vec2(0,0), glm::vec2(0,0), options->border, options->padding, glm::vec4( options->r, options->g, options->b, options->a ), options->highlight) );
  138. slop::mouse = new slop::Mouse( x11, options->nodecorations, ((XShapeRectangle*)memory->rectangle)->window );
  139. // We have no GL context, so the matrix is useless...
  140. glm::mat4 fake;
  141. // This is where we'll run through all of our stuffs
  142. auto last = std::chrono::high_resolution_clock::now();
  143. while( memory->running ) {
  144. slop::mouse->update();
  145. if ( !options->nokeyboard ) {
  146. slop::keyboard->update();
  147. }
  148. // We move our statemachine forward.
  149. auto current = std::chrono::high_resolution_clock::now();
  150. std::chrono::duration<double, std::milli> frametime = current-last;
  151. last = current;
  152. memory->update( frametime.count()/1000.f );
  153. // We don't actually draw anything, but the state machine uses
  154. // this to know when to spawn the window.
  155. memory->draw( fake );
  156. // X11 explodes if we update as fast as possible, here's a tiny sleep.
  157. XFlush(x11->display);
  158. std::this_thread::sleep_for(std::chrono::milliseconds(10));
  159. // Then we draw the framebuffer to the screen
  160. if ( (!options->nokeyboard && slop::keyboard->anyKeyDown()) || slop::mouse->getButton( 3 ) ) {
  161. memory->running = false;
  162. cancelled = true;
  163. }
  164. }
  165. // Now we should have a selection! We parse everything we know about it here.
  166. glm::vec4 output = memory->rectangle->getRect();
  167. // Lets now clear both front and back buffers before closing.
  168. // hopefully it'll be completely transparent while closing!
  169. // Then we clean up.
  170. delete slop::mouse;
  171. Window selectedWindow = memory->selectedWindow;
  172. delete memory;
  173. // Try to detect the window dying.
  174. int tries = 0;
  175. while( tries < 50 ) {
  176. XEvent event;
  177. if ( XCheckTypedEvent( x11->display, UnmapNotify, &event ) ) { break; }
  178. if ( XCheckTypedEvent( x11->display, DestroyNotify, &event ) ) { break; }
  179. std::this_thread::sleep_for(std::chrono::milliseconds(10));
  180. tries++;
  181. }
  182. // Finally return the data.
  183. return slop::SlopSelection( output.x, output.y, output.z, output.w, selectedWindow, cancelled );
  184. }
  185. slop::SlopSelection slop::GLSlopSelect( slop::SlopOptions* options, SlopWindow* window ) {
  186. bool cancelled = false;
  187. slop::mouse = new slop::Mouse( x11, options->nodecorations, window->window );
  188. 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";
  189. std::string frag = "#version 120\nuniform sampler2D texture;\nvarying vec2 uvCoord;\nvoid main()\n {\ngl_FragColor = texture2D( texture, uvCoord );\n}\n";
  190. slop::Shader* textured = new slop::Shader( vert, frag, false );
  191. std::vector<slop::Shader*> shaders;
  192. std::vector<std::string> stringShaders = split( options->shaders, ',' );
  193. for( int i=0;i<stringShaders.size();i++ ) {
  194. std::string sn = stringShaders[i];
  195. if ( sn != "textured" ) {
  196. shaders.push_back( new slop::Shader( sn + ".vert", sn + ".frag" ) );
  197. } else {
  198. shaders.push_back( textured );
  199. }
  200. }
  201. // Init our little state machine, memory is a tad of a misnomer
  202. slop::SlopMemory* memory = new slop::SlopMemory( options, new GLRectangle(glm::vec2(0,0), glm::vec2(0,0), options->border, options->padding, glm::vec4( options->r, options->g, options->b, options->a ), options->highlight) );
  203. slop::Framebuffer* pingpong = new slop::Framebuffer(WidthOfScreen(x11->screen), HeightOfScreen(x11->screen));
  204. // This is where we'll run through all of our stuffs
  205. auto start = std::chrono::high_resolution_clock::now();
  206. auto last = start;
  207. while( memory->running ) {
  208. slop::mouse->update();
  209. if ( !options->nokeyboard ) {
  210. slop::keyboard->update();
  211. }
  212. // We move our statemachine forward.
  213. auto current = std::chrono::high_resolution_clock::now();
  214. std::chrono::duration<double, std::milli> frametime = current-last;
  215. last = current;
  216. memory->update( frametime.count()/1000.f );
  217. // Then we draw our junk to a framebuffer.
  218. window->framebuffer->setShader( textured );
  219. window->framebuffer->bind();
  220. glClearColor (0.0, 0.0, 0.0, 0.0);
  221. glClear (GL_COLOR_BUFFER_BIT);
  222. memory->draw( window->camera );
  223. window->framebuffer->unbind();
  224. std::chrono::duration<double, std::milli> elapsed = current-start;
  225. int i;
  226. // We have our clean buffer, now to slather it with some juicy shader chains.
  227. glEnable( GL_BLEND );
  228. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  229. for (i=0;i<=(int)shaders.size()-2;i+=2) {
  230. pingpong->bind();
  231. glClearColor (0.0, 0.0, 0.0, 0.0);
  232. glClear (GL_COLOR_BUFFER_BIT);
  233. window->framebuffer->setShader( shaders[i] );
  234. window->framebuffer->draw(slop::mouse->getMousePos(), elapsed.count()/1000.f, glm::vec4( options->r, options->g, options->b, options->a ) );
  235. pingpong->unbind();
  236. window->framebuffer->bind();
  237. glClearColor (0.0, 0.0, 0.0, 0.0);
  238. glClear (GL_COLOR_BUFFER_BIT);
  239. pingpong->setShader( shaders[i+1] );
  240. pingpong->draw(slop::mouse->getMousePos(), elapsed.count()/1000.f, glm::vec4( options->r, options->g, options->b, options->a ) );
  241. window->framebuffer->unbind();
  242. }
  243. for (;i<shaders.size();i++) {
  244. pingpong->bind();
  245. glClearColor (0.0, 0.0, 0.0, 0.0);
  246. glClear (GL_COLOR_BUFFER_BIT);
  247. window->framebuffer->setShader( shaders[i] );
  248. window->framebuffer->draw(slop::mouse->getMousePos(), elapsed.count()/1000.f, glm::vec4( options->r, options->g, options->b, options->a ) );
  249. pingpong->unbind();
  250. }
  251. glDisable( GL_BLEND );
  252. if ( i%2 != 0 ) {
  253. window->framebuffer->draw(slop::mouse->getMousePos(), elapsed.count()/1000.f, glm::vec4( options->r, options->g, options->b, options->a ) );
  254. } else {
  255. pingpong->draw(slop::mouse->getMousePos(), elapsed.count()/1000.f, glm::vec4( options->r, options->g, options->b, options->a ) );
  256. }
  257. window->display();
  258. // Here we sleep just to prevent our CPU usage from going to 100%.
  259. std::this_thread::sleep_for(std::chrono::milliseconds(10));
  260. GLenum err = glGetError();
  261. if ( err != GL_NO_ERROR ) {
  262. std::string error;
  263. switch(err) {
  264. case GL_INVALID_OPERATION: error="INVALID_OPERATION"; break;
  265. case GL_INVALID_ENUM: error="INVALID_ENUM"; break;
  266. case GL_INVALID_VALUE: error="INVALID_VALUE"; break;
  267. case GL_OUT_OF_MEMORY: error="OUT_OF_MEMORY"; break;
  268. case GL_INVALID_FRAMEBUFFER_OPERATION: error="INVALID_FRAMEBUFFER_OPERATION"; break;
  269. }
  270. throw new std::runtime_error( "OpenGL threw an error: " + error );
  271. }
  272. if ( (!options->nokeyboard && slop::keyboard->anyKeyDown()) || slop::mouse->getButton( 3 ) ) {
  273. memory->running = false;
  274. cancelled = true;
  275. }
  276. }
  277. // Now we should have a selection! We parse everything we know about it here.
  278. glm::vec4 output = memory->rectangle->getRect();
  279. // Lets now clear both front and back buffers before closing.
  280. // hopefully it'll be completely transparent while closing!
  281. glClearColor (0.0, 0.0, 0.0, 0.0);
  282. glClear (GL_COLOR_BUFFER_BIT);
  283. window->display();
  284. glClear (GL_COLOR_BUFFER_BIT);
  285. window->display();
  286. // Then we clean up.
  287. for( int i=0;i<shaders.size();i++ ) {
  288. delete shaders[i];
  289. }
  290. delete pingpong;
  291. delete window;
  292. delete slop::mouse;
  293. Window selectedWindow = memory->selectedWindow;
  294. delete memory;
  295. // Try to detect the window dying.
  296. int tries = 0;
  297. while( tries < 50 ) {
  298. XEvent event;
  299. if ( XCheckTypedEvent( x11->display, UnmapNotify, &event ) ) { break; }
  300. if ( XCheckTypedEvent( x11->display, DestroyNotify, &event ) ) { break; }
  301. std::this_thread::sleep_for(std::chrono::milliseconds(10));
  302. tries++;
  303. }
  304. // Finally return the data.
  305. return slop::SlopSelection( output.x, output.y, output.z, output.w, selectedWindow, cancelled );
  306. }