fluxbox/src/BaseDisplay.cc

584 lines
14 KiB
C++
Raw Normal View History

// BaseDisplay.cc for Fluxbox Window manager
// Copyright (c) 2001 - 2002 Henrik Kinnunen (fluxgen@linuxmail.org)
//
2001-12-11 20:47:02 +00:00
// BaseDisplay.cc for Blackbox - an X11 Window manager
// Copyright (c) 1997 - 2000 Brad Hughes (bhughes@tcac.net)
//
// 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 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,
2002-01-27 13:08:53 +00:00
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
2001-12-11 20:47:02 +00:00
// 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.
2002-03-23 02:02:01 +00:00
// $Id: BaseDisplay.cc,v 1.11 2002/03/23 02:02:00 pekdon Exp $
2001-12-11 20:47:02 +00:00
// use GNU extensions
2002-01-27 13:08:53 +00:00
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
2001-12-11 20:47:02 +00:00
#endif // _GNU_SOURCE
2002-01-27 13:08:53 +00:00
#ifdef HAVE_CONFIG_H
# include "../config.h"
2001-12-11 20:47:02 +00:00
#endif // HAVE_CONFIG_H
2002-01-27 13:08:53 +00:00
#include "BaseDisplay.hh"
#include "i18n.hh"
#include "Timer.hh"
2002-01-27 13:08:53 +00:00
2001-12-11 20:47:02 +00:00
#include <X11/Xutil.h>
#include <X11/cursorfont.h>
2002-01-27 13:08:53 +00:00
#ifdef SHAPE
# include <X11/extensions/shape.h>
2001-12-11 20:47:02 +00:00
#endif // SHAPE
2002-01-27 13:08:53 +00:00
#ifdef HAVE_FCNTL_H
# include <fcntl.h>
2001-12-11 20:47:02 +00:00
#endif // HAVE_FCNTL_H
2002-01-27 13:08:53 +00:00
#ifdef HAVE_STDIO_H
# include <stdio.h>
2001-12-11 20:47:02 +00:00
#endif // HAVE_STDIO_H
2002-01-27 13:08:53 +00:00
#ifdef STDC_HEADERS
# include <stdlib.h>
# include <string.h>
2001-12-11 20:47:02 +00:00
#endif // STDC_HEADERS
2002-01-27 13:08:53 +00:00
#ifdef HAVE_UNISTD_H
# include <sys/types.h>
# include <unistd.h>
2001-12-11 20:47:02 +00:00
#endif // HAVE_UNISTD_H
2002-01-27 13:08:53 +00:00
#ifdef HAVE_SYS_SELECT_H
# include <sys/select.h>
2001-12-11 20:47:02 +00:00
#endif // HAVE_SYS_SELECT_H
2002-01-27 13:08:53 +00:00
#ifdef HAVE_SIGNAL_H
# include <signal.h>
2001-12-11 20:47:02 +00:00
#endif // HAVE_SIGNAL_H
2002-01-27 13:08:53 +00:00
#ifndef SA_NODEFER
# ifdef SA_INTERRUPT
# define SA_NODEFER SA_INTERRUPT
# else // !SA_INTERRUPT
# define SA_NODEFER (0)
# endif // SA_INTERRUPT
2001-12-11 20:47:02 +00:00
#endif // SA_NODEFER
2002-01-27 13:08:53 +00:00
#ifdef HAVE_SYS_WAIT_H
# include <sys/types.h>
# include <sys/wait.h>
2001-12-11 20:47:02 +00:00
#endif // HAVE_SYS_WAIT_H
#if defined(HAVE_PROCESS_H) && defined(__EMX__)
2002-01-27 13:08:53 +00:00
# include <process.h>
#endif // HAVE_PROCESS_H __EMX__
2001-12-11 20:47:02 +00:00
2002-02-11 10:57:23 +00:00
#ifdef DEBUG
#include <iostream>
using namespace std;
#endif
2001-12-11 20:47:02 +00:00
// X error handler to handle any and all X errors while the application is
// running
static Bool internal_error = False;
static Window last_bad_window = None;
BaseDisplay *base_display;
2002-01-27 13:08:53 +00:00
#ifdef DEBUG
2001-12-11 20:47:02 +00:00
static int handleXErrors(Display *d, XErrorEvent *e) {
2002-01-27 13:08:53 +00:00
char errtxt[128];
2001-12-11 20:47:02 +00:00
2002-01-27 13:08:53 +00:00
XGetErrorText(d, e->error_code, errtxt, 128);
fprintf(stderr,
I18n::instance()->
getMessage(
#ifdef NLS
BaseDisplaySet, BaseDisplayXError,
#else // !NLS
0, 0,
#endif // NLS
"%s: X error: %s(%d) opcodes %d/%d\n resource 0x%lx\n"),
base_display->getApplicationName(), errtxt, e->error_code,
e->request_code, e->minor_code, e->resourceid);
2001-12-11 20:47:02 +00:00
#else // !DEBUG
static int handleXErrors(Display *, XErrorEvent *e) {
#endif // DEBUG
2002-01-27 13:08:53 +00:00
if (e->error_code == BadWindow)
2001-12-11 20:47:02 +00:00
last_bad_window = e->resourceid;
if (internal_error)
abort();
2002-01-27 13:08:53 +00:00
return(False);
2001-12-11 20:47:02 +00:00
}
// signal handler to allow for proper and gentle shutdown
2002-01-27 13:08:53 +00:00
#ifndef HAVE_SIGACTION
2001-12-11 20:47:02 +00:00
static RETSIGTYPE signalhandler(int sig) {
2002-01-27 13:08:53 +00:00
#else // HAVE_SIGACTION
2001-12-11 20:47:02 +00:00
static void signalhandler(int sig) {
#endif // HAVE_SIGACTION
I18n *i18n = I18n::instance();
2002-01-27 13:08:53 +00:00
static int re_enter = 0;
2001-12-11 20:47:02 +00:00
2002-01-27 13:08:53 +00:00
switch (sig) {
case SIGCHLD:
int status;
waitpid(-1, &status, WNOHANG | WUNTRACED);
2001-12-11 20:47:02 +00:00
2002-01-27 13:08:53 +00:00
#ifndef HAVE_SIGACTION
// assume broken, braindead sysv signal semantics
signal(SIGCHLD, (RETSIGTYPE (*)(int)) signalhandler);
#endif // HAVE_SIGACTION
2001-12-11 20:47:02 +00:00
2002-01-27 13:08:53 +00:00
break;
2001-12-11 20:47:02 +00:00
2002-01-27 13:08:53 +00:00
default:
if (base_display->handleSignal(sig)) {
2001-12-11 20:47:02 +00:00
2002-01-27 13:08:53 +00:00
#ifndef HAVE_SIGACTION
// assume broken, braindead sysv signal semantics
signal(sig, (RETSIGTYPE (*)(int)) signalhandler);
#endif // HAVE_SIGACTION
return;
}
2001-12-11 20:47:02 +00:00
2002-01-27 13:08:53 +00:00
fprintf(stderr,
i18n->getMessage(
#ifdef NLS
BaseDisplaySet, BaseDisplaySignalCaught,
#else // !NLS
0, 0,
#endif // NLS
"%s: signal %d caught\n"),
base_display->getApplicationName(), sig);
if (! base_display->isStartup() && ! re_enter) {
internal_error = True;
re_enter = 1;
fprintf(stderr,
i18n->getMessage(
#ifdef NLS
BaseDisplaySet, BaseDisplayShuttingDown,
#else // !NLS
0, 0,
#endif // NLS
"shutting down\n"));
base_display->shutdown();
}
if (sig != SIGTERM && sig != SIGINT) {
fprintf(stderr,
i18n->getMessage(
#ifdef NLS
BaseDisplaySet, BaseDisplayAborting,
#else // !NLS
0, 0,
#endif // NLS
"aborting... dumping core\n"));
abort();
}
exit(0);
break;
}
2001-12-11 20:47:02 +00:00
}
// convenience functions
2002-01-27 13:08:53 +00:00
#ifndef __EMX__
2001-12-11 20:47:02 +00:00
void bexec(const char *command, char* displaystring) {
2002-01-27 13:08:53 +00:00
if (! fork()) {
setsid();
putenv(displaystring);
execl("/bin/sh", "/bin/sh", "-c", command, NULL);
exit(0);
}
2001-12-11 20:47:02 +00:00
}
#endif // !__EMX__
2002-03-18 15:28:25 +00:00
BaseDisplay::BaseDisplay(char *app_name, char *dpy_name):FbAtoms(0),
2002-01-27 13:08:53 +00:00
m_startup(true), m_shutdown(false),
m_display_name(XDisplayName(dpy_name)), m_app_name(app_name),
m_server_grabs(0)
{
2001-12-11 20:47:02 +00:00
last_bad_window = None;
I18n *i18n = I18n::instance();
::base_display = this;
2002-01-27 13:08:53 +00:00
#ifdef HAVE_SIGACTION
2001-12-11 20:47:02 +00:00
struct sigaction action;
action.sa_handler = signalhandler;
action.sa_mask = sigset_t();
action.sa_flags = SA_NOCLDSTOP | SA_NODEFER;
sigaction(SIGSEGV, &action, NULL);
sigaction(SIGFPE, &action, NULL);
sigaction(SIGTERM, &action, NULL);
sigaction(SIGINT, &action, NULL);
sigaction(SIGCHLD, &action, NULL);
sigaction(SIGHUP, &action, NULL);
sigaction(SIGUSR1, &action, NULL);
sigaction(SIGUSR2, &action, NULL);
#else // !HAVE_SIGACTION
signal(SIGSEGV, (RETSIGTYPE (*)(int)) signalhandler);
signal(SIGFPE, (RETSIGTYPE (*)(int)) signalhandler);
signal(SIGTERM, (RETSIGTYPE (*)(int)) signalhandler);
signal(SIGINT, (RETSIGTYPE (*)(int)) signalhandler);
signal(SIGUSR1, (RETSIGTYPE (*)(int)) signalhandler);
signal(SIGUSR2, (RETSIGTYPE (*)(int)) signalhandler);
signal(SIGHUP, (RETSIGTYPE (*)(int)) signalhandler);
signal(SIGCHLD, (RETSIGTYPE (*)(int)) signalhandler);
#endif // HAVE_SIGACTION
2002-01-27 13:08:53 +00:00
if (! (m_display = XOpenDisplay(dpy_name))) {
2001-12-11 20:47:02 +00:00
fprintf(stderr,
2002-01-27 13:08:53 +00:00
i18n->
2001-12-11 20:47:02 +00:00
getMessage(
2002-01-27 13:08:53 +00:00
#ifdef NLS
BaseDisplaySet, BaseDisplayXConnectFail,
#else // !NLS
0, 0,
#endif // NLS
"BaseDisplay::BaseDisplay: connection to X server failed.\n"));
2001-12-11 20:47:02 +00:00
throw static_cast<int>(2); //throw error 2
2002-01-27 13:08:53 +00:00
} else if (fcntl(ConnectionNumber(m_display), F_SETFD, 1) == -1) {
2001-12-11 20:47:02 +00:00
fprintf(stderr,
i18n->
getMessage(
2002-01-27 13:08:53 +00:00
#ifdef NLS
BaseDisplaySet, BaseDisplayCloseOnExecFail,
#else // !NLS
0, 0,
#endif // NLS
"BaseDisplay::BaseDisplay: couldn't mark display connection "
"as close-on-exec\n"));
throw static_cast<int>(2); //throw error 2
}
2002-03-18 15:28:25 +00:00
//initiate atoms
initAtoms(m_display);
2002-01-27 13:08:53 +00:00
number_of_screens = ScreenCount(m_display);
#ifdef SHAPE
shape.extensions = XShapeQueryExtension(m_display, &shape.event_basep,
&shape.error_basep);
2001-12-11 20:47:02 +00:00
#else // !SHAPE
shape.extensions = False;
#endif // SHAPE
2002-01-27 13:08:53 +00:00
cursor.session = XCreateFontCursor(m_display, XC_left_ptr);
cursor.move = XCreateFontCursor(m_display, XC_fleur);
cursor.ll_angle = XCreateFontCursor(m_display, XC_ll_angle);
cursor.lr_angle = XCreateFontCursor(m_display, XC_lr_angle);
2001-12-11 20:47:02 +00:00
XSetErrorHandler((XErrorHandler) handleXErrors);
int i;
for (i = 0; i < number_of_screens; i++) {
ScreenInfo *screeninfo = new ScreenInfo(this, i);
2002-02-11 10:57:23 +00:00
screenInfoList.push_back(screeninfo);
2001-12-11 20:47:02 +00:00
}
}
BaseDisplay::~BaseDisplay(void) {
2002-01-27 13:08:53 +00:00
2002-02-11 10:57:23 +00:00
ScreenInfoList::iterator it = screenInfoList.begin();
ScreenInfoList::iterator it_end = screenInfoList.end();
for (; it != it_end; ++it) {
delete (*it);
2002-01-27 13:08:53 +00:00
}
2001-12-11 20:47:02 +00:00
2002-01-27 13:08:53 +00:00
XCloseDisplay(m_display);
2001-12-11 20:47:02 +00:00
}
void BaseDisplay::eventLoop(void) {
run();
2002-01-27 13:08:53 +00:00
while ((! m_shutdown) && (! internal_error)) {
if (XPending(m_display)) {
2001-12-11 20:47:02 +00:00
XEvent e;
2002-01-27 13:08:53 +00:00
XNextEvent(m_display, &e);
2001-12-11 20:47:02 +00:00
if (last_bad_window != None && e.xany.window == last_bad_window) {
2002-01-27 13:08:53 +00:00
#ifdef DEBUG
2001-12-11 20:47:02 +00:00
fprintf(stderr,
I18n::instance()->
getMessage(
2002-01-27 13:08:53 +00:00
#ifdef NLS
BaseDisplaySet, BaseDisplayBadWindowRemove,
#else // !NLS
0, 0,
#endif // NLS
"BaseDisplay::eventLoop(): removing bad window "
"from event queue\n"));
#endif // DEBUG
2001-12-11 20:47:02 +00:00
} else {
last_bad_window = None;
process_event(&e);
}
} else {
BTimer::updateTimers(ConnectionNumber(m_display)); //handle all timers
2001-12-11 20:47:02 +00:00
}
}
}
2002-01-27 13:08:53 +00:00
const bool BaseDisplay::validateWindow(Window window) {
XEvent event;
if (XCheckTypedWindowEvent(m_display, window, DestroyNotify, &event)) {
XPutBackEvent(m_display, &event);
return false;
}
2001-12-11 20:47:02 +00:00
2002-01-27 13:08:53 +00:00
return true;
2001-12-11 20:47:02 +00:00
}
void BaseDisplay::grab(void) {
2002-01-27 13:08:53 +00:00
if (! m_server_grabs++)
XGrabServer(m_display);
2001-12-11 20:47:02 +00:00
}
void BaseDisplay::ungrab(void) {
2002-01-27 13:08:53 +00:00
if (! --m_server_grabs)
XUngrabServer(m_display);
if (m_server_grabs < 0)
m_server_grabs = 0;
2001-12-11 20:47:02 +00:00
}
ScreenInfo::ScreenInfo(BaseDisplay *d, int num) {
2002-01-27 13:08:53 +00:00
basedisplay = d;
screen_number = num;
root_window = RootWindow(basedisplay->getXDisplay(), screen_number);
depth = DefaultDepth(basedisplay->getXDisplay(), screen_number);
width =
WidthOfScreen(ScreenOfDisplay(basedisplay->getXDisplay(), screen_number));
height =
HeightOfScreen(ScreenOfDisplay(basedisplay->getXDisplay(), screen_number));
// search for a TrueColor Visual... if we can't find one... we will use the
// default visual for the screen
XVisualInfo vinfo_template, *vinfo_return;
int vinfo_nitems;
vinfo_template.screen = screen_number;
vinfo_template.c_class = TrueColor;
visual = (Visual *) 0;
if ((vinfo_return = XGetVisualInfo(basedisplay->getXDisplay(),
VisualScreenMask | VisualClassMask,
&vinfo_template, &vinfo_nitems)) &&
vinfo_nitems > 0) {
for (int i = 0; i < vinfo_nitems; i++) {
if (depth < (vinfo_return + i)->depth) {
depth = (vinfo_return + i)->depth;
visual = (vinfo_return + i)->visual;
}
}
XFree(vinfo_return);
}
2002-02-11 10:57:23 +00:00
if (visual) {
2002-01-27 13:08:53 +00:00
colormap = XCreateColormap(basedisplay->getXDisplay(), root_window,
visual, AllocNone);
2002-02-11 10:57:23 +00:00
} else {
2002-01-27 13:08:53 +00:00
visual = DefaultVisual(basedisplay->getXDisplay(), screen_number);
colormap = DefaultColormap(basedisplay->getXDisplay(), screen_number);
}
2002-03-19 14:30:43 +00:00
#ifdef XINERAMA
// check if we have Xinerama extension enabled
if (XineramaIsActive(basedisplay->getXDisplay())) {
m_hasXinerama = true;
xineramaLastHead = 0;
2002-03-19 14:30:43 +00:00
xineramaInfos =
XineramaQueryScreens(basedisplay->getXDisplay(), &xineramaNumHeads);
} else {
m_hasXinerama = false;
xineramaInfos = 0; // make sure we don't point anywhere we shouldn't
}
#endif // XINERAMA
2001-12-11 20:47:02 +00:00
}
2002-03-19 14:30:43 +00:00
ScreenInfo::~ScreenInfo(void) {
#ifdef XINERAMA
if (m_hasXinerama) { // only free if we first had it
XFree(xineramaInfos);
xineramaInfos = 0;
}
#endif // XINERAMA
}
#ifdef XINERAMA
//---------------- getHead ---------------
// Searches for the head at the coordinates
// x,y. If it fails or Xinerama isn't
// activated it'll return head nr 0
//-----------------------------------------
2002-03-23 02:02:01 +00:00
unsigned int ScreenInfo::getHead(int x, int y) const {
2002-03-19 14:30:43 +00:00
// is Xinerama extensions enabled?
if (hasXinerama()) {
// check if last head is still active
2002-03-23 02:02:01 +00:00
/* if ((xineramaInfos[xineramaLastHead].x_org <= x) &&
2002-03-19 14:30:43 +00:00
((xineramaInfos[xineramaLastHead].x_org +
xineramaInfos[xineramaLastHead].width) > x) &&
(xineramaInfos[xineramaLastHead].y_org <= y) &&
((xineramaInfos[xineramaLastHead].y_org +
xineramaInfos[xineramaLastHead].height) > y)) {
return xineramaLastHead;
2002-03-23 02:02:01 +00:00
} else { */
2002-03-19 14:30:43 +00:00
// go trough all the heads, and search
for (int i = 0; (signed) i < xineramaNumHeads; i++) {
if (xineramaInfos[i].x_org <= x &&
2002-03-23 02:02:01 +00:00
(xineramaInfos[i].x_org + xineramaInfos[i].width) > x &&
xineramaInfos[i].y_org <= y &&
2002-03-23 02:02:01 +00:00
(xineramaInfos[i].y_org + xineramaInfos[i].height) > y)
// return (xineramaLastHead = i);
return i;
2002-03-19 14:30:43 +00:00
}
2002-03-23 02:02:01 +00:00
// }
2002-03-19 14:30:43 +00:00
}
return 0;
2002-03-19 14:30:43 +00:00
}
//------------- getCurrHead --------------
// Searches for the head that the pointer
// currently is on, if it isn't found
// the first one is returned
//----------------------------------------
unsigned int ScreenInfo::getCurrHead(void) const {
2002-03-19 14:30:43 +00:00
// is Xinerama extensions enabled?
if (hasXinerama()) {
int x, y, wX, wY;
2002-03-19 14:30:43 +00:00
unsigned int mask;
Window rRoot, rChild;
// get pointer cordinates
if ( (XQueryPointer(basedisplay->getXDisplay(), root_window,
&rRoot, &rChild, &x, &y, &wX, &wY, &mask)) != 0 ) {
return getHead(x, y);
2002-03-19 14:30:43 +00:00
}
}
return 0;
2002-03-19 14:30:43 +00:00
}
//----------- getHeadWidth ------------
// Returns the width of head
2002-03-19 14:30:43 +00:00
//-------------------------------------
2002-03-23 02:02:01 +00:00
unsigned int ScreenInfo::getHeadWidth(unsigned int head) const {
2002-03-19 14:30:43 +00:00
if (hasXinerama()) {
if ((signed) head >= xineramaNumHeads) {
#ifdef DEBUG
cerr << __FILE__ << ":" <<__LINE__ << ": " <<
"Head: " << head << " doesn't exist!" << endl;
#endif // DEBUG
return xineramaInfos[xineramaNumHeads - 1].width;
} else
return xineramaInfos[head].width;
2002-03-19 14:30:43 +00:00
}
return getWidth();
2002-03-19 14:30:43 +00:00
}
//----------- getHeadHeight ------------
// Returns the heigt of head
2002-03-19 14:30:43 +00:00
//--------------------------------------
unsigned int ScreenInfo::getHeadHeight(unsigned int head) const {
2002-03-19 14:30:43 +00:00
if (hasXinerama()) {
if ((signed) head >= xineramaNumHeads) {
#ifdef DEBUG
cerr << __FILE__ << ":" <<__LINE__ << ": " <<
"Head: " << head << " doesn't exist!" << endl;
#endif // DEBUG
return xineramaInfos[xineramaNumHeads - 1].height;
} else
return xineramaInfos[head].height;
2002-03-19 14:30:43 +00:00
}
return getHeight();
2002-03-19 14:30:43 +00:00
}
//----------- getHeadX -----------------
// Returns the X start of head nr head
//--------------------------------------
int ScreenInfo::getHeadX(unsigned int head) const {
2002-03-19 14:30:43 +00:00
if (hasXinerama()) {
if ((signed) head >= xineramaNumHeads) {
#ifdef DEBUG
cerr << __FILE__ << ":" <<__LINE__ << ": " <<
"Head: " << head << " doesn't exist!" << endl;
#endif // DEBUG
return xineramaInfos[head = xineramaNumHeads - 1].x_org;
} else
return xineramaInfos[head].x_org;
2002-03-19 14:30:43 +00:00
}
return 0;
2002-03-19 14:30:43 +00:00
}
//----------- getHeadY -----------------
// Returns the Y start of head
2002-03-19 14:30:43 +00:00
//--------------------------------------
int ScreenInfo::getHeadY(unsigned int head) const {
2002-03-19 14:30:43 +00:00
if (hasXinerama()) {
if ((signed) head >= xineramaNumHeads) {
#ifdef DEBUG
cerr << __FILE__ << ":" <<__LINE__ << ": " <<
"Head: " << head << " doesn't exist!" << endl;
#endif // DEBUG
return xineramaInfos[xineramaNumHeads - 1].y_org;
} else
return xineramaInfos[head].y_org;
2002-03-19 14:30:43 +00:00
}
return 0;
2002-03-19 14:30:43 +00:00
}
#endif // XINERAMA