summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorStanislaw Halik <sthalik@misaki.pl>2019-01-07 13:15:30 +0100
committerStanislaw Halik <sthalik@misaki.pl>2019-01-16 07:48:47 +0100
commitb3da5c6698d4ec39d62062d23f1da79f9c359cdf (patch)
tree8b74effb405c964653e47a773fb33f7c08c070d0
parentfd3eb4515639e000b899544827b5fd4ff9473937 (diff)
Revert "compat/shm, proto/wine: remove duplication"
This reverts commit bab093ebbe392927a92ef201fe60344d5c1191dd.
-rw-r--r--compat/shm.c150
-rw-r--r--compat/shm.cpp128
-rw-r--r--compat/shm.h87
-rw-r--r--compat/shm.hpp34
-rw-r--r--proto-ft/ftnoir_protocol_ft.h8
-rw-r--r--proto-wine/ftnoir_protocol_wine.h4
-rw-r--r--proto-wine/opentrack-wrapper-wine-main.cxx114
-rw-r--r--proto-wine/opentrack-wrapper-wine-posix.cxx9
-rw-r--r--proto-wine/opentrack-wrapper-wine-windows.cxx10
9 files changed, 216 insertions, 328 deletions
diff --git a/compat/shm.c b/compat/shm.c
deleted file mode 100644
index 95263a08..00000000
--- a/compat/shm.c
+++ /dev/null
@@ -1,150 +0,0 @@
-#define BUILD_SHM
-#include "shm.h"
-
-#ifdef SHM_WIN32
-
-#include <windows.h>
-
-SHM_FUN(void, init, const char* shm_name, const char* mutex_name, int map_size)
-{
- if (mutex_name != NULL)
- {
- self->mutex = CreateMutexA(NULL, false, mutex_name);
-
- if (!self->mutex)
- goto fail;
- }
-
- self->mapped_file = CreateFileMappingA(
- INVALID_HANDLE_VALUE,
- NULL,
- PAGE_READWRITE,
- 0,
- (unsigned)map_size,
- shm_name);
-
- if (!self->mapped_file)
- goto fail;
-
- self->mem = MapViewOfFile(self->mapped_file,
- FILE_MAP_WRITE,
- 0,
- 0,
- (unsigned) map_size);
-
- if (!self->mem)
- goto fail;
-
- return;
-
-fail:
- SHM_FUN_NAME(free)(self);
-}
-
-SHM_FUN0(void, free)
-{
- if (self->mem)
- (void) UnmapViewOfFile(self->mem);
-
- if (self->mapped_file)
- (void) CloseHandle(self->mapped_file);
-
- if (self->mutex)
- (void) CloseHandle(self->mutex);
-
- self->mem = NULL;
- self->mapped_file = NULL;
- self->mutex = NULL;
-}
-
-SHM_FUN0(void, lock)
-{
- if (self->mutex)
- (void)(WaitForSingleObject(self->mutex, INFINITE) == WAIT_OBJECT_0);
-}
-
-SHM_FUN0(void, unlock)
-{
- (void) ReleaseMutex(self->mutex);
-}
-
-SHM_FUN0(bool, success)
-{
- return self->mem != NULL;
-}
-
-#else
-
-#include <stdio.h>
-#include <string.h>
-#include <sys/file.h>
-#include <sys/mman.h>
-#include <fcntl.h>
-#include <limits.h>
-#include <unistd.h>
-#include <sys/types.h>
-#include <alloca.h>
-
-//#pragma GCC diagnostic ignored "-Wunused-result"
-
-SHM_FUN(void, init, const char *shm_name, const char* mutex_name, int map_size)
-{
- char* filename = alloca(strlen(shm_name)+2);
- (void)mutex_name;
-
- self->mem = (void*)-1;
- self->fd = -1;
- self->size = 0;
-
- if (map_size <= 0)
- goto fail;
-
- self->size = map_size;
- strcpy(filename, "/");
- strcat(filename, shm_name);
- self->fd = shm_open(filename, O_RDWR | O_CREAT, 0600);
- (void)ftruncate(self->fd, (off_t)map_size);
- self->mem = mmap(NULL, (size_t)map_size, PROT_READ|PROT_WRITE, MAP_SHARED, self->fd, (off_t)0);
-
- if (self->mem == (void*)-1)
- goto fail;
-
- return;
-
-fail:
- SHM_FUN_NAME(free)(self);
-}
-
-SHM_FUN0(void, free)
-{
- if (self->mem != (void*)-1)
- (void)munmap(self->mem, self->size);
- if (self->fd != -1)
- (void)close(self->fd);
-
- self->mem = (void*)-1;
- self->fd = -1;
- self->size = 0;
-}
-
-SHM_FUN0(void, lock)
-{
- flock(self->fd, LOCK_EX);
-}
-
-SHM_FUN0(void, unlock)
-{
- flock(self->fd, LOCK_UN);
-}
-
-SHM_FUN0(bool, success)
-{
- return self->mem != (void*) -1;
-}
-
-#endif
-
-SHM_FUN0(void*, ptr)
-{
- return self->mem;
-}
diff --git a/compat/shm.cpp b/compat/shm.cpp
index 265d8ff8..1f863190 100644
--- a/compat/shm.cpp
+++ b/compat/shm.cpp
@@ -5,37 +5,133 @@
* copyright notice and this permission notice appear in all copies.
*/
-#define BUILD_SHM
-#include "shm.hpp"
+#include "shm.h"
-SHMXX_TYPE_NAME& SHMXX_TYPE_NAME::operator=(SHMXX_TYPE_NAME&&) noexcept = default;
+#if defined _WIN32
-// NOLINTNEXTLINE(cppcoreguidelines-pro-type-member-init)
-SHMXX_TYPE_NAME::SHMXX_TYPE_NAME(const char* shm_name, const char* mutex_name, int map_size)
+#include <cstring>
+#include <cstdio>
+
+#include <accctrl.h>
+#include <aclapi.h>
+
+#ifdef QT_CORE_LIB
+# include <QDebug>
+# define warn(str, ...) (qDebug() << "shm:" str ": " << __VA_ARGS__)
+#else
+# define warn(str, ...) (void)0
+#endif
+
+shm_wrapper::shm_wrapper(const char* shm_name, const char* mutex_name, int map_size)
{
- SHM_FUN_NAME(init)(&impl, shm_name, mutex_name, map_size);
+ if (mutex_name == nullptr)
+ mutex = nullptr;
+ else
+ {
+ mutex = CreateMutexA(nullptr, false, mutex_name);
+
+ if (!mutex)
+ {
+ warn("CreateMutexA", (int) GetLastError());
+ return;
+ }
+ }
+
+ mapped_file = CreateFileMappingA(
+ INVALID_HANDLE_VALUE,
+ nullptr,
+ PAGE_READWRITE,
+ 0,
+ map_size,
+ shm_name);
+
+ if (!mapped_file)
+ {
+ warn("CreateFileMappingA", (int) GetLastError());
+
+ return;
+ }
+
+ mem = MapViewOfFile(mapped_file,
+ FILE_MAP_WRITE,
+ 0,
+ 0,
+ map_size);
+
+ if (!mem)
+ warn("MapViewOfFile:", (int) GetLastError());
}
-SHMXX_TYPE_NAME::~SHMXX_TYPE_NAME()
+shm_wrapper::~shm_wrapper()
{
- SHM_FUN_NAME(free)(&impl);
+ if (mem && !UnmapViewOfFile(mem))
+ goto fail;
+
+ if (mapped_file && !CloseHandle(mapped_file))
+ goto fail;
+
+ if (mutex && !CloseHandle(mutex))
+ goto fail;
+
+ return;
+
+fail:
+ warn("failed to close mapping", (int) GetLastError());
}
-bool SHMXX_TYPE_NAME::success() noexcept
+bool shm_wrapper::lock()
{
- return SHM_FUN_NAME(success)(&impl);
+ if (mutex)
+ return WaitForSingleObject(mutex, INFINITE) == WAIT_OBJECT_0;
+ else
+ return false;
}
-void SHMXX_TYPE_NAME::lock() noexcept
+
+bool shm_wrapper::unlock()
+{
+ if (mutex)
+ return ReleaseMutex(mutex);
+ else
+ return false;
+}
+#else
+
+#include <limits.h>
+
+#pragma GCC diagnostic ignored "-Wunused-result"
+shm_wrapper::shm_wrapper(const char *shm_name, const char* /*mutex_name*/, int map_size) : size(map_size)
+{
+ char filename[PATH_MAX+2] {};
+ strcpy(filename, "/");
+ strcat(filename, shm_name);
+ fd = shm_open(filename, O_RDWR | O_CREAT, 0600);
+ (void) ftruncate(fd, map_size);
+ mem = mmap(NULL, map_size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, (off_t)0);
+}
+
+shm_wrapper::~shm_wrapper()
+{
+ (void) munmap(mem, size);
+ (void) close(fd);
+}
+
+bool shm_wrapper::lock()
{
- SHM_FUN_NAME(lock)(&impl);
+ return flock(fd, LOCK_EX) == 0;
}
-void SHMXX_TYPE_NAME::unlock() noexcept
+bool shm_wrapper::unlock()
{
- SHM_FUN_NAME(unlock)(&impl);
+ return flock(fd, LOCK_UN) == 0;
}
+#endif
-void* SHMXX_TYPE_NAME::ptr() noexcept
+bool shm_wrapper::success()
{
- return SHM_FUN_NAME(ptr)(&impl);
+#ifndef _WIN32
+ return mem != (void*) -1;
+#else
+ return mem != nullptr;
+#endif
}
+
diff --git a/compat/shm.h b/compat/shm.h
index 856b9c8c..814ce90c 100644
--- a/compat/shm.h
+++ b/compat/shm.h
@@ -1,68 +1,41 @@
-#ifndef SHM_HEADER_GUARD
-#define SHM_HEADER_GUARD
+/* Copyright (c) 2013 Stanislaw Halik <sthalik@misaki.pl>
-#include "macros1.h"
+ * Permission to use, copy, modify, and/or distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ */
+#pragma once
-#ifndef SHM_WIN32_INIT
-# ifdef _WIN32
-# define SHM_WIN32
-# else
-# undef SHM_WIN32
-# endif
+#if defined(_WIN32)
+#include <windows.h>
#else
-# if SHM_WIN32_INIT
-# define SHM_WIN32
-# else
-# undef SHM_WIN32
-# endif
+#include <stdio.h>
+#include <string.h>
+#include <sys/file.h>
+#include <sys/mman.h>
+#include <fcntl.h>
+#include <limits.h>
+#include <unistd.h>
+#include <sys/types.h>
#endif
-#ifndef SHM_TYPE_NAME
-# define SHM_TYPE_NAME shm_mem_impl
-#endif
-
-#ifndef SHM_FUN_PREFIX
-# define SHM_FUN_PREFIX shm_mem_impl_
-#endif
-
-#ifndef SHM_EXPORT
-# define SHM_EXPORT
-#endif
-
-#ifndef __cplusplus
-# define SHM_EXTERN
-# include <stdbool.h>
-struct SHM_TYPE_NAME;
-typedef struct SHM_TYPE_NAME SHM_TYPE_NAME;
-#else
-# define SHM_EXTERN extern "C"
-#endif
+#include "macros.hpp"
+#include "export.hpp"
-struct SHM_TYPE_NAME {
+class OTR_COMPAT_EXPORT shm_wrapper final
+{
void* mem;
-#ifdef SHM_WIN32
- void* mutex;
- void* mapped_file;
+#if defined(_WIN32)
+ HANDLE mutex, mapped_file;
#else
int fd, size;
#endif
-};
-
-#define SHM_FUN_NAME(f) PP_CAT(SHM_FUN_PREFIX, f)
-#define SHM_FUN_(r, f)SHM_EXTERN SHM_EXPORT r SHM_FUN_NAME(f)
-#define SHM_FUN(r, f, ...) SHM_FUN_(r, f)(SHM_TYPE_NAME* self, __VA_ARGS__)
-#define SHM_FUN0(r, f) SHM_FUN_(r, f)(SHM_TYPE_NAME* self)
-
-SHM_FUN(void, init, const char* shm_name, const char* mutex_name, int map_size);
-SHM_FUN0(void, free);
-SHM_FUN0(void, lock);
-SHM_FUN0(void, unlock);
-SHM_FUN0(void*,ptr);
-SHM_FUN0(bool, success);
-#ifndef BUILD_SHM
-# undef SHM_FUN
-# undef SHM_FUN_NAME
-#endif
-
-#endif // SHM_HEADER_GUARD
+public:
+ cc_noinline shm_wrapper(const char *shm_name, const char *mutex_name, int map_size);
+ cc_noinline ~shm_wrapper();
+ cc_noinline bool lock();
+ cc_noinline bool unlock();
+ cc_noinline bool success();
+ inline void* ptr() { return mem; }
+};
diff --git a/compat/shm.hpp b/compat/shm.hpp
deleted file mode 100644
index 428764e4..00000000
--- a/compat/shm.hpp
+++ /dev/null
@@ -1,34 +0,0 @@
-/* Copyright (c) 2013 Stanislaw Halik <sthalik@misaki.pl>
-
- * Permission to use, copy, modify, and/or distribute this software for any
- * purpose with or without fee is hereby granted, provided that the above
- * copyright notice and this permission notice appear in all copies.
- */
-#ifndef SHMXX_HEADER_GUARD
-#define SHMXX_HEADER_GUARD
-
-#include "export.hpp"
-#include "shm.h"
-
-#ifndef SHMXX_TYPE_NAME
-# define SHMXX_TYPE_NAME mem
-#endif
-
-class OTR_COMPAT_EXPORT SHMXX_TYPE_NAME final
-{
- SHM_TYPE_NAME impl;
-
-public:
- SHMXX_TYPE_NAME(const char* shm_name, const char* mutex_name, int map_size);
- ~SHMXX_TYPE_NAME();
-
- bool success() noexcept;
- void* ptr() noexcept;
- void lock() noexcept;
- void unlock() noexcept;
-
- SHMXX_TYPE_NAME& operator=(const SHMXX_TYPE_NAME&) = delete;
- SHMXX_TYPE_NAME& operator=(SHMXX_TYPE_NAME&&) noexcept;
-};
-
-#endif // SHMXX_HEADER_GUARD
diff --git a/proto-ft/ftnoir_protocol_ft.h b/proto-ft/ftnoir_protocol_ft.h
index a01ff722..0056721c 100644
--- a/proto-ft/ftnoir_protocol_ft.h
+++ b/proto-ft/ftnoir_protocol_ft.h
@@ -19,7 +19,7 @@
#include <cinttypes>
#include "freetrackclient/fttypes.h"
-#include "compat/shm.hpp"
+#include "compat/shm.h"
#include "options/options.hpp"
#include <memory>
@@ -46,7 +46,7 @@ public:
QString game_name() override;
private:
settings s;
- mem shm { FREETRACK_HEAP, FREETRACK_MUTEX, sizeof(FTHeap) };
+ shm_wrapper shm { FREETRACK_HEAP, FREETRACK_MUTEX, sizeof(FTHeap) };
FTHeap* pMemData { (FTHeap*) shm.ptr() };
QProcess dummyTrackIR;
@@ -66,8 +66,8 @@ class FTControls: public IProtocolDialog
Q_OBJECT
public:
FTControls();
- void register_protocol(IProtocol *) override {}
- void unregister_protocol() override {}
+ void register_protocol(IProtocol *) {}
+ void unregister_protocol() {}
private:
Ui::UICFTControls ui;
settings s;
diff --git a/proto-wine/ftnoir_protocol_wine.h b/proto-wine/ftnoir_protocol_wine.h
index 8feaf0de..b4cbd305 100644
--- a/proto-wine/ftnoir_protocol_wine.h
+++ b/proto-wine/ftnoir_protocol_wine.h
@@ -1,7 +1,7 @@
#pragma once
#include "api/plugin-api.hpp"
-#include "compat/shm.hpp"
+#include "compat/shm.h"
#include "wine-shm.h"
#include "ui_ftnoir_winecontrols.h"
@@ -33,7 +33,7 @@ public:
#endif
}
private:
- mem lck_shm { WINE_SHM_NAME, WINE_MTX_NAME, sizeof(WineSHM) };
+ shm_wrapper lck_shm { WINE_SHM_NAME, WINE_MTX_NAME, sizeof(WineSHM) };
WineSHM* shm = nullptr;
#ifndef OTR_WINE_NO_WRAPPER
diff --git a/proto-wine/opentrack-wrapper-wine-main.cxx b/proto-wine/opentrack-wrapper-wine-main.cxx
index 84ef57ab..6370e7f4 100644
--- a/proto-wine/opentrack-wrapper-wine-main.cxx
+++ b/proto-wine/opentrack-wrapper-wine-main.cxx
@@ -1,74 +1,80 @@
#include <cerrno>
-#include <unistd.h> // usleep
-#include "compat/macros1.h"
+// OSX sdk 10.8 build error otherwise
+#undef _LIBCPP_MSVCRT
+#include <cstdio>
+
#include "freetrackclient/fttypes.h"
#include "wine-shm.h"
+#include "compat/export.hpp"
enum Axis {
TX = 0, TY, TZ, Yaw, Pitch, Roll
};
-#undef SHM_HEADER_GUARD
-#undef SHMXX_HEADER_GUARD
-#undef SHM_TYPE_NAME
-#undef SHM_FUN_PREFIX
-#undef SHMXX_TYPE_NAME
-#undef SHM_WIN32_INIT
-#define SHM_TYPE_NAME shm_impl_winelib
-#define SHM_FUN_PREFIX shm_impl_winelib_
-#define SHMXX_TYPE_NAME mem_winelib
-#define SHM_WIN32_INIT 1
-#include "compat/shm.hpp"
-
-#undef SHM_HEADER_GUARD
-#undef SHMXX_HEADER_GUARD
-#undef SHM_TYPE_NAME
-#undef SHM_FUN_PREFIX
-#undef SHMXX_TYPE_NAME
-#undef SHM_WIN32_INIT
-#define SHM_TYPE_NAME shm_impl_unix
-#define SHM_FUN_PREFIX shm_impl_unix_
-#define SHMXX_TYPE_NAME mem_unix
-#define SHM_WIN32_INIT 0
-#include "compat/shm.hpp"
+#include "compat/shm.h"
void create_registry_key(void);
+class ShmPosix {
+public:
+ ShmPosix(const char *shmName, const char *mutexName, int mapSize);
+ ~ShmPosix();
+ void lock();
+ void unlock();
+ bool success();
+ inline void* ptr() { return mem; }
+private:
+ void* mem;
+ int fd, size;
+};
+
+class ShmWine {
+public:
+ ShmWine(const char *shmName, const char *mutexName, int mapSize);
+ ~ShmWine();
+ void lock();
+ void unlock();
+ bool success();
+ inline void* ptr() { return mem; }
+private:
+ void* mem;
+ void *hMutex, *hMapFile;
+};
+#include <windows.h>
+
int main(void)
{
- mem_unix lck_unix(WINE_SHM_NAME, WINE_MTX_NAME, sizeof(WineSHM));
- mem_winelib lck_wine("FT_SharedMem", "FT_Mutext", sizeof(FTHeap));
-
- if(!lck_unix.success())
+ ShmPosix lck_posix(WINE_SHM_NAME, WINE_MTX_NAME, sizeof(WineSHM));
+ ShmWine lck_wine("FT_SharedMem", "FT_Mutext", sizeof(FTHeap));
+ if(!lck_posix.success()) {
+ fprintf(stderr, "Can't open posix map: %d\n", errno);
return 1;
- if(!lck_wine.success())
+ }
+ if(!lck_wine.success()) {
+ fprintf(stderr, "Can't open Wine map\n");
return 1;
-
+ }
+ WineSHM* shm_posix = (WineSHM*) lck_posix.ptr();
+ FTHeap* shm_wine = (FTHeap*) lck_wine.ptr();
+ FTData* data = &shm_wine->data;
create_registry_key();
-
- WineSHM& mem_unix = *(WineSHM*) lck_unix.ptr();
- FTHeap& mem_wine = *(FTHeap*) lck_wine.ptr();
- FTData& data = mem_wine.data;
-
- data.CamWidth = 250;
- data.CamHeight = 100;
-
- while (!mem_unix.stop)
- {
- COMPILER_BARRIER();
- data.Yaw = -mem_unix.data[Yaw];
- data.Pitch = -mem_unix.data[Pitch];
- data.Roll = mem_unix.data[Roll];
- data.X = mem_unix.data[TX];
- data.Y = mem_unix.data[TY];
- data.Z = mem_unix.data[TZ];
- data.DataID = 1;
- mem_wine.GameID2 = mem_unix.gameid2;
- mem_unix.gameid = mem_wine.GameID;
+ while (1) {
+ if (shm_posix->stop)
+ break;
+ data->Yaw = -shm_posix->data[Yaw];
+ data->Pitch = -shm_posix->data[Pitch];
+ data->Roll = shm_posix->data[Roll];
+ data->X = shm_posix->data[TX];
+ data->Y = shm_posix->data[TY];
+ data->Z = shm_posix->data[TZ];
+ data->DataID++;
+ data->CamWidth = 250;
+ data->CamHeight = 100;
+ shm_wine->GameID2 = shm_posix->gameid2;
+ shm_posix->gameid = shm_wine->GameID;
for (int i = 0; i < 8; i++)
- mem_wine.table[i] = mem_wine.table[i];
- COMPILER_BARRIER();
- (void)usleep(4 * 1000);
+ shm_wine->table[i] = shm_posix->table[i];
+ (void) Sleep(4);
}
}
diff --git a/proto-wine/opentrack-wrapper-wine-posix.cxx b/proto-wine/opentrack-wrapper-wine-posix.cxx
index 17a74b66..e36407a9 100644
--- a/proto-wine/opentrack-wrapper-wine-posix.cxx
+++ b/proto-wine/opentrack-wrapper-wine-posix.cxx
@@ -1,6 +1,7 @@
-#undef _WIN32
+#ifdef _WIN32
+# undef _WIN32
+#endif
-#define SHM_TYPE_NAME shm_impl_unix
-#define SHM_FUN_PREFIX shm_impl_unix_
-#define SHMXX_TYPE_NAME mem_unix
+#define shm_wrapper ShmPosix
+#include "compat/shm.h"
#include "compat/shm.cpp"
diff --git a/proto-wine/opentrack-wrapper-wine-windows.cxx b/proto-wine/opentrack-wrapper-wine-windows.cxx
index 063e35df..d464cf6c 100644
--- a/proto-wine/opentrack-wrapper-wine-windows.cxx
+++ b/proto-wine/opentrack-wrapper-wine-windows.cxx
@@ -2,18 +2,14 @@
# error "bad cross"
#endif
-#define SHM_TYPE_NAME shm_impl_winelib
-#define SHM_FUN_PREFIX shm_impl_winelib_
-#define SHMXX_TYPE_NAME mem_winelib
+#define shm_wrapper ShmWine
+#include "compat/shm.h"
#include "compat/shm.cpp"
-
#include "wine-shm.h"
#include "compat/library-path.hpp"
-
#include <cstring>
-#include <windows.h>
-#include <winreg.h>
+using std::strcat;
static void write_path(const char* key, const char* subkey)
{