2008-04-10 14:59:57 -06:00
|
|
|
/* xss -- xautolock replacement using MIT-SCREEN-SAVER (it doesn't poll)
|
|
|
|
* Copyright (C) 2008 Neale Pickett <neale@woozle.org>
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or (at
|
|
|
|
* your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2008-04-10 14:26:41 -06:00
|
|
|
#include <sys/wait.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
#include <X11/Xlib.h>
|
2008-04-18 11:43:12 -06:00
|
|
|
#include <X11/Xatom.h>
|
2008-04-10 14:26:41 -06:00
|
|
|
#include <X11/extensions/scrnsaver.h>
|
|
|
|
|
2008-04-11 16:49:42 -06:00
|
|
|
#include "obj.h"
|
|
|
|
|
2008-04-10 14:26:41 -06:00
|
|
|
int child = 0;
|
|
|
|
|
|
|
|
void
|
|
|
|
sigchld(int signum)
|
|
|
|
{
|
2008-04-10 14:59:57 -06:00
|
|
|
(void)waitpid(-1, NULL, WNOHANG);
|
2008-04-10 14:26:41 -06:00
|
|
|
child = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
Display *display = NULL;
|
|
|
|
|
|
|
|
if (argc < 2) {
|
2008-04-15 17:18:35 -06:00
|
|
|
(void)fprintf(stderr, "Usage: %s PROGRAM [ARGUMENT ...]\n", argv[0]);
|
2008-04-11 16:49:42 -06:00
|
|
|
return 64; /* EX_USAGE */
|
2008-04-10 14:26:41 -06:00
|
|
|
}
|
2008-04-10 14:59:57 -06:00
|
|
|
signal(SIGCHLD, sigchld);
|
2008-04-10 14:26:41 -06:00
|
|
|
|
2008-04-11 16:49:42 -06:00
|
|
|
try {
|
|
|
|
int ss_event, ss_error;
|
2008-04-18 11:43:12 -06:00
|
|
|
int screen;
|
2008-04-11 16:49:42 -06:00
|
|
|
XEvent event;
|
2008-04-18 11:43:12 -06:00
|
|
|
Window root;
|
2008-04-11 16:49:42 -06:00
|
|
|
|
|
|
|
if (! (display = XOpenDisplay(NULL))) raise("cannot open display");
|
2008-04-18 11:43:12 -06:00
|
|
|
screen = DefaultScreen(display);
|
2008-04-10 14:26:41 -06:00
|
|
|
if (! XScreenSaverQueryExtension(display, &ss_event, &ss_error)) {
|
2008-04-11 16:49:42 -06:00
|
|
|
raise("X server does not support MIT-SCREEN-SAVER extension.");
|
2008-04-10 14:26:41 -06:00
|
|
|
}
|
2008-04-18 11:43:12 -06:00
|
|
|
if (! XScreenSaverRegister(display, screen, (XID)getpid(), XA_INTEGER)) {
|
|
|
|
raise("cannot register screen saver, is another one already running?");
|
|
|
|
}
|
|
|
|
root = RootWindow(display, screen);
|
|
|
|
XScreenSaverSelectInput(display, root, ScreenSaverNotifyMask);
|
2008-04-18 11:56:30 -06:00
|
|
|
|
2008-04-21 11:20:10 -06:00
|
|
|
/* Tell X to show its provided window off the screen. We make our own. */
|
2008-04-18 11:56:30 -06:00
|
|
|
{
|
|
|
|
XSetWindowAttributes attr;
|
|
|
|
|
|
|
|
attr.background_pixel = BlackPixel(display, screen);
|
|
|
|
XScreenSaverSetAttributes(display, root,
|
|
|
|
-1, -1,
|
|
|
|
1, 1,
|
|
|
|
0,
|
|
|
|
CopyFromParent, CopyFromParent,
|
|
|
|
CopyFromParent,
|
|
|
|
CWBackPixel,
|
|
|
|
&attr);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-04-10 14:59:57 -06:00
|
|
|
while (! XNextEvent(display, &event)) {
|
2008-04-15 17:18:35 -06:00
|
|
|
if (ss_event == event.type) {
|
2008-04-10 14:59:57 -06:00
|
|
|
XScreenSaverNotifyEvent *sevent = (XScreenSaverNotifyEvent *)&event;
|
2008-04-10 14:26:41 -06:00
|
|
|
|
|
|
|
if (ScreenSaverOn == sevent->state) {
|
2008-04-10 14:59:57 -06:00
|
|
|
if (! child) {
|
2008-04-10 14:26:41 -06:00
|
|
|
child = fork();
|
|
|
|
if (0 == child) {
|
|
|
|
(void)execvp(argv[1], argv + 1);
|
|
|
|
perror("exec");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} while (0);
|
|
|
|
|
|
|
|
if (display) {
|
|
|
|
(void)XCloseDisplay(display);
|
|
|
|
}
|
2008-04-11 16:49:42 -06:00
|
|
|
|
|
|
|
except {
|
2008-04-15 17:18:35 -06:00
|
|
|
(void)fprintf(stderr, "Error: %s\n", exception);
|
2008-04-11 16:49:42 -06:00
|
|
|
return 69; /* EX_UNAVAILABLE */
|
2008-04-10 14:26:41 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|