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"
|
|
|
|
#if USE_GTK_UI
|
2011-11-24 06:56:57 +00:00
|
|
|
#include "display-gtk.h"
|
2013-05-03 20:32:23 +00:00
|
|
|
#endif
|
2011-11-24 06:56:57 +00:00
|
|
|
#include <windows.h>
|
2012-09-09 16:06:44 +00:00
|
|
|
#include <shlobj.h>
|
2013-01-12 01:07:22 +00:00
|
|
|
|
|
|
|
const char system_divelist_default_font[] = "Sans 8";
|
2011-11-24 06:56:57 +00:00
|
|
|
|
|
|
|
static HKEY hkey;
|
|
|
|
|
|
|
|
void subsurface_open_conf(void)
|
|
|
|
{
|
|
|
|
LONG success;
|
|
|
|
|
2011-11-26 04:09:01 +00:00
|
|
|
success = RegCreateKeyEx(HKEY_CURRENT_USER, (LPCTSTR)TEXT("Software\\subsurface"),
|
2012-10-04 20:57:06 +00:00
|
|
|
0L, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS,
|
|
|
|
NULL, &hkey, NULL);
|
2011-11-26 03:48:53 +00:00
|
|
|
if (success != ERROR_SUCCESS)
|
|
|
|
printf("CreateKey Software\\subsurface failed %ld\n", success);
|
2011-11-24 06:56:57 +00:00
|
|
|
}
|
|
|
|
|
2013-04-01 10:51:49 +00:00
|
|
|
void subsurface_unset_conf(const char *name)
|
2013-01-11 01:26:10 +00:00
|
|
|
{
|
2013-03-02 14:14:41 +00:00
|
|
|
RegDeleteValue(hkey, (LPCTSTR)name);
|
2013-01-11 01:26:10 +00:00
|
|
|
}
|
|
|
|
|
2013-04-01 10:51:49 +00:00
|
|
|
void subsurface_set_conf(const char *name, const char *value)
|
2011-11-24 06:56:57 +00:00
|
|
|
{
|
2011-11-26 04:09:01 +00:00
|
|
|
/* since we are using the pointer 'value' as both an actual
|
|
|
|
* pointer to the string setting and as a way to pass the
|
|
|
|
* numbers 0 and 1 to this function for booleans, one of the
|
|
|
|
* calls to RegSetValueEx needs to pass &value (when we want
|
|
|
|
* to pass the boolean value), the other one passes value (the
|
|
|
|
* address of the string. */
|
2012-10-04 20:57:06 +00:00
|
|
|
int wlen;
|
2013-01-26 00:17:55 +00:00
|
|
|
wchar_t *wname, *wstring;
|
2012-10-04 20:57:06 +00:00
|
|
|
|
|
|
|
wname = (wchar_t *)g_utf8_to_utf16(name, -1, NULL, NULL, NULL);
|
|
|
|
if (!wname)
|
|
|
|
return;
|
2013-01-11 03:33:53 +00:00
|
|
|
|
|
|
|
wlen = g_utf8_strlen((char *)value, -1);
|
|
|
|
wstring = (wchar_t *)g_utf8_to_utf16((char *)value, -1, NULL, NULL, NULL);
|
|
|
|
if (!wstring || !wlen) {
|
|
|
|
free(wname);
|
|
|
|
return;
|
2011-11-24 06:56:57 +00:00
|
|
|
}
|
2013-01-11 03:33:53 +00:00
|
|
|
RegSetValueExW(hkey, (LPCWSTR)wname, 0, REG_SZ, (const BYTE *)wstring,
|
|
|
|
wlen * sizeof(wchar_t));
|
|
|
|
free(wstring);
|
2012-10-04 20:57:06 +00:00
|
|
|
free(wname);
|
2011-11-24 06:56:57 +00:00
|
|
|
}
|
|
|
|
|
2013-04-01 10:51:49 +00:00
|
|
|
void subsurface_set_conf_int(const char *name, int value)
|
2013-01-11 03:33:53 +00:00
|
|
|
{
|
2013-03-01 00:45:32 +00:00
|
|
|
RegSetValueEx(hkey, (LPCTSTR)name, 0, REG_DWORD, (const BYTE *)&value, 4);
|
2013-01-11 03:33:53 +00:00
|
|
|
}
|
|
|
|
|
2013-04-01 10:51:49 +00:00
|
|
|
void subsurface_set_conf_bool(const char *name, int value)
|
2013-02-12 12:03:23 +00:00
|
|
|
{
|
2013-03-01 00:45:32 +00:00
|
|
|
subsurface_set_conf_int(name, value);
|
2013-02-12 12:03:23 +00:00
|
|
|
}
|
|
|
|
|
2013-04-01 10:51:49 +00:00
|
|
|
const char *subsurface_get_conf(const char *name)
|
2011-11-24 06:56:57 +00:00
|
|
|
{
|
2012-10-04 20:57:06 +00:00
|
|
|
const int csize = 64;
|
|
|
|
int blen = 0;
|
|
|
|
LONG ret = ERROR_MORE_DATA;
|
2013-01-26 00:17:55 +00:00
|
|
|
wchar_t *wstring = NULL, *wname;
|
2012-10-04 20:57:06 +00:00
|
|
|
char *utf8_string;
|
2011-11-24 06:56:57 +00:00
|
|
|
|
2013-01-11 03:33:53 +00:00
|
|
|
wname = (wchar_t *)g_utf8_to_utf16(name, -1, NULL, NULL, NULL);
|
|
|
|
if (!wname)
|
|
|
|
return NULL;
|
|
|
|
blen = 0;
|
|
|
|
/* lest try to load the string in chunks of 'csize' bytes until it fits */
|
2013-01-29 21:10:46 +00:00
|
|
|
while (ret == ERROR_MORE_DATA) {
|
2013-01-11 03:33:53 +00:00
|
|
|
blen += csize;
|
|
|
|
wstring = (wchar_t *)realloc(wstring, blen * sizeof(wchar_t));
|
|
|
|
ret = RegQueryValueExW(hkey, (LPCWSTR)wname, NULL, NULL,
|
|
|
|
(LPBYTE)wstring, (LPDWORD)&blen);
|
|
|
|
}
|
|
|
|
/* that's what happens the first time we start - just return NULL */
|
|
|
|
if (ret != ERROR_SUCCESS) {
|
2012-10-04 20:57:06 +00:00
|
|
|
free(wname);
|
2013-01-11 03:33:53 +00:00
|
|
|
free(wstring);
|
|
|
|
return NULL;
|
2011-11-24 06:56:57 +00:00
|
|
|
}
|
2013-01-11 03:33:53 +00:00
|
|
|
/* convert the returned string into utf-8 */
|
|
|
|
utf8_string = g_utf16_to_utf8(wstring, -1, NULL, NULL, NULL);
|
|
|
|
free(wstring);
|
|
|
|
free(wname);
|
|
|
|
if (!utf8_string)
|
|
|
|
return NULL;
|
|
|
|
if (!g_utf8_validate(utf8_string, -1, NULL)) {
|
|
|
|
free(utf8_string);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return utf8_string;
|
|
|
|
}
|
|
|
|
|
2013-04-01 15:55:50 +00:00
|
|
|
int subsurface_get_conf_int(const char *name)
|
2013-01-11 03:33:53 +00:00
|
|
|
{
|
2013-03-01 00:45:32 +00:00
|
|
|
DWORD value = -1, len = 4;
|
2013-03-01 09:32:52 +00:00
|
|
|
LONG ret = RegQueryValueEx(hkey, (LPCTSTR)TEXT(name), NULL, NULL,
|
2013-03-01 00:45:32 +00:00
|
|
|
(LPBYTE)&value, (LPDWORD)&len);
|
2013-03-01 09:32:52 +00:00
|
|
|
if (ret != ERROR_SUCCESS)
|
|
|
|
return -1;
|
2013-03-01 00:45:32 +00:00
|
|
|
return value;
|
2011-11-24 06:56:57 +00:00
|
|
|
}
|
|
|
|
|
2013-04-01 15:55:50 +00:00
|
|
|
int subsurface_get_conf_bool(const char *name)
|
2013-02-12 12:03:23 +00:00
|
|
|
{
|
2013-03-01 00:45:32 +00:00
|
|
|
int ret = subsurface_get_conf_int(name);
|
|
|
|
if (ret == -1)
|
2013-03-01 09:32:52 +00:00
|
|
|
return ret;
|
2013-03-01 00:45:32 +00:00
|
|
|
return ret != 0;
|
2013-02-12 12:03:23 +00:00
|
|
|
}
|
|
|
|
|
2012-05-02 17:03:48 +00:00
|
|
|
void subsurface_flush_conf(void)
|
2011-11-24 06:56:57 +00:00
|
|
|
{
|
2012-05-05 16:30:39 +00:00
|
|
|
/* this is a no-op */
|
2012-05-02 17:03:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void subsurface_close_conf(void)
|
|
|
|
{
|
2011-11-24 06:56:57 +00:00
|
|
|
RegCloseKey(hkey);
|
|
|
|
}
|
2011-12-14 04:34:56 +00:00
|
|
|
|
2013-05-03 20:32:23 +00:00
|
|
|
#if USE_GTK_UI
|
2012-10-26 22:52:39 +00:00
|
|
|
int subsurface_fill_device_list(GtkListStore *store)
|
2011-12-14 04:34:56 +00:00
|
|
|
{
|
2012-10-29 19:48:43 +00:00
|
|
|
const int bufdef = 512;
|
|
|
|
const char *dlabels[] = {"UEMISSDA", NULL};
|
|
|
|
const char *devdef = "COM1";
|
2012-10-26 22:52:39 +00:00
|
|
|
GtkTreeIter iter;
|
2012-10-29 19:48:43 +00:00
|
|
|
int index = -1, nentries = 0, ret, i;
|
|
|
|
char bufname[bufdef], bufval[bufdef], *p;
|
|
|
|
DWORD nvalues, bufval_len, bufname_len;
|
|
|
|
HKEY key;
|
|
|
|
|
|
|
|
/* add serial ports */
|
|
|
|
ret = RegOpenKeyExA(HKEY_LOCAL_MACHINE, "HARDWARE\\DEVICEMAP\\SERIALCOMM",
|
|
|
|
0, KEY_READ, &key);
|
|
|
|
if (ret == ERROR_SUCCESS) {
|
|
|
|
ret = RegQueryInfoKeyA(key, NULL, NULL, NULL, NULL, NULL, NULL, &nvalues,
|
|
|
|
NULL, NULL, NULL, NULL);
|
|
|
|
if (ret == ERROR_SUCCESS)
|
|
|
|
for (i = 0; i < nvalues; i++) {
|
|
|
|
memset(bufval, 0, bufdef);
|
|
|
|
memset(bufname, 0, bufdef);
|
|
|
|
bufname_len = bufdef;
|
|
|
|
bufval_len = bufdef;
|
|
|
|
ret = RegEnumValueA(key, i, bufname, &bufname_len, NULL, NULL, bufval,
|
|
|
|
&bufval_len);
|
|
|
|
if (ret == ERROR_SUCCESS) {
|
|
|
|
gtk_list_store_append(store, &iter);
|
|
|
|
gtk_list_store_set(store, &iter, 0, bufval, -1);
|
|
|
|
if (is_default_dive_computer_device(bufval))
|
|
|
|
index = nentries;
|
|
|
|
nentries++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* add drive letters that match labels */
|
|
|
|
memset(bufname, 0, bufdef);
|
|
|
|
bufname_len = bufdef;
|
|
|
|
if (GetLogicalDriveStringsA(bufname_len, bufname)) {
|
|
|
|
p = bufname;
|
|
|
|
while (*p) {
|
|
|
|
memset(bufval, 0, bufdef);
|
|
|
|
if (GetVolumeInformationA(p, bufval, bufdef, NULL, NULL, NULL, NULL, 0)) {
|
|
|
|
for (i = 0; dlabels[i] != NULL; i++)
|
|
|
|
if (!strcmp(bufval, dlabels[i])) {
|
2012-10-31 23:51:04 +00:00
|
|
|
char name[80];
|
|
|
|
snprintf(name, sizeof(name), "%s (%s)", p, dlabels[i]);
|
2012-10-29 19:48:43 +00:00
|
|
|
gtk_list_store_append(store, &iter);
|
2012-10-31 23:51:04 +00:00
|
|
|
gtk_list_store_set(store, &iter, 0, name, -1);
|
2012-10-29 19:48:43 +00:00
|
|
|
if (is_default_dive_computer_device(p))
|
|
|
|
index = nentries;
|
|
|
|
nentries++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
p = &p[strlen(p) + 1];
|
|
|
|
}
|
|
|
|
}
|
2012-10-26 22:52:39 +00:00
|
|
|
/* if we can't find anything, use the default */
|
2012-10-29 19:48:43 +00:00
|
|
|
if (!nentries) {
|
|
|
|
gtk_list_store_append(store, &iter);
|
|
|
|
gtk_list_store_set(store, &iter,
|
|
|
|
0, devdef, -1);
|
|
|
|
if (is_default_dive_computer_device(devdef))
|
|
|
|
index = 0;
|
|
|
|
}
|
2012-10-26 22:52:39 +00:00
|
|
|
return index;
|
2011-12-14 04:34:56 +00:00
|
|
|
}
|
2013-05-03 20:32:23 +00:00
|
|
|
#endif /* USE_GTK_UI */
|
2011-12-28 23:57:36 +00:00
|
|
|
|
|
|
|
const char *subsurface_icon_name()
|
|
|
|
{
|
|
|
|
return "subsurface.ico";
|
|
|
|
}
|
2012-01-01 21:41:47 +00:00
|
|
|
|
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-01-12 01:07:22 +00:00
|
|
|
user = g_get_user_name();
|
|
|
|
if (! SUCCEEDED(SHGetFolderPath(NULL, CSIDL_APPDATA, NULL, 0, datapath))) {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2012-10-18 21:30:45 +00:00
|
|
|
const char *subsurface_gettext_domainpath(char *argv0)
|
2012-10-15 12:44:01 +00:00
|
|
|
{
|
2012-10-18 21:30:45 +00:00
|
|
|
/* first hackishly make sure that the LANGUAGE information is correctly set up
|
|
|
|
* in the environment */
|
2012-10-15 23:32:11 +00:00
|
|
|
char buffer[80];
|
2013-02-09 19:29:56 +00:00
|
|
|
gchar *locale = g_win32_getlocale();
|
|
|
|
snprintf(buffer, sizeof(buffer), "LANGUAGE=%s.UTF-8", locale);
|
2012-10-15 23:32:11 +00:00
|
|
|
putenv(buffer);
|
2013-02-09 19:29:56 +00:00
|
|
|
g_free(locale);
|
2012-10-18 21:30:45 +00:00
|
|
|
/* always use translation directory relative to install location, regardless of argv0 */
|
2012-10-16 11:04:37 +00:00
|
|
|
return "./share/locale";
|
2012-10-15 12:44:01 +00:00
|
|
|
}
|
|
|
|
|
2013-05-03 20:32:23 +00:00
|
|
|
#if USE_GTK_UI
|
2012-01-01 21:41:47 +00:00
|
|
|
void subsurface_ui_setup(GtkSettings *settings, GtkWidget *menubar,
|
2012-01-03 04:49:10 +00:00
|
|
|
GtkWidget *vbox, GtkUIManager *ui_manager)
|
2012-01-01 21:41:47 +00:00
|
|
|
{
|
|
|
|
gtk_box_pack_start(GTK_BOX(vbox), menubar, FALSE, FALSE, 0);
|
|
|
|
}
|
2013-05-03 20:32:23 +00:00
|
|
|
#endif /* USE_GTK_UI */
|
2012-10-04 00:44:47 +00:00
|
|
|
|
|
|
|
/* barely documented API */
|
|
|
|
extern int __wgetmainargs(int *, wchar_t ***, wchar_t ***, int, int *);
|
|
|
|
|
|
|
|
/* expand-convert the UTF-16 argument list to a list of UTF-8 strings */
|
|
|
|
void subsurface_command_line_init(gint *argc, gchar ***argv)
|
|
|
|
{
|
2013-03-05 20:24:15 +00:00
|
|
|
wchar_t **wargv, **wenviron, *p, path[MAX_PATH] = {0};
|
2012-10-04 00:44:47 +00:00
|
|
|
gchar **argv_new;
|
|
|
|
gchar *s;
|
|
|
|
/* for si we assume that a struct address will equal the address
|
|
|
|
* of its first and only int member */
|
|
|
|
gint i, n, ret, si;
|
|
|
|
|
2013-03-05 19:56:27 +00:00
|
|
|
/* change the current process path to the module path, so that we can
|
|
|
|
* access relative folders such as ./share and ./xslt */
|
|
|
|
GetModuleFileNameW(NULL, path, MAX_PATH - 1);
|
|
|
|
p = wcsrchr(path, '\\');
|
|
|
|
*(p + 1) = '\0';
|
|
|
|
SetCurrentDirectoryW(path);
|
|
|
|
|
2012-10-04 00:44:47 +00:00
|
|
|
/* memory leak tools may reports a potential issue here at a call
|
|
|
|
* to strcpy_s in msvcrt, wich should be a false positive. but even if there
|
|
|
|
* is some kind of a leak, it should be unique and have the same
|
|
|
|
* lifespan as the process heap. */
|
|
|
|
ret = __wgetmainargs(&n, &wargv, &wenviron, TRUE, &si);
|
|
|
|
if (ret < 0) {
|
|
|
|
g_warning("Cannot convert command line");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
argv_new = g_malloc(sizeof(gchar *) * (n + 1));
|
|
|
|
|
|
|
|
for (i = 0; i < n; ++i) {
|
|
|
|
s = g_utf16_to_utf8((gunichar2 *)wargv[i], -1, NULL, NULL, NULL);
|
|
|
|
if (!s) {
|
|
|
|
g_warning("Cannot convert command line argument (%d) to UTF-8", (i + 1));
|
|
|
|
s = "\0";
|
|
|
|
} else if (!g_utf8_validate(s, -1, NULL)) {
|
|
|
|
g_warning("Cannot validate command line argument '%s' (%d)", s, (i + 1));
|
|
|
|
g_free(s);
|
|
|
|
s = "\0";
|
|
|
|
}
|
|
|
|
argv_new[i] = s;
|
|
|
|
}
|
|
|
|
argv_new[n] = NULL;
|
|
|
|
|
|
|
|
/* update the argument list and count */
|
|
|
|
if (argv && argc) {
|
|
|
|
*argv = argv_new;
|
|
|
|
*argc = n;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* once done, free the argument list */
|
|
|
|
void subsurface_command_line_exit(gint *argc, gchar ***argv)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < *argc; i++)
|
|
|
|
g_free((*argv)[i]);
|
|
|
|
g_free(*argv);
|
|
|
|
}
|
2012-10-19 23:31:06 +00:00
|
|
|
|
2013-01-14 23:20:29 +00:00
|
|
|
gboolean subsurface_launch_for_uri(const char* uri)
|
|
|
|
{
|
2013-01-25 14:55:55 +00:00
|
|
|
gboolean ret = FALSE;
|
|
|
|
wchar_t *wuri = (wchar_t *)g_utf8_to_utf16(uri, -1, NULL, NULL, NULL);
|
|
|
|
if (wuri) {
|
|
|
|
if ((INT_PTR)ShellExecuteW(NULL, L"open", wuri, NULL, NULL, SW_SHOWNORMAL) > 32)
|
|
|
|
ret = TRUE;
|
|
|
|
free(wuri);
|
|
|
|
}
|
|
|
|
if (!ret)
|
|
|
|
g_message("ShellExecute failed for: %s", uri);
|
|
|
|
return ret;
|
2013-01-14 23:20:29 +00:00
|
|
|
}
|
|
|
|
|
2012-10-19 23:31:06 +00:00
|
|
|
/* check if we are running a newer OS version */
|
|
|
|
gboolean subsurface_os_feature_available(os_feature_t f)
|
|
|
|
{
|
|
|
|
switch (f) {
|
|
|
|
case UTF8_FONT_WITH_STARS:
|
|
|
|
if ((GetVersion() & 0xff) < 6)
|
|
|
|
return FALSE; /* version less than Vista */
|
|
|
|
else
|
|
|
|
return TRUE;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
}
|
2013-09-16 21:04:42 +00:00
|
|
|
|
|
|
|
int enumerate_devices (device_callback_t callback, void *userdata)
|
|
|
|
{
|
|
|
|
// Open the registry key.
|
|
|
|
HKEY hKey;
|
|
|
|
int index = -1;
|
|
|
|
LONG rc = RegOpenKeyEx (HKEY_LOCAL_MACHINE, "HARDWARE\\DEVICEMAP\\SERIALCOMM", 0, KEY_QUERY_VALUE, &hKey);
|
|
|
|
if (rc != ERROR_SUCCESS) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the number of values.
|
|
|
|
DWORD count = 0;
|
|
|
|
rc = RegQueryInfoKey (hKey, NULL, NULL, NULL, NULL, NULL, NULL, &count, NULL, NULL, NULL, NULL);
|
|
|
|
if (rc != ERROR_SUCCESS) {
|
|
|
|
RegCloseKey(hKey);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (DWORD i = 0; i < count; ++i) {
|
|
|
|
// Get the value name, data and type.
|
|
|
|
char name[512], data[512];
|
|
|
|
DWORD name_len = sizeof (name);
|
|
|
|
DWORD data_len = sizeof (data);
|
|
|
|
DWORD type = 0;
|
|
|
|
rc = RegEnumValue (hKey, i, name, &name_len, NULL, &type, (LPBYTE) data, &data_len);
|
|
|
|
if (rc != ERROR_SUCCESS) {
|
|
|
|
RegCloseKey(hKey);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ignore non-string values.
|
|
|
|
if (type != REG_SZ)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Prevent a possible buffer overflow.
|
|
|
|
if (data_len >= sizeof (data)) {
|
|
|
|
RegCloseKey(hKey);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Null terminate the string.
|
|
|
|
data[data_len] = 0;
|
|
|
|
|
|
|
|
callback (data, userdata);
|
|
|
|
index++;
|
|
|
|
if (is_default_dive_computer_device(filename))
|
|
|
|
index = i;
|
|
|
|
}
|
|
|
|
|
|
|
|
RegCloseKey(hKey);
|
|
|
|
return index;
|
|
|
|
}
|