xssstate

a simple utility to get the X screensaver state
git clone git://git.suckless.org/xssstate
Log | Files | Refs | README | LICENSE

commit 89cf5792ca6104b5afe150d02813b3f259ca61bd
Author: Christoph Lohmann <20h@r-36.net>
Date:   Fri,  7 Dec 2012 11:32:02 +0100

Initial commit of xgetidle.

Diffstat:
ALICENSE | 22++++++++++++++++++++++
AMakefile | 56++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Aarg.h | 55+++++++++++++++++++++++++++++++++++++++++++++++++++++++
Aconfig.mk | 25+++++++++++++++++++++++++
Axgetidle.1 | 27+++++++++++++++++++++++++++
Axgetidle.c | 68++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
6 files changed, 253 insertions(+), 0 deletions(-)

diff --git a/LICENSE b/LICENSE @@ -0,0 +1,22 @@ +MIT/X Consortium License + +© 2008 Kai Hendry <hendry@webconverger.com> +© 2012 Christoph Lohmann <20h@r-36.net> + +Permission is hereby granted, free of charge, to any person obtaining a +copy of this software and associated documentation files (the "Software"), +to deal in the Software without restriction, including without limitation +the rights to use, copy, modify, merge, publish, distribute, sublicense, +and/or sell copies of the Software, and to permit persons to whom the +Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +DEALINGS IN THE SOFTWARE. diff --git a/Makefile b/Makefile @@ -0,0 +1,56 @@ +# xgetidle – display the X idle time +# See LICENSE file for copyright and license details. + +include config.mk + +SRC = xgetidle.c +OBJ = ${SRC:.c=.o} + +all: options xgetidle + +options: + @echo xgetidle build options: + @echo "CFLAGS = ${CFLAGS}" + @echo "LDFLAGS = ${LDFLAGS}" + @echo "CC = ${CC}" + +.c.o: + @echo CC $< + @${CC} -c ${CFLAGS} $< + +${OBJ}: config.mk + +xgetidle: xgetidle.o + @echo CC -o $@ + @${CC} -o $@ xgetidle.o ${LDFLAGS} + +clean: + @echo cleaning + @rm -f xgetidle ${OBJ} xgetidle-${VERSION}.tar.gz + +dist: clean + @echo creating dist tarball + @mkdir -p xgetidle-${VERSION} + @cp -R LICENSE Makefile README config.mk \ + xgetidle.1 arg.h ${SRC} xgetidle-${VERSION} + @tar -cf xgetidle-${VERSION}.tar xgetidle-${VERSION} + @gzip xgetidle-${VERSION}.tar + @rm -rf xgetidle-${VERSION} + +install: all + @echo installing executable file to ${DESTDIR}${PREFIX}/bin + @mkdir -p ${DESTDIR}${PREFIX}/bin + @cp -f xgetidle ${DESTDIR}${PREFIX}/bin + @chmod 755 ${DESTDIR}${PREFIX}/bin/xgetidle + @echo installing manual page to ${DESTDIR}${MANPREFIX}/man1 + @mkdir -p ${DESTDIR}${MANPREFIX}/man1 + @sed "s/VERSION/${VERSION}/g" < xgetidle.1 > ${DESTDIR}${MANPREFIX}/man1/xgetidle.1 + @chmod 644 ${DESTDIR}${MANPREFIX}/man1/xgetidle.1 + +uninstall: + @echo removing executable file from ${DESTDIR}${PREFIX}/bin + @rm -f ${DESTDIR}${PREFIX}/bin/xgetidle + @echo removing manual page from ${DESTDIR}${MANPREFIX}/man1 + @rm -f ${DESTDIR}${MANPREFIX}/man1/xgetidle.1 + +.PHONY: all options clean dist install uninstall diff --git a/arg.h b/arg.h @@ -0,0 +1,55 @@ +/* + * Copy me if you can. + * by 20h + */ + +#ifndef __ARG_H__ +#define __ARG_H__ + +extern char *argv0; + +#define USED(x) ((void)(x)) + +/* use main(int argc, char *argv[]) */ +#define ARGBEGIN for (argv0 = *argv, argv++, argc--;\ + argv[0] && argv[0][1]\ + && argv[0][0] == '-';\ + argc--, argv++) {\ + char _argc;\ + char **_argv;\ + int brk;\ + if (argv[0][1] == '-' && argv[0][2] == '\0') {\ + argv++;\ + argc--;\ + break;\ + }\ + for (brk = 0, argv[0]++, _argv = argv;\ + argv[0][0] && !brk;\ + argv[0]++) {\ + if (_argv != argv)\ + break;\ + _argc = argv[0][0];\ + switch (_argc) + +#define ARGEND }\ + USED(_argc);\ + }\ + USED(argv);\ + USED(argc); + +#define ARGC() _argc + +#define EARGF(x) ((argv[0][1] == '\0' && argv[1] == NULL)?\ + ((x), abort(), (char *)0) :\ + (brk = 1, (argv[0][1] != '\0')?\ + (&argv[0][1]) :\ + (argc--, argv++, argv[0]))) + +#define ARGF() ((argv[0][1] == '\0' && argv[1] == NULL)?\ + (char *)0 :\ + (brk = 1, (argv[0][1] != '\0')?\ + (&argv[0][1]) :\ + (argc--, argv++, argv[0]))) + +#endif + diff --git a/config.mk b/config.mk @@ -0,0 +1,25 @@ +# xgetidle version +VERSION = 1.0 + +# Customize below to fit your system + +# paths +PREFIX = /usr/local +MANPREFIX = ${PREFIX}/share/man + +# includes and libs +INCS = -I. -I/usr/include +LIBS = -L/usr/lib -lc -lX11 -lXss + +# flags +CPPFLAGS = -DVERSION=\"${VERSION}\" +CFLAGS = -g -std=c99 -pedantic -Wall -O0 ${INCS} ${CPPFLAGS} +LDFLAGS = -g ${LIBS} + +# Solaris +#CFLAGS = -fast ${INCS} -DVERSION=\"${VERSION}\" +#LDFLAGS = ${LIBS} + +# compiler and linker +CC = cc + diff --git a/xgetidle.1 b/xgetidle.1 @@ -0,0 +1,27 @@ +.TH XGETIDLE 1 xgetidle\-VERSION +.SH NAME +xgetidle \- display the current X idle time +.SH SYNOPSIS +.B xgetidle +.RB [ \-s ] +.RB [ \-v ] +.SH DESCRIPTION +.B xgetidle +will display the current idle time of X11 given the XScreenSaver extension. +.SH OPTIONS +.TP +.B \-s +display the result in seconds +.TP +.B \-v +show version information +.SH AUTHORS +See the LICENSE file for the authors. +.SH LICENSE +See the LICENSE file for the terms of redistribution. +.SH SEE ALSO +.BR slock (1) +.BR xlock (1) +.SH BUGS +Please report them. + diff --git a/xgetidle.c b/xgetidle.c @@ -0,0 +1,68 @@ +/* + * See LICENSE file for copyright and license details. + */ + +#include <unistd.h> +#include <stdlib.h> +#include <stdio.h> +#include <stdarg.h> +#include <stdbool.h> +#include <libgen.h> +#include <X11/extensions/scrnsaver.h> + +#include "arg.h" + +char *argv0; + +void +die(const char *errstr, ...) { + va_list ap; + + va_start(ap, errstr); + vfprintf(stderr, errstr, ap); + va_end(ap); + exit(EXIT_FAILURE); +} + +void +usage(void) +{ + die("usage: %s [-sv]\n", basename(argv0)); +} + +int +main(int argc, char *argv[]) { + XScreenSaverInfo *info; + Display *dpy; + int base, errbase; + Bool inseconds; + + inseconds = False; + + ARGBEGIN { + case 's': + inseconds = true; + break; + case 'v': + die("sautolock-"VERSION", © 2008-2012 xgetidle engineers" + ", see LICENSE for details.\n"); + default: + usage(); + } ARGEND; + + if(!(dpy = XOpenDisplay(0))) + die("Cannot open display.\n"); + + if(!XScreenSaverQueryExtension(dpy, &base, &errbase)) + die("Screensaver extension not activated.\n"); + + info = XScreenSaverAllocInfo(); + XScreenSaverQueryInfo(dpy, DefaultRootWindow(dpy), info); + + printf("%ld\n", info->idle / ((inseconds)? 1000 : 1)); + + XCloseDisplay(dpy); + + return 0; +} +