add more spacing
[personal-kdebase.git] / workspace / krunner / lock / main.cc
blob89746f069d2a23965eb7c4bb11877f1c4262d4ba
1 /* This file is part of the KDE project
2 Copyright (C) 1999 David Faure
3 Copyright 2003 Oswald Buddenhagen <ossi@kde.org>
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public
7 License as published by the Free Software Foundation; either
8 version 2 of the License, or (at your option) any later version.
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public License
16 along with this library; see the file COPYING.LIB. If not, write to
17 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
21 #include "lockprocess.h"
22 #include "main.h"
23 #include "kscreensaversettings.h"
25 #include <kcmdlineargs.h>
26 #include <klocale.h>
27 #include <kglobal.h>
28 #include <kdebug.h>
29 #include <kglobalsettings.h>
30 #include <QtDBus/QtDBus>
31 #include "kscreensaver_interface.h"
33 #include <QList>
35 #include <stdlib.h>
36 #include <stdio.h>
37 #include <unistd.h>
39 #include <X11/Xlib.h>
40 #include <fixx11h.h>
42 bool MyApp::x11EventFilter( XEvent *ev )
44 if (ev->type == XKeyPress || ev->type == ButtonPress)
45 emit activity();
46 else if (ev->type == MotionNotify) {
47 time_t tick = time( 0 );
48 if (tick != lastTick) {
49 lastTick = tick;
50 emit activity();
53 return KApplication::x11EventFilter( ev );
57 // -----------------------------------------------------------------------------
59 int main( int argc, char **argv )
61 KCmdLineArgs::init(argc, argv, "kscreenlocker", "krunner", ki18n("KDE Screen Locker"),
62 "2.0" , ki18n("Session Locker for KDE Workspace"));
64 KCmdLineOptions options;
65 options.add("forcelock", ki18n("Force session locking"));
66 options.add("dontlock", ki18n("Only start screen saver"));
67 options.add("blank", ki18n("Only use the blank screen saver"));
68 options.add("plasmasetup", ki18n("start with plasma unlocked for configuring"));
69 KCmdLineArgs::addCmdLineOptions( options );
70 KCmdLineArgs *args = KCmdLineArgs::parsedArgs();
72 putenv(strdup("SESSION_MANAGER="));
74 //KApplication::disableAutoDcopRegistration();
76 int kdesktop_screen_number = 0;
77 int starting_screen = 0;
79 bool child = false;
80 int parent_connection = 0; // socket to the parent saver
81 QList<int> child_sockets;
83 if (KGlobalSettings::isMultiHead())
85 Display *dpy = XOpenDisplay(NULL);
86 if (! dpy) {
87 fprintf(stderr,
88 "%s: FATAL ERROR: could not open display '%s'\n",
89 argv[0], XDisplayName(NULL));
90 exit(1);
93 int number_of_screens = ScreenCount(dpy);
94 starting_screen = kdesktop_screen_number = DefaultScreen(dpy);
95 int pos;
96 QByteArray display_name = XDisplayString(dpy);
97 XCloseDisplay(dpy);
98 kDebug() << "screen " << number_of_screens << " " << kdesktop_screen_number << " " << display_name << " " << starting_screen;
99 dpy = 0;
101 if ((pos = display_name.lastIndexOf('.')) != -1)
102 display_name.remove(pos, 10);
104 QString env;
105 if (number_of_screens != 1) {
106 for (int i = 0; i < number_of_screens; i++) {
107 if (i != starting_screen) {
108 int fd[2];
109 if (pipe(fd)) {
110 perror("pipe");
111 break;
113 if (fork() == 0) {
114 child = true;
115 kdesktop_screen_number = i;
116 parent_connection = fd[0];
117 // break here because we are the child process, we don't
118 // want to fork() anymore
119 break;
120 } else {
121 child_sockets.append(fd[1]);
126 env.sprintf("DISPLAY=%s.%d", display_name.data(),
127 kdesktop_screen_number);
128 kDebug() << "env " << env;
130 if (putenv(strdup(env.toLatin1().data()))) {
131 fprintf(stderr,
132 "%s: WARNING: unable to set DISPLAY environment variable\n",
133 argv[0]);
134 perror("putenv()");
139 MyApp app;
140 kDebug() << "app " << kdesktop_screen_number << " " << starting_screen << " " << child << " " << child_sockets.count() << " " << parent_connection;
141 app.disableSessionManagement();
142 KGlobal::locale()->insertCatalog("libkworkspace");
144 LockProcess process(child, args->isSet("blank"));
145 if (!child)
146 process.setChildren(child_sockets);
147 else
148 process.setParent(parent_connection);
150 bool rt;
151 bool sig = false;
152 if (!child && (args->isSet("forcelock"))) {
153 rt = process.lock();
154 sig = true;
156 else if( child || args->isSet( "dontlock" ))
157 rt = process.dontLock();
158 else if (args->isSet("plasmasetup")) {
159 rt = process.startSetup();
161 else
162 rt = process.defaultSave();
163 if (!rt)
164 return 1;
166 if( sig )
168 org::kde::screensaver kscreensaver("org.kde.screensaver", "/ScreenSaver", QDBusConnection::sessionBus());
169 kscreensaver.saverLockReady();
171 args->clear();
172 return app.exec();
175 #include "main.moc"