Parcourir la source

Workaround lack of clock_gettime in OSX

Rafael Bodill il y a 9 ans
Parent
révision
09109c7fc7
1 fichiers modifiés avec 25 ajouts et 3 suppressions
  1. 25
    3
      src/main.cpp

+ 25
- 3
src/main.cpp Voir le fichier

22
 #include <cstdio>
22
 #include <cstdio>
23
 #include <sstream>
23
 #include <sstream>
24
 
24
 
25
+#ifdef __MACH__
26
+#include <mach/clock.h>
27
+#include <mach/mach.h>
28
+#endif
29
+
25
 #include "x.hpp"
30
 #include "x.hpp"
26
 #include "rectangle.hpp"
31
 #include "rectangle.hpp"
27
 #include "cmdline.h"
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
 int printSelection( std::string format, bool cancelled, int x, int y, int w, int h, int window ) {
51
 int printSelection( std::string format, bool cancelled, int x, int y, int w, int h, int window ) {
30
     size_t pos = 0;
52
     size_t pos = 0;
31
     while ( ( pos = format.find( "%", pos ) ) != std::string::npos ) {
53
     while ( ( pos = format.find( "%", pos ) ) != std::string::npos ) {
223
     bool highlight = options.highlight_flag;
245
     bool highlight = options.highlight_flag;
224
     bool keyboard = !options.nokeyboard_flag;
246
     bool keyboard = !options.nokeyboard_flag;
225
     bool decorations = !options.nodecorations_flag;
247
     bool decorations = !options.nodecorations_flag;
226
-    timespec start, time;
248
+    struct timespec start, time;
227
     int xoffset = 0;
249
     int xoffset = 0;
228
     int yoffset = 0;
250
     int yoffset = 0;
229
     int cx = 0;
251
     int cx = 0;
266
             fprintf( stderr, "Warning: Failed to grab the keyboard. This is non-fatal, keyboard presses might fall through to other applications.\n" );
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
     while ( running ) {
292
     while ( running ) {
271
-        clock_gettime( CLOCK_REALTIME, &time );
293
+        current_utc_time( &time );
272
         // "ticking" the xengine makes it process all queued events.
294
         // "ticking" the xengine makes it process all queued events.
273
         xengine->tick();
295
         xengine->tick();
274
         // If the user presses any key on the keyboard, exit the application.
296
         // If the user presses any key on the keyboard, exit the application.