# Copyright (c) 2009, 2023, Oracle and/or its affiliates.
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice (including the next
# paragraph) shall be included in all copies or substantial portions of the
# Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.

Oracle Bug 15597443 - SUNBT6892799 mouse driver closes fd while processing
    DEVICE_OFF

Makes mouse usable after VT switch on Solaris

Unclear if it should be submitted upstream since it changes the common
behavior on all OS'es, not just Solaris.

--- a/src/mouse.c	Tue Nov 11 16:06:53 2014
+++ b/src/mouse.c	Tue Nov 11 16:09:14 2014
@@ -1753,18 +1753,19 @@
         ErrorF("assigning %p name=%s\n", device, pInfo->name);
 #endif
         MouseInitProperties(device);
-        break;
-
-    case DEVICE_ON:
         pInfo->fd = xf86OpenSerial(pInfo->options);
         if (pInfo->fd == -1)
             xf86Msg(X_WARNING, "%s: cannot open input device\n", pInfo->name);
-        else {
+
+        break;
+
+    case DEVICE_ON:
 #if defined(__NetBSD__) && defined(WSCONS_SUPPORT) && defined(WSMOUSEIO_SETVERSION)
             int version = WSMOUSE_EVENT_VERSION;
             if (ioctl(pInfo->fd, WSMOUSEIO_SETVERSION, &version) == -1)
                 xf86Msg(X_WARNING, "%s: cannot set version\n", pInfo->name);
 #endif
+	if (pInfo->fd != -1) {
             if (pMse->xisbscale)
                 pMse->buffer = XisbNew(pInfo->fd, pMse->xisbscale * 4);
             else
@@ -1820,8 +1821,6 @@
                 XisbFree(pMse->buffer);
                 pMse->buffer = NULL;
             }
-            xf86CloseSerial(pInfo->fd);
-            pInfo->fd = -1;
             if (pMse->emulate3Buttons || pMse->emulate3ButtonsSoft)
             {
                 RemoveBlockAndWakeupHandlers (MouseBlockHandler,
@@ -1832,6 +1831,10 @@
         device->public.on = FALSE;
         break;
     case DEVICE_CLOSE:
+        if (pInfo->fd != -1) {
+            xf86CloseSerial(pInfo->fd);
+            pInfo->fd = -1;
+        }
         free(pMse->mousePriv);
         pMse->mousePriv = NULL;
         break;