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>
|
|
|
|
#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-08-10 13:15:16 +00:00
|
|
|
// used to store the return values of
|
|
|
|
// XShapeQueryExtension.
|
|
|
|
// Necessary since shaped window are an extension to X
|
|
|
|
int Shape::kwin_shape_version = 0;
|
|
|
|
int Shape::kwin_shape_event = 0;
|
2003-09-16 19:28:03 +00:00
|
|
|
|
2006-08-10 13:15:16 +00:00
|
|
|
// does the window w need a shape combine mask around it?
|
|
|
|
bool Shape::hasShape( WId w)
|
2003-09-16 19:28:03 +00:00
|
|
|
{
|
2006-08-10 13:15:16 +00:00
|
|
|
int xws, yws, xbs, ybs;
|
|
|
|
unsigned int wws, hws, wbs, hbs;
|
|
|
|
int boundingShaped = 0, clipShaped = 0;
|
|
|
|
if (!available())
|
|
|
|
return false;
|
2006-08-10 17:00:14 +00:00
|
|
|
XShapeQueryExtents(display(), w,
|
2006-08-10 13:15:16 +00:00
|
|
|
&boundingShaped, &xws, &yws, &wws, &hws,
|
|
|
|
&clipShaped, &xbs, &ybs, &wbs, &hbs);
|
|
|
|
return boundingShaped != 0;
|
2003-09-16 19:28:03 +00:00
|
|
|
}
|
|
|
|
|
2006-08-10 13:15:16 +00:00
|
|
|
int Shape::shapeEvent()
|
2003-09-16 19:28:03 +00:00
|
|
|
{
|
2006-08-10 13:15:16 +00:00
|
|
|
return kwin_shape_event;
|
2003-09-16 19:28:03 +00:00
|
|
|
}
|
|
|
|
|
2006-08-10 13:15:16 +00:00
|
|
|
void Shape::init()
|
2003-09-16 19:28:03 +00:00
|
|
|
{
|
2006-08-10 13:15:16 +00:00
|
|
|
kwin_shape_version = 0;
|
|
|
|
int dummy;
|
2006-08-10 17:00:14 +00:00
|
|
|
if( !XShapeQueryExtension( display(), &kwin_shape_event, &dummy ))
|
2006-08-10 13:15:16 +00:00
|
|
|
return;
|
|
|
|
int major, minor;
|
2006-08-10 17:00:14 +00:00
|
|
|
if( !XShapeQueryVersion( display(), &major, &minor ))
|
2006-08-10 13:15:16 +00:00
|
|
|
return;
|
|
|
|
kwin_shape_version = major * 16 + minor;
|
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
|
|
|
}
|
2005-10-07 12:00:29 +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()
|
|
|
|
{
|
|
|
|
for( int i = 0;
|
|
|
|
;
|
|
|
|
++i )
|
|
|
|
{
|
2006-04-10 11:57:13 +00:00
|
|
|
QKeySequence seq = shortcut().seq( i );
|
|
|
|
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-04-10 11:57:13 +00:00
|
|
|
cut.setSeq( i, QKeySequence());
|
2005-01-10 09:56:21 +00:00
|
|
|
setShortcut( cut );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
KShortcutDialog::accept();
|
|
|
|
}
|
|
|
|
#endif
|
2006-08-10 13:15:16 +00:00
|
|
|
|
|
|
|
|
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
|