texec/texec.c

173 lines
4.8 KiB
C
Raw Permalink Normal View History

2025-11-23 03:40:08 +01:00
/*
2025-11-23 03:42:19 +01:00
* texec.c - Minimal X11 tray applet with mandatory PNG icon (32x32)
*
2025-11-23 03:40:08 +01:00
* Copyright 2025 Johannes Findeisen <you@hanez.org>
* Licensed under the terms of the Apache-2.0 license.
* https://opensource.org/license/apache-2-0
*
* Compile:
2025-11-23 03:47:20 +01:00
* gcc -o texec texec.c -lX11 -lpng
2025-11-23 03:40:08 +01:00
*
* Usage:
2025-11-23 03:42:19 +01:00
* ./texec "<command>" <icon.png>
2025-11-23 03:40:08 +01:00
*
* Left-click: execute command
* Right-click: quit applet
*/
#include <png.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <signal.h>
2025-11-23 03:42:19 +01:00
#include <X11/Xlib.h>
#include <X11/Xatom.h>
#include <X11/Xutil.h>
2025-11-23 03:40:08 +01:00
#define WIDTH 24
#define HEIGHT 24
static Display *dpy;
static int screen;
static Window icon_win;
2025-11-23 03:42:19 +01:00
static char *x_command;
2025-11-23 03:40:08 +01:00
void execute_command() {
2025-11-23 03:42:19 +01:00
if (!x_command) return;
2025-11-23 03:40:08 +01:00
pid_t pid = fork();
if (pid == 0) {
setsid();
2025-11-23 03:42:19 +01:00
execl("/bin/sh", "sh", "-c", x_command, (char *)NULL);
2025-11-23 03:40:08 +01:00
_exit(127);
}
}
/* Minimal PNG loader into XImage */
XImage *load_png(const char *filename) {
FILE *fp = fopen(filename, "rb");
if (!fp) return NULL;
png_structp png = png_create_read_struct(PNG_LIBPNG_VER_STRING,NULL,NULL,NULL);
png_infop info = png_create_info_struct(png);
if (setjmp(png_jmpbuf(png))) return NULL;
png_init_io(png, fp);
png_read_info(png, info);
int w = png_get_image_width(png, info);
int h = png_get_image_height(png, info);
png_byte color_type = png_get_color_type(png, info);
png_byte bit_depth = png_get_bit_depth(png, info);
if (bit_depth == 16) png_set_strip_16(png);
if (color_type == PNG_COLOR_TYPE_PALETTE) png_set_palette_to_rgb(png);
if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8) png_set_expand_gray_1_2_4_to_8(png);
if (png_get_valid(png, info, PNG_INFO_tRNS)) png_set_tRNS_to_alpha(png);
png_set_filler(png, 0xFF, PNG_FILLER_AFTER);
png_read_update_info(png, info);
png_bytep *rows = (png_bytep*)malloc(sizeof(png_bytep) * h);
for (int y=0; y<h; y++) {
rows[y] = (png_byte*)malloc(4*w);
}
png_read_image(png, rows);
fclose(fp);
XImage *img = XCreateImage(dpy, CopyFromParent, 24, ZPixmap, 0,
malloc(w*h*4), w, h, 32, 0);
for (int y=0; y<h; y++) {
for (int x=0; x<w; x++) {
png_bytep px = &(rows[y][x*4]);
unsigned long pixel = (px[0]<<16) | (px[1]<<8) | px[2];
XPutPixel(img, x, y, pixel);
}
}
for (int y=0; y<h; y++) {
free(rows[y]);
}
free(rows);
png_destroy_read_struct(&png, &info, NULL);
return img;
}
void draw_icon(Window win, const char *icon_path) {
GC gc = XCreateGC(dpy, win, 0, NULL);
XImage *img = load_png(icon_path);
if (!img) {
fprintf(stderr,"Failed to load PNG icon: %s\n", icon_path);
exit(1);
}
XPutImage(dpy, win, gc, img, 0,0,0,0, img->width, img->height);
XDestroyImage(img);
XFreeGC(dpy,gc);
}
int main(int argc, char *argv[]) {
if (argc < 3) {
fprintf(stderr,"Usage: %s <command> <icon.png>\n",argv[0]);
return 1;
}
2025-11-23 03:42:19 +01:00
x_command = argv[1];
2025-11-23 03:40:08 +01:00
char *icon_path = argv[2];
signal(SIGCHLD, SIG_IGN);
dpy = XOpenDisplay(NULL);
if (!dpy) {
fprintf(stderr,"Cannot open display\n");
return 1;
}
screen = DefaultScreen(dpy);
Atom _NET_SYSTEM_TRAY_S0 = XInternAtom(dpy, "_NET_SYSTEM_TRAY_S0", False);
Window tray_win = XGetSelectionOwner(dpy, _NET_SYSTEM_TRAY_S0);
if (tray_win == None) {
fprintf(stderr,"No system tray found\n");
return 1;
}
icon_win = XCreateSimpleWindow(dpy, DefaultRootWindow(dpy),
0,0, WIDTH, HEIGHT, 1,
BlackPixel(dpy,screen),
WhitePixel(dpy,screen));
XSelectInput(dpy, icon_win, ExposureMask | ButtonPressMask);
// Send dock request
XEvent ev;
memset(&ev, 0, sizeof(ev));
ev.xclient.type = ClientMessage;
ev.xclient.window = icon_win;
ev.xclient.message_type = XInternAtom(dpy,"_NET_SYSTEM_TRAY_OPCODE",False);
ev.xclient.format = 32;
ev.xclient.data.l[0] = CurrentTime;
ev.xclient.data.l[1] = 0; // SYSTEM_TRAY_REQUEST_DOCK
ev.xclient.data.l[2] = icon_win;
XSendEvent(dpy, tray_win, False, NoEventMask, &ev);
XMapWindow(dpy, icon_win);
draw_icon(icon_win, icon_path);
XEvent xev;
while (1) {
XNextEvent(dpy, &xev);
if (xev.xany.window == icon_win) {
if (xev.type == Expose) draw_icon(icon_win, icon_path);
else if (xev.type == ButtonPress) {
2025-11-23 03:42:19 +01:00
if (xev.xbutton.button == 1) {
execute_command(); // left-click
} else if (xev.xbutton.button == 3) {
break; // right-click: quit
}
2025-11-23 03:40:08 +01:00
}
}
}
XDestroyWindow(dpy, icon_win);
XCloseDisplay(dpy);
return 0;
}