diff options
| author | Manolo Gouy <Manolo> | 2016-04-09 15:15:33 +0000 |
|---|---|---|
| committer | Manolo Gouy <Manolo> | 2016-04-09 15:15:33 +0000 |
| commit | 8b672ee655aaee8e8a2002643846147997a8eaf8 (patch) | |
| tree | 1fd6d1e4d47273cf4a022f69f466d3bd7aa59a91 | |
| parent | d98a8e13e6eff36e3b428b5e5cb93019e23d644b (diff) | |
Rewrite fl_open_uri.cxx under the driver model.
git-svn-id: file:///fltk/svn/fltk/branches/branch-1.3-porting@11563 ea41ed52-d2ee-0310-a9c1-e6b18d33e121
| -rw-r--r-- | FL/Fl_System_Driver.H | 2 | ||||
| -rw-r--r-- | src/drivers/Darwin/Fl_Darwin_System_Driver.H | 3 | ||||
| -rw-r--r-- | src/drivers/Darwin/Fl_Darwin_System_Driver.cxx | 10 | ||||
| -rw-r--r-- | src/drivers/Posix/Fl_Posix_System_Driver.H | 3 | ||||
| -rw-r--r-- | src/drivers/Posix/Fl_Posix_System_Driver.cxx | 135 | ||||
| -rw-r--r-- | src/drivers/WinAPI/Fl_WinAPI_System_Driver.H | 1 | ||||
| -rw-r--r-- | src/drivers/WinAPI/Fl_WinAPI_System_Driver.cxx | 7 | ||||
| -rw-r--r-- | src/fl_open_uri.cxx | 216 |
8 files changed, 193 insertions, 184 deletions
diff --git a/FL/Fl_System_Driver.H b/FL/Fl_System_Driver.H index 07d2a8b27..c7dea05df 100644 --- a/FL/Fl_System_Driver.H +++ b/FL/Fl_System_Driver.H @@ -116,6 +116,8 @@ public: virtual int need_menu_handle_part2() {return 0;} // whether a platform uses additional code in Fl_Menu::handle_part1(int e) virtual int need_menu_handle_part1_extra() {return 0;} + // implement to support fl_open_uri() + virtual int open_uri(const char *uri, char *msg, int msglen) {return 0;} }; #endif // FL_SYSTEM_DRIVER_H diff --git a/src/drivers/Darwin/Fl_Darwin_System_Driver.H b/src/drivers/Darwin/Fl_Darwin_System_Driver.H index 0bfce80d0..6cdd3b77f 100644 --- a/src/drivers/Darwin/Fl_Darwin_System_Driver.H +++ b/src/drivers/Darwin/Fl_Darwin_System_Driver.H @@ -44,6 +44,8 @@ class Fl_Darwin_System_Driver : public Fl_System_Driver { +private: + int run_program(const char *program, char **argv, char *msg, int msglen); public: Fl_Darwin_System_Driver(); virtual int single_arg(const char *arg); @@ -71,6 +73,7 @@ public: virtual int filename_list(const char *d, dirent ***list, int (*sort)(struct dirent **, struct dirent **) ); virtual const char *getpwnam(const char *login); virtual int need_menu_handle_part2() {return 1;} + virtual int open_uri(const char *uri, char *msg, int msglen); }; #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 2584a21c7..c8c8c0c32 100644 --- a/src/drivers/Darwin/Fl_Darwin_System_Driver.cxx +++ b/src/drivers/Darwin/Fl_Darwin_System_Driver.cxx @@ -143,6 +143,16 @@ const char *Fl_Darwin_System_Driver::getpwnam(const char *login) { return pwd ? pwd->pw_dir : NULL; } +int Fl_Darwin_System_Driver::open_uri(const char *uri, char *msg, int msglen) +{ + char *argv[3]; // Command-line arguments + argv[0] = (char*)"open"; + argv[1] = (char*)uri; + argv[2] = (char*)0; + if (msg) snprintf(msg, msglen, "open %s", uri); + return run_program("/usr/bin/open", argv, msg, msglen) != 0; +} + // // End of "$Id$". diff --git a/src/drivers/Posix/Fl_Posix_System_Driver.H b/src/drivers/Posix/Fl_Posix_System_Driver.H index 54f997cc8..7f95ede63 100644 --- a/src/drivers/Posix/Fl_Posix_System_Driver.H +++ b/src/drivers/Posix/Fl_Posix_System_Driver.H @@ -44,6 +44,8 @@ class Fl_Posix_System_Driver : public Fl_System_Driver { +private: + int run_program(const char *program, char **argv, char *msg, int msglen); public: virtual void display_arg(const char *arg); virtual int XParseGeometry(const char*, int*, int*, unsigned int*, unsigned int*); @@ -69,6 +71,7 @@ public: virtual const char *getpwnam(const char *login); virtual int need_menu_handle_part2() {return 1;} virtual int need_menu_handle_part1_extra() {return 1;} + virtual int open_uri(const char *uri, char *msg, int msglen); }; #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 4f9c08c40..b3d6f0bad 100644 --- a/src/drivers/Posix/Fl_Posix_System_Driver.cxx +++ b/src/drivers/Posix/Fl_Posix_System_Driver.cxx @@ -25,6 +25,7 @@ #include <stdio.h> #include <sys/types.h> #include <pwd.h> +#include <unistd.h> // Pointers you can use to change FLTK to a foreign language. // Note: Similar pointers are defined in FL/fl_ask.H and src/fl_ask.cxx @@ -148,6 +149,140 @@ const char *Fl_Posix_System_Driver::getpwnam(const char *login) { return pwd ? pwd->pw_dir : NULL; } +// Find a program in the path... +static char *path_find(const char *program, char *filename, int filesize) { + const char *path; // Search path + char *ptr, // Pointer into filename + *end; // End of filename buffer + + + if ((path = getenv("PATH")) == NULL) path = "/bin:/usr/bin"; + + for (ptr = filename, end = filename + filesize - 1; *path; path ++) { + if (*path == ':') { + if (ptr > filename && ptr[-1] != '/' && ptr < end) *ptr++ = '/'; + + strlcpy(ptr, program, end - ptr + 1); + + if (!access(filename, X_OK)) return filename; + + ptr = filename; + } else if (ptr < end) *ptr++ = *path; + } + + if (ptr > filename) { + if (ptr[-1] != '/' && ptr < end) *ptr++ = '/'; + + strlcpy(ptr, program, end - ptr + 1); + + if (!access(filename, X_OK)) return filename; + } + + return 0; +} + + +int Fl_Posix_System_Driver::open_uri(const char *uri, char *msg, int msglen) +{ + // Run any of several well-known commands to open the URI. + // + // We give preference to the Portland group's xdg-utils + // programs which run the user's preferred web browser, etc. + // based on the current desktop environment in use. We fall + // back on older standards and then finally test popular programs + // until we find one we can use. + // + // Note that we specifically do not support the MAILER and + // BROWSER environment variables because we have no idea whether + // we need to run the listed commands in a terminal program. + char command[FL_PATH_MAX], // Command to run... + *argv[4], // Command-line arguments + remote[1024]; // Remote-mode command... + const char * const *commands; // Array of commands to check... + int i; + static const char * const browsers[] = { + "xdg-open", // Portland + "htmlview", // Freedesktop.org + "firefox", + "mozilla", + "netscape", + "konqueror", // KDE + "opera", + "hotjava", // Solaris + "mosaic", + NULL + }; + static const char * const readers[] = { + "xdg-email", // Portland + "thunderbird", + "mozilla", + "netscape", + "evolution", // GNOME + "kmailservice", // KDE + NULL + }; + static const char * const managers[] = { + "xdg-open", // Portland + "fm", // IRIX + "dtaction", // CDE + "nautilus", // GNOME + "konqueror", // KDE + NULL + }; + + // Figure out which commands to check for... + if (!strncmp(uri, "file://", 7)) commands = managers; + else if (!strncmp(uri, "mailto:", 7) || + !strncmp(uri, "news:", 5)) commands = readers; + else commands = browsers; + + // Find the command to run... + for (i = 0; commands[i]; i ++) + if (path_find(commands[i], command, sizeof(command))) break; + + if (!commands[i]) { + if (msg) { + snprintf(msg, msglen, "No helper application found for \"%s\"", uri); + } + + return 0; + } + + // Handle command-specific arguments... + argv[0] = (char *)commands[i]; + + if (!strcmp(commands[i], "firefox") || + !strcmp(commands[i], "mozilla") || + !strcmp(commands[i], "netscape") || + !strcmp(commands[i], "thunderbird")) { + // program -remote openURL(uri) + snprintf(remote, sizeof(remote), "openURL(%s)", uri); + + argv[1] = (char *)"-remote"; + argv[2] = remote; + argv[3] = 0; + } else if (!strcmp(commands[i], "dtaction")) { + // dtaction open uri + argv[1] = (char *)"open"; + argv[2] = (char *)uri; + argv[3] = 0; + } else { + // program uri + argv[1] = (char *)uri; + argv[2] = 0; + } + + if (msg) { + strlcpy(msg, argv[0], msglen); + + for (i = 1; argv[i]; i ++) { + strlcat(msg, " ", msglen); + strlcat(msg, argv[i], msglen); + } + } + + return run_program(command, argv, msg, msglen) != 0; +} // // End of "$Id$". // diff --git a/src/drivers/WinAPI/Fl_WinAPI_System_Driver.H b/src/drivers/WinAPI/Fl_WinAPI_System_Driver.H index 3e5f06d39..6079b0721 100644 --- a/src/drivers/WinAPI/Fl_WinAPI_System_Driver.H +++ b/src/drivers/WinAPI/Fl_WinAPI_System_Driver.H @@ -75,6 +75,7 @@ public: virtual int filename_isdir(const char* n); virtual int filename_isdir_quick(const char* n); virtual const char *filename_ext(const char *buf); + virtual int open_uri(const char *uri, char *msg, int msglen); }; #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 246a26521..95d94dfca 100644 --- a/src/drivers/WinAPI/Fl_WinAPI_System_Driver.cxx +++ b/src/drivers/WinAPI/Fl_WinAPI_System_Driver.cxx @@ -25,6 +25,7 @@ #include <stdio.h> #include <stdarg.h> #include <windows.h> +#include <shellapi.h> #include <wchar.h> #include <process.h> #include <locale.h> @@ -658,6 +659,12 @@ const char *Fl_WinAPI_System_Driver::filename_ext(const char *buf) { return q ? q : p; } +int Fl_WinAPI_System_Driver::open_uri(const char *uri, char *msg, int msglen) +{ + if (msg) snprintf(msg, msglen, "open %s", uri); + return (int)(ShellExecute(HWND_DESKTOP, "open", uri, NULL, NULL, SW_SHOW) > (void *)32); +} + // // End of "$Id$". // diff --git a/src/fl_open_uri.cxx b/src/fl_open_uri.cxx index dfad504f1..721158dc2 100644 --- a/src/fl_open_uri.cxx +++ b/src/fl_open_uri.cxx @@ -24,33 +24,13 @@ // Include necessary headers... // +#include "config_lib.h" #include <FL/filename.H> +#include <FL/Fl.H> +#include <FL/Fl_System_Driver.H> #include <stdio.h> #include <stdlib.h> -#include <errno.h> -#include <sys/types.h> #include "flstring.h" -#ifdef WIN32 -# include <windows.h> -# include <shellapi.h> -#else -# include <sys/wait.h> -# include <signal.h> -# include <fcntl.h> -# include <unistd.h> -#endif // WIN32 - - -// -// Local functions... -// - -#if !defined(WIN32) && !defined(__APPLE__) // PORTME: Fl_System_Driver - platform open doc -static char *path_find(const char *program, char *filename, int filesize); -#endif // !WIN32 && !__APPLE__ // PORTME: Fl_System_Driver - platform open doc -#ifndef WIN32 -static int run_program(const char *program, char **argv, char *msg, int msglen); -#endif // !WIN32 /** \addtogroup filenames @{ */ @@ -93,9 +73,7 @@ static int run_program(const char *program, char **argv, char *msg, int msglen); * @param msglen Length of optional buffer * @return 1 on success, 0 on failure */ - -int -fl_open_uri(const char *uri, char *msg, int msglen) { +int fl_open_uri(const char *uri, char *msg, int msglen) { // Supported URI schemes... static const char * const schemes[] = { "file://", @@ -125,126 +103,7 @@ fl_open_uri(const char *uri, char *msg, int msglen) { return 0; } - -#ifdef WIN32 - if (msg) snprintf(msg, msglen, "open %s", uri); - - return (int)(ShellExecute(HWND_DESKTOP, "open", uri, NULL, NULL, SW_SHOW) > (void *)32); - -#elif defined(__APPLE__) // PORTME: Fl_System_Driver - platform open doc - char *argv[3]; // Command-line arguments - - argv[0] = (char*)"open"; - argv[1] = (char*)uri; - argv[2] = (char*)0; - - if (msg) snprintf(msg, msglen, "open %s", uri); - - return run_program("/usr/bin/open", argv, msg, msglen) != 0; - -#elif defined(FL_PORTING) -# pragma message "FL_PORTING: add code to open any file type with an external app" - -#else // !WIN32 && !__APPLE__ // PORTME: Fl_System_Driver - platform open doc - // Run any of several well-known commands to open the URI. - // - // We give preference to the Portland group's xdg-utils - // programs which run the user's preferred web browser, etc. - // based on the current desktop environment in use. We fall - // back on older standards and then finally test popular programs - // until we find one we can use. - // - // Note that we specifically do not support the MAILER and - // BROWSER environment variables because we have no idea whether - // we need to run the listed commands in a terminal program. - - char command[FL_PATH_MAX], // Command to run... - *argv[4], // Command-line arguments - remote[1024]; // Remote-mode command... - const char * const *commands; // Array of commands to check... - static const char * const browsers[] = { - "xdg-open", // Portland - "htmlview", // Freedesktop.org - "firefox", - "mozilla", - "netscape", - "konqueror", // KDE - "opera", - "hotjava", // Solaris - "mosaic", - NULL - }; - static const char * const readers[] = { - "xdg-email", // Portland - "thunderbird", - "mozilla", - "netscape", - "evolution", // GNOME - "kmailservice", // KDE - NULL - }; - static const char * const managers[] = { - "xdg-open", // Portland - "fm", // IRIX - "dtaction", // CDE - "nautilus", // GNOME - "konqueror", // KDE - NULL - }; - - // Figure out which commands to check for... - if (!strncmp(uri, "file://", 7)) commands = managers; - else if (!strncmp(uri, "mailto:", 7) || - !strncmp(uri, "news:", 5)) commands = readers; - else commands = browsers; - - // Find the command to run... - for (i = 0; commands[i]; i ++) - if (path_find(commands[i], command, sizeof(command))) break; - - if (!commands[i]) { - if (msg) { - snprintf(msg, msglen, "No helper application found for \"%s\"", uri); - } - - return 0; - } - - // Handle command-specific arguments... - argv[0] = (char *)commands[i]; - - if (!strcmp(commands[i], "firefox") || - !strcmp(commands[i], "mozilla") || - !strcmp(commands[i], "netscape") || - !strcmp(commands[i], "thunderbird")) { - // program -remote openURL(uri) - snprintf(remote, sizeof(remote), "openURL(%s)", uri); - - argv[1] = (char *)"-remote"; - argv[2] = remote; - argv[3] = 0; - } else if (!strcmp(commands[i], "dtaction")) { - // dtaction open uri - argv[1] = (char *)"open"; - argv[2] = (char *)uri; - argv[3] = 0; - } else { - // program uri - argv[1] = (char *)uri; - argv[2] = 0; - } - - if (msg) { - strlcpy(msg, argv[0], msglen); - - for (i = 1; argv[i]; i ++) { - strlcat(msg, " ", msglen); - strlcat(msg, argv[i], msglen); - } - } - - return run_program(command, argv, msg, msglen) != 0; -#endif // WIN32 + return Fl::system_driver()->open_uri(uri, msg, msglen); } /** Decodes a URL-encoded string. @@ -270,45 +129,17 @@ void fl_decode_uri(char *uri) /** @} */ -#if !defined(WIN32) && !defined(__APPLE__) // PORTME: Fl_System_Driver - platform open doc -// Find a program in the path... -static char *path_find(const char *program, char *filename, int filesize) { - const char *path; // Search path - char *ptr, // Pointer into filename - *end; // End of filename buffer - - - if ((path = getenv("PATH")) == NULL) path = "/bin:/usr/bin"; - - for (ptr = filename, end = filename + filesize - 1; *path; path ++) { - if (*path == ':') { - if (ptr > filename && ptr[-1] != '/' && ptr < end) *ptr++ = '/'; - - strlcpy(ptr, program, end - ptr + 1); - - if (!access(filename, X_OK)) return filename; - - ptr = filename; - } else if (ptr < end) *ptr++ = *path; - } - - if (ptr > filename) { - if (ptr[-1] != '/' && ptr < end) *ptr++ = '/'; - - strlcpy(ptr, program, end - ptr + 1); - - if (!access(filename, X_OK)) return filename; - } - - return 0; -} -#endif // !WIN32 && !__APPLE__ // PORTME: Fl_System_Driver - platform open doc +#if defined(FL_CFG_GFX_XLIB) || defined(FL_CFG_GFX_QUARTZ) +// code shared by the Mac OS and USE_X11 platforms +# include <unistd.h> +# include <sys/wait.h> +# include <signal.h> +# include <fcntl.h> +# include <errno.h> -#ifndef WIN32 // Run the specified program, returning 1 on success and 0 on failure -static int -run_program(const char *program, char **argv, char *msg, int msglen) { +static int run_program(const char *program, char **argv, char *msg, int msglen) { pid_t pid; // Process ID of first child int status; // Exit status from first child sigset_t set, oldset; // Signal masks @@ -379,8 +210,26 @@ run_program(const char *program, char **argv, char *msg, int msglen) { // Return indicating success... return 1; } -#endif // !WIN32 +#endif +#if defined(FL_CFG_GFX_QUARTZ) + +#include "drivers/Darwin/Fl_Darwin_System_Driver.H" + +int Fl_Darwin_System_Driver::run_program(const char *program, char **argv, char *msg, int msglen) +{ + return ::run_program(program, argv, msg, msglen); +} + +#elif defined(FL_CFG_GFX_XLIB) +#include "drivers/Posix/Fl_Posix_System_Driver.H" + +int Fl_Posix_System_Driver::run_program(const char *program, char **argv, char *msg, int msglen) +{ + return ::run_program(program, argv, msg, msglen); +} + +#endif #ifdef TEST // @@ -404,7 +253,6 @@ int main(int argc, char **argv) { } #endif // TEST - // // End of "$Id$". // |
