Bladeren bron

Merge pull request #24 from rafi/feature/osx-support

Workaround lack of `clock_gettime` in OSX
Dalton Nell 9 jaren geleden
bovenliggende
commit
0a3d644a52
1 gewijzigde bestanden met toevoegingen van 25 en 3 verwijderingen
  1. 25
    3
      src/main.cpp

+ 25
- 3
src/main.cpp Bestand weergeven

@@ -22,10 +22,32 @@
22 22
 #include <cstdio>
23 23
 #include <sstream>
24 24
 
25
+#ifdef __MACH__
26
+#include <mach/clock.h>
27
+#include <mach/mach.h>
28
+#endif
29
+
25 30
 #include "x.hpp"
26 31
 #include "rectangle.hpp"
27 32
 #include "cmdline.h"
28 33
 
34
+// Work around lack of clock_gettime in OSX
35
+// https://gist.github.com/jbenet/1087739
36
+void current_utc_time(struct timespec *ts) {
37
+    #ifdef __MACH__
38
+        // OS X does not have clock_gettime, use clock_get_time
39
+        clock_serv_t cclock;
40
+        mach_timespec_t mts;
41
+        host_get_clock_service( mach_host_self(), CALENDAR_CLOCK, &cclock );
42
+        clock_get_time( cclock, &mts );
43
+        mach_port_deallocate( mach_task_self(), cclock );
44
+        ts->tv_sec = mts.tv_sec;
45
+        ts->tv_nsec = mts.tv_nsec;
46
+    #else
47
+        clock_gettime( CLOCK_REALTIME, ts );
48
+    #endif
49
+}
50
+
29 51
 int printSelection( std::string format, bool cancelled, int x, int y, int w, int h, int window ) {
30 52
     size_t pos = 0;
31 53
     while ( ( pos = format.find( "%", pos ) ) != std::string::npos ) {
@@ -223,7 +245,7 @@ int app( int argc, char** argv ) {
223 245
     bool highlight = options.highlight_flag;
224 246
     bool keyboard = !options.nokeyboard_flag;
225 247
     bool decorations = !options.nodecorations_flag;
226
-    timespec start, time;
248
+    struct timespec start, time;
227 249
     int xoffset = 0;
228 250
     int yoffset = 0;
229 251
     int cx = 0;
@@ -266,9 +288,9 @@ int app( int argc, char** argv ) {
266 288
             fprintf( stderr, "Warning: Failed to grab the keyboard. This is non-fatal, keyboard presses might fall through to other applications.\n" );
267 289
         }
268 290
     }
269
-    clock_gettime( CLOCK_REALTIME, &start );
291
+    current_utc_time( &start );
270 292
     while ( running ) {
271
-        clock_gettime( CLOCK_REALTIME, &time );
293
+        current_utc_time( &time );
272 294
         // "ticking" the xengine makes it process all queued events.
273 295
         xengine->tick();
274 296
         // If the user presses any key on the keyboard, exit the application.