+ if (sp->_dlopen_gpm != 0) {
+ T(("unload GPM library"));
+ sp->_mouse_gpm_loaded = FALSE;
+ sp->_mouse_fd = -1;
+ }
+}
+
+static void
+load_gpm_library(SCREEN *sp)
+{
+ sp->_mouse_gpm_found = FALSE;
+
+ /*
+ * If we already had a successful dlopen, reuse it.
+ */
+ if (sp->_dlopen_gpm != 0) {
+ sp->_mouse_gpm_found = TRUE;
+ sp->_mouse_gpm_loaded = TRUE;
+ } else 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);
+ dlclose(sp->_dlopen_gpm);
+ sp->_dlopen_gpm = 0;
+ } else {
+ sp->_mouse_gpm_found = TRUE;
+ sp->_mouse_gpm_loaded = TRUE;
+ }
+ }
+}
+#endif /* HAVE_LIBDL */
+
+static bool
+enable_gpm_mouse(SCREEN *sp, bool enable)
+{
+ bool result;
+
+ T((T_CALLED("enable_gpm_mouse(%d)"), enable));
+
+ if (enable && !sp->_mouse_active) {
+#ifdef HAVE_LIBDL
+ if (sp->_mouse_gpm_found && !sp->_mouse_gpm_loaded) {
+ load_gpm_library(sp);
+ }
+#endif
+ if (sp->_mouse_gpm_loaded) {
+ int code;
+
+ /* GPM: initialize connection to gpm server */
+ sp->_mouse_gpm_connect.eventMask = GPM_DOWN | GPM_UP;
+ sp->_mouse_gpm_connect.defaultMask =
+ (unsigned short) (~(sp->_mouse_gpm_connect.eventMask | GPM_HARD));
+ sp->_mouse_gpm_connect.minMod = 0;
+ sp->_mouse_gpm_connect.maxMod =
+ (unsigned short) (~((1 << KG_SHIFT) |
+ (1 << KG_SHIFTL) |
+ (1 << KG_SHIFTR)));
+ /*
+ * Note: GPM hardcodes \E[?1001s and \E[?1000h during its open.
+ * The former is recognized by wscons (SunOS), and the latter by
+ * xterm. Those will not show up in ncurses' traces.
+ */
+ code = my_Gpm_Open(&sp->_mouse_gpm_connect, 0);
+ result = (code >= 0);
+
+ /*
+ * GPM can return a -2 if it is trying to do something with xterm.
+ * Ignore that, since it conflicts with our use of stdin.
+ */
+ if (code == -2) {
+ my_Gpm_Close();
+ }
+ } else {
+ result = FALSE;
+ }
+ sp->_mouse_active = result;
+ T(("GPM open %s", result ? "succeeded" : "failed"));
+ } else {
+ if (!enable && sp->_mouse_active) {
+ /* GPM: close connection to gpm server */
+ my_Gpm_Close();
+ sp->_mouse_active = FALSE;
+ T(("GPM closed"));
+ }
+ result = enable;
+ }
+#ifdef HAVE_LIBDL
+ if (!result) {
+ unload_gpm_library(sp);
+ }
+#endif
+ returnBool(result);
+}
+#endif /* USE_GPM_SUPPORT */
+
+static void
+initialize_mousetype(SCREEN *sp)
+{
+ T((T_CALLED("initialize_mousetype()")));