Files
d2r-mobile/dm/host/compat/linux/compat_linux.h
T
shenleiandClaude Opus 5.5 4d91f80bae Android port (Adreno): Vulkan backend, GameActivity app, touch HUD, lifecycle, minimap quest goals
- host/android: Vulkan replay backend (nvn_vk.c) with pipeline/descriptor caching, ADPF,
  AChoreographer present link; Linux compat layer; Makefile.android (headless dmhost + APK lib)
- android/app: GameActivity shell without Gradle (build_apk.sh), AAudio output, touch HUD port
  (touch_app.c + TouchOverlay.java, three layers: body / joystick / minimap goals),
  lifecycle save-on-background like iOS, art_from_ipa.py for touch / HUD / mapgoal art
- touch: shared touch controls moved from ios/Sources to dm/touch
- patch_nso.py Android ruleset, dmsh_vk.py shader pack, DM_TOUCH_PRODUCT defaults
- hle_audio: AAudio resume waits through STARTING and reopens once on failure
- .gitignore: local resources/ (original game data) never committed

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-14 14:55:05 +09:00

151 lines
8.6 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// Linux / Android 兼容层:宿主源码按 Darwin 写,在 Linux 上用 -include 强制先包含本头,
// 再由 compat/linux/include 下的同名替身头(mach/mach.h、CommonCrypto/CommonDigest.h…)吃掉 Apple 头的 #include。
// 这里只补「同一语义、换个名字」的接口;语义不同的(线程暂停取寄存器、AudioUnit、GCD 信号源、xattr 参数)在源码里按 __APPLE__ 分支。
// Linux / Android compatibility layer: the host sources are written against Darwin; on Linux this header is force-included first with -include,
// and the same-named stand-in headers under compat/linux/include (mach/mach.h, CommonCrypto/CommonDigest.h, ...) absorb the #include of Apple headers.
// Only "same semantics, different name" interfaces are provided here; the ones with different semantics (suspending a thread to read its registers,
// AudioUnit, GCD signal sources, xattr arguments) branch on __APPLE__ in the sources.
#pragma once
#if !defined(__linux__)
#error "compat_linux.h 只给 Linux / Android 用 | compat_linux.h is for Linux / Android only"
#endif
#ifndef _GNU_SOURCE
#define _GNU_SOURCE 1
#endif
#include <errno.h>
#include <fcntl.h>
#include <pthread.h>
#include <stddef.h>
#include <stdint.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <time.h>
#include <unistd.h>
// ---- TargetConditionals:Linux 既不是 iPhone 也不是 OSX;运行时可写代码(探针 / 计数桩)这类「非 iPhone」路径照常可用 ----
// ---- TargetConditionals: Linux is neither iPhone nor OSX; "non-iPhone" paths such as runtime-written code (probes / counting stubs) work as usual ----
#define TARGET_OS_IPHONE 0
#define TARGET_OS_IOS 0
#define TARGET_OS_OSX 0
#define TARGET_OS_MAC 0
#define TARGET_OS_SIMULATOR 0
#define TARGET_OS_MACCATALYST 0
#define DM_LINUX 1
// ---- 时钟:CLOCK_UPTIME_RAW(不含睡眠)= Linux CLOCK_MONOTONIC(同样不含挂起);mach 时基取 1/1 = 纳秒 ----
// ---- Clocks: CLOCK_UPTIME_RAW (excludes sleep) = Linux CLOCK_MONOTONIC (also excludes suspend); the mach time base is 1/1 = nanoseconds ----
#define CLOCK_UPTIME_RAW CLOCK_MONOTONIC
#define CLOCK_UPTIME_RAW_APPROX CLOCK_MONOTONIC_COARSE
#define CLOCK_MONOTONIC_RAW_APPROX CLOCK_MONOTONIC_COARSE
static inline uint64_t clock_gettime_nsec_np(clockid_t c) {
struct timespec ts; if (clock_gettime(c, &ts)) return 0;
return (uint64_t)ts.tv_sec * 1000000000ull + (uint64_t)ts.tv_nsec;
}
typedef int kern_return_t;
#define KERN_SUCCESS 0
#define KERN_FAILURE 5
#define KERN_INVALID_ADDRESS 1
typedef struct { uint32_t numer, denom; } mach_timebase_info_data_t;
typedef mach_timebase_info_data_t *mach_timebase_info_t;
static inline kern_return_t mach_timebase_info(mach_timebase_info_t t) { t->numer = t->denom = 1; return KERN_SUCCESS; }
// 真符号:nvn_entry.S 直接 bl mach_absolute_time | a real symbol: nvn_entry.S calls mach_absolute_time directly
uint64_t mach_absolute_time(void);
static inline uint64_t mach_continuous_time(void) { return clock_gettime_nsec_np(CLOCK_BOOTTIME); }
static inline kern_return_t mach_wait_until(uint64_t deadline) {
struct timespec ts = {(time_t)(deadline / 1000000000ull), (long)(deadline % 1000000000ull)};
while (clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &ts, NULL) == EINTR) {}
return KERN_SUCCESS;
}
// ---- 任务 / 内存:只有「读自己进程一段可能不可读的内存」这一种用法(诊断),用 process_vm_readv 读自己,缺页返回错误而不是崩 ----
// ---- Task / memory: the only use is "read a possibly unreadable range of our own process" (diagnostics); process_vm_readv on ourselves returns an error instead of faulting ----
typedef int mach_port_t;
typedef mach_port_t task_t, thread_t, thread_act_t;
typedef unsigned mach_msg_type_number_t;
typedef uintptr_t vm_address_t;
typedef size_t vm_size_t;
typedef int vm_prot_t;
static inline mach_port_t mach_task_self(void) { return 0; }
static inline mach_port_t mach_thread_self(void) { return (mach_port_t)gettid(); }
static inline mach_port_t pthread_mach_thread_np(pthread_t t) { return (mach_port_t)pthread_gettid_np(t); }
static inline kern_return_t mach_port_deallocate(mach_port_t task, mach_port_t p) { (void)task; (void)p; return KERN_SUCCESS; }
// task_info(TASK_VM_INFO):只给 phys_footprint(取 /proc/self/statm 的常驻页) | task_info(TASK_VM_INFO): only phys_footprint (resident pages from /proc/self/statm)
typedef int task_flavor_t;
typedef int *task_info_t;
#define TASK_VM_INFO 22
typedef struct { uint64_t virtual_size, resident_size, phys_footprint; } task_vm_info_data_t;
#define TASK_VM_INFO_COUNT ((mach_msg_type_number_t)(sizeof(task_vm_info_data_t) / sizeof(int)))
kern_return_t task_info(mach_port_t task, task_flavor_t flavor, task_info_t out, mach_msg_type_number_t *count);
kern_return_t vm_read_overwrite(mach_port_t task, vm_address_t addr, vm_size_t size, vm_address_t data, vm_size_t *outsize);
// ---- 指令缓存 ----
// ---- Instruction cache ----
static inline void sys_icache_invalidate(void *p, size_t n) { __builtin___clear_cache((char *)p, (char *)p + n); }
static inline void sys_dcache_flush(void *p, size_t n) { (void)p; (void)n; }
// ---- QoS:Linux 没有,按「不设」处理;查询返回 UNSPECIFIED ----
// ---- QoS: Linux has none; treated as "not set", queries return UNSPECIFIED ----
typedef enum {
QOS_CLASS_USER_INTERACTIVE = 0x21, QOS_CLASS_USER_INITIATED = 0x19, QOS_CLASS_DEFAULT = 0x15,
QOS_CLASS_UTILITY = 0x11, QOS_CLASS_BACKGROUND = 0x09, QOS_CLASS_UNSPECIFIED = 0x00,
} qos_class_t;
static inline int pthread_set_qos_class_self_np(qos_class_t q, int rel) { (void)q; (void)rel; return 0; }
static inline int pthread_get_qos_class_np(pthread_t t, qos_class_t *q, int *rel) { (void)t; if (q) *q = QOS_CLASS_UNSPECIFIED; if (rel) *rel = 0; return 0; }
static inline qos_class_t qos_class_self(void) { return QOS_CLASS_UNSPECIFIED; }
// ---- pthread 的 Darwin 扩展 ----
// ---- Darwin extensions of pthread ----
// Darwin pthread_setname_np 只给当前线程起名
// Darwin pthread_setname_np only names the current thread
#define pthread_setname_np(n) pthread_setname_np(pthread_self(), (n))
// Darwin 的 stackaddr 是栈顶(高地址)
// Darwin's stackaddr is the stack top (high address)
void *pthread_get_stackaddr_np(pthread_t t);
size_t pthread_get_stacksize_np(pthread_t t);
static inline int pthread_threadid_np(pthread_t t, uint64_t *id) { *id = (uint64_t)pthread_gettid_np(t ? t : pthread_self()); return 0; }
static inline int pthread_main_np(void) { return getpid() == gettid(); }
// ---- 文件 ----
// ---- Files ----
#define st_mtimespec st_mtim
#define st_atimespec st_atim
#define st_ctimespec st_ctim
static inline int getdtablesize(void) { return (int)sysconf(_SC_OPEN_MAX); }
#define F_GETPATH 0x7fff0032 // 由 dm_fcntl_getpath 处理(fcntl 不认识这个号) | handled by dm_fcntl_getpath (fcntl does not know this number)
#ifndef ENOATTR
#define ENOATTR ENODATA
#endif
int dm_fcntl_getpath(int fd, char *out);
// 整树复制(含 user.* 扩展属性);Linux 没有写时复制克隆,存档目录小,复制可接受
// Whole-tree copy (including user.* extended attributes); Linux has no copy-on-write clone, save directories are small so copying is acceptable
int clonefile(const char *src, const char *dst, uint32_t flags);
#define RENAME_SWAP 2u
int renamex_np(const char *from, const char *to, unsigned flags);
// ---- 进程映像:可执行文件路径;Mach-O 段登记表的 ELF 对应(__start_<段> / __stop_<段>) ----
// ---- Process image: executable path; the ELF counterpart of the Mach-O registration sections (__start_<sect> / __stop_<sect>) ----
int _NSGetExecutablePath(char *buf, uint32_t *size);
struct mach_header_64 { uint32_t magic; };
extern const struct mach_header_64 _mh_execute_header;
uint8_t *getsectiondata(const struct mach_header_64 *mh, const char *seg, const char *sect, unsigned long *size);
// ---- 随机数:bionic 自带 arc4random;glibc 2.36 起也有 ----
// ---- Random numbers: bionic ships arc4random; glibc has it since 2.36 ----
#include <stdlib.h>
#include <stdio.h>
#include <sys/ucontext.h>
// ---- 信号上下文:ESR 在 __reserved 的 esr_context 里(magic 0x45535201) ----
// ---- Signal context: ESR lives in the esr_context inside __reserved (magic 0x45535201) ----
static inline uint64_t dm_uc_esr(const ucontext_t *uc) {
const uint8_t *p = (const uint8_t *)uc->uc_mcontext.__reserved, *end = p + sizeof uc->uc_mcontext.__reserved;
while (p + 8 <= end) {
uint32_t magic, size; memcpy(&magic, p, 4); memcpy(&size, p + 4, 4);
if (!magic || size < 8) break;
if (magic == 0x45535201u) { uint64_t esr; memcpy(&esr, p + 8, 8); return esr; }
p += size;
}
return 0;
}