summaryrefslogtreecommitdiff
path: root/ldmicro/lib/linuxUI
diff options
context:
space:
mode:
Diffstat (limited to 'ldmicro/lib/linuxUI')
-rw-r--r--ldmicro/lib/linuxUI/CMakeLists.txt3
-rw-r--r--ldmicro/lib/linuxUI/linuxLD.cpp114
-rw-r--r--ldmicro/lib/linuxUI/linuxLD.h157
-rw-r--r--ldmicro/lib/linuxUI/linuxUI.cpp175
-rw-r--r--ldmicro/lib/linuxUI/linuxUI.h68
5 files changed, 517 insertions, 0 deletions
diff --git a/ldmicro/lib/linuxUI/CMakeLists.txt b/ldmicro/lib/linuxUI/CMakeLists.txt
new file mode 100644
index 0000000..25ba6b2
--- /dev/null
+++ b/ldmicro/lib/linuxUI/CMakeLists.txt
@@ -0,0 +1,3 @@
+project(LinuxUI)
+
+add_library(LinuxUI linuxUI.cpp linuxLD.cpp)
diff --git a/ldmicro/lib/linuxUI/linuxLD.cpp b/ldmicro/lib/linuxUI/linuxLD.cpp
new file mode 100644
index 0000000..e4fd018
--- /dev/null
+++ b/ldmicro/lib/linuxUI/linuxLD.cpp
@@ -0,0 +1,114 @@
+#include "linuxUI.h"
+
+std::vector<HEAPRECORD> HeapRecords;
+
+HANDLE HeapCreate(DWORD flOptions, SIZE_T dwInitialSize, SIZE_T dwMaximumSize)
+{
+ HANDLE hHeap = NULL;
+ HEAPRECORD hHeapRecord;
+ hHeapRecord.dwMaximumSize = dwMaximumSize;
+ hHeap = malloc(dwInitialSize);
+
+ if (hHeap == NULL)
+ return NULL;
+
+ hHeapRecord.dwSize = dwInitialSize;
+ hHeapRecord.hHeap = hHeap;
+ hHeapRecord.dwAllocatedSizeOffset = 0;
+ hHeapRecord.HeapID = HeapRecords.size()+1;
+ HeapRecords.push_back(hHeapRecord);
+
+ return hHeap;
+}
+
+LPVOID HeapAlloc(HANDLE hHeap, DWORD dwFlags, SIZE_T dwBytes)
+{
+ if (hHeap == NULL)
+ {
+ printf("Alloc**********NULL HEAP***************\n");
+ LPVOID p = malloc(dwBytes);
+ return p;
+ }
+
+ auto it = std::find_if(HeapRecords.begin(), HeapRecords.end(), [&hHeap](HEAPRECORD &Record) { return Record.hHeap == hHeap; });
+
+ if (it == HeapRecords.end())
+ return NULL;
+
+ if ((*it).dwAllocatedSizeOffset + dwBytes > (*it).dwSize)
+ {
+ if ((*it).dwMaximumSize != 0)
+ if((*it).dwAllocatedSizeOffset + dwBytes > (*it).dwMaximumSize)
+ return NULL;
+
+ (*it).hHeap = realloc((*it).hHeap, (*it).dwAllocatedSizeOffset + dwBytes);
+ hHeap = (*it).hHeap;
+ }
+
+ /// HEAP_ZERO_MEMORY is set by default
+ DWORD flags = MAP_ANONYMOUS;
+
+ // if ( !((dwFlags & HEAP_ZERO_MEMORY) == HEAP_ZERO_MEMORY) )
+ // flags = MAP_ANONYMOUS | MAP_UNINITIALIZED;
+
+ /* Use for setting a meamory chunck with some value
+ * void * memset ( void * ptr, int value, size_t num );
+ */
+ LPVOID p = mmap(hHeap + (*it).dwAllocatedSizeOffset, dwBytes, PROT_EXEC, flags, -1, 0);
+
+ if (p == NULL)
+ return NULL;
+
+ (*it).dwAllocatedSizeOffset += dwBytes;
+ HEAPCHUNCK chunck;
+ chunck.Chunck = p;
+ chunck.dwSize = dwBytes;
+ (*it).Element.push_back(chunck);
+
+ return p;
+}
+
+BOOL HeapFree(HANDLE hHeap, DWORD dwFlags, LPVOID lpMem)
+{
+ /// if NULL free()
+ if (hHeap == NULL)
+ {
+ printf("free*********NULL HEAP***************\n");
+ free(lpMem);
+ return TRUE;
+ }
+ auto heap_it = std::find_if(HeapRecords.begin(), HeapRecords.end(), [&hHeap](HEAPRECORD &Record) { return Record.hHeap == hHeap; });
+
+ if (heap_it == HeapRecords.end())
+ return FALSE;
+
+ auto chunck_it = std::find_if((*heap_it).Element.begin(), (*heap_it).Element.end(), [&lpMem](HEAPCHUNCK &Chunck) { return Chunck.Chunck == lpMem; });
+
+ if (chunck_it == (*heap_it).Element.end())
+ return FALSE;
+
+ int result = munmap((*chunck_it).Chunck, (*chunck_it).dwSize);
+
+ if (result == 0)
+ {
+ (*heap_it).Element.erase(chunck_it);
+ return TRUE;
+ }
+ else
+ return FALSE;
+
+}
+
+void OutputDebugString(char* str)
+{
+
+}
+
+double GetTickCount(void)
+{
+// timespec now;
+// clock_gettime()
+// if (clock_gettime(CLOCK_MONOTONIC, &now))
+// return 0;
+ return 10.2;//now.tv_sec * 1000.0 + now.tv_nsec / 1000000.0;
+} \ No newline at end of file
diff --git a/ldmicro/lib/linuxUI/linuxLD.h b/ldmicro/lib/linuxUI/linuxLD.h
new file mode 100644
index 0000000..e204c63
--- /dev/null
+++ b/ldmicro/lib/linuxUI/linuxLD.h
@@ -0,0 +1,157 @@
+#ifndef __LINUX_LD__
+#define __LINUX_LD__
+
+#include "linuxUI.h"
+#include <ctype.h>
+#include <vector>
+#include <algorithm>
+#include <sys/mman.h>
+
+/// common windows referances for linux
+
+/// definitions
+#define MAX_PATH PATH_MAX
+/// CALLBACK or __stdcall os defined empty
+#define CALLBACK
+#define CONST const
+
+#define HEAP_ZERO_MEMORY 0x00000008
+
+/// Typedefs
+//typedef int64_t __int64;
+typedef bool BOOL;
+typedef unsigned char BYTE;
+typedef unsigned short WORD;
+typedef unsigned int DWORD;
+typedef unsigned int UINT;
+typedef size_t SIZE_T;
+typedef long LONG;
+typedef wchar_t WCHAR;
+typedef char CHAR;
+
+typedef CONST WCHAR *LPCWSTR;
+typedef CONST CHAR *LPCSTR; /// should be __nullterminated
+
+#ifdef UNICODE
+ typedef LPCWSTR LPCTSTR;
+#else
+ typedef LPCSTR LPCTSTR;
+#endif
+
+typedef WCHAR *LPWSTR;
+typedef CHAR *LPSTR;
+
+#ifdef UNICODE
+ typedef LPWSTR LPTSTR;
+#else
+ typedef LPSTR LPTSTR;
+#endif
+
+typedef void *PVOID;
+typedef void *LPVOID;
+typedef PVOID HFONT;
+typedef PVOID HMODULE;
+typedef PVOID HHOOK;
+typedef PVOID HBRUSH;
+typedef PVOID HFONT;
+typedef PVOID HANDLE;
+typedef HANDLE HINSTANCE;
+typedef HANDLE HDC;
+
+typedef GtkWidget *HWID;
+typedef GtkWidget *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;
+
+#if defined(__UNIX64)
+ typedef __int64_t LONG_PTR;
+#else
+ typedef long LONG_PTR;
+#endif
+
+typedef LONG_PTR LPARAM;
+typedef LONG_PTR LRESULT;
+
+/// 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;
+ }
+};
+
+/// Custom structures
+typedef struct HeapRecordChunckTag{
+ PVOID Chunck;
+ SIZE_T dwSize;
+} HEAPCHUNCK;
+
+typedef struct HeapRecordTag{
+ PVOID hHeap;
+ DWORD HeapID;
+ std::vector<HEAPCHUNCK> Element;
+ SIZE_T dwMaximumSize;
+ SIZE_T dwSize;
+ SIZE_T dwAllocatedSizeOffset;
+} HEAPRECORD;
+
+typedef struct tagSCROLLINFO {
+ UINT cbSize;
+ UINT fMask;
+ int nMin;
+ int nMax;
+ UINT nPage;
+ int nPos;
+ int nTrackPos;
+} SCROLLINFO, *LPCSCROLLINFO;
+
+typedef struct tagNMHDR {
+ HWND hwndFrom;
+ UINT_PTR idFrom;
+ UINT code;
+} NMHDR;
+
+/// Variables
+extern std::vector<HEAPRECORD> HeapRecord;
+
+/// Functions
+HANDLE HeapCreate(
+ DWORD flOptions,
+ SIZE_T dwInitialSize,
+ SIZE_T dwMaximumSize);
+
+LPVOID HeapAlloc(
+ HANDLE hHeap,
+ DWORD dwFlags,
+ SIZE_T dwBytes);
+
+BOOL HeapFree(
+ HANDLE hHeap,
+ DWORD dwFlags,
+ LPVOID lpMem);
+
+/// functions to be ported
+void OutputDebugString(char*);
+double GetTickCount(void);
+
+#endif \ No newline at end of file
diff --git a/ldmicro/lib/linuxUI/linuxUI.cpp b/ldmicro/lib/linuxUI/linuxUI.cpp
new file mode 100644
index 0000000..3bebf6d
--- /dev/null
+++ b/ldmicro/lib/linuxUI/linuxUI.cpp
@@ -0,0 +1,175 @@
+#include "linuxUI.h"
+
+/// EnableMenuItem Variables
+const UINT MF_ENABLED = 0;
+const UINT MF_GRAYED = 1;
+const UINT MF_CHECKED = 2;
+const UINT MF_UNCHECKED = 3;
+
+/// ListStore
+GtkWidget* view;
+GtkTreeViewColumn* column;
+
+/// Wraper function for gtk_window_has_toplevel_focus
+BOOL isFocus(HWID window)
+{
+ return (BOOL) gtk_window_has_toplevel_focus(GTK_WINDOW(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;
+}
+
+int MessageBox(HWID pWindow, char* message, char* title, UINT mFlags)
+ {
+ GtkDialogFlags flags = GTK_DIALOG_DESTROY_WITH_PARENT;
+ GtkMessageType mType;
+
+ if ((mFlags & MB_ICONERROR) == MB_ICONERROR)
+ mType = GTK_MESSAGE_ERROR;
+ else if ((mFlags & MB_ICONQUESTION) == MB_ICONQUESTION)
+ mType = GTK_MESSAGE_QUESTION;
+ else if ((mFlags & MB_ICONWARNING) == MB_ICONWARNING)
+ mType = GTK_MESSAGE_WARNING;
+ else if ((mFlags & MB_ICONINFORMATION) == MB_ICONINFORMATION)
+ mType = GTK_MESSAGE_INFO;
+ else
+ mType = GTK_MESSAGE_OTHER;
+
+ mType = GTK_MESSAGE_ERROR;
+ HWID dialog = gtk_message_dialog_new (GTK_WINDOW(pWindow),
+ flags,
+ mType,
+ GTK_BUTTONS_NONE,
+ message);
+
+ if ((mFlags & MB_OKCANCEL) == MB_OKCANCEL)
+ {
+ gtk_dialog_add_button(GTK_DIALOG(dialog), "_OK", IDOK);
+ gtk_dialog_add_button(GTK_DIALOG(dialog), "_CANCEL", IDCANCEL);
+ }
+ else if ((mFlags & MB_YESNO) == MB_YESNO)
+ {
+ gtk_dialog_add_button(GTK_DIALOG(dialog), "_YES", IDYES);
+ gtk_dialog_add_button(GTK_DIALOG(dialog), "_NO", IDNO);
+ }
+ else if ((mFlags & MB_YESNOCANCEL) == MB_YESNOCANCEL)
+ {
+ gtk_dialog_add_button(GTK_DIALOG(dialog), "_YES", IDYES);
+ gtk_dialog_add_button(GTK_DIALOG(dialog), "_NO", IDNO);
+ gtk_dialog_add_button(GTK_DIALOG(dialog), "_CANCEL", IDCANCEL);
+ }
+ else
+ gtk_dialog_add_button(GTK_DIALOG(dialog), "OK", IDOK);
+
+ gtk_message_dialog_set_markup (GTK_MESSAGE_DIALOG(dialog),
+ title);
+ gtk_message_dialog_format_secondary_markup(GTK_MESSAGE_DIALOG(dialog), message);
+ int result = gtk_dialog_run (GTK_DIALOG (dialog));
+ gtk_widget_destroy (dialog);
+
+ return result;
+ }
+
+BOOL GetSaveFileName(OPENFILENAME *ofn)
+{
+ GtkWidget *dialog;
+ GtkFileChooserAction action = GTK_FILE_CHOOSER_ACTION_OPEN;
+
+ dialog = gtk_file_chooser_dialog_new (ofn->lpstrTitle,
+ GTK_WINDOW(ofn->parentWindow),
+ action,
+ "_Cancel",
+ GTK_RESPONSE_CANCEL,
+ "_Open",
+ GTK_RESPONSE_ACCEPT,
+ NULL);
+ //g_print("filter created\n");
+
+ if (ofn->Flags & OFN_OVERWRITEPROMPT == OFN_OVERWRITEPROMPT)
+ gtk_file_chooser_set_do_overwrite_confirmation(GTK_FILE_CHOOSER(dialog), TRUE);
+
+ GtkFileFilter *filter = gtk_file_filter_new ();
+ char* strFilter = new char[strlen(ofn->lpstrFilter)];
+ DWORD strFilterLen = 0;
+ BOOL filterResetFlag = FALSE;
+
+ for (int i = 0; !(ofn->lpstrFilter[i] == '\0' && ofn->lpstrFilter[i-1] == '\0'); ++i)
+ {
+ memcpy (strFilter + strFilterLen, &ofn->lpstrFilter[i], 1 );
+ ++strFilterLen;
+ if (ofn->lpstrFilter[i] == '\0')
+ if (filterResetFlag)
+ {
+ gtk_file_filter_add_pattern (GTK_FILE_FILTER(filter), strFilter);
+ gtk_file_chooser_add_filter (GTK_FILE_CHOOSER(dialog), filter);
+ filter = gtk_file_filter_new ();
+ strFilterLen = 0;
+ //g_print("filter pat: %s\n", strFilter);
+ //g_print("filter reset\n");
+ filterResetFlag = FALSE;
+ }
+ else
+ {
+ gtk_file_filter_set_name (GTK_FILE_FILTER(filter), strFilter);
+ //g_print("filter name: %s\n", strFilter);
+ strFilterLen = 0;
+ filterResetFlag = TRUE;
+ }
+ }
+ //g_print("filter rules added\n");
+
+ sprintf(strFilter, "*.%s", ofn->lpstrDefExt);
+ gtk_file_filter_add_pattern (GTK_FILE_FILTER(filter), strFilter);
+ //gtk_file_filter_set_name (filter, "int files");
+ gtk_file_chooser_set_filter (GTK_FILE_CHOOSER(dialog), filter);
+
+ delete strFilter;
+
+ //g_print("default filter set\n");
+
+ BOOL exitStatus = gtk_dialog_run (GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT;
+ if (exitStatus)
+ {
+ char* str;
+ str = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER(dialog));
+ strcpy(ofn->lpstrFile, str);
+ g_free(str);
+ }
+ //g_print("file path saved: %s\n", ofn->lpstrFile);
+
+ gtk_widget_destroy (dialog);
+
+ //g_print("exit\n");
+
+ return exitStatus;
+}
+
+void EnableMenuItem(HMENU MenuName, HMENU MenuItem, UINT CheckEnabledItem) {
+ switch (CheckEnabledItem){
+ case MF_ENABLED :
+ gtk_widget_set_sensitive (MenuItem, true);
+ break;
+ case MF_GRAYED :
+ gtk_widget_set_sensitive (MenuItem, false);
+ break;
+ }
+}
+
+void CheckMenuItem(HMENU MenuName, HMENU MenuItem, UINT Check){
+ switch (Check){
+ case MF_CHECKED :
+ gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM(MenuItem), true);
+ break;
+ case MF_UNCHECKED :
+ gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM(MenuItem), false);
+ break;
+ }
+} \ 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..c6bb1ac
--- /dev/null
+++ b/ldmicro/lib/linuxUI/linuxUI.h
@@ -0,0 +1,68 @@
+#ifndef __LINUX_UI__
+#define __LINUX_UI__
+
+/// includes
+#include <gtk/gtk.h>
+#include <linux/limits.h>
+#include <stdio.h>
+#include <inttypes.h>
+#include "linuxLD.h"
+//#include "windows.h"
+
+/// version control
+#define LDMicro_VERSION_MAJOR 1
+#define LDMicro_VERSION_MINOR 0
+
+/// Flags
+/// message box
+#define MB_OK 0x00000001L
+#define MB_OKCANCEL 0x00000002L
+#define MB_YESNO 0x00000004L
+#define MB_YESNOCANCEL 0x00000008L
+
+#define IDOK 1
+#define IDCANCEL 2
+#define IDYES 3
+#define IDNO 4
+
+#define MB_ICONERROR 0x00000010L
+#define MB_ICONQUESTION 0x00000020L
+#define MB_ICONWARNING 0x00000040L
+#define MB_ICONINFORMATION 0x00000080L
+
+/// open/save file
+#define OFN_PATHMUSTEXIST 0x00000100L
+#define OFN_HIDEREADONLY 0x00000200L
+#define OFN_OVERWRITEPROMPT 0x00000400L
+
+/// EnableMenuItem variables
+extern const UINT MF_ENABLED;
+extern const UINT MF_GRAYED;
+extern const UINT MF_CHECKED;
+extern const UINT MF_UNCHECKED;
+
+/// ListStore
+extern GtkWidget* view;
+extern GtkTreeViewColumn* column;
+
+/// data types
+typedef struct OpenFileInfoData {
+ DWORD lStructSize;
+ HWID parentWindow;
+ LPTSTR lpstrFile;
+ LPCTSTR lpstrFilter;
+ DWORD nMaxFile;
+ LPCTSTR lpstrTitle;
+ DWORD Flags;
+ LPCTSTR lpstrDefExt;
+} OPENFILENAME;
+
+/// functions
+BOOL isFocus(HWID);
+COLORREF RGB(int, int, int);
+int MessageBox(HWID, char*, char*, UINT);
+BOOL GetSaveFileName(OPENFILENAME* );
+void EnableMenuItem(HMENU, HMENU, UINT);
+void CheckMenuItem(HMENU, HMENU, UINT);
+
+#endif \ No newline at end of file