Merge branch 'central-widget'
[krunner.git] / lock / main.cc
blobff8f63a6a43c39df5312339394ccd1b46433ebac
1 /* This file is part of the KDE project
2 Copyright (C) 1999 David Faure
3 Copyright (c) 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 "krunner_interface.h"
33 #include <QList>
35 #include <stdlib.h>
36 #include <stdio.h>
38 #include <X11/Xlib.h>
39 #include <fixx11h.h>
41 bool MyApp::x11EventFilter( XEvent *ev )
43 if (ev->type == XKeyPress || ev->type == ButtonPress)
44 emit activity();
45 else if (ev->type == MotionNotify) {
46 time_t tick = time( 0 );
47 if (tick != lastTick) {
48 lastTick = tick;
49 emit activity();
52 return KApplication::x11EventFilter( ev );
56 // -----------------------------------------------------------------------------
58 int main( int argc, char **argv )
60 KCmdLineArgs::init( argc, argv, "krunner_lock", "krunner", ki18n("KRunner Locker"), "2.0" , ki18n("Session Locker for KRunner"));
62 KCmdLineOptions options;
63 options.add("forcelock", ki18n("Force session locking"));
64 options.add("dontlock", ki18n("Only start screensaver"));
65 options.add("blank", ki18n("Only use the blank screensaver"));
66 KCmdLineArgs::addCmdLineOptions( options );
67 KCmdLineArgs *args = KCmdLineArgs::parsedArgs();
69 putenv(strdup("SESSION_MANAGER="));
71 //KApplication::disableAutoDcopRegistration();
73 int kdesktop_screen_number = 0;
74 int starting_screen = 0;
76 bool child = false;
77 int parent_connection = 0; // socket to the parent saver
78 QList<int> child_sockets;
80 if (KGlobalSettings::isMultiHead())
82 Display *dpy = XOpenDisplay(NULL);
83 if (! dpy) {
84 fprintf(stderr,
85 "%s: FATAL ERROR: couldn't open display '%s'\n",
86 argv[0], XDisplayName(NULL));
87 exit(1);
90 int number_of_screens = ScreenCount(dpy);
91 starting_screen = kdesktop_screen_number = DefaultScreen(dpy);
92 int pos;
93 QByteArray display_name = XDisplayString(dpy);
94 XCloseDisplay(dpy);
95 kDebug() << "screen " << number_of_screens << " " << kdesktop_screen_number << " " << display_name << " " << starting_screen << endl;
96 dpy = 0;
98 if ((pos = display_name.lastIndexOf('.')) != -1)
99 display_name.remove(pos, 10);
101 QString env;
102 if (number_of_screens != 1) {
103 for (int i = 0; i < number_of_screens; i++) {
104 if (i != starting_screen) {
105 int fd[2];
106 if (pipe(fd)) {
107 perror("pipe");
108 break;
110 if (fork() == 0) {
111 child = true;
112 kdesktop_screen_number = i;
113 parent_connection = fd[0];
114 // break here because we are the child process, we don't
115 // want to fork() anymore
116 break;
117 } else {
118 child_sockets.append(fd[1]);
123 env.sprintf("DISPLAY=%s.%d", display_name.data(),
124 kdesktop_screen_number);
125 kDebug() << "env " << env << endl;
127 if (putenv(strdup(env.toLatin1().data()))) {
128 fprintf(stderr,
129 "%s: WARNING: unable to set DISPLAY environment variable\n",
130 argv[0]);
131 perror("putenv()");
136 MyApp app;
137 kDebug() << "app " << kdesktop_screen_number << " " << starting_screen << " " << child << " " << child_sockets.count() << " " << parent_connection << endl;
138 app.disableSessionManagement();
139 KGlobal::locale()->insertCatalog("libworkspace");
141 LockProcess process(child, args->isSet( "blank" ));
142 if (!child)
143 process.setChildren(child_sockets);
144 else
145 process.setParent(parent_connection);
147 bool rt;
148 bool sig = false;
149 if( !child && args->isSet( "forcelock" ))
151 rt = process.lock();
152 sig = true;
154 else if( child || args->isSet( "dontlock" ))
155 rt = process.dontLock();
156 else
157 rt = process.defaultSave();
158 if (!rt)
159 return 1;
161 if( sig )
163 org::freedesktop::ScreenSaver runner("org.freedesktop.ScreenSaver", "/ScreenSaver", QDBusConnection::sessionBus());
164 runner.saverLockReady();
166 args->clear();
167 return app.exec();
170 #include "main.moc"