From faaa2b6d1e73e7d32e5373b4acf15a2497ec0e74 Mon Sep 17 00:00:00 2001 From: Manolo Gouy Date: Sun, 10 Apr 2016 08:15:51 +0000 Subject: Rewrite Fl_Preferences.cxx under the driver model. git-svn-id: file:///fltk/svn/fltk/branches/branch-1.3-porting@11571 ea41ed52-d2ee-0310-a9c1-e6b18d33e121 --- src/drivers/Darwin/Fl_Darwin_System_Driver.H | 4 + src/drivers/Darwin/Fl_Darwin_System_Driver.cxx | 38 ++++++- src/drivers/Posix/Fl_Posix_System_Driver.H | 5 + src/drivers/Posix/Fl_Posix_System_Driver.cxx | 85 ++++++++++++++ src/drivers/WinAPI/Fl_WinAPI_System_Driver.H | 4 + src/drivers/WinAPI/Fl_WinAPI_System_Driver.cxx | 147 ++++++++++++++++++++++++- 6 files changed, 281 insertions(+), 2 deletions(-) (limited to 'src/drivers') diff --git a/src/drivers/Darwin/Fl_Darwin_System_Driver.H b/src/drivers/Darwin/Fl_Darwin_System_Driver.H index 77b646a29..df7705008 100644 --- a/src/drivers/Darwin/Fl_Darwin_System_Driver.H +++ b/src/drivers/Darwin/Fl_Darwin_System_Driver.H @@ -76,6 +76,10 @@ public: virtual int open_uri(const char *uri, char *msg, int msglen); virtual int need_test_shortcut_extra() {return 1;} virtual int file_browser_load_filesystem(Fl_File_Browser *browser, char *filename, Fl_File_Icon *icon); + virtual void newUUID(char *uuidBuffer); + virtual char *preference_rootnode(Fl_Preferences *prefs, Fl_Preferences::Root root, const char *vendor, + const char *application); + virtual void *dlopen(const char *filename); }; #endif // FL_DARWIN_SYSTEM_DRIVER_H diff --git a/src/drivers/Darwin/Fl_Darwin_System_Driver.cxx b/src/drivers/Darwin/Fl_Darwin_System_Driver.cxx index 8ad45ad07..746fe95ea 100644 --- a/src/drivers/Darwin/Fl_Darwin_System_Driver.cxx +++ b/src/drivers/Darwin/Fl_Darwin_System_Driver.cxx @@ -23,6 +23,7 @@ #include #include #include +#include #include #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_4 #include @@ -34,6 +35,8 @@ #include #include #include +#include + extern int fl_mac_os_version; // the version number of the running Mac OS X @@ -91,7 +94,7 @@ int Fl_Darwin_System_Driver::clocale_printf(FILE *output, const char *format, va PMSessionPageSetupDialog and PMSessionPrintDialog used by 10.4 or before (in Fl_Printer::start_job()) */ void *Fl_Darwin_System_Driver::get_carbon_function(const char *function_name) { - static void *carbon = dlopen("/System/Library/Frameworks/Carbon.framework/Carbon", RTLD_LAZY); + static void *carbon = ::dlopen("/System/Library/Frameworks/Carbon.framework/Carbon", RTLD_LAZY); return (carbon ? dlsym(carbon, function_name) : NULL); } @@ -192,6 +195,39 @@ int Fl_Darwin_System_Driver::file_browser_load_filesystem(Fl_File_Browser *brows return num_files; } +void Fl_Darwin_System_Driver::newUUID(char *uuidBuffer) +{ + CFUUIDRef theUUID = CFUUIDCreate(NULL); + CFUUIDBytes b = CFUUIDGetUUIDBytes(theUUID); + sprintf(uuidBuffer, "%02X%02X%02X%02X-%02X%02X-%02X%02X-%02X%02X-%02X%02X%02X%02X%02X%02X", + b.byte0, b.byte1, b.byte2, b.byte3, b.byte4, b.byte5, b.byte6, b.byte7, + b.byte8, b.byte9, b.byte10, b.byte11, b.byte12, b.byte13, b.byte14, b.byte15); + CFRelease(theUUID); +} + +char *Fl_Darwin_System_Driver::preference_rootnode(Fl_Preferences *prefs, Fl_Preferences::Root root, + const char *vendor, const char *application) +{ + static char filename[ FL_PATH_MAX ]; + // TODO: verify that this is the Apple sanctioned way of finding these folders + // (On MSWindows, this frequently leads to issues with internationalized systems) + // Carbon: err = FindFolder( kLocalDomain, kPreferencesFolderType, 1, &spec.vRefNum, &spec.parID ); + switch (root) { + case Fl_Preferences::SYSTEM: + strcpy(filename, "/Library/Preferences"); + break; + case Fl_Preferences::USER: + sprintf(filename, "%s/Library/Preferences", getenv("HOME")); + break; + } + snprintf(filename + strlen(filename), sizeof(filename) - strlen(filename), + "/%s/%s.prefs", vendor, application); + return filename; +} + +void *Fl_Darwin_System_Driver::dlopen(const char *filename) { + return ::dlopen(filename, RTLD_LAZY); +} // // End of "$Id$". diff --git a/src/drivers/Posix/Fl_Posix_System_Driver.H b/src/drivers/Posix/Fl_Posix_System_Driver.H index 8d0eb5e97..549729651 100644 --- a/src/drivers/Posix/Fl_Posix_System_Driver.H +++ b/src/drivers/Posix/Fl_Posix_System_Driver.H @@ -76,6 +76,11 @@ public: // this one is in fl_shortcut.cxx virtual const char *shortcut_add_key_name(unsigned key, char *p, char *buf, const char **); virtual int file_browser_load_filesystem(Fl_File_Browser *browser, char *filename, Fl_File_Icon *icon); + virtual void newUUID(char *uuidBuffer); + virtual char *preference_rootnode(Fl_Preferences *prefs, Fl_Preferences::Root root, const char *vendor, + const char *application); + virtual int preferences_need_protection_check() {return 1;} + virtual void *dlopen(const char *filename); }; #endif // FL_POSIX_SYSTEM_DRIVER_H diff --git a/src/drivers/Posix/Fl_Posix_System_Driver.cxx b/src/drivers/Posix/Fl_Posix_System_Driver.cxx index 658587fe6..a5b2d7269 100644 --- a/src/drivers/Posix/Fl_Posix_System_Driver.cxx +++ b/src/drivers/Posix/Fl_Posix_System_Driver.cxx @@ -26,9 +26,13 @@ #include #include #include +#if HAVE_DLFCN_H +# include +#endif #include #include #include +#include #if defined(_AIX) extern "C" { @@ -436,6 +440,87 @@ int Fl_Posix_System_Driver::file_browser_load_filesystem(Fl_File_Browser *browse return num_files; } +void Fl_Posix_System_Driver::newUUID(char *uuidBuffer) +{ + // warning Unix implementation of Fl_Preferences::newUUID() incomplete! + // #include + // void uuid_generate(uuid_t out); + unsigned char b[16]; + time_t t = time(0); // first 4 byte + b[0] = (unsigned char)t; + b[1] = (unsigned char)(t>>8); + b[2] = (unsigned char)(t>>16); + b[3] = (unsigned char)(t>>24); + int r = rand(); // four more bytes + b[4] = (unsigned char)r; + b[5] = (unsigned char)(r>>8); + b[6] = (unsigned char)(r>>16); + b[7] = (unsigned char)(r>>24); + unsigned long a = (unsigned long)&t; // four more bytes + b[8] = (unsigned char)a; + b[9] = (unsigned char)(a>>8); + b[10] = (unsigned char)(a>>16); + b[11] = (unsigned char)(a>>24); + // Now we try to find 4 more "random" bytes. We extract the + // lower 4 bytes from the address of t - it is created on the + // stack so *might* be in a different place each time... + // This is now done via a union to make it compile OK on 64-bit systems. + union { void *pv; unsigned char a[sizeof(void*)]; } v; + v.pv = (void *)(&t); + // NOTE: May need to handle big- or little-endian systems here +# if WORDS_BIGENDIAN + b[8] = v.a[sizeof(void*) - 1]; + b[9] = v.a[sizeof(void*) - 2]; + b[10] = v.a[sizeof(void*) - 3]; + b[11] = v.a[sizeof(void*) - 4]; +# else // data ordered for a little-endian system + b[8] = v.a[0]; + b[9] = v.a[1]; + b[10] = v.a[2]; + b[11] = v.a[3]; +# endif + char name[80]; // last four bytes + gethostname(name, 79); + memcpy(b+12, name, 4); + sprintf(uuidBuffer, "%02X%02X%02X%02X-%02X%02X-%02X%02X-%02X%02X-%02X%02X%02X%02X%02X%02X", + b[0], b[1], b[2], b[3], b[4], b[5], b[6], b[7], + b[8], b[9], b[10], b[11], b[12], b[13], b[14], b[15]); +} + +char *Fl_Posix_System_Driver::preference_rootnode(Fl_Preferences *prefs, Fl_Preferences::Root root, const char *vendor, + const char *application) +{ + static char filename[ FL_PATH_MAX ]; filename[0] = 0; + const char *e; + switch (root) { + case Fl_Preferences::USER: + if ((e = ::getenv("HOME")) != NULL) { + strlcpy(filename, e, sizeof(filename)); + + if (filename[strlen(filename)-1] != '/') { + strlcat(filename, "/.fltk/", sizeof(filename)); + } else { + strlcat(filename, ".fltk/", sizeof(filename)); + } + break; + } + case Fl_Preferences::SYSTEM: + strcpy(filename, "/etc/fltk/"); + break; + } + snprintf(filename + strlen(filename), sizeof(filename) - strlen(filename), + "%s/%s.prefs", vendor, application); + return filename; +} + +void *Fl_Posix_System_Driver::dlopen(const char *filename) +{ +#if HAVE_DLSYM + return ::dlopen(filename, RTLD_LAZY); +#endif + return NULL; +} + // // End of "$Id$". // diff --git a/src/drivers/WinAPI/Fl_WinAPI_System_Driver.H b/src/drivers/WinAPI/Fl_WinAPI_System_Driver.H index 0e6078faf..2712942f8 100644 --- a/src/drivers/WinAPI/Fl_WinAPI_System_Driver.H +++ b/src/drivers/WinAPI/Fl_WinAPI_System_Driver.H @@ -79,6 +79,10 @@ public: virtual int use_recent_tooltip_fix() {return 1;} virtual int file_browser_load_filesystem(Fl_File_Browser *browser, char *filename, Fl_File_Icon *icon); virtual int file_browser_load_directory(const char *directory, char *filename, dirent ***pfiles, Fl_File_Sort_F *sort); + virtual void newUUID(char *uuidBuffer); + virtual char *preference_rootnode(Fl_Preferences *prefs, Fl_Preferences::Root root, const char *vendor, + const char *application); + virtual void *dlopen(const char *filename); }; #endif // FL_WINAPI_SYSTEM_DRIVER_H diff --git a/src/drivers/WinAPI/Fl_WinAPI_System_Driver.cxx b/src/drivers/WinAPI/Fl_WinAPI_System_Driver.cxx index 89c62dbf7..5c4d52421 100644 --- a/src/drivers/WinAPI/Fl_WinAPI_System_Driver.cxx +++ b/src/drivers/WinAPI/Fl_WinAPI_System_Driver.cxx @@ -24,15 +24,21 @@ #include #include #include +#include "../../flstring.h" #include #include #include +#include +// function pointer for the UuidCreate Function +// RPC_STATUS RPC_ENTRY UuidCreate(UUID __RPC_FAR *Uuid); +typedef RPC_STATUS (WINAPI* uuid_func)(UUID __RPC_FAR *Uuid); #include #include #include #include -#include "../../flstring.h" +#include #include +#include // Apparently Borland C++ defines DIRECTORY in , which // interfers with the Fl_File_Icon enumeration of the same name. # ifdef DIRECTORY @@ -724,6 +730,145 @@ int Fl_WinAPI_System_Driver::file_browser_load_directory(const char *directory, return filename_list(filename, pfiles, sort); } +void Fl_WinAPI_System_Driver::newUUID(char *uuidBuffer) +{ + // First try and use the win API function UuidCreate(), but if that is not + // available, fall back to making something up from scratch. + // We do not want to link against the Rpcrt4.dll, as we will rarely use it, + // so we load the DLL dynamically, if it is available, and work from there. + static HMODULE hMod = NULL; + UUID ud; + UUID *pu = &ud; + int got_uuid = 0; + + if (!hMod) { // first time in? + hMod = LoadLibrary("Rpcrt4.dll"); + } + + if (hMod) { // do we have a usable handle to Rpcrt4.dll? + uuid_func uuid_crt = (uuid_func)GetProcAddress(hMod, "UuidCreate"); + if (uuid_crt != NULL) { + RPC_STATUS rpc_res = uuid_crt(pu); + if ( // is the return status OK for our needs? + (rpc_res == RPC_S_OK) || // all is well + (rpc_res == RPC_S_UUID_LOCAL_ONLY) || // only unique to this machine + (rpc_res == RPC_S_UUID_NO_ADDRESS) // probably only locally unique + ) { + got_uuid = -1; + sprintf(uuidBuffer, "%08lX-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X", + pu->Data1, pu->Data2, pu->Data3, pu->Data4[0], pu->Data4[1], + pu->Data4[2], pu->Data4[3], pu->Data4[4], + pu->Data4[5], pu->Data4[6], pu->Data4[7]); + } + } + } + if (got_uuid == 0) { // did not make a UUID - use fallback logic + unsigned char b[16]; + time_t t = time(0); // first 4 byte + b[0] = (unsigned char)t; + b[1] = (unsigned char)(t>>8); + b[2] = (unsigned char)(t>>16); + b[3] = (unsigned char)(t>>24); + int r = rand(); // four more bytes + b[4] = (unsigned char)r; + b[5] = (unsigned char)(r>>8); + b[6] = (unsigned char)(r>>16); + b[7] = (unsigned char)(r>>24); + // Now we try to find 4 more "random" bytes. We extract the + // lower 4 bytes from the address of t - it is created on the + // stack so *might* be in a different place each time... + // This is now done via a union to make it compile OK on 64-bit systems. + union { void *pv; unsigned char a[sizeof(void*)]; } v; + v.pv = (void *)(&t); + // NOTE: This assume that all WinXX systems are little-endian + b[8] = v.a[0]; + b[9] = v.a[1]; + b[10] = v.a[2]; + b[11] = v.a[3]; + TCHAR name[MAX_COMPUTERNAME_LENGTH + 1]; // only used to make last four bytes + DWORD nSize = MAX_COMPUTERNAME_LENGTH + 1; + // GetComputerName() does not depend on any extra libs, and returns something + // analogous to gethostname() + GetComputerName(name, &nSize); + // use the first 4 TCHAR's of the name to create the last 4 bytes of our UUID + for (int ii = 0; ii < 4; ii++) { + b[12 + ii] = (unsigned char)name[ii]; + } + sprintf(uuidBuffer, "%02X%02X%02X%02X-%02X%02X-%02X%02X-%02X%02X-%02X%02X%02X%02X%02X%02X", + b[0], b[1], b[2], b[3], b[4], b[5], b[6], b[7], + b[8], b[9], b[10], b[11], b[12], b[13], b[14], b[15]); + } +} + +char *Fl_WinAPI_System_Driver::preference_rootnode(Fl_Preferences *prefs, Fl_Preferences::Root root, const char *vendor, + const char *application) +{ +# define FLPREFS_RESOURCE "Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders" +# define FLPREFS_RESOURCEW L"Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders" + static char filename[ FL_PATH_MAX ]; + filename[0] = 0; + size_t appDataLen = strlen(vendor) + strlen(application) + 8; + DWORD type, nn; + LONG err; + HKEY key; + + switch (root) { + case Fl_Preferences::SYSTEM: + err = RegOpenKeyW( HKEY_LOCAL_MACHINE, FLPREFS_RESOURCEW, &key ); + if (err == ERROR_SUCCESS) { + nn = (DWORD) (FL_PATH_MAX - appDataLen); + err = RegQueryValueExW( key, L"Common AppData", 0L, &type, + (BYTE*)filename, &nn ); + if ( ( err != ERROR_SUCCESS ) && ( type == REG_SZ ) ) { + filename[0] = 0; + filename[1] = 0; + } + RegCloseKey(key); + } + break; + case Fl_Preferences::USER: + err = RegOpenKeyW( HKEY_CURRENT_USER, FLPREFS_RESOURCEW, &key ); + if (err == ERROR_SUCCESS) { + nn = (DWORD) (FL_PATH_MAX - appDataLen); + err = RegQueryValueExW( key, L"AppData", 0L, &type, + (BYTE*)filename, &nn ); + if ( ( err != ERROR_SUCCESS ) && ( type == REG_SZ ) ) { + filename[0] = 0; + filename[1] = 0; + } + RegCloseKey(key); + } + break; + } + if (!filename[1] && !filename[0]) { + strcpy(filename, "C:\\FLTK"); + } else { +#if 0 + wchar_t *b = (wchar_t*)_wcsdup((wchar_t *)filename); +#else + // cygwin does not come with _wcsdup. Use malloc + wcscpy. + // For implementation of wcsdup functionality See + // - http://linenum.info/p/glibc/2.7/wcsmbs/wcsdup.c + wchar_t *b = (wchar_t*) malloc((wcslen((wchar_t *) filename) + 1) * sizeof(wchar_t)); + wcscpy(b, (wchar_t *) filename); +#endif + // filename[fl_unicode2utf(b, wcslen((wchar_t*)b), filename)] = 0; + unsigned len = fl_utf8fromwc(filename, (FL_PATH_MAX-1), b, (unsigned) wcslen(b)); + filename[len] = 0; + free(b); + } + snprintf(filename + strlen(filename), sizeof(filename) - strlen(filename), + "/%s/%s.prefs", vendor, application); + for (char *s = filename; *s; s++) if (*s == '\\') *s = '/'; + return filename; +} + +void *Fl_WinAPI_System_Driver::dlopen(const char *filename) +{ + return LoadLibrary(filename); +} + + // // End of "$Id$". // -- cgit v1.2.3