xss/xss.c

141 lines
4.3 KiB
C
Raw Normal View History

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>
#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;
int screen;
int bigwindow = 0;
2008-04-10 14:26:41 -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 {
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");
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
}
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);
{
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;
XScreenSaverSetAttributes(display, root,
bigwindow?0:-1, bigwindow?0:-1,
bigwindow?DisplayWidth(display, screen):1,
bigwindow?DisplayHeight(display, screen):1,
0,
CopyFromParent, CopyFromParent,
CopyFromParent,
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);
}
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-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(nargv[1+bigwindow], nargv+1+bigwindow);
2008-04-10 14:26:41 -06:00
perror("exec");
exit(1);
}
} else {
2008-04-10 14:26:41 -06:00
}
}
}
}
} while (0);
if (display) {
(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;
}