Make a branch to make krunner Good Enough For Aaron™.
[kdebase/uwolfer.git] / workspace / kwin / toplevel.cpp
blobcdf72cdc720373f720fcdc31a53ee9917a98f0dc
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 )
49 Toplevel::~Toplevel()
51 #ifdef HAVE_XDAMAGE
52 assert( damage_handle == None );
53 #endif
54 discardWindowPixmap();
55 delete info;
58 kdbgstream& operator<<( kdbgstream& stream, const Toplevel* cl )
60 if( cl == NULL )
61 return stream << "\'NULL\'";
62 cl->debug( stream );
63 return stream;
66 kdbgstream& operator<<( kdbgstream& stream, const ToplevelList& list )
68 stream << "LIST:(";
69 bool first = true;
70 for( ToplevelList::ConstIterator it = list.begin();
71 it != list.end();
72 ++it )
74 if( !first )
75 stream << ":";
76 first = false;
77 stream << *it;
79 stream << ")";
80 return stream;
83 kdbgstream& operator<<( kdbgstream& stream, const ConstToplevelList& list )
85 stream << "LIST:(";
86 bool first = true;
87 for( ConstToplevelList::ConstIterator it = list.begin();
88 it != list.end();
89 ++it )
91 if( !first )
92 stream << ":";
93 first = false;
94 stream << *it;
96 stream << ")";
97 return stream;
100 void Toplevel::detectShape( Window id )
102 is_shape = Extensions::hasShape( id );
105 // used only by Deleted::copy()
106 void Toplevel::copyToDeleted( Toplevel* c )
108 geom = c->geom;
109 vis = c->vis;
110 bit_depth = c->bit_depth;
111 info = c->info;
112 client = c->client;
113 frame = c->frame;
114 wspace = c->wspace;
115 window_pix = c->window_pix;
116 ready_for_painting = c->ready_for_painting;
117 #ifdef HAVE_XDAMAGE
118 damage_handle = None;
119 #endif
120 damage_region = c->damage_region;
121 repaints_region = c->repaints_region;
122 is_shape = c->is_shape;
123 effect_window = c->effect_window;
124 if( effect_window != NULL )
125 effect_window->setWindow( this );
126 resource_name = c->resourceName();
127 resource_class = c->resourceClass();
128 client_machine = c->wmClientMachine( false );
129 wmClientLeaderWin = c->wmClientLeader();
130 window_role = c->windowRole();
131 // this needs to be done already here, otherwise 'c' could very likely
132 // call discardWindowPixmap() in something called during cleanup
133 c->window_pix = None;
136 // before being deleted, remove references to everything that's now
137 // owner by Deleted
138 void Toplevel::disownDataPassedToDeleted()
140 info = NULL;
143 NET::WindowType Toplevel::windowType( bool direct, int supported_types ) const
145 if( supported_types == 0 )
146 supported_types = dynamic_cast< const Client* >( this ) != NULL
147 ? SUPPORTED_MANAGED_WINDOW_TYPES_MASK : SUPPORTED_UNMANAGED_WINDOW_TYPES_MASK;
148 NET::WindowType wt = info->windowType( supported_types );
149 if( direct )
150 return wt;
151 const Client* cl = dynamic_cast< const Client* >( this );
152 #warning TODO
153 // NET::WindowType wt2 = rules()->checkType( wt );
154 NET::WindowType wt2 = wt;
155 if( wt != wt2 )
157 wt = wt2;
158 info->setWindowType( wt ); // force hint change
160 // hacks here
161 if( wt == NET::Menu && cl != NULL )
163 // ugly hack to support the times when NET::Menu meant NET::TopMenu
164 // if it's as wide as the screen, not very high and has its upper-left
165 // corner a bit above the screen's upper-left cornet, it's a topmenu
166 if( x() == 0 && y() < 0 && y() > -10 && height() < 100
167 && abs( width() - workspace()->clientArea( FullArea, cl ).width()) < 10 )
168 wt = NET::TopMenu;
170 // TODO change this to rule
171 const char* const oo_prefix = "openoffice.org"; // QByteArray has no startsWith()
172 // oo_prefix is lowercase, because resourceClass() is forced to be lowercase
173 if( qstrncmp( resourceClass(), oo_prefix, strlen( oo_prefix )) == 0 && wt == NET::Dialog )
174 wt = NET::Normal; // see bug #66065
175 if( wt == NET::Unknown && cl != NULL ) // this is more or less suggested in NETWM spec
176 wt = cl->isTransient() ? NET::Dialog : NET::Normal;
177 return wt;
180 void Toplevel::getWindowRole()
182 window_role = getStringProperty( window(), atoms->wm_window_role).toLower();
186 Returns SM_CLIENT_ID property for a given window.
188 QByteArray Toplevel::staticSessionId(WId w)
190 return getStringProperty(w, atoms->sm_client_id);
194 Returns WM_COMMAND property for a given window.
196 QByteArray Toplevel::staticWmCommand(WId w)
198 return getStringProperty(w, XA_WM_COMMAND, ' ');
202 Returns WM_CLIENT_LEADER property for a given window.
204 Window Toplevel::staticWmClientLeader(WId w)
206 Atom type;
207 int format, status;
208 unsigned long nitems = 0;
209 unsigned long extra = 0;
210 unsigned char *data = 0;
211 Window result = w;
212 KXErrorHandler err;
213 status = XGetWindowProperty( display(), w, atoms->wm_client_leader, 0, 10000,
214 false, XA_WINDOW, &type, &format,
215 &nitems, &extra, &data );
216 if (status == Success && !err.error( false ))
218 if (data && nitems > 0)
219 result = *((Window*) data);
220 XFree(data);
222 return result;
226 void Toplevel::getWmClientLeader()
228 wmClientLeaderWin = staticWmClientLeader(window());
232 Returns sessionId for this client,
233 taken either from its window or from the leader window.
235 QByteArray Toplevel::sessionId()
237 QByteArray result = staticSessionId(window());
238 if (result.isEmpty() && wmClientLeaderWin && wmClientLeaderWin!=window())
239 result = staticSessionId(wmClientLeaderWin);
240 return result;
244 Returns command property for this client,
245 taken either from its window or from the leader window.
247 QByteArray Toplevel::wmCommand()
249 QByteArray result = staticWmCommand(window());
250 if (result.isEmpty() && wmClientLeaderWin && wmClientLeaderWin!=window())
251 result = staticWmCommand(wmClientLeaderWin);
252 return result;
255 void Toplevel::getWmClientMachine()
257 client_machine = getStringProperty(window(), XA_WM_CLIENT_MACHINE);
258 if( client_machine.isEmpty() && wmClientLeaderWin && wmClientLeaderWin!=window())
259 client_machine = getStringProperty(wmClientLeaderWin, XA_WM_CLIENT_MACHINE);
260 if( client_machine.isEmpty())
261 client_machine = "localhost";
265 Returns client machine for this client,
266 taken either from its window or from the leader window.
268 QByteArray Toplevel::wmClientMachine( bool use_localhost ) const
270 QByteArray result = client_machine;
271 if( use_localhost )
272 { // special name for the local machine (localhost)
273 if( result != "localhost" && isLocalMachine( result ))
274 result = "localhost";
276 return result;
280 Returns client leader window for this client.
281 Returns the client window itself if no leader window is defined.
283 Window Toplevel::wmClientLeader() const
285 if (wmClientLeaderWin)
286 return wmClientLeaderWin;
287 return window();
290 void Toplevel::getResourceClass()
292 XClassHint classHint;
293 if( XGetClassHint( display(), window(), &classHint ) )
295 // Qt3.2 and older had this all lowercase, Qt3.3 capitalized resource class.
296 // Force lowercase, so that workarounds listing resource classes still work.
297 resource_name = QByteArray( classHint.res_name ).toLower();
298 resource_class = QByteArray( classHint.res_class ).toLower();
299 XFree( classHint.res_name );
300 XFree( classHint.res_class );
302 else
304 resource_name = resource_class = QByteArray();
308 double Toplevel::opacity() const
310 if( info->opacity() == 0xffffffff )
311 return 1.0;
312 return info->opacity() * 1.0 / 0xffffffff;
315 void Toplevel::setOpacity( double new_opacity )
317 double old_opacity = opacity();
318 new_opacity = qBound( 0.0, new_opacity, 1.0 );
319 if( old_opacity == new_opacity )
320 return;
321 info->setOpacity( static_cast< unsigned long >( new_opacity * 0xffffffff ));
322 if( compositing())
324 addRepaintFull();
325 scene->windowOpacityChanged( this );
326 if( effects )
327 static_cast<EffectsHandlerImpl*>(effects)->windowOpacityChanged( effectWindow(), old_opacity );
331 } // namespace
333 #include "toplevel.moc"