Browse Source

changing the interface to support pure C

naelstrof 7 years ago
parent
commit
93273d564f
3 changed files with 82 additions and 75 deletions
  1. 23
    35
      src/main.cpp
  2. 52
    32
      src/slop.cpp
  3. 7
    8
      src/slop.hpp

+ 23
- 35
src/main.cpp View File

29
 
29
 
30
 using namespace slop;
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
 glm::vec4 parseColor( std::string value ) {
32
 glm::vec4 parseColor( std::string value ) {
49
     std::string valuecopy = value;
33
     std::string valuecopy = value;
50
     glm::vec4 found;
34
     glm::vec4 found;
73
 SlopOptions* getOptions( cxxopts::Options& options ) {
57
 SlopOptions* getOptions( cxxopts::Options& options ) {
74
     slop::SlopOptions* foo = new slop::SlopOptions();
58
     slop::SlopOptions* foo = new slop::SlopOptions();
75
     if ( options.count( "bordersize" ) > 0 ) {
59
     if ( options.count( "bordersize" ) > 0 ) {
76
-        foo->borderSize = options["bordersize"].as<float>();
60
+        foo->border = options["bordersize"].as<float>();
77
     }
61
     }
78
     if ( options.count( "padding" ) > 0 ) {
62
     if ( options.count( "padding" ) > 0 ) {
79
         foo->padding = options["padding"].as<float>();
63
         foo->padding = options["padding"].as<float>();
93
         foo->nokeyboard = options["nokeyboard"].as<bool>();
77
         foo->nokeyboard = options["nokeyboard"].as<bool>();
94
     }
78
     }
95
     if ( options.count( "xdisplay" ) > 0 ) {
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
     if ( options.count( "shader" ) > 0 ) {
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
     if ( options.count( "noopengl" ) > 0 ) {
93
     if ( options.count( "noopengl" ) > 0 ) {
104
         foo->noopengl = options["noopengl"].as<bool>();
94
         foo->noopengl = options["noopengl"].as<bool>();
105
     }
95
     }
106
     if ( options.count( "highlight" ) > 0 ) {
96
     if ( options.count( "highlight" ) > 0 ) {
107
         foo->highlight = options["highlight"].as<bool>();
97
         foo->highlight = options["highlight"].as<bool>();
108
     }
98
     }
99
+    if ( options.count( "quiet" ) > 0 ) {
100
+        foo->quiet = options["quiet"].as<bool>();
101
+    }
109
     if ( options.count( "nodecorations" ) > 0 ) {
102
     if ( options.count( "nodecorations" ) > 0 ) {
110
         foo->nodecorations = options["nodecorations"].as<int>();
103
         foo->nodecorations = options["nodecorations"].as<int>();
111
         if ( foo->nodecorations < 0 || foo->nodecorations > 2 ) {
104
         if ( foo->nodecorations < 0 || foo->nodecorations > 2 ) {
115
     return foo;
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
     std::stringstream output;
112
     std::stringstream output;
120
     for( unsigned int i=0;i<input.length();i++) {
113
     for( unsigned int i=0;i<input.length();i++) {
121
         if ( input[i] == '%' ) {
114
         if ( input[i] == '%' ) {
130
                 case 'w':
123
                 case 'w':
131
                 case 'W': output << round(selection.w); break;
124
                 case 'W': output << round(selection.w); break;
132
                 case 'c':
125
                 case 'c':
133
-                case 'C': output << cancelled; break;
126
+                case 'C': output << selection.cancelled; break;
134
                 case 'h':
127
                 case 'h':
135
                 case 'H': output << round(selection.h); break;
128
                 case 'H': output << round(selection.h); break;
136
                 case 'g':
129
                 case 'g':
247
     options.parse_positional("positional");
240
     options.parse_positional("positional");
248
     options.parse(argc, argv);
241
     options.parse(argc, argv);
249
     // Options just validates all of our input from argv
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
     auto& positional = options["positional"].as<std::vector<std::string>>();
243
     auto& positional = options["positional"].as<std::vector<std::string>>();
255
     if ( positional.size() > 0 ) {
244
     if ( positional.size() > 0 ) {
256
         throw new std::invalid_argument("Unexpected positional argument: " + positional[0]);
245
         throw new std::invalid_argument("Unexpected positional argument: " + positional[0]);
276
 
265
 
277
     // We want to validate our format option if we got one, we do that by just doing a dry run
266
     // We want to validate our format option if we got one, we do that by just doing a dry run
278
     // on a fake selection.
267
     // on a fake selection.
279
-    SlopSelection selection(0,0,0,0,0);
268
+    SlopSelection selection(0,0,0,0,0,true);
280
     std::string format;
269
     std::string format;
281
     bool gotFormat = options.count( "format" ) > 0;
270
     bool gotFormat = options.count( "format" ) > 0;
282
     if ( gotFormat ) {
271
     if ( gotFormat ) {
283
         format = options["format"].as<std::string>();
272
         format = options["format"].as<std::string>();
284
-        formatOutput( format, selection, false );
273
+        formatOutput( format, selection );
285
     }
274
     }
286
 
275
 
287
     // Finally we do the real selection.
276
     // Finally we do the real selection.
288
-    bool cancelled = false;
289
-    selection = SlopSelect(parsedOptions, &cancelled, quiet);
277
+    selection = SlopSelect(parsedOptions);
290
     
278
     
291
     // Here we're done with the parsed option data.
279
     // Here we're done with the parsed option data.
292
     delete parsedOptions;
280
     delete parsedOptions;
293
     // We know if we cancelled or not
281
     // We know if we cancelled or not
294
-    if ( cancelled ) {
295
-        if ( !quiet ) {
282
+    if ( selection.cancelled ) {
283
+        if ( !parsedOptions->quiet ) {
296
             std::cerr << "Selection was cancelled by keystroke or right-click.\n";
284
             std::cerr << "Selection was cancelled by keystroke or right-click.\n";
297
         }
285
         }
298
         return 1;
286
         return 1;
299
     }
287
     }
300
     // If we recieved a format option, we output the specified output.
288
     // If we recieved a format option, we output the specified output.
301
     if ( gotFormat ) {
289
     if ( gotFormat ) {
302
-        std::cout << formatOutput( format, selection, cancelled );
290
+        std::cout << formatOutput( format, selection );
303
         return 0;
291
         return 0;
304
     }
292
     }
305
-    std::cout << formatOutput( "%g\n", selection, cancelled );
293
+    std::cout << formatOutput( "%g\n", selection );
306
     return 0;
294
     return 0;
307
 }
295
 }
308
 
296
 

+ 52
- 32
src/slop.cpp View File

3
 
3
 
4
 #include <chrono>
4
 #include <chrono>
5
 #include <thread>
5
 #include <thread>
6
+#include <string>
7
+#include <vector>
6
 
8
 
7
 #include <stdlib.h>
9
 #include <stdlib.h>
8
 
10
 
25
 Keyboard* keyboard;
27
 Keyboard* keyboard;
26
 Resource* resource;
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
 static int TmpXError(Display * d, XErrorEvent * ev) {
33
 static int TmpXError(Display * d, XErrorEvent * ev) {
32
     return 0;
34
     return 0;
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
 using namespace slop;
56
 using namespace slop;
38
 
57
 
58
+char slop_default_xdisplay[] = ":0";
59
+char slop_default_shaders[] = "textured";
60
+
39
 // Defaults!
61
 // Defaults!
40
 slop::SlopOptions::SlopOptions() {
62
 slop::SlopOptions::SlopOptions() {
41
-    borderSize = 1;
63
+    border = 1;
42
     nokeyboard = false;
64
     nokeyboard = false;
43
     noopengl = false;
65
     noopengl = false;
44
     nodecorations = false;
66
     nodecorations = false;
45
     tolerance = 2;
67
     tolerance = 2;
46
     padding = 0;
68
     padding = 0;
47
-    shaders.push_back("textured");
69
+    shaders = slop_default_shaders;
48
     highlight = false;
70
     highlight = false;
49
     r = 0.5;
71
     r = 0.5;
50
     g = 0.5;
72
     g = 0.5;
51
     b = 0.5;
73
     b = 0.5;
52
     a = 1;
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
         xdisplay = envdisplay;
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
     this->x = x;
86
     this->x = x;
63
     this->y = y;
87
     this->y = y;
64
     this->w = w;
88
     this->w = w;
65
     this->h = h;
89
     this->h = h;
66
     this->id = id;
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
     bool deleteOptions = false;
96
     bool deleteOptions = false;
72
     if ( !options ) {
97
     if ( !options ) {
73
         deleteOptions = true;
98
         deleteOptions = true;
105
     }
130
     }
106
     if ( !success ) {
131
     if ( !success ) {
107
         // If we fail, we launch the XShape version of slop.
132
         // If we fail, we launch the XShape version of slop.
108
-        if ( !quiet && !options->noopengl ) {
133
+        if ( !options->quiet && !options->noopengl ) {
109
             if ( errorstring.length() <= 0 ) {
134
             if ( errorstring.length() <= 0 ) {
110
                 errorstring += "Failed to launch OpenGL context, --shader parameter will be ignored.\n";
135
                 errorstring += "Failed to launch OpenGL context, --shader parameter will be ignored.\n";
111
                 std::cerr << errorstring;
136
                 std::cerr << errorstring;
113
                 std::cerr << errorstring;
138
                 std::cerr << errorstring;
114
             }
139
             }
115
         }
140
         }
116
-        returnval = slop::XShapeSlopSelect( options, cancelled );
141
+        returnval = slop::XShapeSlopSelect( options );
117
     } else {
142
     } else {
118
-        returnval = slop::GLSlopSelect( options, cancelled, window );
143
+        returnval = slop::GLSlopSelect( options, window );
119
     }
144
     }
120
     delete x11;
145
     delete x11;
121
     delete slop::resource;
146
     delete slop::resource;
125
     return returnval;
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
     // Init our little state machine, memory is a tad of a misnomer
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
     slop::mouse = new slop::Mouse( x11, options->nodecorations, ((XShapeRectangle*)memory->rectangle)->window );
157
     slop::mouse = new slop::Mouse( x11, options->nodecorations, ((XShapeRectangle*)memory->rectangle)->window );
132
 
158
 
133
     // We have no GL context, so the matrix is useless...
159
     // We have no GL context, so the matrix is useless...
156
         // Then we draw the framebuffer to the screen
182
         // Then we draw the framebuffer to the screen
157
         if ( (!options->nokeyboard && slop::keyboard->anyKeyDown()) || slop::mouse->getButton( 3 ) ) {
183
         if ( (!options->nokeyboard && slop::keyboard->anyKeyDown()) || slop::mouse->getButton( 3 ) ) {
158
             memory->running = false;
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
         tries++;
206
         tries++;
185
     }
207
     }
186
     // Finally return the data.
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
     slop::mouse = new slop::Mouse( x11, options->nodecorations, window->window );
214
     slop::mouse = new slop::Mouse( x11, options->nodecorations, window->window );
192
 
215
 
193
     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";
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
     std::string frag = "#version 120\nuniform sampler2D texture;\nvarying vec2 uvCoord;\nvoid main()\n {\ngl_FragColor = texture2D( texture, uvCoord );\n}\n";
217
     std::string frag = "#version 120\nuniform sampler2D texture;\nvarying vec2 uvCoord;\nvoid main()\n {\ngl_FragColor = texture2D( texture, uvCoord );\n}\n";
195
     slop::Shader* textured = new slop::Shader( vert, frag, false );
218
     slop::Shader* textured = new slop::Shader( vert, frag, false );
196
     std::vector<slop::Shader*> shaders;
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
         if ( sn != "textured" ) {
223
         if ( sn != "textured" ) {
200
             shaders.push_back( new slop::Shader( sn + ".vert", sn + ".frag" ) );
224
             shaders.push_back( new slop::Shader( sn + ".vert", sn + ".frag" ) );
201
         } else {
225
         } else {
203
         }
227
         }
204
     }
228
     }
205
     // Init our little state machine, memory is a tad of a misnomer
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
     slop::Framebuffer* pingpong = new slop::Framebuffer(WidthOfScreen(x11->screen), HeightOfScreen(x11->screen));
232
     slop::Framebuffer* pingpong = new slop::Framebuffer(WidthOfScreen(x11->screen), HeightOfScreen(x11->screen));
209
 
233
 
282
         }
306
         }
283
         if ( (!options->nokeyboard && slop::keyboard->anyKeyDown()) || slop::mouse->getButton( 3 ) ) {
307
         if ( (!options->nokeyboard && slop::keyboard->anyKeyDown()) || slop::mouse->getButton( 3 ) ) {
284
             memory->running = false;
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
         tries++;
340
         tries++;
321
     }
341
     }
322
     // Finally return the data.
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 View File

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