summaryrefslogtreecommitdiff
path: root/src/drivers
diff options
context:
space:
mode:
authorManolo Gouy <Manolo>2016-04-09 15:15:33 +0000
committerManolo Gouy <Manolo>2016-04-09 15:15:33 +0000
commit8b672ee655aaee8e8a2002643846147997a8eaf8 (patch)
tree1fd6d1e4d47273cf4a022f69f466d3bd7aa59a91 /src/drivers
parentd98a8e13e6eff36e3b428b5e5cb93019e23d644b (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
Diffstat (limited to 'src/drivers')
-rw-r--r--src/drivers/Darwin/Fl_Darwin_System_Driver.H3
-rw-r--r--src/drivers/Darwin/Fl_Darwin_System_Driver.cxx10
-rw-r--r--src/drivers/Posix/Fl_Posix_System_Driver.H3
-rw-r--r--src/drivers/Posix/Fl_Posix_System_Driver.cxx135
-rw-r--r--src/drivers/WinAPI/Fl_WinAPI_System_Driver.H1
-rw-r--r--src/drivers/WinAPI/Fl_WinAPI_System_Driver.cxx7
6 files changed, 159 insertions, 0 deletions
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$".
//