2003-09-16 19:28:03 +00:00
|
|
|
/*****************************************************************
|
|
|
|
KWin - the KDE window manager
|
|
|
|
This file is part of the KDE project.
|
|
|
|
|
|
|
|
Copyright (C) 1999, 2000 Matthias Ettrich <ettrich@kde.org>
|
|
|
|
Copyright (C) 2003 Lubos Lunak <l.lunak@kde.org>
|
|
|
|
|
|
|
|
You can Freely distribute this program under the GNU General Public
|
|
|
|
License. See the file "COPYING" for the exact licensing terms.
|
|
|
|
******************************************************************/
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
|
|
This file is for (very) small utility functions/classes.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "utils.h"
|
2004-07-29 17:08:25 +00:00
|
|
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#ifndef KCMRULES
|
2003-09-16 19:28:03 +00:00
|
|
|
|
|
|
|
#include <kxerrorhandler.h>
|
2003-12-04 13:54:10 +00:00
|
|
|
#include <assert.h>
|
2005-01-17 16:19:33 +00:00
|
|
|
#include <kdebug.h>
|
2006-07-03 10:03:52 +00:00
|
|
|
#include <kshortcut.h>
|
2003-09-16 19:28:03 +00:00
|
|
|
|
|
|
|
#include <X11/Xlib.h>
|
|
|
|
#include <X11/extensions/shape.h>
|
|
|
|
#include <X11/Xatom.h>
|
2005-07-28 14:59:42 +00:00
|
|
|
#include <QX11Info>
|
2006-12-22 15:09:35 +00:00
|
|
|
|
2005-07-28 14:59:42 +00:00
|
|
|
#include <stdio.h>
|
2003-09-16 19:28:03 +00:00
|
|
|
|
2004-07-29 17:08:25 +00:00
|
|
|
#include "atoms.h"
|
2005-10-07 12:00:29 +00:00
|
|
|
#include "notifications.h"
|
2004-07-29 17:08:25 +00:00
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2003-09-16 19:28:03 +00:00
|
|
|
namespace KWinInternal
|
|
|
|
{
|
|
|
|
|
2004-07-29 17:08:25 +00:00
|
|
|
#ifndef KCMRULES
|
|
|
|
|
2006-12-22 15:09:35 +00:00
|
|
|
bool Extensions::has_shape = false;
|
|
|
|
int Extensions::shape_event_base = 0;
|
|
|
|
bool Extensions::has_randr = false;
|
|
|
|
int Extensions::randr_event_base = 0;
|
|
|
|
bool Extensions::has_damage = false;
|
|
|
|
int Extensions::damage_event_base = 0;
|
|
|
|
bool Extensions::has_composite = false;
|
|
|
|
bool Extensions::has_composite_overlay = false;
|
|
|
|
bool Extensions::has_fixes = false;
|
|
|
|
|
|
|
|
void Extensions::init()
|
|
|
|
{
|
|
|
|
int dummy;
|
|
|
|
has_shape = XShapeQueryExtension( display(), &shape_event_base, &dummy);
|
|
|
|
#ifdef HAVE_XRANDR
|
|
|
|
has_randr = XRRQueryExtension( display(), &randr_event_base, &dummy );
|
|
|
|
if( has_randr )
|
|
|
|
{
|
|
|
|
int major, minor;
|
|
|
|
XRRQueryVersion( display(), &major, &minor );
|
|
|
|
has_randr = ( major > 1 || ( major == 1 && minor >= 1 ) );
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
has_randr = false;
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_XDAMAGE
|
|
|
|
has_damage = XDamageQueryExtension( display(), &damage_event_base, &dummy );
|
|
|
|
#else
|
|
|
|
has_damage = false;
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_XCOMPOSITE
|
|
|
|
has_composite = XCompositeQueryExtension( display(), &dummy, &dummy );
|
|
|
|
if( has_composite )
|
|
|
|
{
|
|
|
|
int major, minor;
|
|
|
|
XCompositeQueryVersion( display(), &major, &minor );
|
|
|
|
has_composite = ( major > 0 || minor >= 2 );
|
|
|
|
has_composite_overlay = ( major > 0 || minor >= 3 );
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
has_composite = false;
|
|
|
|
has_composite_overlay = false;
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_XFIXES
|
|
|
|
has_fixes = XFixesQueryExtension( display(), &dummy, &dummy );
|
|
|
|
#else
|
|
|
|
has_fixes = false;
|
|
|
|
#endif
|
|
|
|
}
|
2003-09-16 19:28:03 +00:00
|
|
|
|
2006-12-22 15:09:35 +00:00
|
|
|
int Extensions::shapeNotifyEvent()
|
|
|
|
{
|
|
|
|
return shape_event_base + ShapeNotify;
|
|
|
|
}
|
|
|
|
|
|
|
|
// does the window w need a shape combine mask around it?
|
|
|
|
bool Extensions::hasShape( Window w )
|
2006-09-30 17:28:27 +00:00
|
|
|
{
|
|
|
|
int xws, yws, xbs, ybs;
|
|
|
|
unsigned int wws, hws, wbs, hbs;
|
|
|
|
int boundingShaped = 0, clipShaped = 0;
|
2006-12-22 15:09:35 +00:00
|
|
|
if( !Extensions::shapeAvailable())
|
2006-09-30 17:28:27 +00:00
|
|
|
return false;
|
|
|
|
XShapeQueryExtents(display(), w,
|
|
|
|
&boundingShaped, &xws, &yws, &wws, &hws,
|
|
|
|
&clipShaped, &xbs, &ybs, &wbs, &hbs);
|
|
|
|
return boundingShaped != 0;
|
|
|
|
}
|
|
|
|
|
2006-12-22 15:09:35 +00:00
|
|
|
int Extensions::randrNotifyEvent()
|
2006-11-23 21:16:49 +00:00
|
|
|
{
|
2006-12-22 15:09:35 +00:00
|
|
|
#ifdef HAVE_XRANDR
|
|
|
|
return randr_event_base + RRScreenChangeNotify;
|
|
|
|
#else
|
|
|
|
return 0;
|
|
|
|
#endif
|
2006-11-23 21:16:49 +00:00
|
|
|
}
|
|
|
|
|
2006-12-22 15:09:35 +00:00
|
|
|
int Extensions::damageNotifyEvent()
|
2003-09-16 19:28:03 +00:00
|
|
|
{
|
2006-12-22 15:09:35 +00:00
|
|
|
#ifdef HAVE_XDAMAGE
|
|
|
|
return damage_event_base + XDamageNotify;
|
|
|
|
#else
|
|
|
|
return 0;
|
|
|
|
#endif
|
2003-09-16 19:28:03 +00:00
|
|
|
}
|
|
|
|
|
2004-07-27 08:35:51 +00:00
|
|
|
void Motif::readFlags( WId w, bool& noborder, bool& resize, bool& move,
|
|
|
|
bool& minimize, bool& maximize, bool& close )
|
2003-09-16 19:28:03 +00:00
|
|
|
{
|
|
|
|
Atom type;
|
|
|
|
int format;
|
|
|
|
unsigned long length, after;
|
|
|
|
unsigned char* data;
|
|
|
|
MwmHints* hints = 0;
|
2006-08-10 17:00:14 +00:00
|
|
|
if ( XGetWindowProperty( display(), w, atoms->motif_wm_hints, 0, 5,
|
2006-02-19 01:33:48 +00:00
|
|
|
false, atoms->motif_wm_hints, &type, &format,
|
2003-09-16 19:28:03 +00:00
|
|
|
&length, &after, &data ) == Success )
|
|
|
|
{
|
|
|
|
if ( data )
|
|
|
|
hints = (MwmHints*) data;
|
|
|
|
}
|
2004-07-27 08:35:51 +00:00
|
|
|
noborder = false;
|
|
|
|
resize = true;
|
|
|
|
move = true;
|
|
|
|
minimize = true;
|
|
|
|
maximize = true;
|
|
|
|
close = true;
|
2003-09-16 19:28:03 +00:00
|
|
|
if ( hints )
|
|
|
|
{
|
|
|
|
// To quote from Metacity 'We support those MWM hints deemed non-stupid'
|
|
|
|
if ( hints->flags & MWM_HINTS_FUNCTIONS )
|
|
|
|
{
|
|
|
|
// if MWM_FUNC_ALL is set, other flags say what to turn _off_
|
|
|
|
bool set_value = (( hints->functions & MWM_FUNC_ALL ) == 0 );
|
|
|
|
resize = move = minimize = maximize = close = !set_value;
|
|
|
|
if( hints->functions & MWM_FUNC_RESIZE )
|
|
|
|
resize = set_value;
|
|
|
|
if( hints->functions & MWM_FUNC_MOVE )
|
|
|
|
move = set_value;
|
|
|
|
if( hints->functions & MWM_FUNC_MINIMIZE )
|
|
|
|
minimize = set_value;
|
|
|
|
if( hints->functions & MWM_FUNC_MAXIMIZE )
|
|
|
|
maximize = set_value;
|
|
|
|
if( hints->functions & MWM_FUNC_CLOSE )
|
|
|
|
close = set_value;
|
2004-07-27 08:35:51 +00:00
|
|
|
}
|
|
|
|
if ( hints->flags & MWM_HINTS_DECORATIONS )
|
|
|
|
{
|
|
|
|
if ( hints->decorations == 0 )
|
|
|
|
noborder = true;
|
2003-09-16 19:28:03 +00:00
|
|
|
}
|
|
|
|
XFree( data );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//************************************
|
|
|
|
// KWinSelectionOwner
|
|
|
|
//************************************
|
|
|
|
|
|
|
|
KWinSelectionOwner::KWinSelectionOwner( int screen_P )
|
|
|
|
: KSelectionOwner( make_selection_atom( screen_P ), screen_P )
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
Atom KWinSelectionOwner::make_selection_atom( int screen_P )
|
|
|
|
{
|
|
|
|
if( screen_P < 0 )
|
2006-08-10 17:00:14 +00:00
|
|
|
screen_P = DefaultScreen( display());
|
2003-09-16 19:28:03 +00:00
|
|
|
char tmp[ 30 ];
|
|
|
|
sprintf( tmp, "WM_S%d", screen_P );
|
2006-08-10 17:00:14 +00:00
|
|
|
return XInternAtom( display(), tmp, False );
|
2003-09-16 19:28:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void KWinSelectionOwner::getAtoms()
|
|
|
|
{
|
|
|
|
KSelectionOwner::getAtoms();
|
|
|
|
if( xa_version == None )
|
|
|
|
{
|
|
|
|
Atom atoms[ 1 ];
|
|
|
|
const char* const names[] =
|
|
|
|
{ "VERSION" };
|
2006-08-10 17:00:14 +00:00
|
|
|
XInternAtoms( display(), const_cast< char** >( names ), 1, False, atoms );
|
2003-09-16 19:28:03 +00:00
|
|
|
xa_version = atoms[ 0 ];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void KWinSelectionOwner::replyTargets( Atom property_P, Window requestor_P )
|
|
|
|
{
|
|
|
|
KSelectionOwner::replyTargets( property_P, requestor_P );
|
|
|
|
Atom atoms[ 1 ] = { xa_version };
|
|
|
|
// PropModeAppend !
|
2006-08-10 17:00:14 +00:00
|
|
|
XChangeProperty( display(), requestor_P, property_P, XA_ATOM, 32, PropModeAppend,
|
2003-09-16 19:28:03 +00:00
|
|
|
reinterpret_cast< unsigned char* >( atoms ), 1 );
|
|
|
|
}
|
|
|
|
|
|
|
|
bool KWinSelectionOwner::genericReply( Atom target_P, Atom property_P, Window requestor_P )
|
|
|
|
{
|
|
|
|
if( target_P == xa_version )
|
|
|
|
{
|
2005-01-27 09:58:34 +00:00
|
|
|
long version[] = { 2, 0 };
|
2006-08-10 17:00:14 +00:00
|
|
|
XChangeProperty( display(), requestor_P, property_P, XA_INTEGER, 32,
|
2003-09-16 19:28:03 +00:00
|
|
|
PropModeReplace, reinterpret_cast< unsigned char* >( &version ), 2 );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return KSelectionOwner::genericReply( target_P, property_P, requestor_P );
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
Atom KWinSelectionOwner::xa_version = None;
|
|
|
|
|
|
|
|
|
2005-07-28 14:59:42 +00:00
|
|
|
QByteArray getStringProperty(WId w, Atom prop, char separator)
|
2003-09-16 19:28:03 +00:00
|
|
|
{
|
|
|
|
Atom type;
|
|
|
|
int format, status;
|
|
|
|
unsigned long nitems = 0;
|
|
|
|
unsigned long extra = 0;
|
|
|
|
unsigned char *data = 0;
|
2005-07-28 14:59:42 +00:00
|
|
|
QByteArray result = "";
|
2003-09-16 19:28:03 +00:00
|
|
|
KXErrorHandler handler; // ignore errors
|
2006-08-10 17:00:14 +00:00
|
|
|
status = XGetWindowProperty( display(), w, prop, 0, 10000,
|
2006-02-19 01:33:48 +00:00
|
|
|
false, XA_STRING, &type, &format,
|
2003-09-16 19:28:03 +00:00
|
|
|
&nitems, &extra, &data );
|
|
|
|
if ( status == Success)
|
|
|
|
{
|
|
|
|
if (data && separator)
|
|
|
|
{
|
|
|
|
for (int i=0; i<(int)nitems; i++)
|
|
|
|
if (!data[i] && i+1<(int)nitems)
|
|
|
|
data[i] = separator;
|
|
|
|
}
|
|
|
|
if (data)
|
|
|
|
result = (const char*) data;
|
|
|
|
XFree(data);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2004-05-24 08:54:04 +00:00
|
|
|
static Time next_x_time;
|
|
|
|
static Bool update_x_time_predicate( Display*, XEvent* event, XPointer )
|
|
|
|
{
|
|
|
|
if( next_x_time != CurrentTime )
|
|
|
|
return False;
|
|
|
|
// from qapplication_x11.cpp
|
|
|
|
switch ( event->type ) {
|
|
|
|
case ButtonPress:
|
|
|
|
// fallthrough intended
|
|
|
|
case ButtonRelease:
|
|
|
|
next_x_time = event->xbutton.time;
|
|
|
|
break;
|
|
|
|
case MotionNotify:
|
|
|
|
next_x_time = event->xmotion.time;
|
|
|
|
break;
|
|
|
|
case KeyPress:
|
|
|
|
// fallthrough intended
|
|
|
|
case KeyRelease:
|
|
|
|
next_x_time = event->xkey.time;
|
|
|
|
break;
|
|
|
|
case PropertyNotify:
|
|
|
|
next_x_time = event->xproperty.time;
|
|
|
|
break;
|
|
|
|
case EnterNotify:
|
|
|
|
case LeaveNotify:
|
|
|
|
next_x_time = event->xcrossing.time;
|
|
|
|
break;
|
|
|
|
case SelectionClear:
|
|
|
|
next_x_time = event->xselectionclear.time;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return False;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2006-08-10 17:00:14 +00:00
|
|
|
Updates xTime(). This used to simply fetch current timestamp from the server,
|
|
|
|
but that can cause xTime() to be newer than timestamp of events that are
|
2004-05-24 08:54:04 +00:00
|
|
|
still in our events queue, thus e.g. making XSetInputFocus() caused by such
|
2004-05-24 13:15:30 +00:00
|
|
|
event to be ignored. Therefore events queue is searched for first
|
|
|
|
event with timestamp, and extra PropertyNotify is generated in order to make
|
|
|
|
sure such event is found.
|
2004-05-24 08:54:04 +00:00
|
|
|
*/
|
2003-10-10 12:48:09 +00:00
|
|
|
void updateXTime()
|
2003-09-16 19:28:03 +00:00
|
|
|
{
|
2004-05-24 13:15:30 +00:00
|
|
|
static QWidget* w = 0;
|
|
|
|
if ( !w )
|
|
|
|
w = new QWidget;
|
|
|
|
long data = 1;
|
2006-08-10 17:00:14 +00:00
|
|
|
XChangeProperty(display(), w->winId(), atoms->kwin_running, atoms->kwin_running, 32,
|
2004-05-24 13:15:30 +00:00
|
|
|
PropModeAppend, (unsigned char*) &data, 1);
|
|
|
|
next_x_time = CurrentTime;
|
|
|
|
XEvent dummy;
|
2006-08-10 17:00:14 +00:00
|
|
|
XCheckIfEvent( display(), &dummy, update_x_time_predicate, NULL );
|
2004-05-24 13:15:30 +00:00
|
|
|
if( next_x_time == CurrentTime )
|
2004-05-24 08:54:04 +00:00
|
|
|
{
|
2006-08-10 17:00:14 +00:00
|
|
|
XSync( display(), False );
|
|
|
|
XCheckIfEvent( display(), &dummy, update_x_time_predicate, NULL );
|
2004-05-24 08:54:04 +00:00
|
|
|
}
|
2004-05-24 13:15:30 +00:00
|
|
|
assert( next_x_time != CurrentTime );
|
2005-07-28 14:59:42 +00:00
|
|
|
QX11Info::setAppTime( next_x_time );
|
2004-05-24 13:15:30 +00:00
|
|
|
XEvent ev; // remove the PropertyNotify event from the events queue
|
2006-08-10 17:00:14 +00:00
|
|
|
XWindowEvent( display(), w->winId(), PropertyChangeMask, &ev );
|
2003-09-16 19:28:03 +00:00
|
|
|
}
|
|
|
|
|
2003-12-04 13:54:10 +00:00
|
|
|
static int server_grab_count = 0;
|
2003-09-16 19:28:03 +00:00
|
|
|
|
2003-12-04 13:54:10 +00:00
|
|
|
void grabXServer()
|
|
|
|
{
|
|
|
|
if( ++server_grab_count == 1 )
|
2006-08-10 17:00:14 +00:00
|
|
|
XGrabServer( display());
|
2003-12-04 13:54:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ungrabXServer()
|
|
|
|
{
|
|
|
|
assert( server_grab_count > 0 );
|
|
|
|
if( --server_grab_count == 0 )
|
2005-10-07 12:00:29 +00:00
|
|
|
{
|
2006-08-10 17:00:14 +00:00
|
|
|
XUngrabServer( display());
|
|
|
|
XFlush( display());
|
2005-10-07 12:00:29 +00:00
|
|
|
Notify::sendPendingEvents();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool grabbedXServer()
|
|
|
|
{
|
|
|
|
return server_grab_count > 0;
|
2003-12-04 13:54:10 +00:00
|
|
|
}
|
2004-07-29 17:08:25 +00:00
|
|
|
#endif
|
2003-09-16 19:28:03 +00:00
|
|
|
|
2005-07-28 14:59:42 +00:00
|
|
|
bool isLocalMachine( const QByteArray& host )
|
2004-05-28 13:54:20 +00:00
|
|
|
{
|
|
|
|
#ifdef HOST_NAME_MAX
|
|
|
|
char hostnamebuf[HOST_NAME_MAX];
|
|
|
|
#else
|
|
|
|
char hostnamebuf[256];
|
|
|
|
#endif
|
|
|
|
if (gethostname (hostnamebuf, sizeof hostnamebuf) >= 0)
|
|
|
|
{
|
|
|
|
hostnamebuf[sizeof(hostnamebuf)-1] = 0;
|
|
|
|
if (host == hostnamebuf)
|
|
|
|
return true;
|
2006-04-09 09:31:02 +00:00
|
|
|
if( char *dot = strchr(hostnamebuf, '.'))
|
|
|
|
{
|
|
|
|
*dot = '\0';
|
|
|
|
if( host == hostnamebuf )
|
|
|
|
return true;
|
|
|
|
}
|
2004-05-28 13:54:20 +00:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2005-01-10 09:56:21 +00:00
|
|
|
#ifndef KCMRULES
|
|
|
|
ShortcutDialog::ShortcutDialog( const KShortcut& cut )
|
|
|
|
: KShortcutDialog( cut, false /*TODO???*/ )
|
|
|
|
{
|
|
|
|
// make it a popup, so that it has the grab
|
|
|
|
XSetWindowAttributes attrs;
|
|
|
|
attrs.override_redirect = True;
|
2006-08-10 17:00:14 +00:00
|
|
|
XChangeWindowAttributes( display(), winId(), CWOverrideRedirect, &attrs );
|
2005-07-28 14:59:42 +00:00
|
|
|
setWindowFlags( Qt::Popup );
|
2005-01-10 09:56:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ShortcutDialog::accept()
|
|
|
|
{
|
2006-12-18 12:25:10 +00:00
|
|
|
foreach( const QKeySequence &seq, shortcut() )
|
2005-01-10 09:56:21 +00:00
|
|
|
{
|
2006-04-10 11:57:13 +00:00
|
|
|
if( seq.isEmpty())
|
2005-01-10 09:56:21 +00:00
|
|
|
break;
|
2006-04-10 11:57:13 +00:00
|
|
|
if( seq[0] == Qt::Key_Escape )
|
2005-01-10 09:56:21 +00:00
|
|
|
{
|
|
|
|
reject();
|
|
|
|
return;
|
|
|
|
}
|
2006-04-10 11:57:13 +00:00
|
|
|
if( seq[0] == Qt::Key_Space )
|
2005-01-10 09:56:21 +00:00
|
|
|
{ // clear
|
|
|
|
setShortcut( KShortcut());
|
|
|
|
KShortcutDialog::accept();
|
|
|
|
return;
|
|
|
|
}
|
2006-04-10 11:57:13 +00:00
|
|
|
if( (seq[0] & Qt::KeyboardModifierMask) == 0 )
|
2005-01-10 09:56:21 +00:00
|
|
|
{ // no shortcuts without modifiers
|
|
|
|
KShortcut cut = shortcut();
|
2006-11-27 00:32:31 +00:00
|
|
|
cut.remove( seq );
|
2005-01-10 09:56:21 +00:00
|
|
|
setShortcut( cut );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
KShortcutDialog::accept();
|
|
|
|
}
|
|
|
|
#endif
|
2003-09-16 19:28:03 +00:00
|
|
|
} // namespace
|
|
|
|
|
2004-07-29 17:08:25 +00:00
|
|
|
#ifndef KCMRULES
|
2003-09-16 19:28:03 +00:00
|
|
|
#include "utils.moc"
|
2004-07-29 17:08:25 +00:00
|
|
|
#endif
|