herbe/herbe.c

171 lines
4.1 KiB
C
Raw Normal View History

2020-07-24 13:51:07 +02:00
#include <X11/Xlib.h>
#include <X11/Xft/Xft.h>
2020-07-20 15:41:19 +02:00
#include <stdio.h>
#include <stdlib.h>
2020-07-30 14:41:01 +02:00
#include <signal.h>
#include <unistd.h>
2020-08-08 15:55:18 +02:00
#include <string.h>
#include <stdarg.h>
2020-07-20 15:41:19 +02:00
2020-07-24 13:51:07 +02:00
#include "config.h"
2020-07-30 14:41:01 +02:00
Display *display;
Window window;
static void die(const char *format, ...)
{
va_list ap;
va_start(ap, format);
vfprintf(stderr, format, ap);
fprintf(stderr, "\n");
va_end(ap);
exit(EXIT_FAILURE);
}
2020-08-08 14:55:22 +02:00
int get_max_len(char *body, XftFont *font, int max_text_width)
{
int body_len = strlen(body);
XGlyphInfo info;
XftTextExtentsUtf8(display, font, (FcChar8 *)body, body_len, &info);
if (info.width < max_text_width)
return body_len;
int eol = max_text_width / font->max_advance_width;
info.width = 0;
while (info.width < max_text_width)
{
eol++;
XftTextExtentsUtf8(display, font, (FcChar8 *)body, eol, &info);
}
eol--;
int temp = eol;
while (body[eol] != ' ' && eol)
--eol;
if (eol == 0)
return temp;
else
return ++eol;
}
2020-07-30 14:41:01 +02:00
void expire()
2020-07-20 15:41:19 +02:00
{
2020-07-24 13:51:07 +02:00
XEvent event;
2020-07-30 14:41:01 +02:00
event.type = ButtonPress;
XSendEvent(display, window, 0, 0, &event);
XFlush(display);
}
int main(int argc, char *argv[])
{
2020-08-08 14:55:22 +02:00
if (argc == 1)
die("Usage: %s body", argv[0]);
2020-07-30 14:41:01 +02:00
signal(SIGALRM, expire);
if (duration != 0)
alarm(duration);
2020-07-30 14:41:01 +02:00
2020-08-08 15:55:18 +02:00
display = XOpenDisplay(0);
2020-07-24 13:51:07 +02:00
2020-08-08 15:55:18 +02:00
if (display == 0)
die("Cannot open display");
2020-07-20 15:41:19 +02:00
2020-07-24 13:51:07 +02:00
int screen = DefaultScreen(display);
2020-07-31 10:59:07 +02:00
Visual *visual = DefaultVisual(display, screen);
Colormap colormap = DefaultColormap(display, screen);
2020-07-20 15:41:19 +02:00
2020-08-01 21:00:20 +02:00
int screen_width = DisplayWidth(display, screen);
int screen_height = DisplayHeight(display, screen);
2020-07-29 15:09:34 +02:00
XftColor color;
2020-07-20 15:41:19 +02:00
2020-07-24 13:51:07 +02:00
XSetWindowAttributes attributes;
attributes.override_redirect = True;
2020-07-31 10:59:07 +02:00
XftColorAllocName(display, visual, colormap, background_color, &color);
2020-07-29 15:09:34 +02:00
attributes.background_pixel = color.pixel;
2020-07-31 10:59:07 +02:00
XftColorAllocName(display, visual, colormap, border_color, &color);
2020-07-29 15:09:34 +02:00
attributes.border_pixel = color.pixel;
2020-07-20 15:41:19 +02:00
2020-07-31 10:59:07 +02:00
XftFont *font = XftFontOpenName(display, screen, font_pattern);
2020-07-20 15:41:19 +02:00
2020-08-08 14:55:22 +02:00
int num_of_lines = 0;
2020-08-01 21:00:20 +02:00
int max_text_width = width - 2 * padding;
int words_size = 5;
char **words = malloc(words_size * sizeof(char *));
if (!words)
die("malloc failed");
2020-08-01 21:00:20 +02:00
2020-08-08 14:55:22 +02:00
for (int i = 1; i < argc; i++)
2020-08-01 21:00:20 +02:00
{
2020-08-08 14:55:22 +02:00
char *body = argv[i];
2020-08-01 21:34:48 +02:00
2020-08-08 14:55:22 +02:00
for (unsigned int eol = get_max_len(body, font, max_text_width); eol <= strlen(body) && eol; body += eol, num_of_lines++, eol = get_max_len(body, font, max_text_width))
2020-08-01 21:00:20 +02:00
{
if (words_size <= num_of_lines)
{
words = realloc(words, (words_size += 5) * sizeof(char *));
if (!words)
die("malloc failed");
}
words[num_of_lines] = malloc((eol + 1) * sizeof(char));
if (!words[num_of_lines])
die("malloc failed");
2020-08-08 14:55:22 +02:00
strncpy(words[num_of_lines], body, eol);
words[num_of_lines][eol] = '\0';
2020-08-01 21:00:20 +02:00
}
}
2020-07-31 14:59:52 +02:00
unsigned int x = pos_x;
unsigned int y = pos_y;
2020-08-01 21:00:20 +02:00
unsigned int text_height = font->ascent - font->descent;
2020-08-08 14:55:22 +02:00
unsigned int height = (num_of_lines - 1) * line_spacing + num_of_lines * text_height + 2 * padding;
2020-07-31 10:59:07 +02:00
2020-08-05 20:51:53 +02:00
if (corner == TOP_RIGHT || corner == BOTTOM_RIGHT)
2020-08-01 21:00:20 +02:00
x = screen_width - width - border_size * 2 - pos_x;
2020-08-05 20:51:53 +02:00
if (corner == BOTTOM_LEFT || corner == BOTTOM_RIGHT)
2020-08-01 21:00:20 +02:00
y = screen_height - height - border_size * 2 - pos_y;
2020-07-29 15:09:34 +02:00
2020-08-01 21:00:20 +02:00
window = XCreateWindow(display, RootWindow(display, screen), x, y, width, height, border_size, DefaultDepth(display, screen), CopyFromParent, visual,
CWOverrideRedirect | CWBackPixel | CWBorderPixel, &attributes);
2020-07-20 15:41:19 +02:00
2020-07-31 10:59:07 +02:00
XftDraw *draw = XftDrawCreate(display, window, visual, colormap);
XftColorAllocName(display, visual, colormap, font_color, &color);
2020-07-20 15:41:19 +02:00
2020-07-30 11:21:27 +02:00
XSelectInput(display, window, ExposureMask | ButtonPress);
2020-07-20 15:41:19 +02:00
2020-07-30 11:21:27 +02:00
XMapWindow(display, window);
2020-07-29 19:39:15 +02:00
2020-07-30 11:21:27 +02:00
while (1)
{
2020-08-08 15:55:18 +02:00
XEvent event;
2020-07-30 11:21:27 +02:00
XNextEvent(display, &event);
2020-07-20 15:41:19 +02:00
2020-07-30 11:21:27 +02:00
if (event.type == Expose)
{
2020-07-30 12:41:37 +02:00
XClearWindow(display, window);
2020-08-08 14:55:22 +02:00
for (int i = 0; i < num_of_lines; i++)
XftDrawStringUtf8(draw, &color, font, padding, line_spacing * i + text_height * (i + 1) + padding, (FcChar8 *)words[i], strlen(words[i]));
2020-07-30 11:21:27 +02:00
}
if (event.type == ButtonPress)
break;
}
2020-07-29 15:09:34 +02:00
for (int i = 0; i < num_of_lines; i++)
free(words[i]);
free(words);
XftDrawDestroy(draw);
2020-07-31 10:59:07 +02:00
XftColorFree(display, visual, colormap, &color);
XftFontClose(display, font);
2020-07-29 15:09:34 +02:00
XCloseDisplay(display);
2020-08-08 15:55:18 +02:00
exit(EXIT_SUCCESS);
}