avoiding knotify during KDE startup

Lubos Lunak <[email protected]>
Newsgroups gmane.comp.kde.devel.optimize
Message-ID <[email protected]>
 Hello,

 another fraction of KDE startup time saved, 11s->10s (again hot caches, 
3xKosole,Klipper).

 If one turns off the KDE startup sound, KNotify actually doesn't need to be 
started during KDE startup, because there seem to be no other events, except 
for few KWin events which don't have any representation by default. So the 
patches change KNotifyClient to check ksmserver and kwin events if they have 
representation, and if not, this none presentation is already handled by 
KNotifyClient itself.

 The KNotifyDialog change checks if the ksmserver event is disabled, and if 
yes, turns off starting knotify in startkde. Hmmmm, although thinking of this 
right now, perhaps knotify startup could be simply always avoided in 
startkde? KNotifyClient starts the daemon anyway if necessary, and I doubt 
anybody would notice the delay caused by this with the startkde sound.

 Comments?

-- 
Lubos Lunak
KDE developer
---------------------------------------------------------------------
SuSE CR, s.r.o.  e-mail: [email protected] , [email protected]
Drahobejlova 27  tel: +420 2 9654 2373
190 00 Praha 9   fax: +420 2 9654 2374
Czech Republic   http://www.suse.cz/

_______________________________________________
Kde-optimize mailing list
[email protected]
https://mail.kde.org/mailman/listinfo/kde-optimize
knotifyclient.cpp.patch (text/x-diff, 2.5 KB)
--- knotifyclient.cpp.sav	2004-01-07 10:46:51.000000000 +0100
+++ knotifyclient.cpp	2004-09-14 22:14:20.730364480 +0200
@@ -33,6 +33,40 @@
 
 static const char daemonName[] = "knotify";
 
+static bool canAvoidStartupEvent( const QString& event, const QString& appname, int present )
+{
+    static int checkAvoid = -1;
+    if(( appname != "kwin" && appname != "ksmserver" ) || present > 0 ) {
+        checkAvoid = 0;
+        return false;
+    }
+    if( checkAvoid == -1 ) {
+        KConfig cfg( "knotifyrc", true );
+        cfg.setGroup( "Startup" );
+        checkAvoid = cfg.readBoolEntry( "DelayedStartup", false ) ? 1 : 0;
+    }
+    if( checkAvoid != 1 )
+        return false;
+    // startkde event is in global events file
+    static KConfig* configfile = appname != "ksmserver"
+        ? new KConfig( appname + ".eventsrc", true, false )
+        : new KConfig( "knotify.eventsrc", true, false );
+    static KConfig* eventsfile = appname != "ksmserver"
+        ? new KConfig( appname + "/eventsrc", true, false, "data" )
+        : new KConfig( "knotify/eventsrc", true, false, "data" );
+    configfile->setGroup( event );
+    eventsfile->setGroup( event );
+    int ev1 = configfile->readNumEntry( "presentation", -2 );
+    int ev2 = eventsfile->readNumEntry( "default_presentation", -2 );
+    if(( ev1 == -2 && ev2 == -2 ) // unknown
+        || ev1 > 0 // configured to have presentation
+        || ( ev1 == -2 && ev2 > 0 )) { // not configured, has default presentation
+        checkAvoid = 0;
+        return false;
+    }
+    return true;
+}
+
 static int sendNotifyEvent(const QString &message, const QString &text,
                             int present, int level, const QString &sound,
                             const QString &file, int winId )
@@ -47,8 +81,13 @@ static int sendNotifyEvent(const QString
       return 0;
   }
 
+  QString appname = KNotifyClient::instance()->instanceName();
+
   int uniqueId = kMax( 1, kapp->random() ); // must not be 0 -- means failure!
 
+  if( canAvoidStartupEvent( message, appname, present ))
+      return uniqueId; // done "successfully" - there will be no event presentation
+
   // knotify daemon needs toplevel window
   QWidget* widget = QWidget::find( winId );
   if( widget )
@@ -56,7 +95,6 @@ static int sendNotifyEvent(const QString
 
   QByteArray data;
   QDataStream ds(data, IO_WriteOnly);
-  QString appname = KNotifyClient::instance()->instanceName();
   ds << message << appname << text << sound << file << present << level
      << winId << uniqueId;
knotify.cpp.patch (text/x-diff, 1.8 KB)
--- knotify.cpp.sav	2004-02-17 22:46:59.000000000 +0100
+++ knotify.cpp	2004-09-14 17:31:12.852914400 +0200
@@ -82,6 +82,9 @@ public:
     int volume;
     QTimer *playTimer;
     KAudioManagerPlay *audioManager;
+    bool wasStartupEvent;
+    bool inStartup;
+    QString startupEvents;
 };
 
 // Yes, it's ugly to put this here, but this facilitates the cautious startup
@@ -221,6 +224,8 @@ KNotify::KNotify( bool useArts )
     d->useArts = useArts;
     d->playObjects.setAutoDelete(true);
     d->audioManager = 0;
+    d->wasStartupEvent = false;
+    d->inStartup = true;
     if( useArts )
     {
         connect( soundServer, SIGNAL( restartedServer() ), this, SLOT( restartedArtsd() ) );
@@ -303,7 +308,11 @@ void KNotify::notify(const QString &even
                      int present, int level, int winId, int eventId )
 {
     // kdDebug() << "event=" << event << " fromApp=" << fromApp << " text=" << text << " sound=" << sound <<
-    //    " file=" << file << " present=" << present << " level=" << level <<  " winId=" << winId << " eventId=" << eventId endl;
+    //    " file=" << file << " present=" << present << " level=" << level <<  " winId=" << winId << " eventId=" << eventId << endl;
+    if( d->inStartup ) {
+        d->wasStartupEvent = true;
+        d->startupEvents += "(" + event + ":" + fromApp + ")";
+    }
 
     QString commandline;
 
@@ -746,4 +755,14 @@ void KNotify::restartedArtsd()
     d->audioManager->setAutoRestoreID( "KNotify Aman Play" );
 }
 
+void KNotify::sessionReady()
+{
+    if( d->inStartup && d->wasStartupEvent ) {
+        KConfigGroupSaver s( KGlobal::config(), "Startup" );
+        if( KGlobal::config()->readBoolEntry( "DelayedStartup", false ))
+            kdWarning() << "There were knotify events while startup:" << d->startupEvents << endl;
+    }
+    d->inStartup = false;
+}
+
 // vim: sw=4 sts=4 ts=8 et
knotifydialog.cpp.patch (text/x-diff, 1.9 KB)
--- knotifydialog.cpp.sav	2004-03-29 12:26:35.000000000 +0200
+++ knotifydialog.cpp	2004-09-14 21:59:20.629200656 +0200
@@ -821,9 +821,42 @@ void KNotifyWidget::save()
         kapp->dcopClient()->send("knotify", "", "reconfigure()", "");
     }
 
+    checkDelayedStartup();
+
     emit changed( false );
 }
 
+static bool checkPresentation( KConfig& config, KConfig& events, const char* event )
+{
+    // default value is 1, so that unknown events are not optimized
+    config.setGroup( event );
+    events.setGroup( event );
+    int ev1 = config.readNumEntry( "presentation", -2 );
+    int ev2 = events.readNumEntry( "default_presentation", -2 );
+    if(( ev1 == -2 && ev2 == -2 ) // unknown
+        || ev1 > 0 // configured to have presentation
+        || ( ev1 == -2 && ev2 > 0 )) // not configured, has default presentation
+        return true;
+    return false;
+}
+
+void KNotifyWidget::checkDelayedStartup()
+{
+    KConfig config_ksmserver( "knotify.eventsrc", true, false );
+    KConfig events_ksmserver( "knotify/eventsrc", true, false, "data" );
+//    KConfig config_kwin( "kwin.eventsrc", true, false );
+//    KConfig events_kwin( "kwin/eventsrc", true, false, "data" );
+    bool can_delay = ( !checkPresentation( config_ksmserver, events_ksmserver, "startkde" ));
+    // Most kwin events have no presentation by default, so those will be taken
+    // take of by code in KNotifyClient, as if an event has presentation, knotify
+    // will be loaded, and the event will have a small delay, but that won't be really
+    // noticeable with kwin events during KDE startup.
+//        && checkPresentation( config_kwin, events_kwin, "" ));
+    KConfig cfg( "knotifyrc" );
+    cfg.setGroup( "Startup" );
+    cfg.writeEntry( "DelayedStartup", can_delay );
+}
+
 // returns e.g. "kwin/eventsrc" from a given path
 // "/opt/kde3/share/apps/kwin/eventsrc"
 QString KNotifyWidget::makeRelative( const QString& fullPath )
knotifydialog.h.patch (text/x-diff, 439 B)
--- knotifydialog.h.sav	2004-05-27 17:31:26.000000000 +0200
+++ knotifydialog.h	2004-09-14 18:15:39.804476136 +0200
@@ -276,6 +276,7 @@ namespace KNotify
         void widgetChanged( QListViewItem *item,
                             int what, bool on, QWidget *buddy = 0L );
         void selectItem( QListViewItem *item );
+        void checkDelayedStartup();
 
         ApplicationList m_visibleApps;
         ApplicationList m_allApps;
knotify.h.patch (text/x-diff, 327 B)
--- knotify.h.sav	2003-09-24 13:09:14.000000000 +0200
+++ knotify.h	2004-09-14 17:07:44.068082248 +0200
@@ -67,6 +67,7 @@ k_dcop:
 
 	void reconfigure();
 	void setVolume( int volume );
+        void sessionReady(); // from ksmserver
 
 private:
 	bool notifyBySound(const QString &sound, const QString &appname, int eventId);
server.cpp.patch (text/x-diff, 454 B)
--- server.cpp.sav	2004-09-14 12:17:14.000000000 +0200
+++ server.cpp	2004-09-14 17:06:17.698212464 +0200
@@ -1535,7 +1535,8 @@ void KSMServer::restoreSessionDoneIntern
     restoreLegacySession( KGlobal::config());
 #endif
     upAndRunning( "session ready" );
-    
+    DCOPRef( "knotify" ).send( "sessionReady" ); // knotify startup optimization
+
     // From now on handle X errors as normal shutdown.
     XSetIOErrorHandler(Xio_ErrorHandler);
 }
startkde.patch (text/x-diff, 629 B)
--- startkde.sav	2004-09-14 13:35:01.000000000 +0200
+++ startkde	2004-09-14 18:06:03.987013696 +0200
@@ -203,9 +203,14 @@ else
     ksplash --nodcop
 fi
 
+knotify='+knotify'
+if kreadconfig --file knotifyrc --group Startup --key DelayedStartup --default false --type bool; then
+    knotify=
+fi
+
 # We set LD_BIND_NOW to increase the efficiency of kdeinit.
 # kdeinit unsets this variable before loading applications.
-LD_BIND_NOW=true kdeinit +kcminit +knotify
+LD_BIND_NOW=true kdeinit +kcminit $knotify
 if test $? -ne 0; then
   # Startup error
   echo 'startkde: Could not start kdeinit. Check your installation.'  1>&2
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.