From edcc540cb7bfb7eddb5ad345701dc78a8cd5f7f8 Mon Sep 17 00:00:00 2001
From: Takamichi Horikawa <takamichiho@gmail.com>
Date: Sun, 10 Sep 2017 16:04:53 +0900
Subject: fmdsp: add osx support

---
 fmdsp/fmdsp_platform_mach.c | 51 +++++++++++++++++++++++++++++++++++++++++++++
 sdl/osx/Makefile            |  2 +-
 2 files changed, 52 insertions(+), 1 deletion(-)
 create mode 100644 fmdsp/fmdsp_platform_mach.c

diff --git a/fmdsp/fmdsp_platform_mach.c b/fmdsp/fmdsp_platform_mach.c
new file mode 100644
index 0000000..96f64eb
--- /dev/null
+++ b/fmdsp/fmdsp_platform_mach.c
@@ -0,0 +1,51 @@
+#include "fmdsp_platform_info.h"
+#include <mach/mach.h>
+#include <mach/clock.h>
+#include <sys/times.h>
+#include <stdbool.h>
+#include <limits.h>
+
+static struct {
+  clock_t lastall;
+  clock_t lastcpu;
+  mach_timespec_t lasttimespec;
+  bool clock_initialized;
+  clock_serv_t clock;
+} g;
+
+int fmdsp_cpu_usage(void) {
+  struct tms tmsbuf;
+  clock_t all = times(&tmsbuf);
+  clock_t cpu = tmsbuf.tms_utime + tmsbuf.tms_stime;
+  clock_t percentage = 0;
+  clock_t alld = all - g.lastall;
+  clock_t cpud = cpu - g.lastcpu;
+  if (alld) percentage = cpud * 100 / alld;
+  g.lastall = all;
+  g.lastcpu = cpu;
+  if (!g.lastall) percentage = 0;
+  if (percentage > INT_MAX) percentage = INT_MAX;
+  if (percentage < 0) percentage = 0;
+  return percentage;
+}
+
+int fmdsp_fps_30(void) {
+  if (!g.clock_initialized) {
+    host_get_clock_service(mach_host_self(), SYSTEM_CLOCK, &g.clock);
+    g.clock_initialized = true;
+  }
+  mach_timespec_t time;
+  clock_get_time(g.clock, &time);
+  uint64_t fps = 0;
+  if (g.lasttimespec.tv_sec || g.lasttimespec.tv_nsec) {
+    uint64_t diffns = time.tv_sec - g.lasttimespec.tv_sec;
+    diffns *= 1000000000ull;
+    diffns += time.tv_nsec - g.lasttimespec.tv_nsec;
+    if (diffns) {
+      fps = 30ull * 1000000000ull / diffns;
+    }
+  }
+  g.lasttimespec = time;
+  if (fps > INT_MAX) fps = INT_MAX;
+  return fps;
+}
diff --git a/sdl/osx/Makefile b/sdl/osx/Makefile
index 73bea70..c2035fd 100644
--- a/sdl/osx/Makefile
+++ b/sdl/osx/Makefile
@@ -8,7 +8,7 @@ XCRUN:=xcrun --sdk /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.p
 CC:=$(XCRUN) cc
 OBJS:=main.o
 OBJS+=pacc-gl.o
-OBJS+=fmdsp-pacc.o font_fmdsp_small.o fmdsp_platform_unix.o
+OBJS+=fmdsp-pacc.o font_fmdsp_small.o fmdsp_platform_mach.o
 OBJS+=opna.o opnafm.o opnassg.o opnadrum.o opnaadpcm.o opnatimer.o opnassg-sinc-c.o opnassg-sinc-sse2.o
 OBJS+=fmdriver_pmd.o fmdriver_fmp.o ppz8.o fmdriver_common.o
 OBJS+=fmplayer_file.o fmplayer_work_opna.o fmplayer_file_unix.o fmplayer_drumrom_unix.o
-- 
cgit v1.2.3