- /* GPM does printf's without checking if stdout is a terminal */
- if (isatty(fileno(stdout))) {
- char *env = getenv("TERM");
- /* GPM checks the beginning of the $TERM variable to decide if
- * it should pass xterm events through. There is no real advantage
- * in allowing GPM to do this.
- */
- if (env == 0 || strncmp(env, "xterm", 5))
- return TRUE;
+ bool result = FALSE;
+
+#if USE_WEAK_SYMBOLS
+ /* Danger Robinson: do not use dlopen for libgpm if already loaded */
+ if ((Gpm_Wgetch)) {
+ if (!sp->_mouse_gpm_loaded) {
+ T(("GPM library was already dlopen'd, not by us"));
+ }
+ } else
+#endif
+ /* GPM does printf's without checking if stdout is a terminal */
+ if (NC_ISATTY(fileno(stdout))) {
+ const char *list = getenv("NCURSES_GPM_TERMS");
+ const char *env = getenv("TERM");
+ if (list != 0) {
+ if (env != 0) {
+ result = _nc_name_match(list, env, "|:");
+ }
+ } else {
+ /* GPM checks the beginning of the $TERM variable to decide if it
+ * should pass xterm events through. There is no real advantage in
+ * allowing GPM to do this. Recent versions relax that check, and
+ * pretend that GPM can work with any terminal having the kmous
+ * capability. Perhaps that works for someone. If so, they can
+ * set the environment variable (above).
+ */
+ if (env != 0 && strstr(env, "linux") != 0) {
+ result = TRUE;
+ }
+ }
+ }
+ return result;
+}
+
+#ifdef HAVE_LIBDL
+static void
+unload_gpm_library(SCREEN *sp)
+{
+ if (sp->_dlopen_gpm != 0) {
+ T(("unload GPM library"));
+ sp->_mouse_gpm_loaded = FALSE;
+ sp->_mouse_fd = -1;
+ dlclose(sp->_dlopen_gpm);
+ sp->_dlopen_gpm = 0;
+ }
+}
+
+static void
+load_gpm_library(SCREEN *sp)
+{
+ sp->_mouse_gpm_found = FALSE;
+ if ((sp->_dlopen_gpm = dlopen(LIBGPM_SONAME, my_RTLD)) != 0) {
+#if (defined(__GNUC__) && (__GNUC__ >= 5)) || defined(__clang__)
+#pragma GCC diagnostic push
+#pragma GCC diagnostic ignored "-Wpedantic"
+#endif
+ if (GET_DLSYM(gpm_fd) == 0 ||
+ GET_DLSYM(Gpm_Open) == 0 ||
+ GET_DLSYM(Gpm_Close) == 0 ||
+ GET_DLSYM(Gpm_GetEvent) == 0) {
+#if (defined(__GNUC__) && (__GNUC__ >= 5)) || defined(__clang__)
+#pragma GCC diagnostic pop
+#endif
+ T(("GPM initialization failed: %s", dlerror()));
+ unload_gpm_library(sp);
+ } else {
+ sp->_mouse_gpm_found = TRUE;
+ sp->_mouse_gpm_loaded = TRUE;
+ }