2011-11-24 06:56:57 +00:00
|
|
|
/* windows.c */
|
|
|
|
/* implements Windows specific functions */
|
2012-09-09 16:06:44 +00:00
|
|
|
#include "dive.h"
|
2013-05-03 20:32:23 +00:00
|
|
|
#include "display.h"
|
2014-03-25 14:55:56 +00:00
|
|
|
#define _WIN32_WINNT 0x500
|
2011-11-24 06:56:57 +00:00
|
|
|
#include <windows.h>
|
2012-09-09 16:06:44 +00:00
|
|
|
#include <shlobj.h>
|
2013-12-19 13:00:50 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <dirent.h>
|
|
|
|
#include <zip.h>
|
2013-01-12 01:07:22 +00:00
|
|
|
|
2014-04-19 18:21:16 +00:00
|
|
|
const char system_divelist_default_font[] = "Calibri";
|
2014-03-21 17:56:10 +00:00
|
|
|
const int system_divelist_default_font_size = 8;
|
2011-11-24 06:56:57 +00:00
|
|
|
|
2014-04-14 21:33:46 +00:00
|
|
|
void subsurface_user(struct user_info *user)
|
|
|
|
{ /* Encourage use of at least libgit2-0.20 */ }
|
|
|
|
|
2013-01-12 01:07:22 +00:00
|
|
|
const char *system_default_filename(void)
|
2012-09-09 16:06:44 +00:00
|
|
|
{
|
2013-01-12 01:07:22 +00:00
|
|
|
char datapath[MAX_PATH];
|
|
|
|
const char *user;
|
|
|
|
char *buffer;
|
|
|
|
int len;
|
2012-09-09 16:06:44 +00:00
|
|
|
|
2013-10-07 04:04:25 +00:00
|
|
|
/* I don't think this works on Windows */
|
2013-10-09 13:10:12 +00:00
|
|
|
user = getenv("USERNAME");
|
2014-02-28 04:09:57 +00:00
|
|
|
if (!SUCCEEDED(SHGetFolderPath(NULL, CSIDL_APPDATA, NULL, 0, datapath))) {
|
2013-01-12 01:07:22 +00:00
|
|
|
datapath[0] = '.';
|
|
|
|
datapath[1] = '\0';
|
2012-09-09 16:06:44 +00:00
|
|
|
}
|
2013-01-12 01:07:22 +00:00
|
|
|
len = strlen(datapath) + strlen(user) + 17;
|
|
|
|
buffer = malloc(len);
|
|
|
|
snprintf(buffer, len, "%s\\Subsurface\\%s.xml", datapath, user);
|
|
|
|
return buffer;
|
2012-09-09 16:06:44 +00:00
|
|
|
}
|
|
|
|
|
2014-02-28 04:09:57 +00:00
|
|
|
int enumerate_devices(device_callback_t callback, void *userdata)
|
2013-09-16 21:04:42 +00:00
|
|
|
{
|
|
|
|
// Open the registry key.
|
|
|
|
HKEY hKey;
|
|
|
|
int index = -1;
|
2014-02-28 04:09:57 +00:00
|
|
|
LONG rc = RegOpenKeyEx(HKEY_LOCAL_MACHINE, "HARDWARE\\DEVICEMAP\\SERIALCOMM", 0, KEY_QUERY_VALUE, &hKey);
|
2013-09-16 21:04:42 +00:00
|
|
|
if (rc != ERROR_SUCCESS) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the number of values.
|
|
|
|
DWORD count = 0;
|
2014-02-28 04:09:57 +00:00
|
|
|
rc = RegQueryInfoKey(hKey, NULL, NULL, NULL, NULL, NULL, NULL, &count, NULL, NULL, NULL, NULL);
|
2013-09-16 21:04:42 +00:00
|
|
|
if (rc != ERROR_SUCCESS) {
|
|
|
|
RegCloseKey(hKey);
|
|
|
|
return -1;
|
|
|
|
}
|
2013-09-17 11:50:54 +00:00
|
|
|
DWORD i;
|
|
|
|
for (i = 0; i < count; ++i) {
|
2013-09-16 21:04:42 +00:00
|
|
|
// Get the value name, data and type.
|
|
|
|
char name[512], data[512];
|
2014-02-28 04:09:57 +00:00
|
|
|
DWORD name_len = sizeof(name);
|
|
|
|
DWORD data_len = sizeof(data);
|
2013-09-16 21:04:42 +00:00
|
|
|
DWORD type = 0;
|
2014-02-28 04:09:57 +00:00
|
|
|
rc = RegEnumValue(hKey, i, name, &name_len, NULL, &type, (LPBYTE)data, &data_len);
|
2013-09-16 21:04:42 +00:00
|
|
|
if (rc != ERROR_SUCCESS) {
|
|
|
|
RegCloseKey(hKey);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ignore non-string values.
|
|
|
|
if (type != REG_SZ)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Prevent a possible buffer overflow.
|
2014-02-28 04:09:57 +00:00
|
|
|
if (data_len >= sizeof(data)) {
|
2013-09-16 21:04:42 +00:00
|
|
|
RegCloseKey(hKey);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Null terminate the string.
|
|
|
|
data[data_len] = 0;
|
|
|
|
|
2014-02-28 04:09:57 +00:00
|
|
|
callback(data, userdata);
|
2013-09-16 21:04:42 +00:00
|
|
|
index++;
|
2013-09-17 11:50:54 +00:00
|
|
|
if (is_default_dive_computer_device(name))
|
2013-09-16 21:04:42 +00:00
|
|
|
index = i;
|
|
|
|
}
|
|
|
|
|
|
|
|
RegCloseKey(hKey);
|
|
|
|
return index;
|
|
|
|
}
|
2013-12-19 13:00:50 +00:00
|
|
|
|
|
|
|
/* this function converts a utf-8 string to win32's utf-16 2 byte string.
|
|
|
|
* the caller function should manage the allocated memory.
|
|
|
|
*/
|
|
|
|
static wchar_t *utf8_to_utf16_fl(const char *utf8, char *file, int line)
|
|
|
|
{
|
|
|
|
assert(utf8 != NULL);
|
|
|
|
assert(file != NULL);
|
|
|
|
assert(line);
|
|
|
|
/* estimate buffer size */
|
|
|
|
const int sz = strlen(utf8) + 1;
|
|
|
|
wchar_t *utf16 = (wchar_t *)malloc(sizeof(wchar_t) * sz);
|
|
|
|
if (!utf16) {
|
|
|
|
fprintf(stderr, "%s:%d: %s %d.", file, line, "cannot allocate buffer of size", sz);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (MultiByteToWideChar(CP_UTF8, 0, utf8, -1, utf16, sz))
|
|
|
|
return utf16;
|
|
|
|
fprintf(stderr, "%s:%d: %s", file, line, "cannot convert string.");
|
|
|
|
free((void *)utf16);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define utf8_to_utf16(s) utf8_to_utf16_fl(s, __FILE__, __LINE__)
|
|
|
|
|
|
|
|
/* bellow we provide a set of wrappers for some I/O functions to use wchar_t.
|
|
|
|
* on win32 this solves the issue that we need paths to be utf-16 encoded.
|
|
|
|
*/
|
2014-02-16 21:25:02 +00:00
|
|
|
int subsurface_rename(const char *path, const char *newpath)
|
|
|
|
{
|
|
|
|
int ret = -1;
|
|
|
|
if (!path || !newpath)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
wchar_t *wpath = utf8_to_utf16(path);
|
|
|
|
wchar_t *wnewpath = utf8_to_utf16(newpath);
|
|
|
|
|
|
|
|
if (wpath && wnewpath)
|
|
|
|
ret = _wrename(wpath, wnewpath);
|
|
|
|
free((void *)wpath);
|
|
|
|
free((void *)wnewpath);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-12-19 13:00:50 +00:00
|
|
|
int subsurface_open(const char *path, int oflags, mode_t mode)
|
|
|
|
{
|
|
|
|
int ret = -1;
|
2014-01-07 14:41:19 +00:00
|
|
|
if (!path)
|
|
|
|
return -1;
|
2013-12-19 13:00:50 +00:00
|
|
|
wchar_t *wpath = utf8_to_utf16(path);
|
|
|
|
if (wpath) {
|
|
|
|
ret = _wopen(wpath, oflags, mode);
|
|
|
|
free((void *)wpath);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
FILE *subsurface_fopen(const char *path, const char *mode)
|
|
|
|
{
|
|
|
|
FILE *ret = NULL;
|
2014-01-07 14:41:19 +00:00
|
|
|
if (!path)
|
|
|
|
return ret;
|
2013-12-19 13:00:50 +00:00
|
|
|
wchar_t *wpath = utf8_to_utf16(path);
|
|
|
|
if (wpath) {
|
|
|
|
const int len = strlen(mode);
|
|
|
|
wchar_t wmode[len + 1];
|
|
|
|
for (int i = 0; i < len; i++)
|
|
|
|
wmode[i] = (wchar_t)mode[i];
|
|
|
|
wmode[len] = 0;
|
|
|
|
ret = _wfopen(wpath, wmode);
|
|
|
|
free((void *)wpath);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* here we return a void pointer instead of _WDIR or DIR pointer */
|
|
|
|
void *subsurface_opendir(const char *path)
|
|
|
|
{
|
|
|
|
_WDIR *ret = NULL;
|
2014-01-07 14:41:19 +00:00
|
|
|
if (!path)
|
|
|
|
return ret;
|
2013-12-19 13:00:50 +00:00
|
|
|
wchar_t *wpath = utf8_to_utf16(path);
|
|
|
|
if (wpath) {
|
|
|
|
ret = _wopendir(wpath);
|
|
|
|
free((void *)wpath);
|
|
|
|
return (void *)ret;
|
|
|
|
}
|
|
|
|
return (void *)ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifndef O_BINARY
|
|
|
|
#define O_BINARY 0
|
|
|
|
#endif
|
|
|
|
|
|
|
|
struct zip *subsurface_zip_open_readonly(const char *path, int flags, int *errorp)
|
|
|
|
{
|
2013-12-20 01:01:54 +00:00
|
|
|
#if defined(LIBZIP_VERSION_MAJOR)
|
|
|
|
/* libzip 0.10 has zip_fdopen, let's use it since zip_open doesn't have a
|
|
|
|
* wchar_t version */
|
2013-12-19 13:00:50 +00:00
|
|
|
int fd = subsurface_open(path, O_RDONLY | O_BINARY, 0);
|
|
|
|
struct zip *ret = zip_fdopen(fd, flags, errorp);
|
|
|
|
if (!ret)
|
|
|
|
close(fd);
|
|
|
|
return ret;
|
2013-12-20 01:01:54 +00:00
|
|
|
#else
|
|
|
|
return zip_open(path, flags, errorp);
|
|
|
|
#endif
|
2013-12-19 13:00:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int subsurface_zip_close(struct zip *zip)
|
|
|
|
{
|
|
|
|
return zip_close(zip);
|
|
|
|
}
|
2014-03-25 14:55:56 +00:00
|
|
|
|
|
|
|
/* win32 console */
|
|
|
|
static struct {
|
|
|
|
bool allocated;
|
|
|
|
UINT cp;
|
|
|
|
FILE *out, *err;
|
|
|
|
} console_desc;
|
|
|
|
|
|
|
|
void subsurface_console_init(bool dedicated)
|
|
|
|
{
|
2014-04-02 19:42:44 +00:00
|
|
|
(void)console_desc;
|
2014-03-25 14:55:56 +00:00
|
|
|
/* if this is a console app already, do nothing */
|
|
|
|
#ifndef WIN32_CONSOLE_APP
|
|
|
|
/* just in case of multiple calls */
|
|
|
|
memset((void *)&console_desc, 0, sizeof(console_desc));
|
|
|
|
/* the AttachConsole(..) call can be used to determine if the parent process
|
|
|
|
* is a terminal. if it succeeds, there is no need for a dedicated console
|
|
|
|
* window and we don't need to call the AllocConsole() function. on the other
|
|
|
|
* hand if the user has set the 'dedicated' flag to 'true' and if AttachConsole()
|
|
|
|
* has failed, we create a dedicated console window.
|
|
|
|
*/
|
|
|
|
console_desc.allocated = AttachConsole(ATTACH_PARENT_PROCESS);
|
|
|
|
if (console_desc.allocated)
|
|
|
|
dedicated = false;
|
|
|
|
if (!console_desc.allocated && dedicated)
|
|
|
|
console_desc.allocated = AllocConsole();
|
|
|
|
if (!console_desc.allocated)
|
|
|
|
return;
|
|
|
|
|
|
|
|
console_desc.cp = GetConsoleCP();
|
|
|
|
SetConsoleOutputCP(CP_UTF8); /* make the ouput utf8 */
|
|
|
|
|
|
|
|
/* set some console modes; we don't need to reset these back.
|
|
|
|
* ENABLE_EXTENDED_FLAGS = 0x0080, ENABLE_QUICK_EDIT_MODE = 0x0040 */
|
|
|
|
HANDLE h_in = GetStdHandle(STD_INPUT_HANDLE);
|
|
|
|
if (h_in) {
|
|
|
|
SetConsoleMode(h_in, 0x0080 | 0x0040);
|
|
|
|
CloseHandle(h_in);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* dedicated only; disable the 'x' button as it will close the main process as well */
|
|
|
|
HWND h_cw = GetConsoleWindow();
|
|
|
|
if (h_cw && dedicated) {
|
|
|
|
SetWindowTextA(h_cw, "Subsurface Console");
|
|
|
|
HMENU h_menu = GetSystemMenu(h_cw, 0);
|
|
|
|
if (h_menu) {
|
|
|
|
EnableMenuItem(h_menu, SC_CLOSE, MF_BYCOMMAND | MF_DISABLED);
|
|
|
|
DrawMenuBar(h_cw);
|
|
|
|
}
|
|
|
|
SetConsoleCtrlHandler(NULL, TRUE); /* disable the CTRL handler */
|
|
|
|
}
|
|
|
|
|
|
|
|
/* redirect; on win32, CON is a reserved pipe target, like NUL */
|
|
|
|
console_desc.out = freopen("CON", "w", stdout);
|
|
|
|
console_desc.err = freopen("CON", "w", stderr);
|
|
|
|
if (!dedicated)
|
|
|
|
puts(""); /* add an empty line */
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void subsurface_console_exit(void)
|
|
|
|
{
|
|
|
|
#ifndef WIN32_CONSOLE_APP
|
|
|
|
if (!console_desc.allocated)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* close handles */
|
|
|
|
if (console_desc.out)
|
|
|
|
fclose(console_desc.out);
|
|
|
|
if (console_desc.err)
|
|
|
|
fclose(console_desc.err);
|
|
|
|
|
|
|
|
/* reset code page and free */
|
|
|
|
SetConsoleOutputCP(console_desc.cp);
|
|
|
|
FreeConsole();
|
|
|
|
#endif
|
|
|
|
}
|