not quite so much needs to be delayed to the init() function
[personal-kdebase.git] / workspace / kwin / toplevel.cpp
blobebe9242a168a43b784012d75ab45ac81faa8bac6
1 /********************************************************************
2 KWin - the KDE window manager
3 This file is part of the KDE project.
5 Copyright (C) 2006 Lubos Lunak <l.lunak@kde.org>
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
19 *********************************************************************/
21 #include "toplevel.h"
23 #include <kxerrorhandler.h>
25 #include "atoms.h"
26 #include "client.h"
27 #include "effects.h"
29 namespace KWin
32 Toplevel::Toplevel( Workspace* ws )
33 : vis( NULL )
34 , info( NULL )
35 , ready_for_painting( true )
36 , client( None )
37 , frame( None )
38 , wspace( ws )
39 , window_pix( None )
40 #ifdef HAVE_XDAMAGE
41 , damage_handle( None )
42 #endif
43 , is_shape( false )
44 , effect_window( NULL )
45 , wmClientLeaderWin( 0 )
46 , unredirect( false )
47 , unredirectSuspend( false )
51 Toplevel::~Toplevel()
53 #ifdef HAVE_XDAMAGE
54 assert( damage_handle == None );
55 #endif
56 discardWindowPixmap();
57 delete info;
60 kdbgstream& operator<<( kdbgstream& stream, const Toplevel* cl )
62 if( cl == NULL )
63 return stream << "\'NULL\'";
64 cl->debug( stream );
65 return stream;
68 kdbgstream& operator<<( kdbgstream& stream, const ToplevelList& list )
70 stream << "LIST:(";
71 bool first = true;
72 for( ToplevelList::ConstIterator it = list.begin();
73 it != list.end();
74 ++it )
76 if( !first )
77 stream << ":";
78 first = false;
79 stream << *it;
81 stream << ")";
82 return stream;
85 kdbgstream& operator<<( kdbgstream& stream, const ConstToplevelList& list )
87 stream << "LIST:(";
88 bool first = true;
89 for( ConstToplevelList::ConstIterator it = list.begin();
90 it != list.end();
91 ++it )
93 if( !first )
94 stream << ":";
95 first = false;
96 stream << *it;
98 stream << ")";
99 return stream;
102 void Toplevel::detectShape( Window id )
104 is_shape = Extensions::hasShape( id );
107 // used only by Deleted::copy()
108 void Toplevel::copyToDeleted( Toplevel* c )
110 geom = c->geom;
111 vis = c->vis;
112 bit_depth = c->bit_depth;
113 info = c->info;
114 client = c->client;
115 frame = c->frame;
116 wspace = c->wspace;
117 window_pix = c->window_pix;
118 ready_for_painting = c->ready_for_painting;
119 #ifdef HAVE_XDAMAGE
120 damage_handle = None;
121 #endif
122 damage_region = c->damage_region;
123 repaints_region = c->repaints_region;
124 is_shape = c->is_shape;
125 effect_window = c->effect_window;
126 if( effect_window != NULL )
127 effect_window->setWindow( this );
128 resource_name = c->resourceName();
129 resource_class = c->resourceClass();
130 client_machine = c->wmClientMachine( false );
131 wmClientLeaderWin = c->wmClientLeader();
132 window_role = c->windowRole();
133 // this needs to be done already here, otherwise 'c' could very likely
134 // call discardWindowPixmap() in something called during cleanup
135 c->window_pix = None;
138 // before being deleted, remove references to everything that's now
139 // owner by Deleted
140 void Toplevel::disownDataPassedToDeleted()
142 info = NULL;
145 NET::WindowType Toplevel::windowType( bool direct, int supported_types ) const
147 if( supported_types == 0 )
148 supported_types = dynamic_cast< const Client* >( this ) != NULL
149 ? SUPPORTED_MANAGED_WINDOW_TYPES_MASK : SUPPORTED_UNMANAGED_WINDOW_TYPES_MASK;
150 NET::WindowType wt = info->windowType( supported_types );
151 if( direct )
152 return wt;
153 const Client* cl = dynamic_cast< const Client* >( this );
154 #ifdef __GNUC__
155 #warning TODO
156 #endif
157 // NET::WindowType wt2 = rules()->checkType( wt );
158 NET::WindowType wt2 = wt;
159 if( wt != wt2 )
161 wt = wt2;
162 info->setWindowType( wt ); // force hint change
164 // hacks here
165 if( wt == NET::Menu && cl != NULL )
167 // ugly hack to support the times when NET::Menu meant NET::TopMenu
168 // if it's as wide as the screen, not very high and has its upper-left
169 // corner a bit above the screen's upper-left cornet, it's a topmenu
170 if( x() == 0 && y() < 0 && y() > -10 && height() < 100
171 && abs( width() - workspace()->clientArea( FullArea, cl ).width()) < 10 )
172 wt = NET::TopMenu;
174 // TODO change this to rule
175 const char* const oo_prefix = "openoffice.org"; // QByteArray has no startsWith()
176 // oo_prefix is lowercase, because resourceClass() is forced to be lowercase
177 if( qstrncmp( resourceClass(), oo_prefix, strlen( oo_prefix )) == 0 && wt == NET::Dialog )
178 wt = NET::Normal; // see bug #66065
179 if( wt == NET::Unknown && cl != NULL ) // this is more or less suggested in NETWM spec
180 wt = cl->isTransient() ? NET::Dialog : NET::Normal;
181 return wt;
184 void Toplevel::getWindowRole()
186 window_role = getStringProperty( window(), atoms->wm_window_role).toLower();
190 Returns SM_CLIENT_ID property for a given window.
192 QByteArray Toplevel::staticSessionId(WId w)
194 return getStringProperty(w, atoms->sm_client_id);
198 Returns WM_COMMAND property for a given window.
200 QByteArray Toplevel::staticWmCommand(WId w)
202 return getStringProperty(w, XA_WM_COMMAND, ' ');
206 Returns WM_CLIENT_LEADER property for a given window.
208 Window Toplevel::staticWmClientLeader(WId w)
210 Atom type;
211 int format, status;
212 unsigned long nitems = 0;
213 unsigned long extra = 0;
214 unsigned char *data = 0;
215 Window result = w;
216 KXErrorHandler err;
217 status = XGetWindowProperty( display(), w, atoms->wm_client_leader, 0, 10000,
218 false, XA_WINDOW, &type, &format,
219 &nitems, &extra, &data );
220 if (status == Success && !err.error( false ))
222 if (data && nitems > 0)
223 result = *((Window*) data);
224 XFree(data);
226 return result;
230 void Toplevel::getWmClientLeader()
232 wmClientLeaderWin = staticWmClientLeader(window());
236 Returns sessionId for this client,
237 taken either from its window or from the leader window.
239 QByteArray Toplevel::sessionId()
241 QByteArray result = staticSessionId(window());
242 if (result.isEmpty() && wmClientLeaderWin && wmClientLeaderWin!=window())
243 result = staticSessionId(wmClientLeaderWin);
244 return result;
248 Returns command property for this client,
249 taken either from its window or from the leader window.
251 QByteArray Toplevel::wmCommand()
253 QByteArray result = staticWmCommand(window());
254 if (result.isEmpty() && wmClientLeaderWin && wmClientLeaderWin!=window())
255 result = staticWmCommand(wmClientLeaderWin);
256 return result;
259 void Toplevel::getWmClientMachine()
261 client_machine = getStringProperty(window(), XA_WM_CLIENT_MACHINE);
262 if( client_machine.isEmpty() && wmClientLeaderWin && wmClientLeaderWin!=window())
263 client_machine = getStringProperty(wmClientLeaderWin, XA_WM_CLIENT_MACHINE);
264 if( client_machine.isEmpty())
265 client_machine = "localhost";
269 Returns client machine for this client,
270 taken either from its window or from the leader window.
272 QByteArray Toplevel::wmClientMachine( bool use_localhost ) const
274 QByteArray result = client_machine;
275 if( use_localhost )
276 { // special name for the local machine (localhost)
277 if( result != "localhost" && isLocalMachine( result ))
278 result = "localhost";
280 return result;
284 Returns client leader window for this client.
285 Returns the client window itself if no leader window is defined.
287 Window Toplevel::wmClientLeader() const
289 if (wmClientLeaderWin)
290 return wmClientLeaderWin;
291 return window();
294 void Toplevel::getResourceClass()
296 XClassHint classHint;
297 if( XGetClassHint( display(), window(), &classHint ) )
299 // Qt3.2 and older had this all lowercase, Qt3.3 capitalized resource class.
300 // Force lowercase, so that workarounds listing resource classes still work.
301 resource_name = QByteArray( classHint.res_name ).toLower();
302 resource_class = QByteArray( classHint.res_class ).toLower();
303 XFree( classHint.res_name );
304 XFree( classHint.res_class );
306 else
308 resource_name = resource_class = QByteArray();
312 double Toplevel::opacity() const
314 if( info->opacity() == 0xffffffff )
315 return 1.0;
316 return info->opacity() * 1.0 / 0xffffffff;
319 void Toplevel::setOpacity( double new_opacity )
321 double old_opacity = opacity();
322 new_opacity = qBound( 0.0, new_opacity, 1.0 );
323 if( old_opacity == new_opacity )
324 return;
325 info->setOpacity( static_cast< unsigned long >( new_opacity * 0xffffffff ));
326 if( compositing())
328 addRepaintFull();
329 scene->windowOpacityChanged( this );
330 if( effects )
331 static_cast<EffectsHandlerImpl*>(effects)->windowOpacityChanged( effectWindow(), old_opacity );
335 void Toplevel::deleteEffectWindow()
337 delete effect_window;
338 effect_window = NULL;
341 int Toplevel::screen() const
343 if( !options->xineramaEnabled )
344 return 0;
345 int s = workspace()->screenNumber( geometry().center());
346 if( s < 0 )
348 kDebug(1212) << "Invalid screen: Center" << geometry().center() << ", screen" << s;
349 return 0;
351 return s;
354 bool Toplevel::isOnScreen( int screen ) const
356 if( !options->xineramaEnabled )
357 return screen == 0;
358 return workspace()->screenGeometry( screen ).intersects( geometry());
362 } // namespace
364 #include "toplevel.moc"