• Skip to content
  • Skip to link menu
Trinity API Reference
  • Trinity API Reference
  • twin
 

twin

  • twin
sm.cpp
1 /*****************************************************************
2  KWin - the KDE window manager
3  This file is part of the KDE project.
4 
5 Copyright (C) 1999, 2000 Matthias Ettrich <ettrich@kde.org>
6 Copyright (C) 2003 Lubos Lunak <l.lunak@kde.org>
7 
8 You can Freely distribute this program under the GNU General Public
9 License. See the file "COPYING" for the exact licensing terms.
10 ******************************************************************/
11 
12 #include "sm.h"
13 
14 #include <tqsocketnotifier.h>
15 #include <tqsessionmanager.h>
16 #include <kdebug.h>
17 #include <unistd.h>
18 #include <stdlib.h>
19 #include <pwd.h>
20 #include <fixx11h.h>
21 #include <tdeconfig.h>
22 #include <tdeglobal.h>
23 
24 #include "workspace.h"
25 #include "client.h"
26 
27 namespace KWinInternal
28 {
29 
30 bool SessionManaged::saveState( TQSessionManager& sm )
31  {
32  // If the session manager is ksmserver, save stacking
33  // order, active window, active desktop etc. in phase 1,
34  // as ksmserver assures no interaction will be done
35  // before the WM finishes phase 1. Saving in phase 2 is
36  // too late, as possible user interaction may change some things.
37  // Phase2 is still needed though (ICCCM 5.2)
38  char* sm_vendor = SmcVendor( static_cast< SmcConn >( sm.handle()));
39  bool ksmserver = qstrcmp( sm_vendor, "KDE" ) == 0;
40  free( sm_vendor );
41  if ( !sm.isPhase2() )
42  {
43  Workspace::self()->sessionSaveStarted();
44  if( ksmserver ) // save stacking order etc. before "save file?" etc. dialogs change it
45  Workspace::self()->storeSession( kapp->sessionConfig(), SMSavePhase0 );
46  sm.release(); // Qt doesn't automatically release in this case (bug?)
47  sm.requestPhase2();
48  return true;
49  }
50  Workspace::self()->storeSession( kapp->sessionConfig(), ksmserver ? SMSavePhase2 : SMSavePhase2Full );
51  kapp->sessionConfig()->sync();
52  return true;
53  }
54 
55 // I bet this is broken, just like everywhere else in KDE
56 bool SessionManaged::commitData( TQSessionManager& sm )
57  {
58  if ( !sm.isPhase2() )
59  Workspace::self()->sessionSaveStarted();
60  return true;
61  }
62 
63 // Workspace
64 
70 void Workspace::storeSession( TDEConfig* config, SMSavePhase phase )
71  {
72  config->setGroup("Session" );
73  int count = 0;
74  int active_client = -1;
75  for (ClientList::Iterator it = clients.begin(); it != clients.end(); ++it)
76  {
77  Client* c = (*it);
78  TQCString sessionId = c->sessionId();
79  TQCString wmCommand = c->wmCommand();
80  if ( sessionId.isEmpty() )
81  // remember also applications that are not XSMP capable
82  // and use the obsolete WM_COMMAND / WM_SAVE_YOURSELF
83  if ( wmCommand.isEmpty() )
84  continue;
85  count++;
86  if( c->isActive())
87  active_client = count;
88  TQString n = TQString::number(count);
89  if( phase == SMSavePhase2 || phase == SMSavePhase2Full )
90  {
91  config->writeEntry( TQString("sessionId")+n, sessionId.data() );
92  config->writeEntry( TQString("windowRole")+n, c->windowRole().data() );
93  config->writeEntry( TQString("wmCommand")+n, wmCommand.data() );
94  config->writeEntry( TQString("wmClientMachine")+n, c->wmClientMachine( true ).data() );
95  config->writeEntry( TQString("resourceName")+n, c->resourceName().data() );
96  config->writeEntry( TQString("resourceClass")+n, c->resourceClass().data() );
97  config->writeEntry( TQString("geometry")+n, TQRect( c->calculateGravitation(TRUE), c->clientSize() ) ); // FRAME
98  config->writeEntry( TQString("restore")+n, c->geometryRestore() );
99  config->writeEntry( TQString("fsrestore")+n, c->geometryFSRestore() );
100  config->writeEntry( TQString("maximize")+n, (int) c->maximizeMode() );
101  config->writeEntry( TQString("fullscreen")+n, (int) c->fullScreenMode() );
102  config->writeEntry( TQString("desktop")+n, c->desktop() );
103  // the config entry is called "iconified" for back. comp. reasons
104  // (tdeconf_update script for updating session files would be too complicated)
105  config->writeEntry( TQString("iconified")+n, c->isMinimized() );
106  // the config entry is called "sticky" for back. comp. reasons
107  config->writeEntry( TQString("sticky")+n, c->isOnAllDesktops() );
108  config->writeEntry( TQString("shaded")+n, c->isShade() );
109  config->writeEntry( TQString("shadowed")+n, c->isShadowed() );
110  // the config entry is called "staysOnTop" for back. comp. reasons
111  config->writeEntry( TQString("staysOnTop")+n, c->keepAbove() );
112  config->writeEntry( TQString("keepBelow")+n, c->keepBelow() );
113  config->writeEntry( TQString("skipTaskbar")+n, c->skipTaskbar( true ) );
114  config->writeEntry( TQString("skipPager")+n, c->skipPager() );
115  config->writeEntry( TQString("userNoBorder")+n, c->isUserNoBorder() );
116  config->writeEntry( TQString("windowType")+n, windowTypeToTxt( c->windowType()));
117  config->writeEntry( TQString("shortcut")+n, c->shortcut().toStringInternal());
118  }
119  }
120  // TODO store also stacking order
121  if( phase == SMSavePhase0 )
122  {
123  // it would be much simpler to save these values to the config file,
124  // but both Qt and KDE treat phase1 and phase2 separately,
125  // which results in different sessionkey and different config file :(
126  session_active_client = active_client;
127  session_desktop = currentDesktop();
128  }
129  else if( phase == SMSavePhase2 )
130  {
131  config->writeEntry( "count", count );
132  config->writeEntry( "active", session_active_client );
133  config->writeEntry( "desktop", session_desktop );
134  }
135  else // SMSavePhase2Full
136  {
137  config->writeEntry( "count", count );
138  config->writeEntry( "active", session_active_client );
139  config->writeEntry( "desktop", currentDesktop());
140  }
141  }
142 
143 
149 void Workspace::loadSessionInfo()
150  {
151  session.clear();
152  TDEConfig* config = kapp->sessionConfig();
153  config->setGroup("Session" );
154  int count = config->readNumEntry( "count" );
155  int active_client = config->readNumEntry( "active" );
156  for ( int i = 1; i <= count; i++ )
157  {
158  TQString n = TQString::number(i);
159  SessionInfo* info = new SessionInfo;
160  session.append( info );
161  info->sessionId = config->readEntry( TQString("sessionId")+n ).latin1();
162  info->windowRole = config->readEntry( TQString("windowRole")+n ).latin1();
163  info->wmCommand = config->readEntry( TQString("wmCommand")+n ).latin1();
164  info->wmClientMachine = config->readEntry( TQString("wmClientMachine")+n ).latin1();
165  info->resourceName = config->readEntry( TQString("resourceName")+n ).latin1();
166  info->resourceClass = config->readEntry( TQString("resourceClass")+n ).lower().latin1();
167  info->geometry = config->readRectEntry( TQString("geometry")+n );
168  info->restore = config->readRectEntry( TQString("restore")+n );
169  info->fsrestore = config->readRectEntry( TQString("fsrestore")+n );
170  info->maximized = config->readNumEntry( TQString("maximize")+n, 0 );
171  info->fullscreen = config->readNumEntry( TQString("fullscreen")+n, 0 );
172  info->desktop = config->readNumEntry( TQString("desktop")+n, 0 );
173  info->minimized = config->readBoolEntry( TQString("iconified")+n, FALSE );
174  info->onAllDesktops = config->readBoolEntry( TQString("sticky")+n, FALSE );
175  info->shaded = config->readBoolEntry( TQString("shaded")+n, FALSE );
176  info->shadowed = config->readBoolEntry( TQString("shadowed")+n, TRUE );
177  info->keepAbove = config->readBoolEntry( TQString("staysOnTop")+n, FALSE );
178  info->keepBelow = config->readBoolEntry( TQString("keepBelow")+n, FALSE );
179  info->skipTaskbar = config->readBoolEntry( TQString("skipTaskbar")+n, FALSE );
180  info->skipPager = config->readBoolEntry( TQString("skipPager")+n, FALSE );
181  info->userNoBorder = config->readBoolEntry( TQString("userNoBorder")+n, FALSE );
182  info->windowType = txtToWindowType( config->readEntry( TQString("windowType")+n ).latin1());
183  info->shortcut = config->readEntry( TQString("shortcut")+n );
184  info->active = ( active_client == i );
185  }
186  }
187 
197 SessionInfo* Workspace::takeSessionInfo( Client* c )
198  {
199  SessionInfo *realInfo = 0;
200  TQCString sessionId = c->sessionId();
201  TQCString windowRole = c->windowRole();
202  TQCString wmCommand = c->wmCommand();
203  TQCString wmClientMachine = c->wmClientMachine( true );
204  TQCString resourceName = c->resourceName();
205  TQCString resourceClass = c->resourceClass();
206 
207  // First search ``session''
208  if (! sessionId.isEmpty() )
209  {
210  // look for a real session managed client (algorithm suggested by ICCCM)
211  for (SessionInfo* info = session.first(); info && !realInfo; info = session.next() )
212  if ( info->sessionId == sessionId && sessionInfoWindowTypeMatch( c, info ))
213  {
214  if ( ! windowRole.isEmpty() )
215  {
216  if ( info->windowRole == windowRole )
217  realInfo = session.take();
218  }
219  else
220  {
221  if ( info->windowRole.isEmpty() &&
222  info->resourceName == resourceName &&
223  info->resourceClass == resourceClass )
224  realInfo = session.take();
225  }
226  }
227  }
228  else
229  {
230  // look for a sessioninfo with matching features.
231  for (SessionInfo* info = session.first(); info && !realInfo; info = session.next() )
232  if ( info->resourceName == resourceName &&
233  info->resourceClass == resourceClass &&
234  info->wmClientMachine == wmClientMachine &&
235  sessionInfoWindowTypeMatch( c, info ))
236  if ( wmCommand.isEmpty() || info->wmCommand == wmCommand )
237  realInfo = session.take();
238  }
239 
240  return realInfo;
241  }
242 
243 bool Workspace::sessionInfoWindowTypeMatch( Client* c, SessionInfo* info )
244  {
245  if( info->windowType == -2 )
246  { // undefined (not really part of NET::WindowType)
247  return !c->isSpecialWindow();
248  }
249  return info->windowType == c->windowType();
250  }
251 
252 // maybe needed later
253 #if 0
254 // TDEMainWindow's without name() given have WM_WINDOW_ROLE in the form
255 // of <appname>-mainwindow#<number>
256 // when comparing them for fake session info, it's probably better to check
257 // them without the trailing number
258 bool Workspace::windowRoleMatch( const TQCString& role1, const TQCString& role2 )
259  {
260  if( role1.isEmpty() && role2.isEmpty())
261  return true;
262  int pos1 = role1.find( '#' );
263  int pos2 = role2.find( '#' );
264  bool ret;
265  if( pos1 < 0 || pos2 < 0 || pos1 != pos2 )
266  ret = role1 == role2;
267  else
268  ret = tqstrncmp( role1, role2, pos1 ) == 0;
269  kdDebug() << "WR:" << role1 << ":" << pos1 << ":" << role2 << ":" << pos2 << ":::" << ret << endl;
270  return ret;
271  }
272 #endif
273 
274 static const char* const window_type_names[] =
275  {
276  "Unknown", "Normal" , "Desktop", "Dock", "Toolbar", "Menu", "Dialog",
277  "Override", "TopMenu", "Utility", "Splash"
278  };
279  // change also the two functions below when adding new entries
280 
281 const char* Workspace::windowTypeToTxt( NET::WindowType type )
282  {
283  if( type >= NET::Unknown && type <= NET::Splash )
284  return window_type_names[ type + 1 ]; // +1 (unknown==-1)
285  if( type == -2 ) // undefined (not really part of NET::WindowType)
286  return "Undefined";
287  kdFatal() << "Unknown Window Type" << endl;
288  return NULL;
289  }
290 
291 NET::WindowType Workspace::txtToWindowType( const char* txt )
292  {
293  for( int i = NET::Unknown;
294  i <= NET::Splash;
295  ++i )
296  if( qstrcmp( txt, window_type_names[ i + 1 ] ) == 0 ) // +1
297  return static_cast< NET::WindowType >( i );
298  return static_cast< NET::WindowType >( -2 ); // undefined
299  }
300 
301 
302 
303 
304 // KWin's focus stealing prevention causes problems with user interaction
305 // during session save, as it prevents possible dialogs from getting focus.
306 // Therefore it's temporarily disabled during session saving. Start of
307 // session saving can be detected in SessionManaged::saveState() above,
308 // but Qt doesn't have API for saying when session saved finished (either
309 // successfully, or was cancelled). Therefore, create another connection
310 // to session manager, that will provide this information.
311 // Similarly the remember feature of window-specific settings should be disabled
312 // during KDE shutdown when windows may move e.g. because of Kicker going away
313 // (struts changing). When session saving starts, it can be cancelled, in which
314 // case the shutdown_cancelled callback is invoked, or it's a checkpoint that
315 // is immediatelly followed by save_complete, or finally it's a shutdown that
316 // is immediatelly followed by die callback. So getting save_yourself with shutdown
317 // set disables window-specific settings remembering, getting shutdown_cancelled
318 // re-enables, otherwise KWin will go away after die.
319 static void save_yourself( SmcConn conn_P, SmPointer ptr, int, Bool shutdown, int, Bool )
320  {
321  SessionSaveDoneHelper* session = reinterpret_cast< SessionSaveDoneHelper* >( ptr );
322  if( conn_P != session->connection())
323  return;
324  if( shutdown )
325  Workspace::self()->disableRulesUpdates( true );
326  SmcSaveYourselfDone( conn_P, True );
327  }
328 
329 static void die( SmcConn conn_P, SmPointer ptr )
330  {
331  SessionSaveDoneHelper* session = reinterpret_cast< SessionSaveDoneHelper* >( ptr );
332  if( conn_P != session->connection())
333  return;
334  // session->saveDone(); we will quit anyway
335  session->close();
336  }
337 
338 static void save_complete( SmcConn conn_P, SmPointer ptr )
339  {
340  SessionSaveDoneHelper* session = reinterpret_cast< SessionSaveDoneHelper* >( ptr );
341  if( conn_P != session->connection())
342  return;
343  session->saveDone();
344  }
345 
346 static void shutdown_cancelled( SmcConn conn_P, SmPointer ptr )
347  {
348  SessionSaveDoneHelper* session = reinterpret_cast< SessionSaveDoneHelper* >( ptr );
349  if( conn_P != session->connection())
350  return;
351  Workspace::self()->disableRulesUpdates( false ); // re-enable
352  // no need to differentiate between successful finish and cancel
353  session->saveDone();
354  }
355 
356 void SessionSaveDoneHelper::saveDone()
357  {
358  Workspace::self()->sessionSaveDone();
359  }
360 
361 SessionSaveDoneHelper::SessionSaveDoneHelper()
362  {
363  SmcCallbacks calls;
364  calls.save_yourself.callback = save_yourself;
365  calls.save_yourself.client_data = reinterpret_cast< SmPointer >(this);
366  calls.die.callback = die;
367  calls.die.client_data = reinterpret_cast< SmPointer >(this);
368  calls.save_complete.callback = save_complete;
369  calls.save_complete.client_data = reinterpret_cast< SmPointer >(this);
370  calls.shutdown_cancelled.callback = shutdown_cancelled;
371  calls.shutdown_cancelled.client_data = reinterpret_cast< SmPointer >(this);
372  char* id = NULL;
373  char err[ 11 ];
374  conn = SmcOpenConnection( NULL, 0, 1, 0,
375  SmcSaveYourselfProcMask | SmcDieProcMask | SmcSaveCompleteProcMask
376  | SmcShutdownCancelledProcMask, &calls, NULL, &id, 10, err );
377  if( id != NULL )
378  free( id );
379  if( conn == NULL )
380  return; // no SM
381  // set the required properties, mostly dummy values
382  SmPropValue propvalue[ 5 ];
383  SmProp props[ 5 ];
384  propvalue[ 0 ].length = sizeof( int );
385  int value0 = SmRestartNever; // so that this extra SM connection doesn't interfere
386  propvalue[ 0 ].value = &value0;
387  props[ 0 ].name = const_cast< char* >( SmRestartStyleHint );
388  props[ 0 ].type = const_cast< char* >( SmCARD8 );
389  props[ 0 ].num_vals = 1;
390  props[ 0 ].vals = &propvalue[ 0 ];
391  struct passwd* entry = getpwuid( geteuid() );
392  propvalue[ 1 ].length = entry != NULL ? strlen( entry->pw_name ) : 0;
393  propvalue[ 1 ].value = (SmPointer)( entry != NULL ? entry->pw_name : "" );
394  props[ 1 ].name = const_cast< char* >( SmUserID );
395  props[ 1 ].type = const_cast< char* >( SmARRAY8 );
396  props[ 1 ].num_vals = 1;
397  props[ 1 ].vals = &propvalue[ 1 ];
398  propvalue[ 2 ].length = 0;
399  propvalue[ 2 ].value = (SmPointer)( "" );
400  props[ 2 ].name = const_cast< char* >( SmRestartCommand );
401  props[ 2 ].type = const_cast< char* >( SmLISTofARRAY8 );
402  props[ 2 ].num_vals = 1;
403  props[ 2 ].vals = &propvalue[ 2 ];
404  propvalue[ 3 ].length = 0;
405  propvalue[ 3 ].value = tqApp->argv()[ 0 ];
406  props[ 3 ].name = const_cast< char* >( SmProgram );
407  props[ 3 ].type = const_cast< char* >( SmARRAY8 );
408  props[ 3 ].num_vals = 1;
409  props[ 3 ].vals = &propvalue[ 3 ];
410  propvalue[ 4 ].length = 0;
411  propvalue[ 4 ].value = (SmPointer)( "" );
412  props[ 4 ].name = const_cast< char* >( SmCloneCommand );
413  props[ 4 ].type = const_cast< char* >( SmLISTofARRAY8 );
414  props[ 4 ].num_vals = 1;
415  props[ 4 ].vals = &propvalue[ 4 ];
416  SmProp* p[ 5 ] = { &props[ 0 ], &props[ 1 ], &props[ 2 ], &props[ 3 ], &props[ 4 ] };
417  SmcSetProperties( conn, 5, p );
418  notifier = new TQSocketNotifier( IceConnectionNumber( SmcGetIceConnection( conn )),
419  TQSocketNotifier::Read, TQT_TQOBJECT(this) );
420  connect( notifier, TQT_SIGNAL( activated( int )), TQT_SLOT( processData()));
421  }
422 
423 SessionSaveDoneHelper::~SessionSaveDoneHelper()
424  {
425  close();
426  }
427 
428 void SessionSaveDoneHelper::close()
429  {
430  if( conn != NULL )
431  {
432  delete notifier;
433  SmcCloseConnection( conn, 0, NULL );
434  }
435  conn = NULL;
436  }
437 
438 void SessionSaveDoneHelper::processData()
439  {
440  if( conn != NULL )
441  IceProcessMessages( SmcGetIceConnection( conn ), 0, 0 );
442  }
443 
444 } // namespace
445 
446 #include "sm.moc"

twin

Skip menu "twin"
  • Main Page
  • Alphabetical List
  • Class List
  • File List
  • Class Members

twin

Skip menu "twin"
  • kate
  • libkonq
  • twin
  •   lib
Generated for twin by doxygen 1.8.1.2
This website is maintained by Timothy Pearson.