fluxbox/src/BaseDisplay.cc

462 lines
12 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-11-12 14:40:26 +00:00
// $Id: BaseDisplay.cc,v 1.22 2002/11/12 14:40:26 fluxgen Exp $
2001-12-11 20:47:02 +00:00
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
#ifdef HAVE_CONFIG_H
2002-11-12 14:40:26 +00:00
#include "config.h"
#endif // HAVE_CONFIG_H
// use GNU extensions
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif // _GNU_SOURCE
2001-12-11 20:47:02 +00:00
#include <X11/Xutil.h>
#ifdef SHAPE
#include <X11/extensions/shape.h>
2001-12-11 20:47:02 +00:00
#endif // SHAPE
2002-04-08 22:23:41 +00:00
#ifdef HAVE_FCNTL_H
#include <fcntl.h>
2001-12-11 20:47:02 +00:00
#endif // HAVE_FCNTL_H
2002-11-12 14:40:26 +00:00
#include <cstdio>
#include <cstdlib>
#include <cstring>
2001-12-11 20:47:02 +00:00
2002-04-08 22:23:41 +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-04-08 22:23:41 +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-04-08 22:23:41 +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
2002-11-12 14:40:26 +00:00
#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
2002-11-12 14:40:26 +00:00
#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-11-12 14:40:26 +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
#include <iostream>
using namespace std;
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;
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(
FBNLS::BaseDisplaySet, FBNLS::BaseDisplayXError,
2002-01-27 13:08:53 +00:00
"%s: X error: %s(%d) opcodes %d/%d\n resource 0x%lx\n"),
BaseDisplay::instance()->getApplicationName(), errtxt, e->error_code,
2002-01-27 13:08:53 +00:00
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
}
// convenience functions
2002-01-27 13:08:53 +00:00
#ifndef __EMX__
2002-08-04 15:09:30 +00:00
void bexec(const char *command, char *displaystring) {
2002-01-27 13:08:53 +00:00
if (! fork()) {
setsid();
putenv(displaystring);
2002-08-04 15:09:30 +00:00
execl("/bin/sh", "/bin/sh", "-c", command, 0);
2002-01-27 13:08:53 +00:00
exit(0);
}
2001-12-11 20:47:02 +00:00
}
#endif // !__EMX__
BaseDisplay *BaseDisplay::s_singleton = 0;
2002-08-30 12:58:08 +00:00
Display *BaseDisplay::s_display = 0;
2002-08-14 21:41:21 +00:00
BaseDisplay::BaseDisplay(const char *app_name, const char *dpy_name):
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)
{
if (s_singleton != 0)
throw string("Can't create more than one instance of BaseDisplay!");
s_singleton = this;
2001-12-11 20:47:02 +00:00
last_bad_window = None;
I18n *i18n = I18n::instance();
2002-08-30 12:58:08 +00:00
if (! (s_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(
FBNLS::BaseDisplaySet, FBNLS::BaseDisplayXConnectFail,
"BaseDisplay::BaseDisplay: connection to X server failed.\n"));
2002-01-27 13:08:53 +00:00
2001-12-11 20:47:02 +00:00
throw static_cast<int>(2); //throw error 2
2002-08-30 12:58:08 +00:00
} else if (fcntl(ConnectionNumber(s_display), F_SETFD, 1) == -1) {
2001-12-11 20:47:02 +00:00
fprintf(stderr,
i18n->
getMessage(
FBNLS::BaseDisplaySet, FBNLS::BaseDisplayCloseOnExecFail,
"BaseDisplay::BaseDisplay: couldn't mark display connection "
"as close-on-exec\n"));
2002-01-27 13:08:53 +00:00
throw static_cast<int>(2); //throw error 2
}
2002-08-14 21:41:21 +00:00
2002-01-27 13:08:53 +00:00
2002-08-30 12:58:08 +00:00
number_of_screens = ScreenCount(s_display);
2002-01-27 13:08:53 +00:00
#ifdef SHAPE
2002-08-30 12:58:08 +00:00
shape.extensions = XShapeQueryExtension(s_display, &shape.event_basep,
2002-01-27 13:08:53 +00:00
&shape.error_basep);
2001-12-11 20:47:02 +00:00
#else // !SHAPE
shape.extensions = False;
#endif // SHAPE
2001-12-11 20:47:02 +00:00
XSetErrorHandler((XErrorHandler) handleXErrors);
for (int i = 0; i < number_of_screens; i++) {
ScreenInfo *screeninfo = new ScreenInfo(i);
2002-02-11 10:57:23 +00:00
screenInfoList.push_back(screeninfo);
2001-12-11 20:47:02 +00:00
}
}
BaseDisplay::~BaseDisplay() {
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
}
2002-08-30 12:58:08 +00:00
XCloseDisplay(s_display);
s_display = 0;
s_singleton = 0;
2001-12-11 20:47:02 +00:00
}
BaseDisplay *BaseDisplay::instance() {
if (s_singleton == 0)
throw string("BaseDisplay not created!");
return s_singleton;
}
void BaseDisplay::eventLoop() {
2001-12-11 20:47:02 +00:00
run();
2002-01-27 13:08:53 +00:00
while ((! m_shutdown) && (! internal_error)) {
2002-08-30 12:58:08 +00:00
if (XPending(s_display)) {
2001-12-11 20:47:02 +00:00
XEvent e;
2002-08-30 12:58:08 +00:00
XNextEvent(s_display, &e);
2001-12-11 20:47:02 +00:00
if (last_bad_window != None && e.xany.window == last_bad_window) {
#ifdef DEBUG
2001-12-11 20:47:02 +00:00
fprintf(stderr,
I18n::instance()->
getMessage(
FBNLS::BaseDisplaySet, FBNLS::BaseDisplayBadWindowRemove,
2002-01-27 13:08:53 +00:00
"BaseDisplay::eventLoop(): removing bad window "
"from event queue\n"));
#endif // DEBUG
2001-12-11 20:47:02 +00:00
} else {
last_bad_window = None;
handleEvent(&e);
2001-12-11 20:47:02 +00:00
}
} else {
2002-08-30 12:58:08 +00:00
BTimer::updateTimers(ConnectionNumber(s_display)); //handle all timers
2001-12-11 20:47:02 +00:00
}
}
}
2002-05-17 11:55:41 +00:00
bool BaseDisplay::validateWindow(Window window) {
2002-01-27 13:08:53 +00:00
XEvent event;
2002-08-30 12:58:08 +00:00
if (XCheckTypedWindowEvent(s_display, window, DestroyNotify, &event)) {
XPutBackEvent(s_display, &event);
2002-01-27 13:08:53 +00:00
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() {
2002-01-27 13:08:53 +00:00
if (! m_server_grabs++)
2002-08-30 12:58:08 +00:00
XGrabServer(s_display);
2001-12-11 20:47:02 +00:00
}
void BaseDisplay::ungrab() {
2002-01-27 13:08:53 +00:00
if (! --m_server_grabs)
2002-08-30 12:58:08 +00:00
XUngrabServer(s_display);
2002-01-27 13:08:53 +00:00
if (m_server_grabs < 0)
m_server_grabs = 0;
2001-12-11 20:47:02 +00:00
}
ScreenInfo::ScreenInfo(int num) {
basedisplay = BaseDisplay::instance();
Display * const disp = basedisplay->getXDisplay();
2002-01-27 13:08:53 +00:00
screen_number = num;
root_window = RootWindow(disp, screen_number);
depth = DefaultDepth(disp, screen_number);
2002-01-27 13:08:53 +00:00
width =
WidthOfScreen(ScreenOfDisplay(disp, screen_number));
2002-01-27 13:08:53 +00:00
height =
HeightOfScreen(ScreenOfDisplay(disp, screen_number));
2002-01-27 13:08:53 +00:00
// 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(disp,
2002-01-27 13:08:53 +00:00
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) {
m_colormap = XCreateColormap(disp, root_window,
2002-01-27 13:08:53 +00:00
visual, AllocNone);
2002-02-11 10:57:23 +00:00
} else {
visual = DefaultVisual(disp, screen_number);
m_colormap = DefaultColormap(disp, screen_number);
2002-01-27 13:08:53 +00:00
}
2002-03-19 14:30:43 +00:00
#ifdef XINERAMA
// check if we have Xinerama extension enabled
if (XineramaIsActive(disp)) {
2002-03-19 14:30:43 +00:00
m_hasXinerama = true;
xineramaLastHead = 0;
2002-03-19 14:30:43 +00:00
xineramaInfos =
XineramaQueryScreens(disp, &xineramaNumHeads);
2002-03-19 14:30:43 +00:00
} 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() {
2002-03-19 14:30:43 +00:00
#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