+#endif /* USE_EMX_MOUSE */
+
+#if USE_SYSMOUSE
+static void
+sysmouse_server(SCREEN *sp)
+{
+ struct mouse_info the_mouse;
+ MEVENT *work;
+
+ the_mouse.operation = MOUSE_GETINFO;
+ if (sp != 0
+ && sp->_mouse_fd >= 0
+ && sp->_sysmouse_tail < FIFO_SIZE
+ && ioctl(sp->_mouse_fd, CONS_MOUSECTL, &the_mouse) != -1) {
+
+ if (sp->_sysmouse_head > sp->_sysmouse_tail) {
+ sp->_sysmouse_tail = 0;
+ sp->_sysmouse_head = 0;
+ }
+ work = &(sp->_sysmouse_fifo[sp->_sysmouse_tail]);
+ memset(work, 0, sizeof(*work));
+ work->id = NORMAL_EVENT; /* there's only one mouse... */
+
+ sp->_sysmouse_old_buttons = sp->_sysmouse_new_buttons;
+ sp->_sysmouse_new_buttons = the_mouse.u.data.buttons & 0x7;
+
+ if (sp->_sysmouse_new_buttons) {
+ if (sp->_sysmouse_new_buttons & 1)
+ work->bstate |= BUTTON1_PRESSED;
+ if (sp->_sysmouse_new_buttons & 2)
+ work->bstate |= BUTTON2_PRESSED;
+ if (sp->_sysmouse_new_buttons & 4)
+ work->bstate |= BUTTON3_PRESSED;
+ } else {
+ if (sp->_sysmouse_old_buttons & 1)
+ work->bstate |= BUTTON1_RELEASED;
+ if (sp->_sysmouse_old_buttons & 2)
+ work->bstate |= BUTTON2_RELEASED;
+ if (sp->_sysmouse_old_buttons & 4)
+ work->bstate |= BUTTON3_RELEASED;
+ }
+
+ /* for cosmetic bug in syscons.c on FreeBSD 3.[34] */
+ the_mouse.operation = MOUSE_HIDE;
+ ioctl(sp->_mouse_fd, CONS_MOUSECTL, &the_mouse);
+ the_mouse.operation = MOUSE_SHOW;
+ ioctl(sp->_mouse_fd, CONS_MOUSECTL, &the_mouse);
+
+ /*
+ * We're only interested if the button is pressed or released.
+ * FIXME: implement continuous event-tracking.
+ */
+ if (sp->_sysmouse_new_buttons != sp->_sysmouse_old_buttons) {
+ sp->_sysmouse_tail += 1;
+ }
+ work->x = the_mouse.u.data.x / sp->_sysmouse_char_width;
+ work->y = the_mouse.u.data.y / sp->_sysmouse_char_height;
+ }
+}
+
+static void
+handle_sysmouse(int sig GCC_UNUSED)
+{
+ sysmouse_server(CURRENT_SCREEN);
+}
+#endif /* USE_SYSMOUSE */
+
+#ifndef USE_TERM_DRIVER
+#define xterm_kmous "\033[M"
+
+static void
+init_xterm_mouse(SCREEN *sp)
+{
+ sp->_mouse_type = M_XTERM;
+ sp->_mouse_format = MF_X10;
+ sp->_mouse_xtermcap = tigetstr("XM");
+ if (VALID_STRING(sp->_mouse_xtermcap)) {
+ char *code = strstr(sp->_mouse_xtermcap, "[?");
+ if (code != 0) {
+ code += 2;
+ while ((*code >= '0') && (*code <= '9')) {
+ char *next = code;
+ while ((*next >= '0') && (*next <= '9')) {
+ ++next;
+ }
+ if (!strncmp(code, "1006", (size_t) (next - code))) {
+ sp->_mouse_format = MF_SGR1006;
+ }
+#ifdef EXP_XTERM_1005
+ if (!strncmp(code, "1005", (size_t) (next - code))) {
+ sp->_mouse_format = MF_XTERM_1005;
+ }
+#endif
+ if (*next == ';') {
+ while (*next == ';') {
+ ++next;
+ }
+ code = next;
+ } else {
+ break;
+ }
+ }
+ }
+ } else {
+ int code = tigetnum("XM");
+ switch (code) {
+ case 1006:
+ break;
+ default:
+ code = 1000;
+ break;
+ }
+ sp->_mouse_xtermcap = "\033[?1000%?%p1%{1}%=%th%el%;";
+ }
+}
+#endif
+
+static void
+enable_xterm_mouse(SCREEN *sp, int enable)
+{
+#if USE_EMX_MOUSE
+ sp->_emxmouse_activated = enable;
+#else
+ NCURSES_PUTP2("xterm-mouse", TPARM_1(sp->_mouse_xtermcap, enable));
+#endif
+ sp->_mouse_active = enable;
+}
+
+#if USE_GPM_SUPPORT
+static bool
+allow_gpm_mouse(SCREEN *sp GCC_UNUSED)
+{
+ 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;
+ }
+}
+