Add input-focus support for System 7, where the Control Manager can't do
[u/mdw/putty] / winstore.c
index 3199c1c..90d262f 100644 (file)
@@ -6,6 +6,7 @@
 #include <windows.h>
 #include <stdio.h>
 #include <stdlib.h>
+#include <limits.h>
 #include "putty.h"
 #include "storage.h"
 
@@ -66,7 +67,10 @@ void *open_settings_w(const char *sessionname)
     int ret;
     char *p;
 
-    p = smalloc(3 * strlen(sessionname) + 1);
+    if (!sessionname || !*sessionname)
+       sessionname = "Default Settings";
+
+    p = snewn(3 * strlen(sessionname) + 1, char);
     mungestr(sessionname, p);
 
     ret = RegCreateKey(HKEY_CURRENT_USER, puttystr, &subkey1);
@@ -106,7 +110,10 @@ void *open_settings_r(const char *sessionname)
     HKEY subkey1, sesskey;
     char *p;
 
-    p = smalloc(3 * strlen(sessionname) + 1);
+    if (!sessionname || !*sessionname)
+       sessionname = "Default Settings";
+
+    p = snewn(3 * strlen(sessionname) + 1, char);
     mungestr(sessionname, p);
 
     if (RegOpenKey(HKEY_CURRENT_USER, puttystr, &subkey1) != ERROR_SUCCESS) {
@@ -150,6 +157,67 @@ int read_setting_i(void *handle, const char *key, int defvalue)
        return val;
 }
 
+int read_setting_fontspec(void *handle, const char *name, FontSpec *result)
+{
+    char *settingname;
+    FontSpec ret;
+
+    if (!read_setting_s(handle, name, ret.name, sizeof(ret.name)))
+       return 0;
+    settingname = dupcat(name, "IsBold", NULL);
+    ret.isbold = read_setting_i(handle, settingname, -1);
+    sfree(settingname);
+    if (ret.isbold == -1) return 0;
+    settingname = dupcat(name, "CharSet", NULL);
+    ret.charset = read_setting_i(handle, settingname, -1);
+    sfree(settingname);
+    if (ret.charset == -1) return 0;
+    settingname = dupcat(name, "Height", NULL);
+    ret.height = read_setting_i(handle, settingname, INT_MIN);
+    sfree(settingname);
+    if (ret.height == INT_MIN) return 0;
+    if (ret.height < 0) {
+       int oldh, newh;
+       HDC hdc = GetDC(NULL);
+       int logpix = GetDeviceCaps(hdc, LOGPIXELSY);
+       ReleaseDC(NULL, hdc);
+
+       oldh = -ret.height;
+       newh = MulDiv(oldh, 72, logpix) + 1;
+       if (MulDiv(newh, logpix, 72) > oldh)
+           newh--;
+       ret.height = newh;
+    }
+    *result = ret;
+    return 1;
+}
+
+void write_setting_fontspec(void *handle, const char *name, FontSpec font)
+{
+    char *settingname;
+
+    write_setting_s(handle, name, font.name);
+    settingname = dupcat(name, "IsBold", NULL);
+    write_setting_i(handle, settingname, font.isbold);
+    sfree(settingname);
+    settingname = dupcat(name, "CharSet", NULL);
+    write_setting_i(handle, settingname, font.charset);
+    sfree(settingname);
+    settingname = dupcat(name, "Height", NULL);
+    write_setting_i(handle, settingname, font.height);
+    sfree(settingname);
+}
+
+int read_setting_filename(void *handle, const char *name, Filename *result)
+{
+    return !!read_setting_s(handle, name, result->path, sizeof(result->path));
+}
+
+void write_setting_filename(void *handle, const char *name, Filename result)
+{
+    write_setting_s(handle, name, result.path);
+}
+
 void close_settings_r(void *handle)
 {
     RegCloseKey((HKEY) handle);
@@ -163,7 +231,7 @@ void del_settings(const char *sessionname)
     if (RegOpenKey(HKEY_CURRENT_USER, puttystr, &subkey1) != ERROR_SUCCESS)
        return;
 
-    p = smalloc(3 * strlen(sessionname) + 1);
+    p = snewn(3 * strlen(sessionname) + 1, char);
     mungestr(sessionname, p);
     RegDeleteKey(subkey1, p);
     sfree(p);
@@ -184,7 +252,7 @@ void *enum_settings_start(void)
     if (RegOpenKey(HKEY_CURRENT_USER, puttystr, &key) != ERROR_SUCCESS)
        return NULL;
 
-    ret = smalloc(sizeof(*ret));
+    ret = snew(struct enumsettings);
     if (ret) {
        ret->key = key;
        ret->i = 0;
@@ -197,7 +265,7 @@ char *enum_settings_next(void *handle, char *buffer, int buflen)
 {
     struct enumsettings *e = (struct enumsettings *) handle;
     char *otherbuf;
-    otherbuf = smalloc(3 * buflen);
+    otherbuf = snewn(3 * buflen, char);
     if (RegEnumKey(e->key, e->i++, otherbuf, 3 * buflen) == ERROR_SUCCESS) {
        unmungestr(otherbuf, buffer, buflen);
        sfree(otherbuf);
@@ -242,8 +310,8 @@ int verify_host_key(const char *hostname, int port,
      * Now read a saved key in from the registry and see what it
      * says.
      */
-    otherstr = smalloc(len);
-    regname = smalloc(3 * (strlen(hostname) + strlen(keytype)) + 15);
+    otherstr = snewn(len, char);
+    regname = snewn(3 * (strlen(hostname) + strlen(keytype)) + 15, char);
 
     hostkey_regname(regname, hostname, port, keytype);
 
@@ -262,7 +330,7 @@ int verify_host_key(const char *hostname, int port,
         * under just the hostname and translate that.
         */
        char *justhost = regname + 1 + strcspn(regname, ":");
-       char *oldstyle = smalloc(len + 10);     /* safety margin */
+       char *oldstyle = snewn(len + 10, char); /* safety margin */
        readlen = len;
        ret = RegQueryValueEx(rkey, justhost, NULL, &type,
                              oldstyle, &readlen);
@@ -338,7 +406,7 @@ void store_host_key(const char *hostname, int port,
     char *regname;
     HKEY rkey;
 
-    regname = smalloc(3 * (strlen(hostname) + strlen(keytype)) + 15);
+    regname = snewn(3 * (strlen(hostname) + strlen(keytype)) + 15, char);
 
     hostkey_regname(regname, hostname, port, keytype);