size_t os_utf8_to_wcs_ptr(const char *str, size_t len, wchar_t **pstr) { size_t out_len = os_utf8_to_wcs(str, len, NULL, 0); *pstr = bmalloc((out_len + 1) * sizeof(wchar_t)); return os_utf8_to_wcs(str, out_len, *pstr, out_len + 1); }
int os_stat(const char *file, struct stat *st) { if (file) { wchar_t w_file[512]; size_t size = os_utf8_to_wcs(file, 0, w_file, sizeof(w_file)); if (size > 0) { struct _stat st_w32; int ret = _wstat(w_file, &st_w32); if (ret == 0) { st->st_dev = st_w32.st_dev; st->st_ino = st_w32.st_ino; st->st_mode = st_w32.st_mode; st->st_nlink = st_w32.st_nlink; st->st_uid = st_w32.st_uid; st->st_gid = st_w32.st_gid; st->st_rdev = st_w32.st_rdev; st->st_size = st_w32.st_size; st->st_atime = st_w32.st_atime; st->st_mtime = st_w32.st_mtime; st->st_ctime = st_w32.st_ctime; } return ret; } } return -1; }
void load_text_from_file(struct ft2_source *srcdata, const char *filename) { FILE *tmp_file = NULL; uint32_t filesize = 0; char *tmp_read = NULL; uint16_t header = 0; size_t bytes_read; tmp_file = os_fopen(filename, "rb"); if (tmp_file == NULL) { if (!srcdata->file_load_failed) { blog(LOG_WARNING, "Failed to open file %s", filename); srcdata->file_load_failed = true; } return; } fseek(tmp_file, 0, SEEK_END); filesize = (uint32_t)ftell(tmp_file); fseek(tmp_file, 0, SEEK_SET); bytes_read = fread(&header, 2, 1, tmp_file); if (bytes_read == 2 && header == 0xFEFF) { // File is already in UTF-16 format if (srcdata->text != NULL) { bfree(srcdata->text); srcdata->text = NULL; } srcdata->text = bzalloc(filesize); bytes_read = fread(srcdata->text, filesize - 2, 1, tmp_file); srcdata->m_timestamp = get_modified_timestamp(srcdata->text_file); bfree(tmp_read); fclose(tmp_file); return; } fseek(tmp_file, 0, SEEK_SET); srcdata->m_timestamp = get_modified_timestamp(srcdata->text_file); tmp_read = bzalloc(filesize + 1); bytes_read = fread(tmp_read, filesize, 1, tmp_file); fclose(tmp_file); if (srcdata->text != NULL) { bfree(srcdata->text); srcdata->text = NULL; } srcdata->text = bzalloc((strlen(tmp_read) + 1)*sizeof(wchar_t)); os_utf8_to_wcs(tmp_read, strlen(tmp_read), srcdata->text, (strlen(tmp_read) + 1)); remove_cr(srcdata->text); bfree(tmp_read); }
size_t os_utf8_to_mbs(const char *str, size_t len, char **pstr) { wchar_t *wstr = NULL; char *dst = NULL; size_t wlen = os_utf8_to_wcs(str, len, &wstr); size_t out_len = os_wcs_to_mbs(wstr, wlen, &dst); bfree(wstr); *pstr = dst; return out_len; }
FILE *os_fopen(const char *path, const char *mode) { #ifdef _WIN32 wchar_t *wpath = NULL; FILE *file = NULL; os_utf8_to_wcs(path, 0, &wpath); file = os_wfopen(wpath, mode); bfree(wpath); return file; #else return fopen(path, mode); #endif }
void *os_dlopen(const char *path) { wchar_t *wpath; HMODULE h_library = NULL; os_utf8_to_wcs(path, 0, &wpath); h_library = LoadLibraryW(wpath); bfree(wpath); if (!h_library) blog(LOG_INFO, "LoadLibrary failed for '%s', error: %u", path, GetLastError()); return h_library; }
bool os_file_exists(const char *path) { WIN32_FIND_DATA wfd; HANDLE hFind; wchar_t *path_utf16; if (!os_utf8_to_wcs(path, 0, &path_utf16)) return false; hFind = FindFirstFileW(path_utf16, &wfd); if (hFind != INVALID_HANDLE_VALUE) FindClose(hFind); bfree(path_utf16); return hFind != INVALID_HANDLE_VALUE; }
int os_mkdir(const char *path) { wchar_t *path_utf16; BOOL success; if (!os_utf8_to_wcs(path, 0, &path_utf16)) return MKDIR_ERROR; success = CreateDirectory(path_utf16, NULL); bfree(path_utf16); if (!success) return (GetLastError() == ERROR_ALREADY_EXISTS) ? MKDIR_EXISTS : MKDIR_ERROR; return MKDIR_SUCCESS; }
FILE *os_wfopen(const wchar_t *path, const char *mode) { FILE *file; #ifdef _MSC_VER wchar_t *wcs_mode; os_utf8_to_wcs(mode, 0, &wcs_mode); file = _wfopen(path, wcs_mode); bfree(wcs_mode); #else char *mbs_path; os_wcs_to_utf8(path, 0, &mbs_path); file = fopen(mbs_path, mode); bfree(mbs_path); #endif return file; }
int64_t os_get_free_space(const char *path) { ULARGE_INTEGER remainingSpace; char abs_path[512]; wchar_t w_abs_path[512]; if (os_get_abs_path(path, abs_path, 512) > 0) { if (os_utf8_to_wcs(abs_path, 0, w_abs_path, 512) > 0) { BOOL success = GetDiskFreeSpaceExW(w_abs_path, (PULARGE_INTEGER)&remainingSpace, NULL, NULL); if (success) return (int64_t)remainingSpace.QuadPart; } } return -1; }
size_t os_get_abs_path(const char *path, char *abspath, size_t size) { wchar_t wpath[512]; wchar_t wabspath[512]; size_t out_len = 0; size_t len; if (!abspath) return 0; len = os_utf8_to_wcs(path, 0, wpath, 512); if (!len) return 0; if (_wfullpath(wabspath, wpath, 512) != NULL) out_len = os_wcs_to_utf8(wabspath, 0, abspath, size); return out_len; }
void *os_dlopen(const char *path) { struct dstr dll_name; wchar_t *wpath; HMODULE h_library = NULL; dstr_init_copy(&dll_name, path); dstr_cat(&dll_name, ".dll"); os_utf8_to_wcs(dll_name.array, 0, &wpath); h_library = LoadLibraryW(wpath); bfree(wpath); dstr_free(&dll_name); if (!h_library) blog(LOG_INFO, "LoadLibrary failed for '%s', error: %u", path, GetLastError()); return h_library; }
void read_from_end(struct ft2_source *srcdata, const char *filename) { FILE *tmp_file = NULL; uint32_t filesize = 0, cur_pos = 0; char *tmp_read = NULL; uint16_t value = 0, line_breaks = 0; size_t bytes_read; char bvalue; bool utf16 = false; tmp_file = fopen(filename, "rb"); if (tmp_file == NULL) { if (!srcdata->file_load_failed) { blog(LOG_WARNING, "Failed to open file %s", filename); srcdata->file_load_failed = true; } return; } bytes_read = fread(&value, 2, 1, tmp_file); if (bytes_read == 2 && value == 0xFEFF) utf16 = true; fseek(tmp_file, 0, SEEK_END); filesize = (uint32_t)ftell(tmp_file); cur_pos = filesize; while (line_breaks <= 6 && cur_pos != 0) { if (!utf16) cur_pos--; else cur_pos -= 2; fseek(tmp_file, cur_pos, SEEK_SET); if (!utf16) { bytes_read = fread(&bvalue, 1, 1, tmp_file); if (bytes_read == 1 && bvalue == '\n') line_breaks++; } else { bytes_read = fread(&value, 2, 1, tmp_file); if (bytes_read == 2 && value == L'\n') line_breaks++; } } if (cur_pos != 0) cur_pos += (utf16) ? 2 : 1; fseek(tmp_file, cur_pos, SEEK_SET); if (utf16) { if (srcdata->text != NULL) { bfree(srcdata->text); srcdata->text = NULL; } srcdata->text = bzalloc(filesize - cur_pos); bytes_read = fread(srcdata->text, (filesize - cur_pos), 1, tmp_file); srcdata->m_timestamp = get_modified_timestamp(srcdata->text_file); bfree(tmp_read); fclose(tmp_file); return; } tmp_read = bzalloc((filesize - cur_pos) + 1); bytes_read = fread(tmp_read, filesize - cur_pos, 1, tmp_file); fclose(tmp_file); if (srcdata->text != NULL) { bfree(srcdata->text); srcdata->text = NULL; } srcdata->text = bzalloc((strlen(tmp_read) + 1)*sizeof(wchar_t)); os_utf8_to_wcs(tmp_read, strlen(tmp_read), srcdata->text, (strlen(tmp_read) + 1)); srcdata->m_timestamp = get_modified_timestamp(srcdata->text_file); bfree(tmp_read); }
static bool audio_monitor_init(struct audio_monitor *monitor, obs_source_t *source) { IMMDeviceEnumerator *immde = NULL; WAVEFORMATEX *wfex = NULL; bool success = false; UINT32 frames; HRESULT hr; pthread_mutex_init_value(&monitor->playback_mutex); monitor->source = source; const char *id = obs->audio.monitoring_device_id; if (!id) { return false; } if (source->info.output_flags & OBS_SOURCE_DO_NOT_SELF_MONITOR) { obs_data_t *s = obs_source_get_settings(source); const char *s_dev_id = obs_data_get_string(s, "device_id"); bool match = devices_match(s_dev_id, id); obs_data_release(s); if (match) { monitor->ignore = true; return true; } } /* ------------------------------------------ * * Init device */ hr = CoCreateInstance(&CLSID_MMDeviceEnumerator, NULL, CLSCTX_ALL, &IID_IMMDeviceEnumerator, (void**)&immde); if (FAILED(hr)) { return false; } if (strcmp(id, "default") == 0) { hr = immde->lpVtbl->GetDefaultAudioEndpoint(immde, eRender, eConsole, &monitor->device); } else { wchar_t w_id[512]; os_utf8_to_wcs(id, 0, w_id, 512); hr = immde->lpVtbl->GetDevice(immde, w_id, &monitor->device); } if (FAILED(hr)) { goto fail; } /* ------------------------------------------ * * Init client */ hr = monitor->device->lpVtbl->Activate(monitor->device, &IID_IAudioClient, CLSCTX_ALL, NULL, (void**)&monitor->client); if (FAILED(hr)) { goto fail; } hr = monitor->client->lpVtbl->GetMixFormat(monitor->client, &wfex); if (FAILED(hr)) { goto fail; } hr = monitor->client->lpVtbl->Initialize(monitor->client, AUDCLNT_SHAREMODE_SHARED, 0, 10000000, 0, wfex, NULL); if (FAILED(hr)) { goto fail; } /* ------------------------------------------ * * Init resampler */ const struct audio_output_info *info = audio_output_get_info( obs->audio.audio); WAVEFORMATEXTENSIBLE *ext = (WAVEFORMATEXTENSIBLE*)wfex; struct resample_info from; struct resample_info to; from.samples_per_sec = info->samples_per_sec; from.speakers = info->speakers; from.format = AUDIO_FORMAT_FLOAT_PLANAR; to.samples_per_sec = (uint32_t)wfex->nSamplesPerSec; to.speakers = convert_speaker_layout(ext->dwChannelMask, wfex->nChannels); to.format = AUDIO_FORMAT_FLOAT; monitor->sample_rate = (uint32_t)wfex->nSamplesPerSec; monitor->channels = wfex->nChannels; monitor->resampler = audio_resampler_create(&to, &from); if (!monitor->resampler) { goto fail; } /* ------------------------------------------ * * Init client */ hr = monitor->client->lpVtbl->GetBufferSize(monitor->client, &frames); if (FAILED(hr)) { goto fail; } hr = monitor->client->lpVtbl->GetService(monitor->client, &IID_IAudioRenderClient, (void**)&monitor->render); if (FAILED(hr)) { goto fail; } if (pthread_mutex_init(&monitor->playback_mutex, NULL) != 0) { goto fail; } hr = monitor->client->lpVtbl->Start(monitor->client); if (FAILED(hr)) { goto fail; } success = true; fail: safe_release(immde); if (wfex) CoTaskMemFree(wfex); return success; }