00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include <qwidget.h>
00025 #ifdef Q_WS_X11 //FIXME
00026 #include "kwinmodule.h"
00027 #include "kwin.h"
00028 #include <X11/Xatom.h>
00029 #include "kapplication.h"
00030 #include "kdebug.h"
00031 #include <qtl.h>
00032 #include <qptrlist.h>
00033 #include <klocale.h>
00034 #include <dcopclient.h>
00035 #include "netwm.h"
00036
00037 static KWinModulePrivate* static_d = 0;
00038
00039 class KWinModulePrivate : public QWidget, public NETRootInfo
00040 {
00041 public:
00042 KWinModulePrivate(int _what)
00043 : QWidget(0,0), NETRootInfo( qt_xdisplay(),
00044 ( _what >= KWinModule::INFO_WINDOWS ?
00045 (ClientList | ClientListStacking) : 0
00046 ) |
00047 NumberOfDesktops |
00048 DesktopGeometry |
00049 CurrentDesktop |
00050 DesktopNames |
00051 ActiveWindow |
00052 WorkArea |
00053 KDESystemTrayWindows,
00054 -1, false
00055 ),
00056 strutSignalConnected( false ),
00057 what( _what )
00058 {
00059 kapp->installX11EventFilter( this );
00060 (void ) kapp->desktop();
00061 activate();
00062 updateStackingOrder();
00063 }
00064 ~KWinModulePrivate()
00065 {
00066 }
00067 QPtrList<KWinModule> modules;
00068
00069 QValueList<WId> windows;
00070 QValueList<WId> stackingOrder;
00071 QValueList<WId> systemTrayWindows;
00072
00073 struct StrutData
00074 {
00075 StrutData( WId window_, const NETStrut& strut_, int desktop_ )
00076 : window( window_ ), strut( strut_ ), desktop( desktop_ ) {};
00077 StrutData() {};
00078 WId window;
00079 NETStrut strut;
00080 int desktop;
00081 };
00082 QValueList<StrutData> strutWindows;
00083 QValueList<WId> possibleStrutWindows;
00084 bool strutSignalConnected;
00085 int what;
00086
00087 void addClient(Window);
00088 void removeClient(Window);
00089 void addSystemTrayWin(Window);
00090 void removeSystemTrayWin(Window);
00091
00092 bool x11Event( XEvent * ev );
00093
00094 void updateStackingOrder();
00095 bool removeStrutWindow( WId );
00096 };
00097
00098
00099 KWinModule::KWinModule( QObject* parent )
00100 : QObject( parent, "kwin_module" )
00101 {
00102 init(INFO_ALL);
00103 }
00104
00105 KWinModule::KWinModule( QObject* parent, int what )
00106 : QObject( parent, "kwin_module" )
00107 {
00108 init(what);
00109 }
00110
00111 void KWinModule::init(int what)
00112 {
00113 if (what >= INFO_WINDOWS)
00114 what = INFO_WINDOWS;
00115 else
00116 what = INFO_DESKTOP;
00117
00118 if ( !static_d )
00119 {
00120 static_d = new KWinModulePrivate(what);
00121 }
00122 else if (static_d->what < what)
00123 {
00124 QPtrList<KWinModule> modules = static_d->modules;
00125 delete static_d;
00126 static_d = new KWinModulePrivate(what);
00127 static_d->modules = modules;
00128 }
00129
00130 d = static_d;
00131 d->modules.append( this );
00132 }
00133
00134 KWinModule::~KWinModule()
00135 {
00136 d->modules.removeRef( this );
00137 if ( d->modules.isEmpty() ) {
00138 delete d;
00139 static_d = 0;
00140 }
00141 }
00142
00143 const QValueList<WId>& KWinModule::windows() const
00144 {
00145 return d->windows;
00146 }
00147
00148 const QValueList<WId>& KWinModule::stackingOrder() const
00149 {
00150 return d->stackingOrder;
00151 }
00152
00153
00154 bool KWinModule::hasWId(WId w) const
00155 {
00156 return d->windows.contains( w );
00157 }
00158
00159 const QValueList<WId>& KWinModule::systemTrayWindows() const
00160 {
00161 return d->systemTrayWindows;
00162 }
00163
00164 bool KWinModulePrivate::x11Event( XEvent * ev )
00165 {
00166 if ( ev->xany.window == qt_xrootwin() ) {
00167 int m = NETRootInfo::event( ev );
00168
00169 if ( m & CurrentDesktop )
00170 for ( QPtrListIterator<KWinModule> mit( modules ); mit.current(); ++mit )
00171 emit (*mit)->currentDesktopChanged( currentDesktop() );
00172 if ( m & ActiveWindow )
00173 for ( QPtrListIterator<KWinModule> mit( modules ); mit.current(); ++mit )
00174 emit (*mit)->activeWindowChanged( activeWindow() );
00175 if ( m & DesktopNames )
00176 for ( QPtrListIterator<KWinModule> mit( modules ); mit.current(); ++mit )
00177 emit (*mit)->desktopNamesChanged();
00178 if ( m & NumberOfDesktops )
00179 for ( QPtrListIterator<KWinModule> mit( modules ); mit.current(); ++mit )
00180 emit (*mit)->numberOfDesktopsChanged( numberOfDesktops() );
00181 if ( m & WorkArea )
00182 for ( QPtrListIterator<KWinModule> mit( modules ); mit.current(); ++mit )
00183 emit (*mit)->workAreaChanged();
00184 if ( m & ClientListStacking ) {
00185 updateStackingOrder();
00186 for ( QPtrListIterator<KWinModule> mit( modules ); mit.current(); ++mit )
00187 emit (*mit)->stackingOrderChanged();
00188 }
00189 } else if ( windows.contains( ev->xany.window ) ){
00190 NETWinInfo ni( qt_xdisplay(), ev->xany.window, qt_xrootwin(), 0 );
00191 unsigned long dirty[ 2 ];
00192 ni.event( ev, dirty, 2 );
00193 if ( ev->type ==PropertyNotify ) {
00194 if( ev->xproperty.atom == XA_WM_HINTS )
00195 dirty[ NETWinInfo::PROTOCOLS ] |= NET::WMIcon;
00196 else if( ev->xproperty.atom == XA_WM_NAME )
00197 dirty[ NETWinInfo::PROTOCOLS ] |= NET::WMName;
00198 else if( ev->xproperty.atom == XA_WM_ICON_NAME )
00199 dirty[ NETWinInfo::PROTOCOLS ] |= NET::WMIconName;
00200 }
00201 if ( (dirty[ NETWinInfo::PROTOCOLS ] & NET::WMStrut) != 0 ) {
00202 removeStrutWindow( ev->xany.window );
00203 if ( !possibleStrutWindows.contains( ev->xany.window ) )
00204 possibleStrutWindows.append( ev->xany.window );
00205 }
00206 if ( dirty[ NETWinInfo::PROTOCOLS ] || dirty[ NETWinInfo::PROTOCOLS2 ] ) {
00207 for ( QPtrListIterator<KWinModule> mit( modules ); mit.current(); ++mit ) {
00208 emit (*mit)->windowChanged( ev->xany.window );
00209 emit (*mit)->windowChanged( ev->xany.window, dirty );
00210 emit (*mit)->windowChanged( ev->xany.window, dirty[ NETWinInfo::PROTOCOLS ] );
00211 if ( (dirty[ NETWinInfo::PROTOCOLS ] & NET::WMStrut) != 0 )
00212 emit (*mit)->strutChanged();
00213 }
00214 }
00215 }
00216
00217 return false;
00218 }
00219
00220 bool KWinModulePrivate::removeStrutWindow( WId w )
00221 {
00222 for( QValueList< StrutData >::Iterator it = strutWindows.begin();
00223 it != strutWindows.end();
00224 ++it )
00225 if( (*it).window == w ) {
00226 strutWindows.remove( it );
00227 return true;
00228 }
00229 return false;
00230 }
00231
00232 void KWinModulePrivate::updateStackingOrder()
00233 {
00234 stackingOrder.clear();
00235 for ( int i = 0; i < clientListStackingCount(); i++ )
00236 stackingOrder.append( clientListStacking()[i] );
00237 }
00238
00239 void KWinModulePrivate::addClient(Window w)
00240 {
00241 if ( (what >= KWinModule::INFO_WINDOWS) && !QWidget::find( w ) )
00242 XSelectInput( qt_xdisplay(), w, PropertyChangeMask | StructureNotifyMask );
00243 bool emit_strutChanged = false;
00244 if( strutSignalConnected && modules.count() > 0 ) {
00245 NETWinInfo info( qt_xdisplay(), w, qt_xrootwin(), NET::WMStrut | NET::WMDesktop );
00246 NETStrut strut = info.strut();
00247 if ( strut.left || strut.top || strut.right || strut.bottom ) {
00248 strutWindows.append( StrutData( w, strut, info.desktop()));
00249 emit_strutChanged = true;
00250 }
00251 } else
00252 possibleStrutWindows.append( w );
00253 windows.append( w );
00254 for ( QPtrListIterator<KWinModule> mit( modules ); mit.current(); ++mit ) {
00255 emit (*mit)->windowAdded( w );
00256 if ( emit_strutChanged )
00257 emit (*mit)->strutChanged();
00258 }
00259 }
00260
00261 void KWinModulePrivate::removeClient(Window w)
00262 {
00263 bool emit_strutChanged = removeStrutWindow( w );
00264 if( strutSignalConnected && possibleStrutWindows.contains( w ) && modules.count() > 0 ) {
00265 NETWinInfo info( qt_xdisplay(), w, qt_xrootwin(), NET::WMStrut );
00266 NETStrut strut = info.strut();
00267 if ( strut.left || strut.top || strut.right || strut.bottom ) {
00268 emit_strutChanged = true;
00269 }
00270 }
00271 possibleStrutWindows.remove( w );
00272 windows.remove( w );
00273 for ( QPtrListIterator<KWinModule> mit( modules ); mit.current(); ++mit ) {
00274 emit (*mit)->windowRemoved( w );
00275 if ( emit_strutChanged )
00276 emit (*mit)->strutChanged();
00277 }
00278 }
00279
00280 void KWinModulePrivate::addSystemTrayWin(Window w)
00281 {
00282 systemTrayWindows.append( w );
00283 for ( QPtrListIterator<KWinModule> mit( modules ); mit.current(); ++mit )
00284 emit (*mit)->systemTrayWindowAdded( w );
00285 }
00286
00287 void KWinModulePrivate::removeSystemTrayWin(Window w)
00288 {
00289 systemTrayWindows.remove( w );
00290 for ( QPtrListIterator<KWinModule> mit( modules ); mit.current(); ++mit )
00291 emit (*mit)->systemTrayWindowRemoved( w );
00292 }
00293
00294 int KWinModule::currentDesktop() const
00295 {
00296 return d->currentDesktop();
00297 }
00298
00299 int KWinModule::numberOfDesktops() const
00300 {
00301 return d->numberOfDesktops();
00302 }
00303
00304 WId KWinModule::activeWindow() const
00305 {
00306 return d->activeWindow();
00307 }
00308
00309 QRect KWinModule::workArea( int desktop ) const
00310 {
00311 int desk = (desktop > 0 && desktop <= (int) d->numberOfDesktops() ) ? desktop : currentDesktop();
00312 if ( desk <= 0 )
00313 return QApplication::desktop()->geometry();
00314 NETRect r = d->workArea( desk );
00315 return QRect( r.pos.x, r.pos.y, r.size.width, r.size.height );
00316 }
00317
00318 QRect KWinModule::workArea( const QValueList<WId>& exclude, int desktop ) const
00319 {
00320 QRect all = QApplication::desktop()->geometry();
00321 QRect a = all;
00322
00323 if (desktop == -1)
00324 desktop = d->currentDesktop();
00325
00326 QValueList<WId>::ConstIterator it1;
00327 for( it1 = d->windows.begin(); it1 != d->windows.end(); ++it1 ) {
00328
00329 if(exclude.contains(*it1) > 0) continue;
00330
00331
00332
00333
00334 NETStrut strut;
00335 QValueList< KWinModulePrivate::StrutData >::Iterator it2 = d->strutWindows.begin();
00336 for( ;
00337 it2 != d->strutWindows.end();
00338 ++it2 )
00339 if( (*it2).window == *it1 )
00340 break;
00341 if( it2 != d->strutWindows.end()) {
00342 if(!((*it2).desktop == desktop || (*it2).desktop == NETWinInfo::OnAllDesktops ))
00343 continue;
00344 strut = (*it2).strut;
00345 } else if( d->possibleStrutWindows.contains( *it1 )) {
00346 NETWinInfo info( qt_xdisplay(), (*it1), qt_xrootwin(), NET::WMStrut | NET::WMDesktop);
00347 strut = info.strut();
00348 d->possibleStrutWindows.remove( *it1 );
00349 d->strutWindows.append( KWinModulePrivate::StrutData( *it1, info.strut(), info.desktop()));
00350 if(!(info.desktop() == desktop || info.desktop() == NETWinInfo::OnAllDesktops))
00351 continue;
00352 } else
00353 continue;
00354
00355 QRect r = all;
00356 if ( strut.left > 0 )
00357 r.setLeft( r.left() + (int) strut.left );
00358 if ( strut.top > 0 )
00359 r.setTop( r.top() + (int) strut.top );
00360 if ( strut.right > 0 )
00361 r.setRight( r.right() - (int) strut.right );
00362 if ( strut.bottom > 0 )
00363 r.setBottom( r.bottom() - (int) strut.bottom );
00364
00365 a = a.intersect(r);
00366 }
00367 return a;
00368 }
00369
00370 void KWinModule::connectNotify( const char* signal )
00371 {
00372 if( !d->strutSignalConnected && qstrcmp( signal, SIGNAL(strutChanged())) == 0 )
00373 d->strutSignalConnected = true;
00374 QObject::connectNotify( signal );
00375 }
00376
00377 QString KWinModule::desktopName( int desktop ) const
00378 {
00379 const char* name = d->desktopName( (desktop > 0 && desktop <= (int) d->numberOfDesktops() ) ? desktop : currentDesktop() );
00380 if ( name && name[0] )
00381 return QString::fromUtf8( name );
00382 return i18n("Desktop %1").arg( desktop );
00383 }
00384
00385 void KWinModule::setDesktopName( int desktop, const QString& name )
00386 {
00387 if (desktop <= 0 || desktop > (int) d->numberOfDesktops() )
00388 desktop = currentDesktop();
00389 d->setDesktopName( desktop, name.utf8().data() );
00390 }
00391
00392
00393 void KWinModule::doNotManage( const QString& title )
00394 {
00395 if ( !kapp->dcopClient()->isAttached() )
00396 kapp->dcopClient()->attach();
00397 QByteArray data, replyData;
00398 QCString replyType;
00399 QDataStream arg(data, IO_WriteOnly);
00400 arg << title;
00401 kapp->dcopClient()->call("kwin", "", "doNotManage(QString)",
00402 data, replyType, replyData);
00403 }
00404
00405 #include "kwinmodule.moc"
00406 #endif