sys.dtwmrc bound Mod4 Shift 3 and 4 (and Ctrl for the clipboard) to
`dtscreenshot` since 2026-11-15, and the program did not exist: the key
reached dtwm and nothing happened (tools/mcde-keytest.sh, t0222).
programs/dtscreenshot/dtscreenshot.c: C99, Xlib and libpng only, no toolkit.
(none) the root window -- every monitor -- as a PNG,
"Screenshot YYYY-MM-DD at HH.MM.SS.png" in ~/Desktop, else in ~
-area the rectangle the pointer drags out, XOR outline, Escape cancels
-clipboard image/png on the CLIPBOARD selection, held until another owner
-delay N wait before the picture (a menu to open first)
-o FILE this file instead of the default name
Nothing runs until the key is pressed; the area is grabbed with a crosshair
cursor; a picture larger than the server's maximum request is refused with a
message (the clipboard is handed over in one property, no INCR).
Wired in three places as ext/src/mcde/AGENTS.md asks: programs/Makefile.am's
SUBDIRS, configure.ac's AC_CONFIG_FILES, the source directory with its
Makefile.am; dt/bin/dtscreenshot is a PLIST line.
Measured on Xvfb :99 with the development build (build/mcde-root/dt/bin):
dtscreenshot -o /tmp/shot-test.png -> 800x600 PNG, exit 0
dtscreenshot -area -o /tmp/shot-area.png with
xdotool mousemove 100 100 mousedown 1 mousemove 300 250 mouseup 1
-> 200x150 PNG
dtscreenshot -clipboard, then
xclip -selection clipboard -t image/png -o -> 800x600 PNG
tools/mcde-keytest.sh: Mod4 Shift 3 puts
"Screenshot 2026-11-15 at 08.51.30.png" on the session's Desktop
(0 before, 1 after; build/logs/mcde-keytest-2026-11-15f.txt).
make mcde-linux MCDE_DIRS=plist: 3210 installed names under dt/, ok.
482 lines
12 KiB
C
482 lines
12 KiB
C
/*
|
|
* dtscreenshot.c -- MCDE: a picture of the screen (Shift+Command+3 / 4)
|
|
*
|
|
* License: CNCL 1.0 (Could Not Care Less), see LICENSE at the top of the tree.
|
|
*
|
|
* usage: dtscreenshot [-area] [-clipboard] [-delay SECONDS] [-o FILE]
|
|
*
|
|
* (none) the whole screen (every monitor: the root window), as a PNG file
|
|
* "Screenshot YYYY-MM-DD at HH.MM.SS.png" in ~/Desktop, else in ~
|
|
* -area the rectangle the pointer drags out (crosshair, XOR outline like
|
|
* dtwm's); Escape or a rectangle without area cancels, quietly
|
|
* -clipboard not a file: the PNG becomes the CLIPBOARD selection (image/png,
|
|
* TARGETS) and the program stays until someone else takes the
|
|
* selection; shown by `xclip -selection clipboard -t image/png -o'
|
|
* -delay N wait N seconds before the picture (menus to open first)
|
|
* -o FILE this file instead of the default name (not with -clipboard)
|
|
*
|
|
* Prints the file it wrote. Exit 0 done or cancelled, 1 failed, 2 usage.
|
|
*
|
|
* Nothing runs until the key is pressed; Xlib and libpng only, no toolkit. The
|
|
* clipboard image is handed over in one property (no INCR): a picture over the
|
|
* server's maximum request size is refused with a message. C99, POSIX.
|
|
*/
|
|
|
|
#define _POSIX_C_SOURCE 200809L
|
|
|
|
#include <errno.h>
|
|
#include <fcntl.h>
|
|
#include <limits.h>
|
|
#include <png.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <sys/stat.h>
|
|
#include <time.h>
|
|
#include <unistd.h>
|
|
|
|
#include <X11/Xatom.h>
|
|
#include <X11/Xlib.h>
|
|
#include <X11/Xutil.h>
|
|
#include <X11/cursorfont.h>
|
|
#include <X11/keysym.h>
|
|
|
|
struct buf {
|
|
unsigned char *p;
|
|
size_t len, cap;
|
|
};
|
|
|
|
static void
|
|
usage(void)
|
|
{
|
|
fprintf(stderr,
|
|
"usage: dtscreenshot [-area] [-clipboard] [-delay SECONDS] [-o FILE]\n");
|
|
exit(2);
|
|
}
|
|
|
|
static void
|
|
buf_write(png_structp png, png_bytep data, size_t n)
|
|
{
|
|
struct buf *b = png_get_io_ptr(png);
|
|
|
|
if (b->len + n > b->cap) {
|
|
size_t cap = b->cap ? b->cap : 65536;
|
|
unsigned char *p;
|
|
|
|
while (cap < b->len + n)
|
|
cap *= 2;
|
|
p = realloc(b->p, cap);
|
|
if (p == NULL)
|
|
png_error(png, "out of memory");
|
|
b->p = p;
|
|
b->cap = cap;
|
|
}
|
|
memcpy(b->p + b->len, data, n);
|
|
b->len += n;
|
|
}
|
|
|
|
static void
|
|
buf_flush(png_structp png)
|
|
{
|
|
(void)png;
|
|
}
|
|
|
|
/* the 8-bit value of a channel of W bits */
|
|
static unsigned
|
|
to8(unsigned long v, int w)
|
|
{
|
|
if (w >= 8)
|
|
return (unsigned)(v >> (w - 8));
|
|
if (w == 0)
|
|
return 0;
|
|
return (unsigned)((v << (8 - w)) | (v >> (2 * w - 8)));
|
|
}
|
|
|
|
static int
|
|
shift_of(unsigned long mask, int *width)
|
|
{
|
|
int s = 0, w = 0;
|
|
|
|
if (mask == 0) {
|
|
*width = 0;
|
|
return 0;
|
|
}
|
|
while (!(mask & 1)) {
|
|
mask >>= 1;
|
|
s++;
|
|
}
|
|
while (mask & 1) {
|
|
mask >>= 1;
|
|
w++;
|
|
}
|
|
*width = w;
|
|
return s;
|
|
}
|
|
|
|
/* the picture as a PNG in memory; 0 on success */
|
|
static int
|
|
encode(XImage *im, Visual *vis, struct buf *out)
|
|
{
|
|
png_structp png;
|
|
png_infop info;
|
|
unsigned char *row;
|
|
int x, y, rs, gs, bs, rw, gw, bw;
|
|
|
|
if (vis->class != TrueColor && vis->class != DirectColor) {
|
|
fprintf(stderr, "dtscreenshot: the screen has no true colour visual\n");
|
|
return -1;
|
|
}
|
|
rs = shift_of(vis->red_mask, &rw);
|
|
gs = shift_of(vis->green_mask, &gw);
|
|
bs = shift_of(vis->blue_mask, &bw);
|
|
row = malloc((size_t)im->width * 3);
|
|
png = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
|
|
info = png != NULL ? png_create_info_struct(png) : NULL;
|
|
if (row == NULL || info == NULL) {
|
|
fprintf(stderr, "dtscreenshot: out of memory\n");
|
|
free(row);
|
|
if (png != NULL)
|
|
png_destroy_write_struct(&png, NULL);
|
|
return -1;
|
|
}
|
|
if (setjmp(png_jmpbuf(png))) {
|
|
fprintf(stderr, "dtscreenshot: cannot encode the picture\n");
|
|
png_destroy_write_struct(&png, &info);
|
|
free(row);
|
|
return -1;
|
|
}
|
|
png_set_write_fn(png, out, buf_write, buf_flush);
|
|
png_set_IHDR(png, info, (png_uint_32)im->width, (png_uint_32)im->height,
|
|
8, PNG_COLOR_TYPE_RGB, PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT,
|
|
PNG_FILTER_TYPE_DEFAULT);
|
|
png_write_info(png, info);
|
|
for (y = 0; y < im->height; y++) {
|
|
for (x = 0; x < im->width; x++) {
|
|
unsigned long p = XGetPixel(im, x, y);
|
|
|
|
row[3 * x] = (unsigned char)to8((p & vis->red_mask) >> rs, rw);
|
|
row[3 * x + 1] = (unsigned char)to8((p & vis->green_mask) >> gs, gw);
|
|
row[3 * x + 2] = (unsigned char)to8((p & vis->blue_mask) >> bs, bw);
|
|
}
|
|
png_write_row(png, row);
|
|
}
|
|
png_write_end(png, NULL);
|
|
png_destroy_write_struct(&png, &info);
|
|
free(row);
|
|
return 0;
|
|
}
|
|
|
|
/* the rectangle dragged out with the pointer; 0 = got one, 1 = cancelled */
|
|
static int
|
|
pick_area(Display *dpy, Window root, int *rx, int *ry, int *rw, int *rh)
|
|
{
|
|
Cursor cur = XCreateFontCursor(dpy, XC_crosshair);
|
|
XGCValues gv;
|
|
GC gc;
|
|
XEvent ev;
|
|
int sx = 0, sy = 0, cx = 0, cy = 0, ox = 0, oy = 0;
|
|
int drag = 0, shown = 0, rc = 1, done = 0;
|
|
|
|
if (XGrabPointer(dpy, root, False,
|
|
ButtonPressMask | ButtonReleaseMask | PointerMotionMask,
|
|
GrabModeAsync, GrabModeAsync, None, cur, CurrentTime) != GrabSuccess) {
|
|
fprintf(stderr, "dtscreenshot: cannot grab the pointer\n");
|
|
return -1;
|
|
}
|
|
(void)XGrabKeyboard(dpy, root, False, GrabModeAsync, GrabModeAsync,
|
|
CurrentTime);
|
|
memset(&gv, 0, sizeof gv);
|
|
gv.function = GXxor;
|
|
gv.foreground = WhitePixel(dpy, DefaultScreen(dpy)) ^
|
|
BlackPixel(dpy, DefaultScreen(dpy));
|
|
gv.subwindow_mode = IncludeInferiors;
|
|
gv.line_width = 1;
|
|
gc = XCreateGC(dpy, root, GCFunction | GCForeground | GCSubwindowMode |
|
|
GCLineWidth, &gv);
|
|
while (!done) {
|
|
int x, y, w, h;
|
|
|
|
XNextEvent(dpy, &ev);
|
|
switch (ev.type) {
|
|
case KeyPress:
|
|
if (XLookupKeysym(&ev.xkey, 0) == XK_Escape)
|
|
done = 1;
|
|
break;
|
|
case ButtonPress:
|
|
if (ev.xbutton.button == Button1) {
|
|
sx = cx = ev.xbutton.x_root;
|
|
sy = cy = ev.xbutton.y_root;
|
|
drag = 1;
|
|
} else {
|
|
done = 1;
|
|
}
|
|
break;
|
|
case MotionNotify:
|
|
if (!drag)
|
|
break;
|
|
if (shown) { /* the old outline off */
|
|
x = sx < ox ? sx : ox;
|
|
y = sy < oy ? sy : oy;
|
|
XDrawRectangle(dpy, root, gc, x, y,
|
|
(unsigned)abs(ox - sx), (unsigned)abs(oy - sy));
|
|
}
|
|
cx = ev.xmotion.x_root;
|
|
cy = ev.xmotion.y_root;
|
|
x = sx < cx ? sx : cx;
|
|
y = sy < cy ? sy : cy;
|
|
XDrawRectangle(dpy, root, gc, x, y, (unsigned)abs(cx - sx),
|
|
(unsigned)abs(cy - sy));
|
|
ox = cx;
|
|
oy = cy;
|
|
shown = 1;
|
|
break;
|
|
case ButtonRelease:
|
|
if (!drag || ev.xbutton.button != Button1)
|
|
break;
|
|
cx = ev.xbutton.x_root;
|
|
cy = ev.xbutton.y_root;
|
|
done = 1;
|
|
if (cx != sx && cy != sy) {
|
|
*rx = sx < cx ? sx : cx;
|
|
*ry = sy < cy ? sy : cy;
|
|
*rw = abs(cx - sx);
|
|
*rh = abs(cy - sy);
|
|
rc = 0;
|
|
}
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
if (done && shown) { /* the outline off before the picture */
|
|
int x2 = sx < ox ? sx : ox, y2 = sy < oy ? sy : oy;
|
|
|
|
XDrawRectangle(dpy, root, gc, x2, y2, (unsigned)abs(ox - sx),
|
|
(unsigned)abs(oy - sy));
|
|
shown = 0;
|
|
}
|
|
}
|
|
XFreeGC(dpy, gc);
|
|
XUngrabKeyboard(dpy, CurrentTime);
|
|
XUngrabPointer(dpy, CurrentTime);
|
|
XFreeCursor(dpy, cur);
|
|
XSync(dpy, False);
|
|
return rc;
|
|
}
|
|
|
|
/* the default file: ~/Desktop (else ~) / Screenshot DATE at TIME.png */
|
|
static int
|
|
default_name(char *path, size_t n)
|
|
{
|
|
const char *home = getenv("HOME");
|
|
struct stat st;
|
|
struct tm tm;
|
|
time_t t = time(NULL);
|
|
char name[64], dir[PATH_MAX];
|
|
|
|
if (home == NULL || home[0] == '\0') {
|
|
fprintf(stderr, "dtscreenshot: no HOME\n");
|
|
return -1;
|
|
}
|
|
if (snprintf(dir, sizeof dir, "%s/Desktop", home) >= (int)sizeof dir)
|
|
return -1;
|
|
if (stat(dir, &st) != 0 || !S_ISDIR(st.st_mode))
|
|
snprintf(dir, sizeof dir, "%s", home);
|
|
localtime_r(&t, &tm);
|
|
strftime(name, sizeof name, "Screenshot %Y-%m-%d at %H.%M.%S.png", &tm);
|
|
if (snprintf(path, n, "%s/%s", dir, name) >= (int)n)
|
|
return -1;
|
|
return 0;
|
|
}
|
|
|
|
static int
|
|
save_file(const char *path, const struct buf *b)
|
|
{
|
|
int fd = open(path, O_WRONLY | O_CREAT | O_EXCL, 0666);
|
|
size_t off = 0;
|
|
|
|
if (fd < 0) {
|
|
fprintf(stderr, "dtscreenshot: %s: %s\n", path, strerror(errno));
|
|
return -1;
|
|
}
|
|
while (off < b->len) {
|
|
ssize_t w = write(fd, b->p + off, b->len - off);
|
|
|
|
if (w < 0) {
|
|
if (errno == EINTR)
|
|
continue;
|
|
fprintf(stderr, "dtscreenshot: %s: %s\n", path,
|
|
strerror(errno));
|
|
close(fd);
|
|
unlink(path);
|
|
return -1;
|
|
}
|
|
off += (size_t)w;
|
|
}
|
|
if (close(fd) != 0) {
|
|
fprintf(stderr, "dtscreenshot: %s: %s\n", path, strerror(errno));
|
|
unlink(path);
|
|
return -1;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
/* own CLIPBOARD and answer until another client takes it */
|
|
static int
|
|
serve_clipboard(Display *dpy, Window root, const struct buf *b)
|
|
{
|
|
Atom clipboard = XInternAtom(dpy, "CLIPBOARD", False);
|
|
Atom targets = XInternAtom(dpy, "TARGETS", False);
|
|
Atom timestamp = XInternAtom(dpy, "TIMESTAMP", False);
|
|
Atom png = XInternAtom(dpy, "image/png", False);
|
|
long maxreq = XExtendedMaxRequestSize(dpy);
|
|
Window w;
|
|
XEvent ev;
|
|
|
|
if (maxreq == 0)
|
|
maxreq = XMaxRequestSize(dpy);
|
|
if ((long)(b->len / 4) + 64 > maxreq) {
|
|
fprintf(stderr,
|
|
"dtscreenshot: the picture (%zu bytes) is too big for the clipboard "
|
|
"of this server; use a file\n", b->len);
|
|
return -1;
|
|
}
|
|
w = XCreateSimpleWindow(dpy, root, -10, -10, 1, 1, 0, 0, 0);
|
|
XSelectInput(dpy, w, PropertyChangeMask);
|
|
XSetSelectionOwner(dpy, clipboard, w, CurrentTime);
|
|
if (XGetSelectionOwner(dpy, clipboard) != w) {
|
|
fprintf(stderr, "dtscreenshot: cannot own the clipboard\n");
|
|
return -1;
|
|
}
|
|
for (;;) {
|
|
XNextEvent(dpy, &ev);
|
|
if (ev.type == SelectionClear) {
|
|
return 0;
|
|
} else if (ev.type == SelectionRequest) {
|
|
XSelectionRequestEvent *rq = &ev.xselectionrequest;
|
|
XSelectionEvent se;
|
|
Atom prop = rq->property != None ? rq->property : rq->target;
|
|
|
|
memset(&se, 0, sizeof se);
|
|
se.type = SelectionNotify;
|
|
se.display = rq->display;
|
|
se.requestor = rq->requestor;
|
|
se.selection = rq->selection;
|
|
se.target = rq->target;
|
|
se.time = rq->time;
|
|
se.property = None;
|
|
if (rq->target == targets) {
|
|
Atom list[3];
|
|
|
|
list[0] = targets;
|
|
list[1] = timestamp;
|
|
list[2] = png;
|
|
XChangeProperty(dpy, rq->requestor, prop, XA_ATOM, 32,
|
|
PropModeReplace, (unsigned char *)list, 3);
|
|
se.property = prop;
|
|
} else if (rq->target == timestamp) {
|
|
long t = (long)rq->time;
|
|
|
|
XChangeProperty(dpy, rq->requestor, prop, XA_INTEGER, 32,
|
|
PropModeReplace, (unsigned char *)&t, 1);
|
|
se.property = prop;
|
|
} else if (rq->target == png) {
|
|
XChangeProperty(dpy, rq->requestor, prop, png, 8,
|
|
PropModeReplace, b->p, (int)b->len);
|
|
se.property = prop;
|
|
}
|
|
XSendEvent(dpy, rq->requestor, False, 0, (XEvent *)&se);
|
|
XFlush(dpy);
|
|
}
|
|
}
|
|
}
|
|
|
|
int
|
|
main(int argc, char **argv)
|
|
{
|
|
int area = 0, clip = 0, delay = 0, i;
|
|
const char *outfile = NULL;
|
|
char path[PATH_MAX];
|
|
Display *dpy;
|
|
Window root;
|
|
XImage *im;
|
|
XWindowAttributes wa;
|
|
struct buf b = { NULL, 0, 0 };
|
|
int x = 0, y = 0, w, h, rc = 1;
|
|
|
|
for (i = 1; i < argc; i++) {
|
|
if (strcmp(argv[i], "-area") == 0) {
|
|
area = 1;
|
|
} else if (strcmp(argv[i], "-clipboard") == 0) {
|
|
clip = 1;
|
|
} else if (strcmp(argv[i], "-delay") == 0 && i + 1 < argc) {
|
|
delay = atoi(argv[++i]);
|
|
if (delay < 0 || delay > 60)
|
|
usage();
|
|
} else if (strcmp(argv[i], "-o") == 0 && i + 1 < argc) {
|
|
outfile = argv[++i];
|
|
} else {
|
|
usage();
|
|
}
|
|
}
|
|
if (clip && outfile != NULL)
|
|
usage();
|
|
dpy = XOpenDisplay(NULL);
|
|
if (dpy == NULL) {
|
|
fprintf(stderr, "dtscreenshot: cannot open the display\n");
|
|
return 1;
|
|
}
|
|
root = DefaultRootWindow(dpy);
|
|
if (delay > 0)
|
|
sleep((unsigned)delay);
|
|
XGetWindowAttributes(dpy, root, &wa);
|
|
w = wa.width;
|
|
h = wa.height;
|
|
if (area) {
|
|
int r = pick_area(dpy, root, &x, &y, &w, &h);
|
|
|
|
if (r != 0)
|
|
return r < 0 ? 1 : 0;
|
|
if (x < 0) {
|
|
w += x;
|
|
x = 0;
|
|
}
|
|
if (y < 0) {
|
|
h += y;
|
|
y = 0;
|
|
}
|
|
if (x + w > wa.width)
|
|
w = wa.width - x;
|
|
if (y + h > wa.height)
|
|
h = wa.height - y;
|
|
if (w < 1 || h < 1)
|
|
return 0;
|
|
usleep(100000); /* the outline's last pixels are redrawn */
|
|
}
|
|
im = XGetImage(dpy, root, x, y, (unsigned)w, (unsigned)h, AllPlanes,
|
|
ZPixmap);
|
|
if (im == NULL) {
|
|
fprintf(stderr, "dtscreenshot: cannot read the screen\n");
|
|
return 1;
|
|
}
|
|
if (encode(im, wa.visual, &b) != 0) {
|
|
XDestroyImage(im);
|
|
return 1;
|
|
}
|
|
XDestroyImage(im);
|
|
if (clip) {
|
|
rc = serve_clipboard(dpy, root, &b);
|
|
} else {
|
|
if (outfile == NULL) {
|
|
if (default_name(path, sizeof path) != 0)
|
|
return 1;
|
|
outfile = path;
|
|
}
|
|
rc = save_file(outfile, &b);
|
|
if (rc == 0)
|
|
printf("%s\n", outfile);
|
|
}
|
|
free(b.p);
|
|
XCloseDisplay(dpy);
|
|
return rc == 0 ? 0 : 1;
|
|
}
|