diff options
author | Rr42 | 2018-05-29 10:46:19 +0530 |
---|---|---|
committer | Rr42 | 2018-05-29 10:46:19 +0530 |
commit | 5c01aef5fd87870b63511f17bd50405a0df08a3b (patch) | |
tree | fd24160ed213c2a37ea6c8ea6e816fcfed984628 /ldmicro/lib | |
parent | 08e70fc82ebfae4cc11ad6eee0dc3197b1d1672b (diff) | |
download | LDMicroGtk-5c01aef5fd87870b63511f17bd50405a0df08a3b.tar.gz LDMicroGtk-5c01aef5fd87870b63511f17bd50405a0df08a3b.tar.bz2 LDMicroGtk-5c01aef5fd87870b63511f17bd50405a0df08a3b.zip |
Freeze library updated and renamed to freezeLD (working)
Diffstat (limited to 'ldmicro/lib')
-rw-r--r-- | ldmicro/lib/freezeLD/CMakeLists.txt | 4 | ||||
-rw-r--r-- | ldmicro/lib/freezeLD/freezeLD.cpp | 265 | ||||
-rw-r--r-- | ldmicro/lib/freezeLD/freezeLD.h | 52 | ||||
-rw-r--r-- | ldmicro/lib/linuxUI/CMakeLists.txt | 3 | ||||
-rw-r--r-- | ldmicro/lib/linuxUI/linuxLD.h | 61 | ||||
-rw-r--r-- | ldmicro/lib/linuxUI/linuxUI.cpp | 18 | ||||
-rw-r--r-- | ldmicro/lib/linuxUI/linuxUI.h | 19 |
7 files changed, 422 insertions, 0 deletions
diff --git a/ldmicro/lib/freezeLD/CMakeLists.txt b/ldmicro/lib/freezeLD/CMakeLists.txt new file mode 100644 index 0000000..761f348 --- /dev/null +++ b/ldmicro/lib/freezeLD/CMakeLists.txt @@ -0,0 +1,4 @@ +project( FreezeLD ) + +add_library(FreezeLD freezeLD.cpp) +target_link_libraries(FreezeLD LinuxUI) diff --git a/ldmicro/lib/freezeLD/freezeLD.cpp b/ldmicro/lib/freezeLD/freezeLD.cpp new file mode 100644 index 0000000..5241af1 --- /dev/null +++ b/ldmicro/lib/freezeLD/freezeLD.cpp @@ -0,0 +1,265 @@ +/* + * A library for storing parameters in a key file + * + * This library is an analog to the windows freeze library + * developed by Jonathan Westhues. + * + * R Ramana, 2018 + */ +#include "linuxUI.h" +#include "freezeLD.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(HWID hwid, char *subKey, char *name) +{ + g_print("freezing"); + char* moveToKeyLocatin = (char *)malloc(strlen(subKey) + 35); + if(!moveToKeyLocatin) + return; + sprintf(moveToKeyLocatin, "mkdir -p %s/%s", LDMICRO_REGISTER, subKey); + system(moveToKeyLocatin); + sprintf(moveToKeyLocatin, "cd %s/%s", LDMICRO_REGISTER, subKey); + if (-1 == system(moveToKeyLocatin)) + return; + free(moveToKeyLocatin); + + char *keyName = (char *)malloc(strlen(name) + 30); + if(!keyName) + return; + + Key newKey; + + int val; + g_print("get width"); + sprintf(keyName, "%s_width", name); + std::ofstream Register(keyName, std::ios::binary | std::ios::trunc); + if (!Register.is_open()) + return; + gtk_window_get_size(GTK_WINDOW(hwid), &val, NULL); + newKey.type = 'i'; + newKey.val.i = val; + Register.write((char*) &newKey, sizeof(newKey)); + Register.close(); + + g_print("get height"); + sprintf(keyName, "%s_height", name); + Register.open(keyName, std::ios::binary | std::ios::trunc); + if (!Register.is_open()) + return; + gtk_window_get_size(GTK_WINDOW(hwid), NULL, &val); + newKey.type = 'i'; + newKey.val.i = val; + Register.write((char*) &newKey, sizeof(newKey)); + Register.close(); + + g_print("get posX"); + sprintf(keyName, "%s_posX", name); + Register.open(keyName, std::ios::binary | std::ios::trunc); + if (!Register.is_open()) + return; + gtk_window_get_position(GTK_WINDOW(hwid), &val, NULL); + newKey.type = 'i'; + newKey.val.i = val; + Register.write((char*) &newKey, sizeof(newKey)); + Register.close(); + + g_print("get posY"); + sprintf(keyName, "%s_posY", name); + Register.open(keyName, std::ios::binary | std::ios::trunc); + if (!Register.is_open()) + return; + gtk_window_get_position(GTK_WINDOW(hwid), NULL, &val); + newKey.type = 'i'; + newKey.val.i = val; + Register.write((char*) &newKey, sizeof(newKey)); + Register.close(); + + g_print("get max"); + sprintf(keyName, "%s_maximized", name); + Register.open(keyName, std::ios::binary | std::ios::trunc); + if (!Register.is_open()) + return; + newKey.type = 'b'; + newKey.val.b = gtk_window_is_maximized(GTK_WINDOW(hwid)); + Register.write((char*) &newKey, sizeof(newKey)); + Register.close(); + + free(keyName); + g_print("freezed"); +} + +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(HWID hwid, char *subKey, char *name) +{ + char* moveToKeyLocatin = (char *)malloc(strlen(name) + 30); + if(!moveToKeyLocatin) + return; + sprintf(moveToKeyLocatin, "cd %s/%s", LDMICRO_REGISTER, subKey); + if (-1 == system(moveToKeyLocatin)) + return; + free(moveToKeyLocatin); + + char *keyName = (char *)malloc(strlen(name) + 30); + if(!keyName) + return; + + Key newKey1, newKey2; + + /// set size + sprintf(keyName, "%s_width", name); + std::ifstream Register(keyName, std::ios::binary); + if (!Register.is_open()) + return; + Register.read((char*) &newKey1, sizeof(newKey1)); + Register.close(); + + sprintf(keyName, "%s_height", name); + Register.open(keyName, std::ios::binary); + if (!Register.is_open()) + return; + Register.read((char*) &newKey2, sizeof(newKey2)); + Register.close(); + if (newKey1.type == 'i' && newKey2.type == 'i') + gtk_window_resize(GTK_WINDOW(hwid), newKey1.val.i, newKey2.val.i); + + + /// set position + sprintf(keyName, "%s_posX", name); + Register.open(keyName, std::ios::binary); + if (!Register.is_open()) + return; + Register.read((char*) &newKey1, sizeof(newKey1)); + Register.close(); + + sprintf(keyName, "%s_posY", name); + Register.open(keyName, std::ios::binary); + if (!Register.is_open()) + return; + Register.read((char*) &newKey2, sizeof(newKey2)); + Register.close(); + if (newKey1.type == 'i' && newKey2.type == 'i') + gtk_window_move(GTK_WINDOW(hwid), newKey1.val.i, newKey2.val.i); + + + sprintf(keyName, "%s_maximized", name); + Register.open(keyName, std::ios::binary); + if (!Register.is_open()) + return; + Register.read((char*) &newKey1, sizeof(newKey1)); + Register.close(); + if (newKey1.type == 'b') + if (newKey1.val.b) + gtk_window_maximize(GTK_WINDOW(hwid)); + + + /// gtk_window_move handles off-screen window placement + + free(keyName); +} + +/* + * store a DWORD setting in the registry + */ +void FreezeDWORDF(DWORD val, char *subKey, char *name) +{ + char* moveToKeyLocatin = (char *)malloc(strlen(name) + 30); + if(!moveToKeyLocatin) + return; + sprintf(moveToKeyLocatin, "mkdir -p %s/%s", LDMICRO_REGISTER, subKey); + system(moveToKeyLocatin); + sprintf(moveToKeyLocatin, "cd %s/%s", LDMICRO_REGISTER, subKey); + if (-1 == system(moveToKeyLocatin)) + return; + free(moveToKeyLocatin); + + Key newKey; + newKey.type = 'D'; + newKey.val.D = val; + std::ofstream Register(name, std::ios::binary | std::ios::trunc); + Register.write((char*) &newKey, sizeof(newKey)); + Register.close(); +} + +/* + * retrieve a DWORD setting, or return the default if that setting is unavailable + */ +DWORD ThawDWORDF(DWORD val, char *subKey, char *name) +{ + char* moveToKeyLocatin = (char *)malloc(strlen(name) + 30); + if(!moveToKeyLocatin) + return val; + sprintf(moveToKeyLocatin, "cd %s/%s", LDMICRO_REGISTER, subKey); + if (-1 == system(moveToKeyLocatin)) + return val; + free(moveToKeyLocatin); + + Key newKey; + + std::ifstream Register(name, std::ios::binary); + Register.read((char*) &newKey, sizeof(newKey)); + Register.close(); + if(Register.bad()) + return val; + + if(newKey.type == 'D') + return newKey.val.D; + else + return val; +} + +/* + * store a string setting in the registry + */ +void FreezeStringF(char *val, char *subKey, char *name) +{ + char* moveToKeyLocatin = (char *)malloc(strlen(name) + 30); + if(!moveToKeyLocatin) + return; + sprintf(moveToKeyLocatin, "mkdir -p %s/%s", LDMICRO_REGISTER, subKey); + system(moveToKeyLocatin); + sprintf(moveToKeyLocatin, "cd %s/%s", LDMICRO_REGISTER, subKey); + if (-1 == system(moveToKeyLocatin)) + return; + free(moveToKeyLocatin); + + std::ofstream Register(name, std::ios::trunc); + Register << strlen(val)+1 << "\n"; + Register << val; + Register.close(); +} + +/* + * retrieve a string setting, or return the default if that setting is unavailable + */ +void ThawStringF(char *val, int max, char *subKey, char *name) +{ + char* moveToKeyLocatin = (char *)malloc(strlen(name) + 30); + if(!moveToKeyLocatin) + return; + sprintf(moveToKeyLocatin, "cd %s/%s", LDMICRO_REGISTER, subKey); + if (-1 == system(moveToKeyLocatin)) + return; + free(moveToKeyLocatin); + + std::ifstream Register(name); + int l; + Register >> l; + if (l >= max) + return; + Register >> val; +} + diff --git a/ldmicro/lib/freezeLD/freezeLD.h b/ldmicro/lib/freezeLD/freezeLD.h new file mode 100644 index 0000000..f14ef07 --- /dev/null +++ b/ldmicro/lib/freezeLD/freezeLD.h @@ -0,0 +1,52 @@ +/* + * A library for storing parameters in a key file + * + * This library is an analog to the windows freeze library + * developed by Jonathan Westhues. + * + * R Ramana, 2018 + */ + +#ifndef __FREEZE_H +#define __FREEZE_H + +#define LDMICRO_REGISTER "/usr/share/ldmicro" + +#define FREEZE_SUBKEY "LDMicro" + +// #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(HWID hWid, char *subKey, char *name); + +#define ThawWindowPos(hwnd) ThawWindowPosF(hwnd, FREEZE_SUBKEY, #hwnd) +void ThawWindowPosF(HWID hWid, char *subKey, char *name); + +#define FreezeDWORD(val) FreezeDWORDF(val, FREEZE_SUBKEY, #val) +void FreezeDWORDF(DWORD val, char *subKey, char *name); + +#define ThawDWORD(val) val = ThawDWORDF(val, FREEZE_SUBKEY, #val) +DWORD ThawDWORDF(DWORD val, char *subKey, char *name); + +#define FreezeString(val) FreezeStringF(val, FREEZE_SUBKEY, #val) +void FreezeStringF(char *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); + +typedef union regKeyVal{ + int i; + float f; + bool b; + DWORD D; +} KeyVal; + + +typedef struct regKeys{ + char type; + KeyVal val; +} Key; + +#endif diff --git a/ldmicro/lib/linuxUI/CMakeLists.txt b/ldmicro/lib/linuxUI/CMakeLists.txt new file mode 100644 index 0000000..16226dc --- /dev/null +++ b/ldmicro/lib/linuxUI/CMakeLists.txt @@ -0,0 +1,3 @@ +project(LinuxUI) + +add_library(LinuxUI linuxUI.cpp) diff --git a/ldmicro/lib/linuxUI/linuxLD.h b/ldmicro/lib/linuxUI/linuxLD.h new file mode 100644 index 0000000..9aba23d --- /dev/null +++ b/ldmicro/lib/linuxUI/linuxLD.h @@ -0,0 +1,61 @@ +#ifndef __LINUX_LD__ +#define __LINUX_LD__ + +#include "linuxUI.h" + +/// common windows referances for linux + +/// definitions +#define MAX_PATH PATH_MAX +/// CALLBACK or __stdcall os defined empty +#define CALLBACK + +/// typedefs +//typedef int64_t __int64; +typedef bool BOOL; +typedef unsigned char BYTE; +typedef unsigned int DWORD; +typedef long LONG; + +/// all handles will hold a GtkWindow* type +typedef void* PVOID; +typedef GtkWidget* HANDLE; +typedef HANDLE HINSTANCE; +typedef HANDLE HWID; +typedef HANDLE HDC; +typedef HANDLE HMENU; + +typedef GtkWindow* HWND; + +typedef GtkApplication* HAPP; + +/// Check if system is x64 or x86 +#if defined(__UNIX64) +typedef uint64_t UINT_PTR; +#else +typedef unsigned int UINT_PTR; +#endif + +typedef UINT_PTR WPARAM; +typedef unsigned int UINT; + +/// custom classes +class COLORREF : public GdkRGBA{ + public: + COLORREF() + { + this->red = 0.0; + this->green = 0.0; + this->blue = 0.0; + this->alpha = 1.0; + } + COLORREF(int r, int g, int b) + { + this->red = r/255.0; + this->green = g/255.0; + this->blue = b/255.0; + this->alpha = 1.0; + } +}; + +#endif diff --git a/ldmicro/lib/linuxUI/linuxUI.cpp b/ldmicro/lib/linuxUI/linuxUI.cpp new file mode 100644 index 0000000..8237d7d --- /dev/null +++ b/ldmicro/lib/linuxUI/linuxUI.cpp @@ -0,0 +1,18 @@ +#include "linuxUI.h" + +/// Wraper function for gtk_window_has_toplevel_focus +BOOL isFocus(HWID window) +{ + return (BOOL) gtk_window_has_toplevel_focus(GTK_WINDOW(gtk_widget_get_parent_window(GTK_WIDGET(window)))); +} + +COLORREF RGB(int red, int green, int blue) +{ + COLORREF col; + col.red = red/255.0; + col.green = green/255.0; + col.blue = blue/255.0; + col.alpha = 1.0; + + return col; +}
\ No newline at end of file diff --git a/ldmicro/lib/linuxUI/linuxUI.h b/ldmicro/lib/linuxUI/linuxUI.h new file mode 100644 index 0000000..3e49de0 --- /dev/null +++ b/ldmicro/lib/linuxUI/linuxUI.h @@ -0,0 +1,19 @@ +#ifndef __LINUX_UI__ +#define __LINUX_UI__ + +/// includes +#include <gtk/gtk.h> +#include <linux/limits.h> +#include <stdio.h> +#include <inttypes.h> +#include "linuxLD.h" + +/// version control +#define LDMicro_VERSION_MAJOR 1 +#define LDMicro_VERSION_MINOR 0 + +/// functions +BOOL isFocus(HWID); +COLORREF RGB(int, int, int); + +#endif
\ No newline at end of file |