diff --git a/audio/CMakeLists.txt b/audio/CMakeLists.txt index 866c94d..c905383 100644 --- a/audio/CMakeLists.txt +++ b/audio/CMakeLists.txt @@ -1,4 +1,11 @@ +include(MDEPkgConfig) + setup_lib(audio Audio) +target_link_libraries( + MDEAudio + PRIVATE + MDECore +) find_package(PkgConfig) @@ -29,7 +36,7 @@ macro(check_backend uppername name) target_link_libraries( MDEAudio PRIVATE - ${${uppername}_LIBRARIES} pthread + ${${uppername}_LIBRARIES} ) target_compile_definitions( MDEAudio diff --git a/audio/drv_alsa.c b/audio/drv_alsa.c index f87ce7b..f8b42df 100644 --- a/audio/drv_alsa.c +++ b/audio/drv_alsa.c @@ -8,7 +8,7 @@ typedef struct driver_context { snd_pcm_hw_params_t* params; } driver_context_t; -#include "pthread/decl.c" +#include "thread/decl.c" static void drv_write(driver_t* drv, void* data, int frames) { if(snd_pcm_writei(drv->context.device, data, frames) == -EPIPE) { @@ -72,5 +72,5 @@ static void drv_close(driver_t* drv) { } } -#include "pthread/body.c" +#include "thread/body.c" #endif diff --git a/audio/drv_ao.c b/audio/drv_ao.c index 5ee31a4..2fd75cd 100644 --- a/audio/drv_ao.c +++ b/audio/drv_ao.c @@ -8,7 +8,7 @@ typedef struct driver_context { ao_device* device; } driver_context_t; -#include "pthread/decl.c" +#include "thread/decl.c" static void drv_write(driver_t* drv, void* data, int frames) { ao_play(drv->context.device, (char*)data, frames * 2 * 2); @@ -42,5 +42,5 @@ static void drv_close(driver_t* drv) { } } -#include "pthread/body.c" +#include "thread/body.c" #endif diff --git a/audio/pthread/body.c b/audio/thread/body.c similarity index 64% rename from audio/pthread/body.c rename to audio/thread/body.c index d579f3d..1c30746 100644 --- a/audio/pthread/body.c +++ b/audio/thread/body.c @@ -1,4 +1,4 @@ -static void* thread_routine(void* arg) { +static void thread_routine(void* arg) { driver_t* drv = arg; void* data; @@ -9,17 +9,15 @@ static void* thread_routine(void* arg) { drv_write(drv, data, drv->frames); - pthread_mutex_lock(&drv->mutex); + MDEMutexLock(drv->mutex); if(drv->quit) { - pthread_mutex_unlock(&drv->mutex); + MDEMutexUnlock(&drv->mutex); break; } - pthread_mutex_unlock(&drv->mutex); + MDEMutexUnlock(drv->mutex); } free(data); - - return NULL; } MDEAudio MDEAudioOpen(MDEAudioHandler handler, void* user) { @@ -32,7 +30,7 @@ MDEAudio MDEAudioOpen(MDEAudioHandler handler, void* user) { drv->user = user; drv->init = 0; - pthread_mutex_init(&drv->mutex, NULL); + drv->mutex = MDEMutexCreate(); if(!(drv->init = drv_open(drv))) { MDEAudioClose(drv); @@ -44,16 +42,15 @@ MDEAudio MDEAudioOpen(MDEAudioHandler handler, void* user) { void MDEAudioClose(MDEAudio handle) { driver_t* drv = handle; - void* ret; if(drv->init) { - pthread_mutex_lock(&drv->mutex); + MDEMutexLock(drv->mutex); drv->quit = 1; - pthread_mutex_unlock(&drv->mutex); - pthread_join(drv->thread, &ret); + MDEMutexLock(drv->mutex); + MDEThreadJoin(drv->thread); } - pthread_mutex_destroy(&drv->mutex); + MDEMutexDestroy(drv->mutex); drv_close(drv); @@ -63,5 +60,5 @@ void MDEAudioClose(MDEAudio handle) { void MDEAudioStart(MDEAudio handle) { driver_t* drv = handle; - pthread_create(&drv->thread, NULL, thread_routine, handle); + drv->thread = MDEThreadCreate(thread_routine, handle); } diff --git a/audio/pthread/decl.c b/audio/thread/decl.c similarity index 59% rename from audio/pthread/decl.c rename to audio/thread/decl.c index b213e76..2ab766e 100644 --- a/audio/pthread/decl.c +++ b/audio/thread/decl.c @@ -1,6 +1,6 @@ #include - -#include +#include +#include typedef struct driver { driver_context_t context; @@ -11,7 +11,7 @@ typedef struct driver { MDEAudioHandler handler; void* user; - int quit; - pthread_t thread; - pthread_mutex_t mutex; + int quit; + MDEThread thread; + MDEMutex mutex; } driver_t; diff --git a/core/directory.c b/core/directory.c index d7db29e..31bfd6e 100644 --- a/core/directory.c +++ b/core/directory.c @@ -1,30 +1,46 @@ #include #include +#include +#ifdef _WIN32 +#include +#include +#else #include +#endif #include void MDEDirectoryScan(const char* path, void (*call)(const char* name, int dir, int symlink, void* user), void* user) { - DIR* dir = opendir(path); + void* dir = MwDirectoryOpen(path); if(dir != NULL) { - struct dirent* d; - while((d = readdir(dir)) != NULL) { + MwDirectoryEntry* d; + while((d = MwDirectoryRead(dir)) != NULL) { char* p; struct stat s; - if(strcmp(d->d_name, "..") == 0 || strcmp(d->d_name, ".") == 0) continue; + if(strcmp(d->name, "..") == 0 || strcmp(d->name, ".") == 0) continue; - p = malloc(strlen(path) + 1 + strlen(d->d_name) + 1); + p = malloc(strlen(path) + 1 + strlen(d->name) + 1); strcpy(p, path); +#ifdef _WIN32 + if(path[strlen(path) - 1] != '\\') strcat(p, "\\"); +#else if(path[strlen(path) - 1] != '/') strcat(p, "/"); - strcat(p, d->d_name); +#endif + strcat(p, d->name); +#ifdef _WIN32 + if(stat(p, &s) == 0) { + call(p, d->type == MwDIRECTORY_DIRECTORY ? 1 : 0, 0, user); + } +#else if(lstat(p, &s) == 0) { call(p, S_ISDIR(s.st_mode) ? 1 : 0, S_ISLNK(s.st_mode) ? 1 : 0, user); } +#endif free(p); } - closedir(dir); + MwDirectoryClose(dir); } } @@ -37,8 +53,13 @@ void MDEDirectoryCreate(const char* path, int mode) { b[0] = path[i]; b[1] = 0; strcat(r, b); +#ifdef _WIN32 + if(path[i] == '\\' || path[i] == 0) { + mkdir(r); +#else if(path[i] == '/' || path[i] == 0) { mkdir(r, mode); +#endif if(path[i] == 0) break; } } @@ -51,3 +72,26 @@ char* MDEDirectoryCurrentPath(void) { return MDEStringDuplicate(p); } + +char* MDEDirectoryConfigPath(void) { +#ifdef _WIN32 + char* shp = malloc(MAX_PATH); + LPITEMIDLIST pidl; + if(SHGetSpecialFolderLocation(NULL, CSIDL_APPDATA, &pidl) == S_OK) { + SHGetPathFromIDList(pidl, shp); + CoTaskMemFree(pidl); + return shp; + } + free(shp); + + shp = getenv("USERPROFILE"); + return shp == NULL ? NULL : MDEStringDuplicate(shp); +#else + struct passwd* pwd = getpwuid(getuid()); + char* p = malloc(strlen(pwd->pw_dir) + strlen("/.config") + 1); + strcpy(p, pwd->pw_dir); + strcat(p, "/.config"); + + return p; +#endif +} diff --git a/core/file.c b/core/file.c index d8d4c92..80be362 100644 --- a/core/file.c +++ b/core/file.c @@ -2,6 +2,10 @@ #include #include +#ifdef _WIN32 +#include +#endif + void MDEFileCopy(const char* src, const char* dst) { char buffer[4096]; FILE* in; @@ -65,6 +69,13 @@ char* MDEFileOptimizeAbsolutePath(const char* path) { } char* MDEFileAbsolutePath(const char* path) { +#ifdef _WIN32 + char* r = malloc(MAX_PATH); + + GetFullPathName(path, MAX_PATH, r, NULL); + + return r; +#else char* p; char* r; @@ -83,4 +94,5 @@ char* MDEFileAbsolutePath(const char* path) { free(p); return r; +#endif } diff --git a/core/mutex.c b/core/mutex.c new file mode 100644 index 0000000..cd90210 --- /dev/null +++ b/core/mutex.c @@ -0,0 +1,22 @@ +#include + +#ifdef _WIN32 +#include + +MDEMutex MDEMutexCreate(void) { + return CreateEvent(NULL, FALSE, TRUE, NULL); +} + +void MDEMutexDestroy(MDEMutex mutex) { + CloseHandle(mutex); +} + +void MDEMutexLock(MDEMutex mutex) { + WaitForSingleObject(mutex, INFINITE); +} + +void MDEMutexUnlock(MDEMutex mutex) { + SetEvent(mutex); +} +#else +#endif diff --git a/core/thread.c b/core/thread.c new file mode 100644 index 0000000..1d9ac66 --- /dev/null +++ b/core/thread.c @@ -0,0 +1,34 @@ +#include + +#ifdef _WIN32 +#include + +static DWORD WINAPI call_wrapper(LPVOID param) { + void** p = param; + void (*call)(void* user_data) = p[0]; + void* ud = p[1]; + free(p); + + call(ud); + + return 0; +} + +MDEThread MDEThreadCreate(void (*call)(void* user_data), void* user_data) { + void** p = malloc(sizeof(*p) * 2); + + p[0] = call; + p[1] = user_data; + + return CreateThread(NULL, 0, call_wrapper, (void*)p, 0, NULL); +} + +void MDEThreadDestroy(MDEThread thread) { + CloseHandle(thread); +} + +void MDEThreadJoin(MDEThread thread) { + WaitForSingleObject(thread, INFINITE); +} +#else +#endif diff --git a/core/users.c b/core/users.c index f748c57..7015e8c 100644 --- a/core/users.c +++ b/core/users.c @@ -1,6 +1,12 @@ #include #include +#ifdef _WIN32 +void MDEUsersList(void (*call)(const char* name, void* user), void* user) { + /* wow */ + call("Administrator", user); +} +#else #include void MDEUsersList(void (*call)(const char* name, void* user), void* user) { @@ -37,3 +43,4 @@ void MDEUsersList(void (*call)(const char* name, void* user), void* user) { } endpwent(); } +#endif diff --git a/include/MDE/Audio/Audio.h b/include/MDE/Audio/Audio.h index 0d271d8..686f35e 100644 --- a/include/MDE/Audio/Audio.h +++ b/include/MDE/Audio/Audio.h @@ -3,8 +3,6 @@ #include -#include - #ifdef __cplusplus extern "C" { #endif diff --git a/include/MDE/Core/Directory.h b/include/MDE/Core/Directory.h index 67b4492..0ebd35b 100644 --- a/include/MDE/Core/Directory.h +++ b/include/MDE/Core/Directory.h @@ -13,6 +13,8 @@ void MDEDirectoryCreate(const char* path, int mode); char* MDEDirectoryCurrentPath(void); +char* MDEDirectoryConfigPath(void); + #ifdef __cplusplus } #endif diff --git a/include/MDE/Core/Mutex.h b/include/MDE/Core/Mutex.h new file mode 100644 index 0000000..69455d9 --- /dev/null +++ b/include/MDE/Core/Mutex.h @@ -0,0 +1,24 @@ +#ifndef __MDE_CORE_MUTEX_H__ +#define __MDE_CORE_MUTEX_H__ + +#include + +#ifdef __cplusplus +extern "C" { +#endif + +typedef void* MDEMutex; + +MDEMutex MDEMutexCreate(void); + +void MDEMutexDestroy(MDEMutex mutex); + +void MDEMutexLock(MDEMutex mutex); + +void MDEMutexUnlock(MDEMutex mutex); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/include/MDE/Core/Thread.h b/include/MDE/Core/Thread.h new file mode 100644 index 0000000..76ad19b --- /dev/null +++ b/include/MDE/Core/Thread.h @@ -0,0 +1,22 @@ +#ifndef __MDE_CORE_THREAD_H__ +#define __MDE_CORE_THREAD_H__ + +#include + +#ifdef __cplusplus +extern "C" { +#endif + +typedef void* MDEThread; + +MDEThread MDEThreadCreate(void (*call)(void* user_data), void* user_data); + +void MDEThreadDestroy(MDEThread thread); + +void MDEThreadJoin(MDEThread thread); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/include/MDE/Foundation.h b/include/MDE/Foundation.h index 175c534..f380fb3 100644 --- a/include/MDE/Foundation.h +++ b/include/MDE/Foundation.h @@ -8,6 +8,8 @@ #include #include #include +#include +#include #include diff --git a/include/MDE/MachDep.h b/include/MDE/MachDep.h index 249344f..de0cffd 100644 --- a/include/MDE/MachDep.h +++ b/include/MDE/MachDep.h @@ -6,6 +6,11 @@ #include #include #include +#ifdef _WIN32 +#include +#include +#else #include +#endif #endif