diff options
-rw-r--r-- | common/linux/freeze.cpp | 408 | ||||
-rw-r--r-- | common/linux/freeze.h | 44 | ||||
-rw-r--r-- | ldmicro/includes/linuxUI/linuxUI.h | 7 | ||||
-rw-r--r-- | ldmicro/linuxUI.cpp | 2 |
4 files changed, 248 insertions, 213 deletions
diff --git a/common/linux/freeze.cpp b/common/linux/freeze.cpp index 01724a6..0d27ca7 100644 --- a/common/linux/freeze.cpp +++ b/common/linux/freeze.cpp @@ -8,205 +8,221 @@ */ #include "freeze.h" #include <linuxUI/linuxUI.h> +#include <cstdlib> +#include <fstream> #include <stdlib.h> #include <stdio.h> -// /* -// * store a window's position in the registry, or fail silently if the registry calls don't work -// */ -// void FreezeWindowPosF(HWND hwnd, char *subKey, char *name) -// { -// RECT r; -// GetWindowRect(hwnd, &r); - -// HKEY software; -// if(RegOpenKeyEx(HKEY_CURRENT_USER, "Software", 0, KEY_ALL_ACCESS, &software) != ERROR_SUCCESS) -// return; - -// char *keyName = (char *)malloc(strlen(name) + 30); -// if(!keyName) -// return; - -// HKEY sub; -// if(RegCreateKeyEx(software, subKey, 0, "", REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &sub, NULL) != ERROR_SUCCESS) -// return; - -// sprintf(keyName, "%s_left", name); -// if(RegSetValueEx(sub, keyName, 0, REG_DWORD, (BYTE *)&(r.left), sizeof(DWORD)) != ERROR_SUCCESS) -// return; - -// sprintf(keyName, "%s_right", name); -// if(RegSetValueEx(sub, keyName, 0, REG_DWORD, (BYTE *)&(r.right), sizeof(DWORD)) != ERROR_SUCCESS) -// return; - -// sprintf(keyName, "%s_top", name); -// if(RegSetValueEx(sub, keyName, 0, REG_DWORD, (BYTE *)&(r.top), sizeof(DWORD)) != ERROR_SUCCESS) -// return; - -// sprintf(keyName, "%s_bottom", name); -// if(RegSetValueEx(sub, keyName, 0, REG_DWORD, (BYTE *)&(r.bottom), sizeof(DWORD)) != ERROR_SUCCESS) -// return; - -// sprintf(keyName, "%s_maximized", name); -// DWORD v = IsZoomed(hwnd); -// if(RegSetValueEx(sub, keyName, 0, REG_DWORD, (BYTE *)&(v), sizeof(DWORD)) != ERROR_SUCCESS) -// return; - -// free(keyName); -// } - -// static void Clamp(LONG *v, LONG min, LONG max) -// { -// if(*v < min) *v = min; -// if(*v > max) *v = max; -// } - -// /* -// * retrieve a window's position from the registry, or do nothing if there is no info saved -// */ -// void ThawWindowPosF(HWND hwnd, char *subKey, char *name) -// { -// HKEY software; -// if(RegOpenKeyEx(HKEY_CURRENT_USER, "Software", 0, KEY_ALL_ACCESS, &software) != ERROR_SUCCESS) -// return; - -// HKEY sub; -// if(RegOpenKeyEx(software, subKey, 0, KEY_ALL_ACCESS, &sub) != ERROR_SUCCESS) -// return; - -// char *keyName = (char *)malloc(strlen(name) + 30); -// if(!keyName) -// return; - -// DWORD l; -// RECT r; - -// sprintf(keyName, "%s_left", name); -// l = sizeof(DWORD); -// if(RegQueryValueEx(sub, keyName, NULL, NULL, (BYTE *)&(r.left), &l) != ERROR_SUCCESS) -// return; - -// sprintf(keyName, "%s_right", name); -// l = sizeof(DWORD); -// if(RegQueryValueEx(sub, keyName, NULL, NULL, (BYTE *)&(r.right), &l) != ERROR_SUCCESS) -// return; - -// sprintf(keyName, "%s_top", name); -// l = sizeof(DWORD); -// if(RegQueryValueEx(sub, keyName, NULL, NULL, (BYTE *)&(r.top), &l) != ERROR_SUCCESS) -// return; - -// sprintf(keyName, "%s_bottom", name); -// l = sizeof(DWORD); -// if(RegQueryValueEx(sub, keyName, NULL, NULL, (BYTE *)&(r.bottom), &l) != ERROR_SUCCESS) -// return; - -// sprintf(keyName, "%s_maximized", name); -// DWORD v; -// l = sizeof(DWORD); -// if(RegQueryValueEx(sub, keyName, NULL, NULL, (BYTE *)&v, &l) != ERROR_SUCCESS) -// return; -// if(v) -// ShowWindow(hwnd, SW_MAXIMIZE); - -// RECT dr; -// GetWindowRect(GetDesktopWindow(), &dr); - -// // If it somehow ended up off-screen, then put it back. -// Clamp(&(r.left), dr.left, dr.right); -// Clamp(&(r.right), dr.left, dr.right); -// Clamp(&(r.top), dr.top, dr.bottom); -// Clamp(&(r.bottom), dr.top, dr.bottom); -// if(r.right - r.left < 100) { -// r.left -= 300; r.right += 300; -// } -// if(r.bottom - r.top < 100) { -// r.top -= 300; r.bottom += 300; -// } -// Clamp(&(r.left), dr.left, dr.right); -// Clamp(&(r.right), dr.left, dr.right); -// Clamp(&(r.top), dr.top, dr.bottom); -// Clamp(&(r.bottom), dr.top, dr.bottom); - -// MoveWindow(hwnd, r.left, r.top, r.right - r.left, r.bottom - r.top, TRUE); - -// free(keyName); -// } - -// /* -// * store a DWORD setting in the registry -// */ -// void FreezeDWORDF(DWORD val, char *subKey, char *name) -// { -// HKEY software; -// if(RegOpenKeyEx(HKEY_CURRENT_USER, "Software", 0, KEY_ALL_ACCESS, &software) != ERROR_SUCCESS) -// return; - -// HKEY sub; -// if(RegCreateKeyEx(software, subKey, 0, "", REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &sub, NULL) != ERROR_SUCCESS) -// return; +/* + * store a window's position in the registry, or fail silently if the registry calls don't work + */ +void FreezeWindowPosF(HWND hwnd, char *subKey, char *name) +{ + if (-1 == system("mkdir -p /usr/share/ldmicro")) + return; + + std::ofstream Register("binConf", std::ios::binary | std::ios::trunc); + if (!Register.is_open()) + return; + + char *keyName = (char *)malloc(strlen(name) + 30); + if(!keyName) + return; + + Key newkey; + + int val; + + sprintf(keyName, "%s_width", name); + gtk_window_get_size(GTK_WINDOW(hwnd), &val, NULL); + newKey.name = keyName; + newKey.type = 'i'; + newKey.val = val; + Register.write((char*) &newKey, sizeof(newKey)); + + sprintf(keyName, "%s_height", name); + gtk_window_get_size(GTK_WINDOW(hwnd), NULL, &val); + newKey.name = keyName; + newKey.type = 'i'; + newKey.val = val; + Register.write((char*) &newKey, sizeof(newKey)); + + + sprintf(keyName, "%s_posX", name); + gtk_window_get_position(GTK_WINDOW(hwnd), &val, NULL); + newKey.name = keyName; + newKey.type = 'i'; + newKey.val = val; + Register.write((char*) &newKey, sizeof(newKey)); + + sprintf(keyName, "%s_posY", name); + gtk_window_get_position(GTK_WINDOW(hwnd), NULL, &val); + newKey.name = keyName; + newKey.type = 'i'; + newKey.val = val; + Register.write((char*) &newKey, sizeof(newKey)); + + sprintf(keyName, "%s_maximized", name); + newKey.name = keyName; + newKey.type = 'b'; + newKey.val = gtk_window_is_maximized(GTK_WINDOW(hwnd)); + Register.write((char*) &newKey, sizeof(newKey)); + + free(keyName); +} + +static void Clamp(LONG *v, LONG min, LONG max) +{ + if(*v < min) *v = min; + if(*v > max) *v = max; +} + +/* + * retrieve a window's position from the registry, or do nothing if there is no info saved + */ +void ThawWindowPosF(HWND hwnd, char *subKey, char *name) +{ + HKEY software; + if(RegOpenKeyEx(HKEY_CURRENT_USER, "Software", 0, KEY_ALL_ACCESS, &software) != ERROR_SUCCESS) + return; + + HKEY sub; + if(RegOpenKeyEx(software, subKey, 0, KEY_ALL_ACCESS, &sub) != ERROR_SUCCESS) + return; + + char *keyName = (char *)malloc(strlen(name) + 30); + if(!keyName) + return; + + DWORD l; + RECT r; + + sprintf(keyName, "%s_left", name); + l = sizeof(DWORD); + if(RegQueryValueEx(sub, keyName, NULL, NULL, (BYTE *)&(r.left), &l) != ERROR_SUCCESS) + return; + + sprintf(keyName, "%s_right", name); + l = sizeof(DWORD); + if(RegQueryValueEx(sub, keyName, NULL, NULL, (BYTE *)&(r.right), &l) != ERROR_SUCCESS) + return; + + sprintf(keyName, "%s_top", name); + l = sizeof(DWORD); + if(RegQueryValueEx(sub, keyName, NULL, NULL, (BYTE *)&(r.top), &l) != ERROR_SUCCESS) + return; + + sprintf(keyName, "%s_bottom", name); + l = sizeof(DWORD); + if(RegQueryValueEx(sub, keyName, NULL, NULL, (BYTE *)&(r.bottom), &l) != ERROR_SUCCESS) + return; + + sprintf(keyName, "%s_maximized", name); + DWORD v; + l = sizeof(DWORD); + if(RegQueryValueEx(sub, keyName, NULL, NULL, (BYTE *)&v, &l) != ERROR_SUCCESS) + return; + if(v) + ShowWindow(hwnd, SW_MAXIMIZE); + + RECT dr; + GetWindowRect(GetDesktopWindow(), &dr); + + // If it somehow ended up off-screen, then put it back. + Clamp(&(r.left), dr.left, dr.right); + Clamp(&(r.right), dr.left, dr.right); + Clamp(&(r.top), dr.top, dr.bottom); + Clamp(&(r.bottom), dr.top, dr.bottom); + if(r.right - r.left < 100) { + r.left -= 300; r.right += 300; + } + if(r.bottom - r.top < 100) { + r.top -= 300; r.bottom += 300; + } + Clamp(&(r.left), dr.left, dr.right); + Clamp(&(r.right), dr.left, dr.right); + Clamp(&(r.top), dr.top, dr.bottom); + Clamp(&(r.bottom), dr.top, dr.bottom); + + MoveWindow(hwnd, r.left, r.top, r.right - r.left, r.bottom - r.top, TRUE); + + free(keyName); +} + +/* + * store a DWORD setting in the registry + */ +void FreezeDWORDF(DWORD val, char *subKey, char *name) +{ + HKEY software; + if(RegOpenKeyEx(HKEY_CURRENT_USER, "Software", 0, KEY_ALL_ACCESS, &software) != ERROR_SUCCESS) + return; + + HKEY sub; + if(RegCreateKeyEx(software, subKey, 0, "", REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &sub, NULL) != ERROR_SUCCESS) + return; -// if(RegSetValueEx(sub, name, 0, REG_DWORD, (BYTE *)&val, sizeof(DWORD)) != ERROR_SUCCESS) -// return; -// } - -// /* -// * retrieve a DWORD setting, or return the default if that setting is unavailable -// */ -// DWORD ThawDWORDF(DWORD val, char *subKey, char *name) -// { -// HKEY software; -// if(RegOpenKeyEx(HKEY_CURRENT_USER, "Software", 0, KEY_ALL_ACCESS, &software) != ERROR_SUCCESS) -// return val; - -// HKEY sub; -// if(RegOpenKeyEx(software, subKey, 0, KEY_ALL_ACCESS, &sub) != ERROR_SUCCESS) -// return val; - -// DWORD l = sizeof(DWORD); -// DWORD v; -// if(RegQueryValueEx(sub, name, NULL, NULL, (BYTE *)&v, &l) != ERROR_SUCCESS) -// return val; - -// return v; -// } - -// /* -// * store a string setting in the registry -// */ -// void FreezeStringF(char *val, char *subKey, char *name) -// { -// HKEY software; -// if(RegOpenKeyEx(HKEY_CURRENT_USER, "Software", 0, KEY_ALL_ACCESS, &software) != ERROR_SUCCESS) -// return; - -// HKEY sub; -// if(RegCreateKeyEx(software, subKey, 0, "", REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &sub, NULL) != ERROR_SUCCESS) -// return; + if(RegSetValueEx(sub, name, 0, REG_DWORD, (BYTE *)&val, sizeof(DWORD)) != ERROR_SUCCESS) + return; +} + +/* + * retrieve a DWORD setting, or return the default if that setting is unavailable + */ +DWORD ThawDWORDF(DWORD val, char *subKey, char *name) +{ + HKEY software; + if(RegOpenKeyEx(HKEY_CURRENT_USER, "Software", 0, KEY_ALL_ACCESS, &software) != ERROR_SUCCESS) + return val; + + HKEY sub; + if(RegOpenKeyEx(software, subKey, 0, KEY_ALL_ACCESS, &sub) != ERROR_SUCCESS) + return val; + + DWORD l = sizeof(DWORD); + DWORD v; + if(RegQueryValueEx(sub, name, NULL, NULL, (BYTE *)&v, &l) != ERROR_SUCCESS) + return val; + + return v; +} + +/* + * store a string setting in the registry + */ +void FreezeStringF(char *val, char *subKey, char *name) +{ + HKEY software; + if(RegOpenKeyEx(HKEY_CURRENT_USER, "Software", 0, KEY_ALL_ACCESS, &software) != ERROR_SUCCESS) + return; + + HKEY sub; + if(RegCreateKeyEx(software, subKey, 0, "", REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &sub, NULL) != ERROR_SUCCESS) + return; -// if(RegSetValueEx(sub, name, 0, REG_SZ, (BYTE *)val, strlen(val)+1) != ERROR_SUCCESS) -// return; -// } - -// /* -// * retrieve a string setting, or return the default if that setting is unavailable -// */ -// void ThawStringF(char *val, int max, char *subKey, char *name) -// { -// HKEY software; -// if(RegOpenKeyEx(HKEY_CURRENT_USER, "Software", 0, KEY_ALL_ACCESS, &software) != ERROR_SUCCESS) -// return; - -// HKEY sub; -// if(RegOpenKeyEx(software, subKey, 0, KEY_ALL_ACCESS, &sub) != ERROR_SUCCESS) -// return; - -// DWORD l = max; -// if(RegQueryValueEx(sub, name, NULL, NULL, (BYTE *)val, &l) != ERROR_SUCCESS) -// return; -// if(l >= (DWORD)max) return; - -// val[l] = '\0'; -// return; -// } + if(RegSetValueEx(sub, name, 0, REG_SZ, (BYTE *)val, strlen(val)+1) != ERROR_SUCCESS) + return; +} + +/* + * retrieve a string setting, or return the default if that setting is unavailable + */ +void ThawStringF(char *val, int max, char *subKey, char *name) +{ + HKEY software; + if(RegOpenKeyEx(HKEY_CURRENT_USER, "Software", 0, KEY_ALL_ACCESS, &software) != ERROR_SUCCESS) + return; + + HKEY sub; + if(RegOpenKeyEx(software, subKey, 0, KEY_ALL_ACCESS, &sub) != ERROR_SUCCESS) + return; + + DWORD l = max; + if(RegQueryValueEx(sub, name, NULL, NULL, (BYTE *)val, &l) != ERROR_SUCCESS) + return; + if(l >= (DWORD)max) return; + + val[l] = '\0'; + return; +} diff --git a/common/linux/freeze.h b/common/linux/freeze.h index 892b5b1..0827f6e 100644 --- a/common/linux/freeze.h +++ b/common/linux/freeze.h @@ -10,27 +10,41 @@ #ifndef __FREEZE_H #define __FREEZE_H -// #ifndef FREEZE_SUBKEY -// #error must define FREEZE_SUBKEY to a string uniquely identifying the app -// #endif +#ifndef FREEZE_SUBKEY +#error must define FREEZE_SUBKEY to a string uniquely identifying the app +#endif + +#define FreezeWindowPos(hwnd) FreezeWindowPosF(hwnd, FREEZE_SUBKEY, #hwnd) +void FreezeWindowPosF(HWND hWnd, char *subKey, char *name); + +#define ThawWindowPos(hwnd) ThawWindowPosF(hwnd, FREEZE_SUBKEY, #hwnd) +void ThawWindowPosF(HWND hWnd, char *subKey, char *name); -// #define FreezeWindowPos(hwnd) FreezeWindowPosF(hwnd, FREEZE_SUBKEY, #hwnd) -// void FreezeWindowPosF(HWND hWnd, char *subKey, char *name); +#define FreezeDWORD(val) FreezeDWORDF(val, FREEZE_SUBKEY, #val) +void FreezeDWORDF(DWORD val, char *subKey, char *name); -// #define ThawWindowPos(hwnd) ThawWindowPosF(hwnd, FREEZE_SUBKEY, #hwnd) -// void ThawWindowPosF(HWND hWnd, char *subKey, char *name); +#define ThawDWORD(val) val = ThawDWORDF(val, FREEZE_SUBKEY, #val) +DWORD ThawDWORDF(DWORD val, char *subKey, char *name); -// #define FreezeDWORD(val) FreezeDWORDF(val, FREEZE_SUBKEY, #val) -// void FreezeDWORDF(DWORD val, char *subKey, char *name); +#define FreezeString(val) FreezeStringF(val, FREEZE_SUBKEY, #val) +void FreezeStringF(char *val, char *subKey, char *name); -// #define ThawDWORD(val) val = ThawDWORDF(val, FREEZE_SUBKEY, #val) -// DWORD ThawDWORDF(DWORD val, char *subKey, char *name); +#define ThawString(val, max) ThawStringF(val, max, FREEZE_SUBKEY, #val) +void ThawStringF(char *val, int max, char *subKey, char *name); -// #define FreezeString(val) FreezeStringF(val, FREEZE_SUBKEY, #val) -// void FreezeStringF(char *val, char *subKey, char *name); +typedef union regKeyVal{ + int i; + float f; + double d; + bool b; + DWORD D; +} KeyVal; -// #define ThawString(val, max) ThawStringF(val, max, FREEZE_SUBKEY, #val) -// void ThawStringF(char *val, int max, char *subKey, char *name); +typedef struct regKeys{ + char name[15]; + char type; + KeyVal val; +} Key; #endif diff --git a/ldmicro/includes/linuxUI/linuxUI.h b/ldmicro/includes/linuxUI/linuxUI.h index bfbf3bc..803f915 100644 --- a/ldmicro/includes/linuxUI/linuxUI.h +++ b/ldmicro/includes/linuxUI/linuxUI.h @@ -1,3 +1,6 @@ +#ifndef __LINUX_UI__ +#define __LINUX_UI__ + /// includes #include <gtk/gtk.h> #include <linux/limits.h> @@ -76,4 +79,6 @@ class COLORREF : public GdkRGBA{ /// functions BOOL isFocus(HWND); -COLORREF RGB(int, int, int);
\ No newline at end of file +COLORREF RGB(int, int, int); + +#endif
\ No newline at end of file diff --git a/ldmicro/linuxUI.cpp b/ldmicro/linuxUI.cpp index 16e8368..8b65426 100644 --- a/ldmicro/linuxUI.cpp +++ b/ldmicro/linuxUI.cpp @@ -66,6 +66,6 @@ int main (int argc, char** argv) g_signal_connect (app, "activate", G_CALLBACK (load), NULL); status = g_application_run (G_APPLICATION (app), argc, argv); g_object_unref (app); - + return status; }
\ No newline at end of file |