1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun * Copyright 1990,91 by Thomas Roell, Dinkelscherben, Germany.
3*4882a593Smuzhiyun *
4*4882a593Smuzhiyun * Permission to use, copy, modify, distribute, and sell this software and its
5*4882a593Smuzhiyun * documentation for any purpose is hereby granted without fee, provided that
6*4882a593Smuzhiyun * the above copyright notice appear in all copies and that both that
7*4882a593Smuzhiyun * copyright notice and this permission notice appear in supporting
8*4882a593Smuzhiyun * documentation, and that the name of Thomas Roell not be used in
9*4882a593Smuzhiyun * advertising or publicity pertaining to distribution of the software without
10*4882a593Smuzhiyun * specific, written prior permission. Thomas Roell makes no representations
11*4882a593Smuzhiyun * about the suitability of this software for any purpose. It is provided
12*4882a593Smuzhiyun * "as is" without express or implied warranty.
13*4882a593Smuzhiyun *
14*4882a593Smuzhiyun * THOMAS ROELL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15*4882a593Smuzhiyun * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
16*4882a593Smuzhiyun * EVENT SHALL THOMAS ROELL BE LIABLE FOR ANY SPECIAL, INDIRECT OR
17*4882a593Smuzhiyun * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
18*4882a593Smuzhiyun * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
19*4882a593Smuzhiyun * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
20*4882a593Smuzhiyun * PERFORMANCE OF THIS SOFTWARE.
21*4882a593Smuzhiyun *
22*4882a593Smuzhiyun */
23*4882a593Smuzhiyun /*
24*4882a593Smuzhiyun * Copyright (c) 1994-2003 by The XFree86 Project, Inc.
25*4882a593Smuzhiyun *
26*4882a593Smuzhiyun * Permission is hereby granted, free of charge, to any person obtaining a
27*4882a593Smuzhiyun * copy of this software and associated documentation files (the "Software"),
28*4882a593Smuzhiyun * to deal in the Software without restriction, including without limitation
29*4882a593Smuzhiyun * the rights to use, copy, modify, merge, publish, distribute, sublicense,
30*4882a593Smuzhiyun * and/or sell copies of the Software, and to permit persons to whom the
31*4882a593Smuzhiyun * Software is furnished to do so, subject to the following conditions:
32*4882a593Smuzhiyun *
33*4882a593Smuzhiyun * The above copyright notice and this permission notice shall be included in
34*4882a593Smuzhiyun * all copies or substantial portions of the Software.
35*4882a593Smuzhiyun *
36*4882a593Smuzhiyun * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
37*4882a593Smuzhiyun * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
38*4882a593Smuzhiyun * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
39*4882a593Smuzhiyun * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
40*4882a593Smuzhiyun * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
41*4882a593Smuzhiyun * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
42*4882a593Smuzhiyun * OTHER DEALINGS IN THE SOFTWARE.
43*4882a593Smuzhiyun *
44*4882a593Smuzhiyun * Except as contained in this notice, the name of the copyright holder(s)
45*4882a593Smuzhiyun * and author(s) shall not be used in advertising or otherwise to promote
46*4882a593Smuzhiyun * the sale, use or other dealings in this Software without prior written
47*4882a593Smuzhiyun * authorization from the copyright holder(s) and author(s).
48*4882a593Smuzhiyun */
49*4882a593Smuzhiyun
50*4882a593Smuzhiyun /* [JCH-96/01/21] Extended std reverse map to four buttons. */
51*4882a593Smuzhiyun
52*4882a593Smuzhiyun #ifdef HAVE_XORG_CONFIG_H
53*4882a593Smuzhiyun #include <xorg-config.h>
54*4882a593Smuzhiyun #endif
55*4882a593Smuzhiyun
56*4882a593Smuzhiyun #include <X11/X.h>
57*4882a593Smuzhiyun #include <X11/Xproto.h>
58*4882a593Smuzhiyun #include <X11/Xatom.h>
59*4882a593Smuzhiyun #include "misc.h"
60*4882a593Smuzhiyun #include "xf86.h"
61*4882a593Smuzhiyun #include "xf86Priv.h"
62*4882a593Smuzhiyun #define XF86_OS_PRIVS
63*4882a593Smuzhiyun #include "xf86_OSlib.h"
64*4882a593Smuzhiyun #include <X11/keysym.h>
65*4882a593Smuzhiyun
66*4882a593Smuzhiyun #ifdef XFreeXDGA
67*4882a593Smuzhiyun #include "dgaproc.h"
68*4882a593Smuzhiyun #endif
69*4882a593Smuzhiyun
70*4882a593Smuzhiyun #include <X11/extensions/XI.h>
71*4882a593Smuzhiyun #include <X11/extensions/XIproto.h>
72*4882a593Smuzhiyun #include "inputstr.h"
73*4882a593Smuzhiyun #include "xf86Xinput.h"
74*4882a593Smuzhiyun
75*4882a593Smuzhiyun #include "mi.h"
76*4882a593Smuzhiyun #include "mipointer.h"
77*4882a593Smuzhiyun
78*4882a593Smuzhiyun #include "xkbsrv.h"
79*4882a593Smuzhiyun #include "xkbstr.h"
80*4882a593Smuzhiyun
81*4882a593Smuzhiyun #ifdef DPMSExtension
82*4882a593Smuzhiyun #include <X11/extensions/dpmsconst.h>
83*4882a593Smuzhiyun #include "dpmsproc.h"
84*4882a593Smuzhiyun #endif
85*4882a593Smuzhiyun
86*4882a593Smuzhiyun #include "xf86platformBus.h"
87*4882a593Smuzhiyun #include "systemd-logind.h"
88*4882a593Smuzhiyun
89*4882a593Smuzhiyun #ifdef XF86PM
90*4882a593Smuzhiyun extern void (*xf86OSPMClose) (void);
91*4882a593Smuzhiyun #endif
92*4882a593Smuzhiyun
93*4882a593Smuzhiyun static void xf86VTSwitch(void);
94*4882a593Smuzhiyun
95*4882a593Smuzhiyun /*
96*4882a593Smuzhiyun * Allow arbitrary drivers or other XFree86 code to register with our main
97*4882a593Smuzhiyun * Wakeup handler.
98*4882a593Smuzhiyun */
99*4882a593Smuzhiyun typedef struct x_IHRec {
100*4882a593Smuzhiyun int fd;
101*4882a593Smuzhiyun InputHandlerProc ihproc;
102*4882a593Smuzhiyun void *data;
103*4882a593Smuzhiyun Bool enabled;
104*4882a593Smuzhiyun Bool is_input;
105*4882a593Smuzhiyun struct x_IHRec *next;
106*4882a593Smuzhiyun } IHRec, *IHPtr;
107*4882a593Smuzhiyun
108*4882a593Smuzhiyun static IHPtr InputHandlers = NULL;
109*4882a593Smuzhiyun
110*4882a593Smuzhiyun Bool
LegalModifier(unsigned int key,DeviceIntPtr pDev)111*4882a593Smuzhiyun LegalModifier(unsigned int key, DeviceIntPtr pDev)
112*4882a593Smuzhiyun {
113*4882a593Smuzhiyun return TRUE;
114*4882a593Smuzhiyun }
115*4882a593Smuzhiyun
116*4882a593Smuzhiyun /*
117*4882a593Smuzhiyun * TimeSinceLastInputEvent --
118*4882a593Smuzhiyun * Function used for screensaver purposes by the os module. Returns the
119*4882a593Smuzhiyun * time in milliseconds since there last was any input.
120*4882a593Smuzhiyun */
121*4882a593Smuzhiyun int
TimeSinceLastInputEvent(void)122*4882a593Smuzhiyun TimeSinceLastInputEvent(void)
123*4882a593Smuzhiyun {
124*4882a593Smuzhiyun if (xf86Info.lastEventTime == 0) {
125*4882a593Smuzhiyun xf86Info.lastEventTime = GetTimeInMillis();
126*4882a593Smuzhiyun }
127*4882a593Smuzhiyun return GetTimeInMillis() - xf86Info.lastEventTime;
128*4882a593Smuzhiyun }
129*4882a593Smuzhiyun
130*4882a593Smuzhiyun /*
131*4882a593Smuzhiyun * SetTimeSinceLastInputEvent --
132*4882a593Smuzhiyun * Set the lastEventTime to now.
133*4882a593Smuzhiyun */
134*4882a593Smuzhiyun void
SetTimeSinceLastInputEvent(void)135*4882a593Smuzhiyun SetTimeSinceLastInputEvent(void)
136*4882a593Smuzhiyun {
137*4882a593Smuzhiyun xf86Info.lastEventTime = GetTimeInMillis();
138*4882a593Smuzhiyun }
139*4882a593Smuzhiyun
140*4882a593Smuzhiyun /*
141*4882a593Smuzhiyun * ProcessInputEvents --
142*4882a593Smuzhiyun * Retrieve all waiting input events and pass them to DIX in their
143*4882a593Smuzhiyun * correct chronological order. Only reads from the system pointer
144*4882a593Smuzhiyun * and keyboard.
145*4882a593Smuzhiyun */
146*4882a593Smuzhiyun void
ProcessInputEvents(void)147*4882a593Smuzhiyun ProcessInputEvents(void)
148*4882a593Smuzhiyun {
149*4882a593Smuzhiyun int x, y;
150*4882a593Smuzhiyun
151*4882a593Smuzhiyun mieqProcessInputEvents();
152*4882a593Smuzhiyun
153*4882a593Smuzhiyun /* FIXME: This is a problem if we have multiple pointers */
154*4882a593Smuzhiyun miPointerGetPosition(inputInfo.pointer, &x, &y);
155*4882a593Smuzhiyun
156*4882a593Smuzhiyun xf86SetViewport(xf86Info.currentScreen, x, y);
157*4882a593Smuzhiyun }
158*4882a593Smuzhiyun
159*4882a593Smuzhiyun /*
160*4882a593Smuzhiyun * Handle keyboard events that cause some kind of "action"
161*4882a593Smuzhiyun * (i.e., server termination, video mode changes, VT switches, etc.)
162*4882a593Smuzhiyun */
163*4882a593Smuzhiyun void
xf86ProcessActionEvent(ActionEvent action,void * arg)164*4882a593Smuzhiyun xf86ProcessActionEvent(ActionEvent action, void *arg)
165*4882a593Smuzhiyun {
166*4882a593Smuzhiyun DebugF("ProcessActionEvent(%d,%p)\n", (int) action, arg);
167*4882a593Smuzhiyun switch (action) {
168*4882a593Smuzhiyun case ACTION_TERMINATE:
169*4882a593Smuzhiyun if (!xf86Info.dontZap) {
170*4882a593Smuzhiyun xf86Msg(X_INFO, "Server zapped. Shutting down.\n");
171*4882a593Smuzhiyun #ifdef XFreeXDGA
172*4882a593Smuzhiyun DGAShutdown();
173*4882a593Smuzhiyun #endif
174*4882a593Smuzhiyun GiveUp(0);
175*4882a593Smuzhiyun }
176*4882a593Smuzhiyun break;
177*4882a593Smuzhiyun case ACTION_NEXT_MODE:
178*4882a593Smuzhiyun if (!xf86Info.dontZoom)
179*4882a593Smuzhiyun xf86ZoomViewport(xf86Info.currentScreen, 1);
180*4882a593Smuzhiyun break;
181*4882a593Smuzhiyun case ACTION_PREV_MODE:
182*4882a593Smuzhiyun if (!xf86Info.dontZoom)
183*4882a593Smuzhiyun xf86ZoomViewport(xf86Info.currentScreen, -1);
184*4882a593Smuzhiyun break;
185*4882a593Smuzhiyun case ACTION_SWITCHSCREEN:
186*4882a593Smuzhiyun if (!xf86Info.dontVTSwitch && arg) {
187*4882a593Smuzhiyun int vtno = *((int *) arg);
188*4882a593Smuzhiyun
189*4882a593Smuzhiyun if (vtno != xf86Info.vtno) {
190*4882a593Smuzhiyun if (!xf86VTActivate(vtno)) {
191*4882a593Smuzhiyun ErrorF("Failed to switch from vt%02d to vt%02d: %s\n",
192*4882a593Smuzhiyun xf86Info.vtno, vtno, strerror(errno));
193*4882a593Smuzhiyun }
194*4882a593Smuzhiyun }
195*4882a593Smuzhiyun }
196*4882a593Smuzhiyun break;
197*4882a593Smuzhiyun case ACTION_SWITCHSCREEN_NEXT:
198*4882a593Smuzhiyun if (!xf86Info.dontVTSwitch) {
199*4882a593Smuzhiyun if (!xf86VTActivate(xf86Info.vtno + 1)) {
200*4882a593Smuzhiyun /* If first try failed, assume this is the last VT and
201*4882a593Smuzhiyun * try wrapping around to the first vt.
202*4882a593Smuzhiyun */
203*4882a593Smuzhiyun if (!xf86VTActivate(1)) {
204*4882a593Smuzhiyun ErrorF("Failed to switch from vt%02d to next vt: %s\n",
205*4882a593Smuzhiyun xf86Info.vtno, strerror(errno));
206*4882a593Smuzhiyun }
207*4882a593Smuzhiyun }
208*4882a593Smuzhiyun }
209*4882a593Smuzhiyun break;
210*4882a593Smuzhiyun case ACTION_SWITCHSCREEN_PREV:
211*4882a593Smuzhiyun if (!xf86Info.dontVTSwitch && xf86Info.vtno > 0) {
212*4882a593Smuzhiyun if (!xf86VTActivate(xf86Info.vtno - 1)) {
213*4882a593Smuzhiyun /* Don't know what the maximum VT is, so can't wrap around */
214*4882a593Smuzhiyun ErrorF("Failed to switch from vt%02d to previous vt: %s\n",
215*4882a593Smuzhiyun xf86Info.vtno, strerror(errno));
216*4882a593Smuzhiyun }
217*4882a593Smuzhiyun }
218*4882a593Smuzhiyun break;
219*4882a593Smuzhiyun default:
220*4882a593Smuzhiyun break;
221*4882a593Smuzhiyun }
222*4882a593Smuzhiyun }
223*4882a593Smuzhiyun
224*4882a593Smuzhiyun /*
225*4882a593Smuzhiyun * xf86Wakeup --
226*4882a593Smuzhiyun * Os wakeup handler.
227*4882a593Smuzhiyun */
228*4882a593Smuzhiyun
229*4882a593Smuzhiyun /* ARGSUSED */
230*4882a593Smuzhiyun void
xf86Wakeup(void * blockData,int err)231*4882a593Smuzhiyun xf86Wakeup(void *blockData, int err)
232*4882a593Smuzhiyun {
233*4882a593Smuzhiyun if (xf86VTSwitchPending())
234*4882a593Smuzhiyun xf86VTSwitch();
235*4882a593Smuzhiyun }
236*4882a593Smuzhiyun
237*4882a593Smuzhiyun /*
238*4882a593Smuzhiyun * xf86ReadInput --
239*4882a593Smuzhiyun * input thread handler
240*4882a593Smuzhiyun */
241*4882a593Smuzhiyun
242*4882a593Smuzhiyun static void
xf86ReadInput(int fd,int ready,void * closure)243*4882a593Smuzhiyun xf86ReadInput(int fd, int ready, void *closure)
244*4882a593Smuzhiyun {
245*4882a593Smuzhiyun InputInfoPtr pInfo = closure;
246*4882a593Smuzhiyun
247*4882a593Smuzhiyun pInfo->read_input(pInfo);
248*4882a593Smuzhiyun }
249*4882a593Smuzhiyun
250*4882a593Smuzhiyun /*
251*4882a593Smuzhiyun * xf86AddEnabledDevice --
252*4882a593Smuzhiyun *
253*4882a593Smuzhiyun */
254*4882a593Smuzhiyun void
xf86AddEnabledDevice(InputInfoPtr pInfo)255*4882a593Smuzhiyun xf86AddEnabledDevice(InputInfoPtr pInfo)
256*4882a593Smuzhiyun {
257*4882a593Smuzhiyun InputThreadRegisterDev(pInfo->fd, xf86ReadInput, pInfo);
258*4882a593Smuzhiyun }
259*4882a593Smuzhiyun
260*4882a593Smuzhiyun /*
261*4882a593Smuzhiyun * xf86RemoveEnabledDevice --
262*4882a593Smuzhiyun *
263*4882a593Smuzhiyun */
264*4882a593Smuzhiyun void
xf86RemoveEnabledDevice(InputInfoPtr pInfo)265*4882a593Smuzhiyun xf86RemoveEnabledDevice(InputInfoPtr pInfo)
266*4882a593Smuzhiyun {
267*4882a593Smuzhiyun InputThreadUnregisterDev(pInfo->fd);
268*4882a593Smuzhiyun }
269*4882a593Smuzhiyun
270*4882a593Smuzhiyun /*
271*4882a593Smuzhiyun * xf86PrintBacktrace --
272*4882a593Smuzhiyun * Print a stack backtrace for debugging purposes.
273*4882a593Smuzhiyun */
274*4882a593Smuzhiyun void
xf86PrintBacktrace(void)275*4882a593Smuzhiyun xf86PrintBacktrace(void)
276*4882a593Smuzhiyun {
277*4882a593Smuzhiyun xorg_backtrace();
278*4882a593Smuzhiyun }
279*4882a593Smuzhiyun
280*4882a593Smuzhiyun static void
xf86ReleaseKeys(DeviceIntPtr pDev)281*4882a593Smuzhiyun xf86ReleaseKeys(DeviceIntPtr pDev)
282*4882a593Smuzhiyun {
283*4882a593Smuzhiyun KeyClassPtr keyc;
284*4882a593Smuzhiyun int i;
285*4882a593Smuzhiyun
286*4882a593Smuzhiyun if (!pDev || !pDev->key)
287*4882a593Smuzhiyun return;
288*4882a593Smuzhiyun
289*4882a593Smuzhiyun keyc = pDev->key;
290*4882a593Smuzhiyun
291*4882a593Smuzhiyun /*
292*4882a593Smuzhiyun * Hmm... here is the biggest hack of every time !
293*4882a593Smuzhiyun * It may be possible that a switch-vt procedure has finished BEFORE
294*4882a593Smuzhiyun * you released all keys neccessary to do this. That peculiar behavior
295*4882a593Smuzhiyun * can fool the X-server pretty much, cause it assumes that some keys
296*4882a593Smuzhiyun * were not released. TWM may stuck alsmost completly....
297*4882a593Smuzhiyun * OK, what we are doing here is after returning from the vt-switch
298*4882a593Smuzhiyun * exeplicitely unrelease all keyboard keys before the input-devices
299*4882a593Smuzhiyun * are reenabled.
300*4882a593Smuzhiyun */
301*4882a593Smuzhiyun
302*4882a593Smuzhiyun for (i = keyc->xkbInfo->desc->min_key_code;
303*4882a593Smuzhiyun i < keyc->xkbInfo->desc->max_key_code; i++) {
304*4882a593Smuzhiyun if (key_is_down(pDev, i, KEY_POSTED)) {
305*4882a593Smuzhiyun input_lock();
306*4882a593Smuzhiyun QueueKeyboardEvents(pDev, KeyRelease, i);
307*4882a593Smuzhiyun input_unlock();
308*4882a593Smuzhiyun }
309*4882a593Smuzhiyun }
310*4882a593Smuzhiyun }
311*4882a593Smuzhiyun
312*4882a593Smuzhiyun void
xf86DisableInputDeviceForVTSwitch(InputInfoPtr pInfo)313*4882a593Smuzhiyun xf86DisableInputDeviceForVTSwitch(InputInfoPtr pInfo)
314*4882a593Smuzhiyun {
315*4882a593Smuzhiyun if (!pInfo->dev)
316*4882a593Smuzhiyun return;
317*4882a593Smuzhiyun
318*4882a593Smuzhiyun if (!pInfo->dev->enabled)
319*4882a593Smuzhiyun pInfo->flags |= XI86_DEVICE_DISABLED;
320*4882a593Smuzhiyun
321*4882a593Smuzhiyun xf86ReleaseKeys(pInfo->dev);
322*4882a593Smuzhiyun ProcessInputEvents();
323*4882a593Smuzhiyun DisableDevice(pInfo->dev, TRUE);
324*4882a593Smuzhiyun }
325*4882a593Smuzhiyun
326*4882a593Smuzhiyun void
xf86EnableInputDeviceForVTSwitch(InputInfoPtr pInfo)327*4882a593Smuzhiyun xf86EnableInputDeviceForVTSwitch(InputInfoPtr pInfo)
328*4882a593Smuzhiyun {
329*4882a593Smuzhiyun if (pInfo->dev && (pInfo->flags & XI86_DEVICE_DISABLED) == 0)
330*4882a593Smuzhiyun EnableDevice(pInfo->dev, TRUE);
331*4882a593Smuzhiyun pInfo->flags &= ~XI86_DEVICE_DISABLED;
332*4882a593Smuzhiyun }
333*4882a593Smuzhiyun
334*4882a593Smuzhiyun /*
335*4882a593Smuzhiyun * xf86UpdateHasVTProperty --
336*4882a593Smuzhiyun * Update a flag property on the root window to say whether the server VT
337*4882a593Smuzhiyun * is currently the active one as some clients need to know this.
338*4882a593Smuzhiyun */
339*4882a593Smuzhiyun static void
xf86UpdateHasVTProperty(Bool hasVT)340*4882a593Smuzhiyun xf86UpdateHasVTProperty(Bool hasVT)
341*4882a593Smuzhiyun {
342*4882a593Smuzhiyun Atom property_name;
343*4882a593Smuzhiyun int32_t value = hasVT ? 1 : 0;
344*4882a593Smuzhiyun int i;
345*4882a593Smuzhiyun
346*4882a593Smuzhiyun property_name = MakeAtom(HAS_VT_ATOM_NAME, sizeof(HAS_VT_ATOM_NAME) - 1,
347*4882a593Smuzhiyun FALSE);
348*4882a593Smuzhiyun if (property_name == BAD_RESOURCE)
349*4882a593Smuzhiyun FatalError("Failed to retrieve \"HAS_VT\" atom\n");
350*4882a593Smuzhiyun for (i = 0; i < xf86NumScreens; i++) {
351*4882a593Smuzhiyun dixChangeWindowProperty(serverClient,
352*4882a593Smuzhiyun xf86ScrnToScreen(xf86Screens[i])->root,
353*4882a593Smuzhiyun property_name, XA_INTEGER, 32,
354*4882a593Smuzhiyun PropModeReplace, 1, &value, TRUE);
355*4882a593Smuzhiyun }
356*4882a593Smuzhiyun }
357*4882a593Smuzhiyun
358*4882a593Smuzhiyun void
xf86VTLeave(void)359*4882a593Smuzhiyun xf86VTLeave(void)
360*4882a593Smuzhiyun {
361*4882a593Smuzhiyun int i;
362*4882a593Smuzhiyun InputInfoPtr pInfo;
363*4882a593Smuzhiyun IHPtr ih;
364*4882a593Smuzhiyun
365*4882a593Smuzhiyun DebugF("xf86VTSwitch: Leaving, xf86Exiting is %s\n",
366*4882a593Smuzhiyun BOOLTOSTRING((dispatchException & DE_TERMINATE) ? TRUE : FALSE));
367*4882a593Smuzhiyun #ifdef DPMSExtension
368*4882a593Smuzhiyun if (DPMSPowerLevel != DPMSModeOn)
369*4882a593Smuzhiyun DPMSSet(serverClient, DPMSModeOn);
370*4882a593Smuzhiyun #endif
371*4882a593Smuzhiyun for (i = 0; i < xf86NumScreens; i++) {
372*4882a593Smuzhiyun if (!(dispatchException & DE_TERMINATE))
373*4882a593Smuzhiyun if (xf86Screens[i]->EnableDisableFBAccess)
374*4882a593Smuzhiyun (*xf86Screens[i]->EnableDisableFBAccess) (xf86Screens[i], FALSE);
375*4882a593Smuzhiyun }
376*4882a593Smuzhiyun
377*4882a593Smuzhiyun /*
378*4882a593Smuzhiyun * Keep the order: Disable Device > LeaveVT
379*4882a593Smuzhiyun * EnterVT > EnableDevice
380*4882a593Smuzhiyun */
381*4882a593Smuzhiyun for (ih = InputHandlers; ih; ih = ih->next) {
382*4882a593Smuzhiyun if (ih->is_input)
383*4882a593Smuzhiyun xf86DisableInputHandler(ih);
384*4882a593Smuzhiyun else
385*4882a593Smuzhiyun xf86DisableGeneralHandler(ih);
386*4882a593Smuzhiyun }
387*4882a593Smuzhiyun for (pInfo = xf86InputDevs; pInfo; pInfo = pInfo->next)
388*4882a593Smuzhiyun xf86DisableInputDeviceForVTSwitch(pInfo);
389*4882a593Smuzhiyun
390*4882a593Smuzhiyun input_lock();
391*4882a593Smuzhiyun for (i = 0; i < xf86NumScreens; i++)
392*4882a593Smuzhiyun xf86Screens[i]->LeaveVT(xf86Screens[i]);
393*4882a593Smuzhiyun for (i = 0; i < xf86NumGPUScreens; i++)
394*4882a593Smuzhiyun xf86GPUScreens[i]->LeaveVT(xf86GPUScreens[i]);
395*4882a593Smuzhiyun
396*4882a593Smuzhiyun if (!xf86VTSwitchAway())
397*4882a593Smuzhiyun goto switch_failed;
398*4882a593Smuzhiyun
399*4882a593Smuzhiyun #ifdef XF86PM
400*4882a593Smuzhiyun if (xf86OSPMClose)
401*4882a593Smuzhiyun xf86OSPMClose();
402*4882a593Smuzhiyun xf86OSPMClose = NULL;
403*4882a593Smuzhiyun #endif
404*4882a593Smuzhiyun
405*4882a593Smuzhiyun for (i = 0; i < xf86NumScreens; i++) {
406*4882a593Smuzhiyun /*
407*4882a593Smuzhiyun * zero all access functions to
408*4882a593Smuzhiyun * trap calls when switched away.
409*4882a593Smuzhiyun */
410*4882a593Smuzhiyun xf86Screens[i]->vtSema = FALSE;
411*4882a593Smuzhiyun }
412*4882a593Smuzhiyun if (xorgHWAccess)
413*4882a593Smuzhiyun xf86DisableIO();
414*4882a593Smuzhiyun
415*4882a593Smuzhiyun xf86UpdateHasVTProperty(FALSE);
416*4882a593Smuzhiyun
417*4882a593Smuzhiyun return;
418*4882a593Smuzhiyun
419*4882a593Smuzhiyun switch_failed:
420*4882a593Smuzhiyun DebugF("xf86VTSwitch: Leave failed\n");
421*4882a593Smuzhiyun for (i = 0; i < xf86NumScreens; i++) {
422*4882a593Smuzhiyun if (!xf86Screens[i]->EnterVT(xf86Screens[i]))
423*4882a593Smuzhiyun FatalError("EnterVT failed for screen %d\n", i);
424*4882a593Smuzhiyun }
425*4882a593Smuzhiyun for (i = 0; i < xf86NumGPUScreens; i++) {
426*4882a593Smuzhiyun if (!xf86GPUScreens[i]->EnterVT(xf86GPUScreens[i]))
427*4882a593Smuzhiyun FatalError("EnterVT failed for gpu screen %d\n", i);
428*4882a593Smuzhiyun }
429*4882a593Smuzhiyun if (!(dispatchException & DE_TERMINATE)) {
430*4882a593Smuzhiyun for (i = 0; i < xf86NumScreens; i++) {
431*4882a593Smuzhiyun if (xf86Screens[i]->EnableDisableFBAccess)
432*4882a593Smuzhiyun (*xf86Screens[i]->EnableDisableFBAccess) (xf86Screens[i], TRUE);
433*4882a593Smuzhiyun }
434*4882a593Smuzhiyun }
435*4882a593Smuzhiyun dixSaveScreens(serverClient, SCREEN_SAVER_FORCER, ScreenSaverReset);
436*4882a593Smuzhiyun
437*4882a593Smuzhiyun for (pInfo = xf86InputDevs; pInfo; pInfo = pInfo->next)
438*4882a593Smuzhiyun xf86EnableInputDeviceForVTSwitch(pInfo);
439*4882a593Smuzhiyun for (ih = InputHandlers; ih; ih = ih->next) {
440*4882a593Smuzhiyun if (ih->is_input)
441*4882a593Smuzhiyun xf86EnableInputHandler(ih);
442*4882a593Smuzhiyun else
443*4882a593Smuzhiyun xf86EnableGeneralHandler(ih);
444*4882a593Smuzhiyun }
445*4882a593Smuzhiyun input_unlock();
446*4882a593Smuzhiyun }
447*4882a593Smuzhiyun
448*4882a593Smuzhiyun void
xf86VTEnter(void)449*4882a593Smuzhiyun xf86VTEnter(void)
450*4882a593Smuzhiyun {
451*4882a593Smuzhiyun int i;
452*4882a593Smuzhiyun InputInfoPtr pInfo;
453*4882a593Smuzhiyun IHPtr ih;
454*4882a593Smuzhiyun
455*4882a593Smuzhiyun DebugF("xf86VTSwitch: Entering\n");
456*4882a593Smuzhiyun if (!xf86VTSwitchTo())
457*4882a593Smuzhiyun return;
458*4882a593Smuzhiyun
459*4882a593Smuzhiyun #ifdef XF86PM
460*4882a593Smuzhiyun xf86OSPMClose = xf86OSPMOpen();
461*4882a593Smuzhiyun #endif
462*4882a593Smuzhiyun
463*4882a593Smuzhiyun if (xorgHWAccess)
464*4882a593Smuzhiyun xf86EnableIO();
465*4882a593Smuzhiyun for (i = 0; i < xf86NumScreens; i++) {
466*4882a593Smuzhiyun xf86Screens[i]->vtSema = TRUE;
467*4882a593Smuzhiyun if (!xf86Screens[i]->EnterVT(xf86Screens[i]))
468*4882a593Smuzhiyun FatalError("EnterVT failed for screen %d\n", i);
469*4882a593Smuzhiyun }
470*4882a593Smuzhiyun for (i = 0; i < xf86NumGPUScreens; i++) {
471*4882a593Smuzhiyun xf86GPUScreens[i]->vtSema = TRUE;
472*4882a593Smuzhiyun if (!xf86GPUScreens[i]->EnterVT(xf86GPUScreens[i]))
473*4882a593Smuzhiyun FatalError("EnterVT failed for gpu screen %d\n", i);
474*4882a593Smuzhiyun }
475*4882a593Smuzhiyun for (i = 0; i < xf86NumScreens; i++) {
476*4882a593Smuzhiyun if (xf86Screens[i]->EnableDisableFBAccess)
477*4882a593Smuzhiyun (*xf86Screens[i]->EnableDisableFBAccess) (xf86Screens[i], TRUE);
478*4882a593Smuzhiyun }
479*4882a593Smuzhiyun
480*4882a593Smuzhiyun /* Turn screen saver off when switching back */
481*4882a593Smuzhiyun dixSaveScreens(serverClient, SCREEN_SAVER_FORCER, ScreenSaverReset);
482*4882a593Smuzhiyun
483*4882a593Smuzhiyun for (pInfo = xf86InputDevs; pInfo; pInfo = pInfo->next) {
484*4882a593Smuzhiyun /* Devices with server managed fds get enabled on logind resume */
485*4882a593Smuzhiyun if (!(pInfo->flags & XI86_SERVER_FD))
486*4882a593Smuzhiyun xf86EnableInputDeviceForVTSwitch(pInfo);
487*4882a593Smuzhiyun }
488*4882a593Smuzhiyun
489*4882a593Smuzhiyun for (ih = InputHandlers; ih; ih = ih->next) {
490*4882a593Smuzhiyun if (ih->is_input)
491*4882a593Smuzhiyun xf86EnableInputHandler(ih);
492*4882a593Smuzhiyun else
493*4882a593Smuzhiyun xf86EnableGeneralHandler(ih);
494*4882a593Smuzhiyun }
495*4882a593Smuzhiyun #ifdef XSERVER_PLATFORM_BUS
496*4882a593Smuzhiyun /* check for any new output devices */
497*4882a593Smuzhiyun xf86platformVTProbe();
498*4882a593Smuzhiyun #endif
499*4882a593Smuzhiyun
500*4882a593Smuzhiyun xf86UpdateHasVTProperty(TRUE);
501*4882a593Smuzhiyun
502*4882a593Smuzhiyun input_unlock();
503*4882a593Smuzhiyun }
504*4882a593Smuzhiyun
505*4882a593Smuzhiyun /*
506*4882a593Smuzhiyun * xf86VTSwitch --
507*4882a593Smuzhiyun * Handle requests for switching the vt.
508*4882a593Smuzhiyun */
509*4882a593Smuzhiyun static void
xf86VTSwitch(void)510*4882a593Smuzhiyun xf86VTSwitch(void)
511*4882a593Smuzhiyun {
512*4882a593Smuzhiyun DebugF("xf86VTSwitch()\n");
513*4882a593Smuzhiyun
514*4882a593Smuzhiyun #ifdef XFreeXDGA
515*4882a593Smuzhiyun if (!DGAVTSwitch())
516*4882a593Smuzhiyun return;
517*4882a593Smuzhiyun #endif
518*4882a593Smuzhiyun
519*4882a593Smuzhiyun /*
520*4882a593Smuzhiyun * Since all screens are currently all in the same state it is sufficient
521*4882a593Smuzhiyun * check the first. This might change in future.
522*4882a593Smuzhiyun *
523*4882a593Smuzhiyun * VTLeave is always handled here (VT_PROCESS guarantees this is safe),
524*4882a593Smuzhiyun * if we use systemd_logind xf86VTEnter() gets called by systemd-logind.c
525*4882a593Smuzhiyun * once it has resumed all drm nodes.
526*4882a593Smuzhiyun */
527*4882a593Smuzhiyun if (xf86VTOwner())
528*4882a593Smuzhiyun xf86VTLeave();
529*4882a593Smuzhiyun else if (!systemd_logind_controls_session())
530*4882a593Smuzhiyun xf86VTEnter();
531*4882a593Smuzhiyun }
532*4882a593Smuzhiyun
533*4882a593Smuzhiyun /* Input handler registration */
534*4882a593Smuzhiyun
535*4882a593Smuzhiyun static void
xf86InputHandlerNotify(int fd,int ready,void * data)536*4882a593Smuzhiyun xf86InputHandlerNotify(int fd, int ready, void *data)
537*4882a593Smuzhiyun {
538*4882a593Smuzhiyun IHPtr ih = data;
539*4882a593Smuzhiyun
540*4882a593Smuzhiyun if (ih->enabled && ih->fd >= 0 && ih->ihproc) {
541*4882a593Smuzhiyun ih->ihproc(ih->fd, ih->data);
542*4882a593Smuzhiyun }
543*4882a593Smuzhiyun }
544*4882a593Smuzhiyun
545*4882a593Smuzhiyun static void *
addInputHandler(int fd,InputHandlerProc proc,void * data)546*4882a593Smuzhiyun addInputHandler(int fd, InputHandlerProc proc, void *data)
547*4882a593Smuzhiyun {
548*4882a593Smuzhiyun IHPtr ih;
549*4882a593Smuzhiyun
550*4882a593Smuzhiyun if (fd < 0 || !proc)
551*4882a593Smuzhiyun return NULL;
552*4882a593Smuzhiyun
553*4882a593Smuzhiyun ih = calloc(sizeof(*ih), 1);
554*4882a593Smuzhiyun if (!ih)
555*4882a593Smuzhiyun return NULL;
556*4882a593Smuzhiyun
557*4882a593Smuzhiyun ih->fd = fd;
558*4882a593Smuzhiyun ih->ihproc = proc;
559*4882a593Smuzhiyun ih->data = data;
560*4882a593Smuzhiyun ih->enabled = TRUE;
561*4882a593Smuzhiyun
562*4882a593Smuzhiyun if (!SetNotifyFd(fd, xf86InputHandlerNotify, X_NOTIFY_READ, ih)) {
563*4882a593Smuzhiyun free(ih);
564*4882a593Smuzhiyun return NULL;
565*4882a593Smuzhiyun }
566*4882a593Smuzhiyun
567*4882a593Smuzhiyun ih->next = InputHandlers;
568*4882a593Smuzhiyun InputHandlers = ih;
569*4882a593Smuzhiyun
570*4882a593Smuzhiyun return ih;
571*4882a593Smuzhiyun }
572*4882a593Smuzhiyun
573*4882a593Smuzhiyun void *
xf86AddInputHandler(int fd,InputHandlerProc proc,void * data)574*4882a593Smuzhiyun xf86AddInputHandler(int fd, InputHandlerProc proc, void *data)
575*4882a593Smuzhiyun {
576*4882a593Smuzhiyun IHPtr ih = addInputHandler(fd, proc, data);
577*4882a593Smuzhiyun
578*4882a593Smuzhiyun if (ih)
579*4882a593Smuzhiyun ih->is_input = TRUE;
580*4882a593Smuzhiyun return ih;
581*4882a593Smuzhiyun }
582*4882a593Smuzhiyun
583*4882a593Smuzhiyun void *
xf86AddGeneralHandler(int fd,InputHandlerProc proc,void * data)584*4882a593Smuzhiyun xf86AddGeneralHandler(int fd, InputHandlerProc proc, void *data)
585*4882a593Smuzhiyun {
586*4882a593Smuzhiyun IHPtr ih = addInputHandler(fd, proc, data);
587*4882a593Smuzhiyun
588*4882a593Smuzhiyun return ih;
589*4882a593Smuzhiyun }
590*4882a593Smuzhiyun
591*4882a593Smuzhiyun /**
592*4882a593Smuzhiyun * Set the handler for the console's fd. Replaces (and returns) the previous
593*4882a593Smuzhiyun * handler or NULL, whichever appropriate.
594*4882a593Smuzhiyun * proc may be NULL if the server should not handle events on the console.
595*4882a593Smuzhiyun */
596*4882a593Smuzhiyun InputHandlerProc
xf86SetConsoleHandler(InputHandlerProc proc,void * data)597*4882a593Smuzhiyun xf86SetConsoleHandler(InputHandlerProc proc, void *data)
598*4882a593Smuzhiyun {
599*4882a593Smuzhiyun static IHPtr handler = NULL;
600*4882a593Smuzhiyun InputHandlerProc old_proc = NULL;
601*4882a593Smuzhiyun
602*4882a593Smuzhiyun if (handler) {
603*4882a593Smuzhiyun old_proc = handler->ihproc;
604*4882a593Smuzhiyun xf86RemoveGeneralHandler(handler);
605*4882a593Smuzhiyun }
606*4882a593Smuzhiyun
607*4882a593Smuzhiyun handler = xf86AddGeneralHandler(xf86Info.consoleFd, proc, data);
608*4882a593Smuzhiyun
609*4882a593Smuzhiyun return old_proc;
610*4882a593Smuzhiyun }
611*4882a593Smuzhiyun
612*4882a593Smuzhiyun static void
removeInputHandler(IHPtr ih)613*4882a593Smuzhiyun removeInputHandler(IHPtr ih)
614*4882a593Smuzhiyun {
615*4882a593Smuzhiyun IHPtr p;
616*4882a593Smuzhiyun
617*4882a593Smuzhiyun if (ih->fd >= 0)
618*4882a593Smuzhiyun RemoveNotifyFd(ih->fd);
619*4882a593Smuzhiyun if (ih == InputHandlers)
620*4882a593Smuzhiyun InputHandlers = ih->next;
621*4882a593Smuzhiyun else {
622*4882a593Smuzhiyun p = InputHandlers;
623*4882a593Smuzhiyun while (p && p->next != ih)
624*4882a593Smuzhiyun p = p->next;
625*4882a593Smuzhiyun if (ih)
626*4882a593Smuzhiyun p->next = ih->next;
627*4882a593Smuzhiyun }
628*4882a593Smuzhiyun free(ih);
629*4882a593Smuzhiyun }
630*4882a593Smuzhiyun
631*4882a593Smuzhiyun int
xf86RemoveInputHandler(void * handler)632*4882a593Smuzhiyun xf86RemoveInputHandler(void *handler)
633*4882a593Smuzhiyun {
634*4882a593Smuzhiyun IHPtr ih;
635*4882a593Smuzhiyun int fd;
636*4882a593Smuzhiyun
637*4882a593Smuzhiyun if (!handler)
638*4882a593Smuzhiyun return -1;
639*4882a593Smuzhiyun
640*4882a593Smuzhiyun ih = handler;
641*4882a593Smuzhiyun fd = ih->fd;
642*4882a593Smuzhiyun
643*4882a593Smuzhiyun removeInputHandler(ih);
644*4882a593Smuzhiyun
645*4882a593Smuzhiyun return fd;
646*4882a593Smuzhiyun }
647*4882a593Smuzhiyun
648*4882a593Smuzhiyun int
xf86RemoveGeneralHandler(void * handler)649*4882a593Smuzhiyun xf86RemoveGeneralHandler(void *handler)
650*4882a593Smuzhiyun {
651*4882a593Smuzhiyun IHPtr ih;
652*4882a593Smuzhiyun int fd;
653*4882a593Smuzhiyun
654*4882a593Smuzhiyun if (!handler)
655*4882a593Smuzhiyun return -1;
656*4882a593Smuzhiyun
657*4882a593Smuzhiyun ih = handler;
658*4882a593Smuzhiyun fd = ih->fd;
659*4882a593Smuzhiyun
660*4882a593Smuzhiyun removeInputHandler(ih);
661*4882a593Smuzhiyun
662*4882a593Smuzhiyun return fd;
663*4882a593Smuzhiyun }
664*4882a593Smuzhiyun
665*4882a593Smuzhiyun void
xf86DisableInputHandler(void * handler)666*4882a593Smuzhiyun xf86DisableInputHandler(void *handler)
667*4882a593Smuzhiyun {
668*4882a593Smuzhiyun IHPtr ih;
669*4882a593Smuzhiyun
670*4882a593Smuzhiyun if (!handler)
671*4882a593Smuzhiyun return;
672*4882a593Smuzhiyun
673*4882a593Smuzhiyun ih = handler;
674*4882a593Smuzhiyun ih->enabled = FALSE;
675*4882a593Smuzhiyun if (ih->fd >= 0)
676*4882a593Smuzhiyun RemoveNotifyFd(ih->fd);
677*4882a593Smuzhiyun }
678*4882a593Smuzhiyun
679*4882a593Smuzhiyun void
xf86DisableGeneralHandler(void * handler)680*4882a593Smuzhiyun xf86DisableGeneralHandler(void *handler)
681*4882a593Smuzhiyun {
682*4882a593Smuzhiyun IHPtr ih;
683*4882a593Smuzhiyun
684*4882a593Smuzhiyun if (!handler)
685*4882a593Smuzhiyun return;
686*4882a593Smuzhiyun
687*4882a593Smuzhiyun ih = handler;
688*4882a593Smuzhiyun ih->enabled = FALSE;
689*4882a593Smuzhiyun if (ih->fd >= 0)
690*4882a593Smuzhiyun RemoveNotifyFd(ih->fd);
691*4882a593Smuzhiyun }
692*4882a593Smuzhiyun
693*4882a593Smuzhiyun void
xf86EnableInputHandler(void * handler)694*4882a593Smuzhiyun xf86EnableInputHandler(void *handler)
695*4882a593Smuzhiyun {
696*4882a593Smuzhiyun IHPtr ih;
697*4882a593Smuzhiyun
698*4882a593Smuzhiyun if (!handler)
699*4882a593Smuzhiyun return;
700*4882a593Smuzhiyun
701*4882a593Smuzhiyun ih = handler;
702*4882a593Smuzhiyun ih->enabled = TRUE;
703*4882a593Smuzhiyun if (ih->fd >= 0)
704*4882a593Smuzhiyun SetNotifyFd(ih->fd, xf86InputHandlerNotify, X_NOTIFY_READ, ih);
705*4882a593Smuzhiyun }
706*4882a593Smuzhiyun
707*4882a593Smuzhiyun void
xf86EnableGeneralHandler(void * handler)708*4882a593Smuzhiyun xf86EnableGeneralHandler(void *handler)
709*4882a593Smuzhiyun {
710*4882a593Smuzhiyun IHPtr ih;
711*4882a593Smuzhiyun
712*4882a593Smuzhiyun if (!handler)
713*4882a593Smuzhiyun return;
714*4882a593Smuzhiyun
715*4882a593Smuzhiyun ih = handler;
716*4882a593Smuzhiyun ih->enabled = TRUE;
717*4882a593Smuzhiyun if (ih->fd >= 0)
718*4882a593Smuzhiyun SetNotifyFd(ih->fd, xf86InputHandlerNotify, X_NOTIFY_READ, ih);
719*4882a593Smuzhiyun }
720*4882a593Smuzhiyun
721*4882a593Smuzhiyun void
DDXRingBell(int volume,int pitch,int duration)722*4882a593Smuzhiyun DDXRingBell(int volume, int pitch, int duration)
723*4882a593Smuzhiyun {
724*4882a593Smuzhiyun xf86OSRingBell(volume, pitch, duration);
725*4882a593Smuzhiyun }
726*4882a593Smuzhiyun
727*4882a593Smuzhiyun Bool
xf86VTOwner(void)728*4882a593Smuzhiyun xf86VTOwner(void)
729*4882a593Smuzhiyun {
730*4882a593Smuzhiyun /* at system startup xf86Screens[0] won't be set - but we will own the VT */
731*4882a593Smuzhiyun if (xf86NumScreens == 0)
732*4882a593Smuzhiyun return TRUE;
733*4882a593Smuzhiyun return xf86Screens[0]->vtSema;
734*4882a593Smuzhiyun }
735