#ifdef HAVE_XORG_CONFIG_H
#include <xorg-config.h>
#endif
#include <X11/X.h>
#include <X11/Xpoll.h>
#include <X11/Xproto.h>
#include "misc.h"
#include "compiler.h"
#include "xf86.h"
#include "xf86Priv.h"
#define XF86_OS_PRIVS
#include "xf86_OSlib.h"
#include <X11/keysym.h>
#ifdef XFreeXDGA
#include "dgaproc.h"
#endif
#include <X11/extensions/XI.h>
#include <X11/extensions/XIproto.h>
#include "inputstr.h"
#include "xf86Xinput.h"
#include "mi.h"
#include "mipointer.h"
#include "xkbsrv.h"
#include "xkbstr.h"
#ifdef DPMSExtension
#include <X11/extensions/dpmsconst.h>
#include "dpmsproc.h"
#endif
Bool VTSwitchEnabled = TRUE;
extern fd_set EnabledDevices;
#ifdef XF86PM
extern void (*xf86OSPMClose)(void);
#endif
static void xf86VTSwitch(void);
typedef struct x_IHRec {
int fd;
InputHandlerProc ihproc;
pointer data;
Bool enabled;
struct x_IHRec * next;
} IHRec, *IHPtr;
static IHPtr InputHandlers = NULL;
Bool
LegalModifier(unsigned int key, DeviceIntPtr pDev)
{
return TRUE;
}
int
TimeSinceLastInputEvent(void)
{
if (xf86Info.lastEventTime == 0) {
xf86Info.lastEventTime = GetTimeInMillis();
}
return GetTimeInMillis() - xf86Info.lastEventTime;
}
void
SetTimeSinceLastInputEvent(void)
{
xf86Info.lastEventTime = GetTimeInMillis();
}
void
ProcessInputEvents (void)
{
int x, y;
mieqProcessInputEvents();
miPointerGetPosition(inputInfo.pointer, &x, &y);
xf86SetViewport(xf86Info.currentScreen, x, y);
}
void
xf86ProcessActionEvent(ActionEvent action, void *arg)
{
DebugF("ProcessActionEvent(%d,%x)\n", (int) action, arg);
switch (action) {
case ACTION_TERMINATE:
if (!xf86Info.dontZap) {
#ifdef XFreeXDGA
DGAShutdown();
#endif
GiveUp(0);
}
break;
case ACTION_NEXT_MODE:
if (!xf86Info.dontZoom)
xf86ZoomViewport(xf86Info.currentScreen, 1);
break;
case ACTION_PREV_MODE:
if (!xf86Info.dontZoom)
xf86ZoomViewport(xf86Info.currentScreen, -1);
break;
case ACTION_SWITCHSCREEN:
if (VTSwitchEnabled && !xf86Info.dontVTSwitch && arg) {
int vtno = *((int *) arg);
if (vtno != xf86Info.vtno) {
if (!xf86VTActivate(vtno)) {
ErrorF("Failed to switch from vt%02d to vt%02d: %s\n",
xf86Info.vtno, vtno, strerror(errno));
}
}
}
break;
case ACTION_SWITCHSCREEN_NEXT:
if (VTSwitchEnabled && !xf86Info.dontVTSwitch) {
if (!xf86VTActivate(xf86Info.vtno + 1)) {
if (!xf86VTActivate(1)) {
ErrorF("Failed to switch from vt%02d to next vt: %s\n",
xf86Info.vtno, strerror(errno));
}
}
}
break;
case ACTION_SWITCHSCREEN_PREV:
if (VTSwitchEnabled && !xf86Info.dontVTSwitch && xf86Info.vtno > 0) {
if (!xf86VTActivate(xf86Info.vtno - 1)) {
ErrorF("Failed to switch from vt%02d to previous vt: %s\n",
xf86Info.vtno, strerror(errno));
}
}
break;
default:
break;
}
}
void
xf86Wakeup(pointer blockData, int err, pointer pReadmask)
{
fd_set* LastSelectMask = (fd_set*)pReadmask;
fd_set devicesWithInput;
InputInfoPtr pInfo;
if (err >= 0) {
XFD_ANDSET(&devicesWithInput, LastSelectMask, &EnabledDevices);
if (XFD_ANYSET(&devicesWithInput)) {
pInfo = xf86InputDevs;
while (pInfo) {
if (pInfo->read_input && pInfo->fd >= 0 &&
(FD_ISSET(pInfo->fd, &devicesWithInput) != 0)) {
int sigstate = xf86BlockSIGIO();
FD_CLR(pInfo->fd, &devicesWithInput);
pInfo->read_input(pInfo);
xf86UnblockSIGIO(sigstate);
}
pInfo = pInfo->next;
}
}
}
if (err >= 0) {
IHPtr ih;
for (ih = InputHandlers; ih; ih = ih->next) {
if (ih->enabled && ih->fd >= 0 && ih->ihproc &&
(FD_ISSET(ih->fd, ((fd_set *)pReadmask)) != 0)) {
ih->ihproc(ih->fd, ih->data);
}
}
}
if (xf86VTSwitchPending()) xf86VTSwitch();
}
static void
xf86SigioReadInput(int fd, void *closure)
{
int errno_save = errno;
InputInfoPtr pInfo = closure;
pInfo->read_input(pInfo);
errno = errno_save;
}
void
xf86AddEnabledDevice(InputInfoPtr pInfo)
{
if (!xf86InstallSIGIOHandler (pInfo->fd, xf86SigioReadInput, pInfo)) {
AddEnabledDevice(pInfo->fd);
}
}
void
xf86RemoveEnabledDevice(InputInfoPtr pInfo)
{
if (!xf86RemoveSIGIOHandler (pInfo->fd)) {
RemoveEnabledDevice(pInfo->fd);
}
}
static int *xf86SignalIntercept = NULL;
void
xf86InterceptSignals(int *signo)
{
if ((xf86SignalIntercept = signo))
*signo = -1;
}
static void (*xf86SigIllHandler)(void) = NULL;
void
xf86InterceptSigIll(void (*sigillhandler)(void))
{
xf86SigIllHandler = sigillhandler;
}
int
xf86SigWrapper(int signo)
{
if ((signo == SIGILL) && xf86SigIllHandler) {
(*xf86SigIllHandler)();
return 0;
}
if (xf86SignalIntercept && (*xf86SignalIntercept < 0)) {
*xf86SignalIntercept = signo;
return 0;
}
xf86Info.caughtSignal = TRUE;
return 1;
}
void
xf86PrintBacktrace(void)
{
xorg_backtrace();
}
static void
xf86ReleaseKeys(DeviceIntPtr pDev)
{
KeyClassPtr keyc;
int i, j, nevents, sigstate;
if (!pDev || !pDev->key)
return;
keyc = pDev->key;
for (i = keyc->xkbInfo->desc->min_key_code;
i < keyc->xkbInfo->desc->max_key_code;
i++) {
if (key_is_down(pDev, i, KEY_POSTED)) {
sigstate = xf86BlockSIGIO ();
nevents = GetKeyboardEvents(xf86Events, pDev, KeyRelease, i);
for (j = 0; j < nevents; j++)
mieqEnqueue(pDev, (InternalEvent*)(xf86Events + j)->event);
xf86UnblockSIGIO(sigstate);
}
}
}
static void
xf86VTSwitch(void)
{
int i;
static int prevSIGIO;
InputInfoPtr pInfo;
IHPtr ih;
DebugF("xf86VTSwitch()\n");
#ifdef XFreeXDGA
if(!DGAVTSwitch())
return;
#endif
if (xf86Screens[0]->vtSema) {
DebugF("xf86VTSwitch: Leaving, xf86Exiting is %s\n",
BOOLTOSTRING((dispatchException & DE_TERMINATE) ? TRUE : FALSE));
#ifdef DPMSExtension
if (DPMSPowerLevel != DPMSModeOn)
DPMSSet(serverClient, DPMSModeOn);
#endif
for (i = 0; i < xf86NumScreens; i++) {
if (!(dispatchException & DE_TERMINATE))
if (xf86Screens[i]->EnableDisableFBAccess)
(*xf86Screens[i]->EnableDisableFBAccess) (i, FALSE);
}
for (ih = InputHandlers; ih; ih = ih->next)
xf86DisableInputHandler(ih);
for (pInfo = xf86InputDevs; pInfo; pInfo = pInfo->next) {
if (pInfo->dev) {
xf86ReleaseKeys(pInfo->dev);
ProcessInputEvents();
DisableDevice(pInfo->dev, TRUE);
}
}
prevSIGIO = xf86BlockSIGIO();
for (i = 0; i < xf86NumScreens; i++)
xf86Screens[i]->LeaveVT(i, 0);
xf86AccessLeave();
if (!xf86VTSwitchAway()) {
DebugF("xf86VTSwitch: Leave failed\n");
xf86AccessEnter();
for (i = 0; i < xf86NumScreens; i++) {
if (!xf86Screens[i]->EnterVT(i, 0))
FatalError("EnterVT failed for screen %d\n", i);
}
if (!(dispatchException & DE_TERMINATE)) {
for (i = 0; i < xf86NumScreens; i++) {
if (xf86Screens[i]->EnableDisableFBAccess)
(*xf86Screens[i]->EnableDisableFBAccess) (i, TRUE);
}
}
dixSaveScreens(serverClient, SCREEN_SAVER_FORCER, ScreenSaverReset);
pInfo = xf86InputDevs;
while (pInfo) {
if (pInfo->dev)
EnableDevice(pInfo->dev, TRUE);
pInfo = pInfo->next;
}
for (ih = InputHandlers; ih; ih = ih->next)
xf86EnableInputHandler(ih);
xf86UnblockSIGIO(prevSIGIO);
} else {
#ifdef XF86PM
if (xf86OSPMClose)
xf86OSPMClose();
xf86OSPMClose = NULL;
#endif
for (i = 0; i < xf86NumScreens; i++) {
xf86Screens[i]->vtSema = FALSE;
}
if (xorgHWAccess)
xf86DisableIO();
}
} else {
DebugF("xf86VTSwitch: Entering\n");
if (!xf86VTSwitchTo()) return;
#ifdef XF86PM
xf86OSPMClose = xf86OSPMOpen();
#endif
if (xorgHWAccess)
xf86EnableIO();
xf86AccessEnter();
for (i = 0; i < xf86NumScreens; i++) {
xf86Screens[i]->vtSema = TRUE;
if (!xf86Screens[i]->EnterVT(i, 0))
FatalError("EnterVT failed for screen %d\n", i);
}
for (i = 0; i < xf86NumScreens; i++) {
if (xf86Screens[i]->EnableDisableFBAccess)
(*xf86Screens[i]->EnableDisableFBAccess)(i, TRUE);
}
dixSaveScreens(serverClient, SCREEN_SAVER_FORCER, ScreenSaverReset);
pInfo = xf86InputDevs;
while (pInfo) {
if (pInfo->dev)
EnableDevice(pInfo->dev, TRUE);
pInfo = pInfo->next;
}
for (ih = InputHandlers; ih; ih = ih->next)
xf86EnableInputHandler(ih);
xf86UnblockSIGIO(prevSIGIO);
}
}
static pointer
addInputHandler(int fd, InputHandlerProc proc, pointer data)
{
IHPtr ih;
if (fd < 0 || !proc)
return NULL;
ih = calloc(sizeof(*ih), 1);
if (!ih)
return NULL;
ih->fd = fd;
ih->ihproc = proc;
ih->data = data;
ih->enabled = TRUE;
ih->next = InputHandlers;
InputHandlers = ih;
return ih;
}
pointer
xf86AddInputHandler(int fd, InputHandlerProc proc, pointer data)
{
IHPtr ih = addInputHandler(fd, proc, data);
if (ih)
AddEnabledDevice(fd);
return ih;
}
pointer
xf86AddGeneralHandler(int fd, InputHandlerProc proc, pointer data)
{
IHPtr ih = addInputHandler(fd, proc, data);
if (ih)
AddGeneralSocket(fd);
return ih;
}
InputHandlerProc
xf86SetConsoleHandler(InputHandlerProc proc, pointer data)
{
static IHPtr handler = NULL;
IHPtr old_handler = handler;
if (old_handler)
xf86RemoveGeneralHandler(old_handler);
handler = xf86AddGeneralHandler(xf86Info.consoleFd, proc, data);
return (old_handler) ? old_handler->ihproc : NULL;
}
static void
removeInputHandler(IHPtr ih)
{
IHPtr p;
if (ih == InputHandlers)
InputHandlers = ih->next;
else {
p = InputHandlers;
while (p && p->next != ih)
p = p->next;
if (ih)
p->next = ih->next;
}
free(ih);
}
int
xf86RemoveInputHandler(pointer handler)
{
IHPtr ih;
int fd;
if (!handler)
return -1;
ih = handler;
fd = ih->fd;
if (ih->fd >= 0)
RemoveEnabledDevice(ih->fd);
removeInputHandler(ih);
return fd;
}
int
xf86RemoveGeneralHandler(pointer handler)
{
IHPtr ih;
int fd;
if (!handler)
return -1;
ih = handler;
fd = ih->fd;
if (ih->fd >= 0)
RemoveGeneralSocket(ih->fd);
removeInputHandler(ih);
return fd;
}
void
xf86DisableInputHandler(pointer handler)
{
IHPtr ih;
if (!handler)
return;
ih = handler;
ih->enabled = FALSE;
if (ih->fd >= 0)
RemoveEnabledDevice(ih->fd);
}
void
xf86DisableGeneralHandler(pointer handler)
{
IHPtr ih;
if (!handler)
return;
ih = handler;
ih->enabled = FALSE;
if (ih->fd >= 0)
RemoveGeneralSocket(ih->fd);
}
void
xf86EnableInputHandler(pointer handler)
{
IHPtr ih;
if (!handler)
return;
ih = handler;
ih->enabled = TRUE;
if (ih->fd >= 0)
AddEnabledDevice(ih->fd);
}
void
xf86EnableGeneralHandler(pointer handler)
{
IHPtr ih;
if (!handler)
return;
ih = handler;
ih->enabled = TRUE;
if (ih->fd >= 0)
AddGeneralSocket(ih->fd);
}
Bool
xf86EnableVTSwitch(Bool new)
{
static Bool def = TRUE;
Bool old;
old = VTSwitchEnabled;
if (!new) {
def = VTSwitchEnabled;
VTSwitchEnabled = FALSE;
} else {
VTSwitchEnabled = def;
}
return old;
}
void
DDXRingBell(int volume, int pitch, int duration) {
xf86OSRingBell(volume, pitch, duration);
}