Преглед на файлове

changing the interface to support pure C

naelstrof преди 7 години
родител
ревизия
93273d564f
променени са 3 файла, в които са добавени 82 реда и са изтрити 75 реда
  1. 23
    35
      src/main.cpp
  2. 52
    32
      src/slop.cpp
  3. 7
    8
      src/slop.hpp

+ 23
- 35
src/main.cpp Целия файл

@@ -29,22 +29,6 @@
29 29
 
30 30
 using namespace slop;
31 31
 
32
-template<typename Out>
33
-static void split(const std::string &s, char delim, Out result) {
34
-    std::stringstream ss;
35
-    ss.str(s);
36
-    std::string item;
37
-    while (std::getline(ss, item, delim)) {
38
-        *(result++) = item;
39
-    }
40
-}
41
-
42
-static std::vector<std::string> split(const std::string &s, char delim) {
43
-    std::vector<std::string> elems;
44
-    split(s, delim, std::back_inserter(elems));
45
-    return elems;
46
-}
47
-
48 32
 glm::vec4 parseColor( std::string value ) {
49 33
     std::string valuecopy = value;
50 34
     glm::vec4 found;
@@ -73,7 +57,7 @@ glm::vec4 parseColor( std::string value ) {
73 57
 SlopOptions* getOptions( cxxopts::Options& options ) {
74 58
     slop::SlopOptions* foo = new slop::SlopOptions();
75 59
     if ( options.count( "bordersize" ) > 0 ) {
76
-        foo->borderSize = options["bordersize"].as<float>();
60
+        foo->border = options["bordersize"].as<float>();
77 61
     }
78 62
     if ( options.count( "padding" ) > 0 ) {
79 63
         foo->padding = options["padding"].as<float>();
@@ -93,19 +77,28 @@ SlopOptions* getOptions( cxxopts::Options& options ) {
93 77
         foo->nokeyboard = options["nokeyboard"].as<bool>();
94 78
     }
95 79
     if ( options.count( "xdisplay" ) > 0 ) {
96
-        foo->xdisplay = options["xdisplay"].as<std::string>();
80
+        std::string xdisplay = options["xdisplay"].as<std::string>();
81
+        char* cxdisplay = new char[xdisplay.length()+1];
82
+        memcpy( cxdisplay, xdisplay.c_str(), xdisplay.length() );
83
+        cxdisplay[xdisplay.length()]='\0';
84
+        foo->xdisplay = cxdisplay;
97 85
     }
98
-    std::string shaders = "textured";
99 86
     if ( options.count( "shader" ) > 0 ) {
100
-        shaders = options["shader"].as<std::string>();
87
+        std::string shaders = options["shader"].as<std::string>();
88
+        char* cshaders = new char[shaders.length()+1];
89
+        memcpy( cshaders, shaders.c_str(), shaders.length() );
90
+        cshaders[shaders.length()]='\0';
91
+        foo->shaders = cshaders;
101 92
     }
102
-    foo->shaders = split( shaders, ',' );
103 93
     if ( options.count( "noopengl" ) > 0 ) {
104 94
         foo->noopengl = options["noopengl"].as<bool>();
105 95
     }
106 96
     if ( options.count( "highlight" ) > 0 ) {
107 97
         foo->highlight = options["highlight"].as<bool>();
108 98
     }
99
+    if ( options.count( "quiet" ) > 0 ) {
100
+        foo->quiet = options["quiet"].as<bool>();
101
+    }
109 102
     if ( options.count( "nodecorations" ) > 0 ) {
110 103
         foo->nodecorations = options["nodecorations"].as<int>();
111 104
         if ( foo->nodecorations < 0 || foo->nodecorations > 2 ) {
@@ -115,7 +108,7 @@ SlopOptions* getOptions( cxxopts::Options& options ) {
115 108
     return foo;
116 109
 }
117 110
 
118
-std::string formatOutput( std::string input, SlopSelection selection, bool cancelled ) {
111
+std::string formatOutput( std::string input, SlopSelection selection ) {
119 112
     std::stringstream output;
120 113
     for( unsigned int i=0;i<input.length();i++) {
121 114
         if ( input[i] == '%' ) {
@@ -130,7 +123,7 @@ std::string formatOutput( std::string input, SlopSelection selection, bool cance
130 123
                 case 'w':
131 124
                 case 'W': output << round(selection.w); break;
132 125
                 case 'c':
133
-                case 'C': output << cancelled; break;
126
+                case 'C': output << selection.cancelled; break;
134 127
                 case 'h':
135 128
                 case 'H': output << round(selection.h); break;
136 129
                 case 'g':
@@ -247,10 +240,6 @@ int app( int argc, char** argv ) {
247 240
     options.parse_positional("positional");
248 241
     options.parse(argc, argv);
249 242
     // Options just validates all of our input from argv
250
-    bool quiet = false;
251
-    if ( options.count( "quiet" ) > 0 ) {
252
-        quiet = options["quiet"].as<bool>();
253
-    }
254 243
     auto& positional = options["positional"].as<std::vector<std::string>>();
255 244
     if ( positional.size() > 0 ) {
256 245
         throw new std::invalid_argument("Unexpected positional argument: " + positional[0]);
@@ -276,33 +265,32 @@ int app( int argc, char** argv ) {
276 265
 
277 266
     // We want to validate our format option if we got one, we do that by just doing a dry run
278 267
     // on a fake selection.
279
-    SlopSelection selection(0,0,0,0,0);
268
+    SlopSelection selection(0,0,0,0,0,true);
280 269
     std::string format;
281 270
     bool gotFormat = options.count( "format" ) > 0;
282 271
     if ( gotFormat ) {
283 272
         format = options["format"].as<std::string>();
284
-        formatOutput( format, selection, false );
273
+        formatOutput( format, selection );
285 274
     }
286 275
 
287 276
     // Finally we do the real selection.
288
-    bool cancelled = false;
289
-    selection = SlopSelect(parsedOptions, &cancelled, quiet);
277
+    selection = SlopSelect(parsedOptions);
290 278
     
291 279
     // Here we're done with the parsed option data.
292 280
     delete parsedOptions;
293 281
     // We know if we cancelled or not
294
-    if ( cancelled ) {
295
-        if ( !quiet ) {
282
+    if ( selection.cancelled ) {
283
+        if ( !parsedOptions->quiet ) {
296 284
             std::cerr << "Selection was cancelled by keystroke or right-click.\n";
297 285
         }
298 286
         return 1;
299 287
     }
300 288
     // If we recieved a format option, we output the specified output.
301 289
     if ( gotFormat ) {
302
-        std::cout << formatOutput( format, selection, cancelled );
290
+        std::cout << formatOutput( format, selection );
303 291
         return 0;
304 292
     }
305
-    std::cout << formatOutput( "%g\n", selection, cancelled );
293
+    std::cout << formatOutput( "%g\n", selection );
306 294
     return 0;
307 295
 }
308 296
 

+ 52
- 32
src/slop.cpp Целия файл

@@ -3,6 +3,8 @@
3 3
 
4 4
 #include <chrono>
5 5
 #include <thread>
6
+#include <string>
7
+#include <vector>
6 8
 
7 9
 #include <stdlib.h>
8 10
 
@@ -25,8 +27,8 @@ Mouse* mouse;
25 27
 Keyboard* keyboard;
26 28
 Resource* resource;
27 29
 
28
-SlopSelection GLSlopSelect( slop::SlopOptions* options, bool* cancelled, slop::SlopWindow* window );
29
-SlopSelection XShapeSlopSelect( slop::SlopOptions* options, bool* cancelled);
30
+SlopSelection GLSlopSelect( slop::SlopOptions* options, slop::SlopWindow* window );
31
+SlopSelection XShapeSlopSelect( slop::SlopOptions* options );
30 32
 
31 33
 static int TmpXError(Display * d, XErrorEvent * ev) {
32 34
     return 0;
@@ -34,40 +36,63 @@ static int TmpXError(Display * d, XErrorEvent * ev) {
34 36
 
35 37
 }
36 38
 
39
+template<typename Out>
40
+static void split(const std::string &s, char delim, Out result) {
41
+    std::stringstream ss;
42
+    ss.str(s);
43
+    std::string item;
44
+    while (std::getline(ss, item, delim)) {
45
+        *(result++) = item;
46
+    }
47
+}
48
+
49
+static std::vector<std::string> split(const std::string &s, char delim) {
50
+    std::vector<std::string> elems;
51
+    split(s, delim, std::back_inserter(elems));
52
+    return elems;
53
+}
54
+
55
+
37 56
 using namespace slop;
38 57
 
58
+char slop_default_xdisplay[] = ":0";
59
+char slop_default_shaders[] = "textured";
60
+
39 61
 // Defaults!
40 62
 slop::SlopOptions::SlopOptions() {
41
-    borderSize = 1;
63
+    border = 1;
42 64
     nokeyboard = false;
43 65
     noopengl = false;
44 66
     nodecorations = false;
45 67
     tolerance = 2;
46 68
     padding = 0;
47
-    shaders.push_back("textured");
69
+    shaders = slop_default_shaders;
48 70
     highlight = false;
49 71
     r = 0.5;
50 72
     g = 0.5;
51 73
     b = 0.5;
52 74
     a = 1;
75
+    quiet = false;
53 76
 
54
-    char * envdisplay = getenv("DISPLAY");
55
-    if (envdisplay == NULL)
56
-        xdisplay = ":0";
57
-    else
77
+    char* envdisplay = getenv("DISPLAY");
78
+    if (envdisplay == NULL) {
79
+        xdisplay = slop_default_xdisplay;
80
+    } else {
58 81
         xdisplay = envdisplay;
82
+    }
59 83
 }
60 84
 
61
-slop::SlopSelection::SlopSelection( float x, float y, float w, float h, int id ) {
85
+slop::SlopSelection::SlopSelection( float x, float y, float w, float h, int id, bool cancelled ) {
62 86
     this->x = x;
63 87
     this->y = y;
64 88
     this->w = w;
65 89
     this->h = h;
66 90
     this->id = id;
91
+    this->cancelled = cancelled;
67 92
 }
68 93
 
69
-slop::SlopSelection slop::SlopSelect( slop::SlopOptions* options, bool* cancelled, bool quiet) {
70
-    slop::SlopSelection returnval(0,0,0,0,0);
94
+slop::SlopSelection slop::SlopSelect( slop::SlopOptions* options ) {
95
+    slop::SlopSelection returnval(0,0,0,0,0,true);
71 96
     bool deleteOptions = false;
72 97
     if ( !options ) {
73 98
         deleteOptions = true;
@@ -105,7 +130,7 @@ slop::SlopSelection slop::SlopSelect( slop::SlopOptions* options, bool* cancelle
105 130
     }
106 131
     if ( !success ) {
107 132
         // If we fail, we launch the XShape version of slop.
108
-        if ( !quiet && !options->noopengl ) {
133
+        if ( !options->quiet && !options->noopengl ) {
109 134
             if ( errorstring.length() <= 0 ) {
110 135
                 errorstring += "Failed to launch OpenGL context, --shader parameter will be ignored.\n";
111 136
                 std::cerr << errorstring;
@@ -113,9 +138,9 @@ slop::SlopSelection slop::SlopSelect( slop::SlopOptions* options, bool* cancelle
113 138
                 std::cerr << errorstring;
114 139
             }
115 140
         }
116
-        returnval = slop::XShapeSlopSelect( options, cancelled );
141
+        returnval = slop::XShapeSlopSelect( options );
117 142
     } else {
118
-        returnval = slop::GLSlopSelect( options, cancelled, window );
143
+        returnval = slop::GLSlopSelect( options, window );
119 144
     }
120 145
     delete x11;
121 146
     delete slop::resource;
@@ -125,9 +150,10 @@ slop::SlopSelection slop::SlopSelect( slop::SlopOptions* options, bool* cancelle
125 150
     return returnval;
126 151
 }
127 152
 
128
-slop::SlopSelection slop::XShapeSlopSelect( slop::SlopOptions* options, bool* cancelled ) {
153
+slop::SlopSelection slop::XShapeSlopSelect( slop::SlopOptions* options ) {
129 154
     // Init our little state machine, memory is a tad of a misnomer
130
-    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) );
155
+    bool cancelled = false;
156
+    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) );
131 157
     slop::mouse = new slop::Mouse( x11, options->nodecorations, ((XShapeRectangle*)memory->rectangle)->window );
132 158
 
133 159
     // We have no GL context, so the matrix is useless...
@@ -156,11 +182,7 @@ slop::SlopSelection slop::XShapeSlopSelect( slop::SlopOptions* options, bool* ca
156 182
         // Then we draw the framebuffer to the screen
157 183
         if ( (!options->nokeyboard && slop::keyboard->anyKeyDown()) || slop::mouse->getButton( 3 ) ) {
158 184
             memory->running = false;
159
-            if ( cancelled ) {
160
-                *cancelled = true;
161
-            }
162
-        } else {
163
-            *cancelled = false;
185
+            cancelled = true;
164 186
         }
165 187
     }
166 188
 
@@ -184,18 +206,20 @@ slop::SlopSelection slop::XShapeSlopSelect( slop::SlopOptions* options, bool* ca
184 206
         tries++;
185 207
     }
186 208
     // Finally return the data.
187
-    return slop::SlopSelection( output.x, output.y, output.z, output.w, selectedWindow );
209
+    return slop::SlopSelection( output.x, output.y, output.z, output.w, selectedWindow, cancelled );
188 210
 }
189 211
 
190
-slop::SlopSelection slop::GLSlopSelect( slop::SlopOptions* options, bool* cancelled, SlopWindow* window ) {
212
+slop::SlopSelection slop::GLSlopSelect( slop::SlopOptions* options, SlopWindow* window ) {
213
+    bool cancelled = false;
191 214
     slop::mouse = new slop::Mouse( x11, options->nodecorations, window->window );
192 215
 
193 216
     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";
194 217
     std::string frag = "#version 120\nuniform sampler2D texture;\nvarying vec2 uvCoord;\nvoid main()\n {\ngl_FragColor = texture2D( texture, uvCoord );\n}\n";
195 218
     slop::Shader* textured = new slop::Shader( vert, frag, false );
196 219
     std::vector<slop::Shader*> shaders;
197
-    for( int i=0;i<options->shaders.size();i++ ) {
198
-        std::string sn = options->shaders[i];
220
+    std::vector<std::string> stringShaders = split( options->shaders, ',' );
221
+    for( int i=0;i<stringShaders.size();i++ ) {
222
+        std::string sn = stringShaders[i];
199 223
         if ( sn != "textured" ) {
200 224
             shaders.push_back( new slop::Shader( sn + ".vert", sn + ".frag" ) );
201 225
         } else {
@@ -203,7 +227,7 @@ slop::SlopSelection slop::GLSlopSelect( slop::SlopOptions* options, bool* cancel
203 227
         }
204 228
     }
205 229
     // Init our little state machine, memory is a tad of a misnomer
206
-    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) );
230
+    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) );
207 231
 
208 232
     slop::Framebuffer* pingpong = new slop::Framebuffer(WidthOfScreen(x11->screen), HeightOfScreen(x11->screen));
209 233
 
@@ -282,11 +306,7 @@ slop::SlopSelection slop::GLSlopSelect( slop::SlopOptions* options, bool* cancel
282 306
         }
283 307
         if ( (!options->nokeyboard && slop::keyboard->anyKeyDown()) || slop::mouse->getButton( 3 ) ) {
284 308
             memory->running = false;
285
-            if ( cancelled ) {
286
-                *cancelled = true;
287
-            }
288
-        } else {
289
-            *cancelled = false;
309
+            cancelled = true;
290 310
         }
291 311
     }
292 312
 
@@ -320,5 +340,5 @@ slop::SlopSelection slop::GLSlopSelect( slop::SlopOptions* options, bool* cancel
320 340
         tries++;
321 341
     }
322 342
     // Finally return the data.
323
-    return slop::SlopSelection( output.x, output.y, output.z, output.w, selectedWindow );
343
+    return slop::SlopSelection( output.x, output.y, output.z, output.w, selectedWindow, cancelled );
324 344
 }

+ 7
- 8
src/slop.hpp Целия файл

@@ -21,32 +21,31 @@
21 21
 #ifndef N_SLOP_H_
22 22
 #define N_SLOP_H_
23 23
 
24
-#include <string>
25
-#include <vector>
26
-
27 24
 namespace slop {
28 25
 
29 26
 class SlopOptions {
30 27
 public:
31 28
     SlopOptions();
32
-    float borderSize;
29
+    bool quiet;
30
+    float border;
33 31
     float padding;
34 32
     float tolerance;
35 33
     bool highlight;
36 34
     bool noopengl;
37 35
     bool nokeyboard;
38 36
     int nodecorations;
39
-    std::vector<std::string> shaders;
37
+    char* shaders;
40 38
     float r;
41 39
     float g;
42 40
     float b;
43 41
     float a;
44
-    std::string xdisplay;
42
+    char* xdisplay;
45 43
 };
46 44
 
47 45
 class SlopSelection {
48 46
 public:
49
-    SlopSelection( float x, float y, float w, float h, int id );
47
+    SlopSelection( float x, float y, float w, float h, int id, bool cancelled );
48
+    bool cancelled;
50 49
     float x;
51 50
     float y;
52 51
     float w;
@@ -55,7 +54,7 @@ public:
55 54
     int id;
56 55
 };
57 56
 
58
-SlopSelection SlopSelect( SlopOptions* options = NULL, bool* cancelled = NULL, bool quiet = false );
57
+SlopSelection SlopSelect( SlopOptions* options = NULL );
59 58
 
60 59
 }
61 60