From fc458b9310c15c1659a7269c64100ea95ba7709b Mon Sep 17 00:00:00 2001 From: Miod Vallat Date: Sat, 24 Nov 2007 16:28:10 +0000 Subject: Since switches from X to consoles are aysnchronous, wsmoused(8) can be awakened before the X server has release the mouse device. Instead of an arbitrary sleep, loop (with increasing delays) until we can open the device again. Found the hard way and tested by jmc@ --- usr.sbin/wsmoused/wsmoused.c | 33 ++++++++++++++++++++++++++------- 1 file changed, 26 insertions(+), 7 deletions(-) (limited to 'usr.sbin/wsmoused') diff --git a/usr.sbin/wsmoused/wsmoused.c b/usr.sbin/wsmoused/wsmoused.c index bc90bb29b59..43c9fd88886 100644 --- a/usr.sbin/wsmoused/wsmoused.c +++ b/usr.sbin/wsmoused/wsmoused.c @@ -1,4 +1,4 @@ -/* $OpenBSD: wsmoused.c,v 1.22 2007/09/18 20:19:20 otto Exp $ */ +/* $OpenBSD: wsmoused.c,v 1.23 2007/11/24 16:28:09 miod Exp $ */ /* * Copyright (c) 2001 Jean-Baptiste Marchand, Julien Montagne and Jerome Verdon @@ -453,6 +453,7 @@ wsmoused(void) the X server release it */ struct wscons_event sleeping; + unsigned int tries; /* restore mouse resolution to default value */ res = WSMOUSE_RES_DEFAULT; @@ -467,13 +468,31 @@ wsmoused(void) ioctl(mouse.cfd, WSDISPLAYIO_WSMOUSED, &sleeping); - /* waiting for availability of mouse device */ - sleep(1); + /* + * Since the X server could still be running + * (e.g. when switching from the graphics + * screen to a virtual text console), it might + * not have freed the device yet. + * + * Try to open the device until it succeeds. + */ + tries = 0; + for (;;) { + if ((mouse.mfd = open(mouse.portname, + O_RDONLY | O_NONBLOCK, 0)) != -1) + break; + + if (tries < 10) { + tries++; + sleep(1); + } else { + logerr(1, "unable to open %s, " + "will retry in 10 seconds", + mouse.portname); + sleep(10); + } + } - if ((mouse.mfd = open(mouse.portname, - O_RDONLY | O_NONBLOCK, 0)) == -1) - logerr(1, "unable to open %s", - mouse.portname); mouse_init(); } } else { -- cgit v1.2.3