windows support

This commit is contained in:
Nishi 2025-11-25 11:08:13 +09:00
commit b9004acd79
Signed by: nishi
GPG key ID: 27EF69B208EB9343
16 changed files with 208 additions and 32 deletions

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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);
}

View file

@ -1,6 +1,6 @@
#include <MDE/Audio/Audio.h>
#include <pthread.h>
#include <MDE/Core/Thread.h>
#include <MDE/Core/Mutex.h>
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;

View file

@ -1,30 +1,46 @@
#include <MDE/Core/Directory.h>
#include <MDE/Core/String.h>
#include <Mw/Milsko.h>
#ifdef _WIN32
#include <windows.h>
#include <shlobj.h>
#else
#include <dirent.h>
#endif
#include <sys/stat.h>
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
}

View file

@ -2,6 +2,10 @@
#include <MDE/Core/String.h>
#include <MDE/Core/Directory.h>
#ifdef _WIN32
#include <windows.h>
#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
}

22
core/mutex.c Normal file
View file

@ -0,0 +1,22 @@
#include <MDE/Core/Mutex.h>
#ifdef _WIN32
#include <windows.h>
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

34
core/thread.c Normal file
View file

@ -0,0 +1,34 @@
#include <MDE/Core/Thread.h>
#ifdef _WIN32
#include <windows.h>
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

View file

@ -1,6 +1,12 @@
#include <MDE/Core/Users.h>
#include <MDE/Core/String.h>
#ifdef _WIN32
void MDEUsersList(void (*call)(const char* name, void* user), void* user) {
/* wow */
call("Administrator", user);
}
#else
#include <pwd.h>
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

View file

@ -3,8 +3,6 @@
#include <MDE/MachDep.h>
#include <pthread.h>
#ifdef __cplusplus
extern "C" {
#endif

View file

@ -13,6 +13,8 @@ void MDEDirectoryCreate(const char* path, int mode);
char* MDEDirectoryCurrentPath(void);
char* MDEDirectoryConfigPath(void);
#ifdef __cplusplus
}
#endif

24
include/MDE/Core/Mutex.h Normal file
View file

@ -0,0 +1,24 @@
#ifndef __MDE_CORE_MUTEX_H__
#define __MDE_CORE_MUTEX_H__
#include <MDE/MachDep.h>
#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

22
include/MDE/Core/Thread.h Normal file
View file

@ -0,0 +1,22 @@
#ifndef __MDE_CORE_THREAD_H__
#define __MDE_CORE_THREAD_H__
#include <MDE/MachDep.h>
#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

View file

@ -8,6 +8,8 @@
#include <MDE/Core/Users.h>
#include <MDE/Core/Directory.h>
#include <MDE/Core/AboutDialog.h>
#include <MDE/Core/Thread.h>
#include <MDE/Core/Mutex.h>
#include <MDE/Audio/Audio.h>

View file

@ -6,6 +6,11 @@
#include <stdarg.h>
#include <string.h>
#include <strings.h>
#ifdef _WIN32
#include <io.h>
#include <direct.h>
#else
#include <unistd.h>
#endif
#endif