00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013 #include <kconfig.h>
00014
00015 #include "main.h"
00016
00017 #include <klocale.h>
00018 #include <stdlib.h>
00019 #include <kcmdlineargs.h>
00020 #include <kaboutdata.h>
00021 #include <dcopclient.h>
00022 #include <unistd.h>
00023 #include <signal.h>
00024 #include <fcntl.h>
00025
00026 #include "atoms.h"
00027 #include "options.h"
00028 #include "sm.h"
00029
00030 #define INT8 _X11INT8
00031 #define INT32 _X11INT32
00032 #include <X11/Xproto.h>
00033 #undef INT8
00034 #undef INT32
00035
00036 extern Time qt_x_time;
00037
00038 namespace KWinInternal
00039 {
00040
00041 Options* options;
00042
00043 Atoms* atoms;
00044
00045 int screen_number = -1;
00046
00047 static bool initting = FALSE;
00048
00049 static
00050 int x11ErrorHandler(Display *d, XErrorEvent *e)
00051 {
00052 char msg[80], req[80], number[80];
00053 bool ignore_badwindow = TRUE;
00054
00055 if (initting &&
00056 (
00057 e->request_code == X_ChangeWindowAttributes
00058 || e->request_code == X_GrabKey
00059 )
00060 && (e->error_code == BadAccess))
00061 {
00062 fputs(i18n("kwin: it looks like there's already a window manager running. kwin not started.\n").local8Bit(), stderr);
00063 exit(1);
00064 }
00065
00066 if (ignore_badwindow && (e->error_code == BadWindow || e->error_code == BadColor))
00067 return 0;
00068
00069 XGetErrorText(d, e->error_code, msg, sizeof(msg));
00070 sprintf(number, "%d", e->request_code);
00071 XGetErrorDatabaseText(d, "XRequest", number, "<unknown>", req, sizeof(req));
00072
00073 fprintf(stderr, "kwin: %s(0x%lx): %s\n", req, e->resourceid, msg);
00074
00075 if (initting)
00076 {
00077 fputs(i18n("kwin: failure during initialization; aborting").local8Bit(), stderr);
00078 exit(1);
00079 }
00080 return 0;
00081 }
00082
00083 Application::Application( )
00084 : KApplication( ), owner( screen_number )
00085 {
00086 KCmdLineArgs* args = KCmdLineArgs::parsedArgs();
00087 if (!config()->isImmutable() && args->isSet("lock"))
00088 {
00089 config()->setReadOnly(true);
00090 config()->reparseConfiguration();
00091 }
00092
00093 if (screen_number == -1)
00094 screen_number = DefaultScreen(qt_xdisplay());
00095
00096 if( !owner.claim( args->isSet( "replace" ), true ))
00097 {
00098 fputs(i18n("kwin: unable to claim manager selection, another wm running? (try using --replace)\n").local8Bit(), stderr);
00099 ::exit(1);
00100 }
00101 connect( &owner, SIGNAL( lostOwnership()), SLOT( lostSelection()));
00102
00103 initting = TRUE;
00104
00105
00106 XSetErrorHandler( x11ErrorHandler );
00107
00108
00109 XSelectInput(qt_xdisplay(), qt_xrootwin(), SubstructureRedirectMask );
00110 syncX();
00111
00112 options = new Options;
00113 atoms = new Atoms;
00114
00115
00116 (void) new Workspace( isSessionRestored() );
00117
00118 syncX();
00119
00120 initting = FALSE;
00121 dcopClient()->send( "ksplash", "", "upAndRunning(QString)", QString("wm started"));
00122 }
00123
00124
00125 Application::~Application()
00126 {
00127 delete Workspace::self();
00128 if( owner.ownerWindow() != None )
00129 XSetInputFocus( qt_xdisplay(), PointerRoot, RevertToPointerRoot, qt_x_time );
00130 delete options;
00131 }
00132
00133 void Application::lostSelection()
00134 {
00135 delete Workspace::self();
00136
00137 XSelectInput(qt_xdisplay(), qt_xrootwin(), PropertyChangeMask );
00138 quit();
00139 }
00140
00141 bool Application::x11EventFilter( XEvent *e )
00142 {
00143 if ( Workspace::self()->workspaceEvent( e ) )
00144 return TRUE;
00145 return KApplication::x11EventFilter( e );
00146 }
00147
00148 static void sighandler(int)
00149 {
00150 QApplication::exit();
00151 }
00152
00153
00154 }
00155
00156 static const char version[] = "3.0";
00157 static const char description[] = I18N_NOOP( "The KDE window manager." );
00158
00159 static KCmdLineOptions args[] =
00160 {
00161 { "lock", I18N_NOOP("Disable configuration options"), 0 },
00162 { "replace", I18N_NOOP("Replace already running ICCCM2.0 compliant window manager."), 0 },
00163 KCmdLineLastOption
00164 };
00165
00166 extern "C"
00167 int kdemain( int argc, char * argv[] )
00168 {
00169 bool restored = false;
00170 for (int arg = 1; arg < argc; arg++)
00171 {
00172 if (! qstrcmp(argv[arg], "-session"))
00173 {
00174 restored = true;
00175 break;
00176 }
00177 }
00178
00179 if (! restored)
00180 {
00181
00182
00183
00184 QCString multiHead = getenv("KDE_MULTIHEAD");
00185 if (multiHead.lower() == "true")
00186 {
00187
00188 Display* dpy = XOpenDisplay( NULL );
00189 if ( !dpy )
00190 {
00191 fprintf(stderr, "%s: FATAL ERROR while trying to open display %s\n",
00192 argv[0], XDisplayName(NULL ) );
00193 exit (1);
00194 }
00195
00196 int number_of_screens = ScreenCount( dpy );
00197 KWinInternal::screen_number = DefaultScreen( dpy );
00198 int pos;
00199 QCString display_name = XDisplayString( dpy );
00200 XCloseDisplay( dpy );
00201 dpy = 0;
00202
00203 if ((pos = display_name.findRev('.')) != -1 )
00204 display_name.remove(pos,10);
00205
00206 QCString envir;
00207 if (number_of_screens != 1)
00208 {
00209 for (int i = 0; i < number_of_screens; i++ )
00210 {
00211
00212
00213 if ( i != KWinInternal::screen_number && fork() == 0 )
00214 {
00215 KWinInternal::screen_number = i;
00216
00217
00218 break;
00219 }
00220 }
00221
00222
00223 envir.sprintf("DISPLAY=%s.%d", display_name.data(), KWinInternal::screen_number);
00224
00225 if (putenv( strdup(envir.data())) )
00226 {
00227 fprintf(stderr,
00228 "%s: WARNING: unable to set DISPLAY environment variable\n",
00229 argv[0]);
00230 perror("putenv()");
00231 }
00232 }
00233 }
00234 }
00235
00236 KAboutData aboutData( "kwin", I18N_NOOP("KWin"),
00237 version, description, KAboutData::License_GPL,
00238 I18N_NOOP("(c) 1999-2003, The KDE Developers"));
00239 aboutData.addAuthor("Matthias Ettrich",0, "ettrich@kde.org");
00240 aboutData.addAuthor("Cristian Tibirna",0, "tibirna@kde.org");
00241 aboutData.addAuthor("Daniel M. Duley",0, "mosfet@kde.org");
00242 aboutData.addAuthor("Lubos Lunak", 0, "l.lunak@kde.org");
00243
00244 KCmdLineArgs::init(argc, argv, &aboutData);
00245 KCmdLineArgs::addCmdLineOptions( args );
00246
00247 if (signal(SIGTERM, KWinInternal::sighandler) == SIG_IGN)
00248 signal(SIGTERM, SIG_IGN);
00249 if (signal(SIGINT, KWinInternal::sighandler) == SIG_IGN)
00250 signal(SIGINT, SIG_IGN);
00251 if (signal(SIGHUP, KWinInternal::sighandler) == SIG_IGN)
00252 signal(SIGHUP, SIG_IGN);
00253
00254 KApplication::disableAutoDcopRegistration();
00255 KWinInternal::Application a;
00256 KWinInternal::SessionManaged weAreIndeed;
00257 KWinInternal::SessionSaveDoneHelper helper;
00258
00259 fcntl(ConnectionNumber(qt_xdisplay()), F_SETFD, 1);
00260
00261 QCString appname;
00262 if (KWinInternal::screen_number == 0)
00263 appname = "kwin";
00264 else
00265 appname.sprintf("kwin-screen-%d", KWinInternal::screen_number);
00266
00267 DCOPClient* client = a.dcopClient();
00268 client->registerAs( appname.data(), false);
00269 client->setDefaultObject( "KWinInterface" );
00270
00271 return a.exec();
00272 }
00273
00274 #include "main.moc"