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[])
|
|
|
|
{
|
2008-04-21 16:11:21 -06:00
|
|
|
Display *display = NULL;
|
|
|
|
int screen;
|
|
|
|
int bigwindow = 0;
|
2008-04-10 14:26:41 -06:00
|
|
|
|
2008-04-21 16:11:21 -06:00
|
|
|
if ((argc > 1) && (0 == (strcmp(argv[1], "-w")))) {
|
|
|
|
bigwindow = 1;
|
|
|
|
}
|
|
|
|
if (argc - bigwindow < 2) {
|
|
|
|
(void)fprintf(stderr, "Usage: %s [-w] PROGRAM [ARGUMENT ...]\n", argv[0]);
|
|
|
|
(void)fprintf(stderr, "\n");
|
|
|
|
(void)fprintf(stderr, "-w Map X server window, export $XSS_WINDOW. This window\n");
|
|
|
|
(void)fprintf(stderr, " will be unmapped by any keypress or mouse movement.\n");
|
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 {
|
2008-04-21 16:11:21 -06:00
|
|
|
int ss_event, ss_error;
|
|
|
|
XEvent event;
|
|
|
|
Window root;
|
|
|
|
char *nargv[argc+1];
|
|
|
|
char id[50];
|
|
|
|
int i;
|
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 16:11:21 -06:00
|
|
|
XSetWindowAttributes wa;
|
|
|
|
XScreenSaverInfo info;
|
|
|
|
Pixmap pmap;
|
|
|
|
XColor black;
|
|
|
|
|
|
|
|
pmap = XCreateBitmapFromData(display, root, "\0", 1, 1);
|
|
|
|
black.pixel = BlackPixel(display, screen);
|
|
|
|
wa.cursor = XCreatePixmapCursor(display, pmap, pmap, &black, &black, 0, 0);
|
|
|
|
wa.background_pixel = BlackPixel(display, screen);
|
|
|
|
if (! (XFreePixmap(display, pmap))) break;
|
2008-04-18 11:56:30 -06:00
|
|
|
XScreenSaverSetAttributes(display, root,
|
2008-04-21 16:11:21 -06:00
|
|
|
bigwindow?0:-1, bigwindow?0:-1,
|
|
|
|
bigwindow?DisplayWidth(display, screen):1,
|
|
|
|
bigwindow?DisplayHeight(display, screen):1,
|
2008-04-18 11:56:30 -06:00
|
|
|
0,
|
|
|
|
CopyFromParent, CopyFromParent,
|
|
|
|
CopyFromParent,
|
2008-04-21 16:11:21 -06:00
|
|
|
CWBackPixel | CWCursor,
|
|
|
|
&wa);
|
|
|
|
XScreenSaverQueryInfo(display, (Drawable)root, &info);
|
|
|
|
(void)snprintf(id, sizeof(id), "0x%lx", (unsigned long)info.window);
|
|
|
|
(void)setenv("XSS_WINDOW", id, 1);
|
2008-04-18 11:56:30 -06:00
|
|
|
}
|
|
|
|
|
2008-04-21 16:11:21 -06:00
|
|
|
for (i = 0; i < argc; i += 1) {
|
|
|
|
if (bigwindow && (0 == strcmp(argv[i], "XSS_WINDOW"))) {
|
|
|
|
nargv[i] = id;
|
|
|
|
} else {
|
|
|
|
nargv[i] = argv[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
nargv[argc] = NULL;
|
2008-04-18 11:56:30 -06:00
|
|
|
|
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) {
|
2008-04-21 16:11:21 -06:00
|
|
|
(void)execvp(nargv[1+bigwindow], nargv+1+bigwindow);
|
2008-04-10 14:26:41 -06:00
|
|
|
perror("exec");
|
|
|
|
exit(1);
|
|
|
|
}
|
2008-04-21 16:11:21 -06:00
|
|
|
} else {
|
2008-04-10 14:26:41 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} while (0);
|
|
|
|
|
|
|
|
if (display) {
|
2008-04-21 16:11:21 -06:00
|
|
|
(void)XScreenSaverUnregister(display, screen);
|
2008-04-10 14:26:41 -06:00
|
|
|
(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;
|
|
|
|
}
|