diff --git a/Makefile b/Makefile index 41439c9..6c2ff57 100644 --- a/Makefile +++ b/Makefile @@ -1,9 +1,9 @@ include config.mk -SRC = popup.c +SRC = popup.c format_output.c OBJ = $(SRC:.c=.o) -all: options popup +all: options dictpopup options: @echo popup build options: @@ -11,20 +11,33 @@ options: @echo "LDFLAGS = $(LDFLAGS)" @echo "CC = $(CC)" +config.h: + cp config.def.h config.h + .c.o: $(CC) -c $(CFLAGS) $< -popup: popup.o - $(CC) -o $@ popup.o $(LDFLAGS) +popup.o: config.h + +$(OBJ): config.h config.mk + +dictpopup: $(OBJ) + $(CC) -o format_output format_output.o + $(CC) -o popup popup.o $(LDFLAGS) + +clean: + rm -f popup $(OBJ) format_output install: all mkdir -p ${DESTDIR}${PREFIX}/bin cp -f popup ${DESTDIR}${PREFIX}/bin + cp -f dictpopup ${DESTDIR}${PREFIX}/bin + cp -f format_output ${DESTDIR}${PREFIX}/bin uninstall: rm -f ${DESTDIR}${PREFIX}/bin/popup + rm -f ${DESTDIR}${PREFIX}/bin/dictpopup + rm -f ${DESTDIR}${PREFIX}/bin/format_output -clean: - rm -f popup $(OBJ) -.PHONY: all install uninstall clean +.PHONY: all options clean install uninstall diff --git a/README.md b/README.md index cf7deb0..cffd49b 100644 --- a/README.md +++ b/README.md @@ -4,18 +4,25 @@ This is a very lightweight program to show a popup with the translation of the s ![image](https://github.com/GenjiFujimoto/dictpopup/assets/50422430/c4a3663b-fd91-4a66-95ad-f1528071c932) +![screenshot-2023-05-28-08-34-19](https://github.com/GenjiFujimoto/dictpopup/assets/50422430/179ad9f9-b4fa-4731-92c6-105ced37c353) + ## Dependencies -xclip, sdcv +sselp (can be replaced with xclip -o), sdcv. \ +Optional: lynx (for html support) ## Setup First setup [sdcv](https://github.com/Dushistov/sdcv) according to their github page. -Then compile with `make` and place `dictpopup` as well as `popup` in your PATH. +Then install with `sudo make install`. \ +Uninstall with `sudo make uninstall` ## Usage -Call `dictpopup` to translate and display the popup of the selected text. The -popup can be dismissed by clicking on it. -The styling can be changed in `config.h`, but it has the be recompiled. -There is also a xresources patch from [herbe](https://github.com/dudik/herbe) -that you could try to apply. +Call `dictpopup [html] []`.\ +If no word as an argument is supplied, the selection is used.\ +If the string `html` is supplied as the first argument, then html support will be enabled.\ +The popup can be dismissed by clicking on it. -You can also use `popup` like `popup "Hello"` for other things if you want. +The styling can be changed in `config.h` and then recompile to apply. +There is also a xresources patch from [herbe](https://github.com/dudik/herbe) +that you could try to apply if you like. + +`popup` can also be used as a standalone program to show the contents of stdin. diff --git a/config.h b/config.def.h similarity index 59% rename from config.h rename to config.def.h index 82d0d0f..c22dace 100644 --- a/config.h +++ b/config.def.h @@ -5,10 +5,13 @@ static const char *font_pattern = "Noto Sans Mono CJK JP:size=12"; static const unsigned line_spacing = 5; static const unsigned int padding = 15; -static const unsigned int width = 450; +static const unsigned int width = 480; static const unsigned int border_size = 1; static const unsigned int duration = 5; /* in seconds */ -#define DISMISS_BUTTON Button1 -#define ACTION_BUTTON Button3 +#define MIN_BORDER_DISTANCE 3 /* The minimum distance to the monitor boundary */ + +#define DISMISS_BUTTON Button1 /* left click */ +#define ACTION1_BUTTON Button4 /* scroll up */ +#define ACTION2_BUTTON Button5 /* scroll down */ diff --git a/dictpopup b/dictpopup index 1006830..0a53d2f 100755 --- a/dictpopup +++ b/dictpopup @@ -1,14 +1,24 @@ #!/bin/sh -# Looks up argument instead, if provided -word=${1:-$(xclip -o)} -[ -z $word ] && exit 1 +# Depends on: +# - sdcv +# - sselp (for selection support) +# - lynx (for html support) +# invocation: dictpopup [html] [word] +# - If no argument provided, the selection is used +# - If supplied with the html option, the dictionary output will be piped through lynx +# in order to format html as plain text -dict_lookup=$(sdcv -n --utf8-output -e "$word") -if echo "$dict_lookup" | grep -F -q "Nothing similar to"; then - popup "$dict_lookup" +# TODO: Switch between dictionaries / dictionary results. +# Implementation idea (in C): Store each dictionary entry in an array. Invoke popup with a dictionary entry. +# Increment, decrement array and reinvoke or quit according to exit code of popup + +if [ "$1" = "html" ]; then + word=${2:-$(sselp)} + sdcv -n --utf8-output -e "$word" | format_output | + lynx -dump -stdin -assume_charset=UTF-8 -display_charset=UTF-8 | + popup else - echo "$dict_lookup" | tail -n +5 | sed 's|
|\n|g' | sed 's|<[^>]*>||g' | xargs -d '\n' popup + word=${1:-$(sselp)} + sdcv -n --utf8-output -e "$word" | format_output | popup fi -# Instead of sed also possible: -# `lynx -dump -stdin -assume_charset=UTF-8 -display_charset=UTF-8` diff --git a/format_output.c b/format_output.c new file mode 100644 index 0000000..dc5ed18 --- /dev/null +++ b/format_output.c @@ -0,0 +1,32 @@ +#include +#include + +int main() +{ + int second = 0; + static char *buf = NULL; + static size_t size = 0; + ssize_t len = 0; + for (long n=1; (len = getline(&buf, &size, stdin)) > 0; n++) + { + if (n < 5) + continue; + if (len && buf[len - 1] == '\n') + buf[len - 1] = '\0'; + if (strncmp(buf,"-->", 3) == 0) + { + if (second) + { + second = 0; + } + else + { + putchar('\n'); + second = 1; + } + } + else if (buf[0] != 0) /* Don't print newline at EOF */ + puts(buf); + } + return 0; +} diff --git a/popup.c b/popup.c index f84f8e2..97d5559 100644 --- a/popup.c +++ b/popup.c @@ -1,6 +1,5 @@ #include #include -#include #include #include #include @@ -14,15 +13,17 @@ #include "config.h" -/* macros */ #define MAX(A, B) ((A) > (B) ? (A) : (B)) #define MIN(A, B) ((A) < (B) ? (A) : (B)) #define INTERSECT(x,y,w,h,r) (MAX(0, MIN((x)+(w),(r).x_org+(r).width) - MAX((x),(r).x_org)) \ * MAX(0, MIN((y)+(h),(r).y_org+(r).height) - MAX((y),(r).y_org))) -#define EXIT_ACTION 0 +//TODO: Make selection possible + +#define EXIT_DISMISS 0 #define EXIT_FAIL 1 -#define EXIT_DISMISS 2 +#define EXIT_ACTION1 2 +#define EXIT_ACTION2 3 static Display *dpy; static Window window; @@ -75,48 +76,21 @@ int get_max_len(char *string, XftFont *font, int max_text_width) if (info.width <= max_text_width) return eol; - int temp = eol; - while (string[eol] != ' ' && eol) - --eol; + /* prefer splitting at spaces */ + /* int temp = eol; */ + /* while (string[eol] != ' ' && eol) */ + /* --eol; */ - if (eol == 0) - return temp; - else - return ++eol; + /* if (eol == 0) */ + /* return temp; */ + /* else */ + /* return ++eol; */ + return eol; } -void expire(int sig) +int main() { - XEvent event; - event.type = ButtonPress; - event.xbutton.button = (sig == SIGUSR2) ? (ACTION_BUTTON) : (DISMISS_BUTTON); - XSendEvent(dpy, window, 0, 0, &event); - XFlush(dpy); -} - -int main(int argc, char *argv[]) -{ - if (argc == 1) - die("Usage: %s body", argv[0]); - - struct sigaction act_expire, act_ignore; - - act_expire.sa_handler = expire; - act_expire.sa_flags = SA_RESTART; - sigemptyset(&act_expire.sa_mask); - - act_ignore.sa_handler = SIG_IGN; - act_ignore.sa_flags = 0; - sigemptyset(&act_ignore.sa_mask); - - sigaction(SIGALRM, &act_expire, 0); - sigaction(SIGTERM, &act_expire, 0); - sigaction(SIGINT, &act_expire, 0); - - sigaction(SIGUSR1, &act_ignore, 0); - sigaction(SIGUSR2, &act_ignore, 0); - if (!(dpy = XOpenDisplay(0))) die("Cannot open display"); @@ -142,9 +116,13 @@ int main(int argc, char *argv[]) XftFont *font = XftFontOpenName(dpy, screen, font_pattern); - for (int i = 1; i < argc; i++) + static char *buf = NULL; + static size_t size = 0; + char *line = NULL; + while (getline(&buf, &size, stdin) > 0) { - for (unsigned int eol = get_max_len(argv[i], font, max_text_width); eol; argv[i] += eol, num_of_lines++, eol = get_max_len(argv[i], font, max_text_width)) + line=buf; + for (unsigned int eol = get_max_len(line, font, max_text_width); eol; line += eol, num_of_lines++, eol = get_max_len(line, font, max_text_width)) { if (lines_size <= num_of_lines) { @@ -157,42 +135,53 @@ int main(int argc, char *argv[]) if (!lines[num_of_lines]) die("malloc failed"); - strncpy(lines[num_of_lines], argv[i], eol); + strncpy(lines[num_of_lines], line, eol); lines[num_of_lines][eol] = '\0'; } } + free(buf); + if (num_of_lines == 0) + die("stdin is empty"); unsigned int text_height = font->ascent - font->descent; unsigned int height = (num_of_lines - 1) * line_spacing + num_of_lines * text_height + 2 * padding; - int x, y, di; + int x, y, di, x_offset, y_offset; unsigned int du; Window dw; if (!XQueryPointer(dpy, window, &dw, &dw, &x, &y, &di, &di, &du)) - die("Could not query pointer position"); + die("Could not query pointer position"); #ifdef XINERAMA XineramaScreenInfo *info; int n, i=0; - // TODO: Better error handling like dmenu if ((info = XineramaQueryScreens(dpy, &n))){ for (i = 0; i < n; i++) if (INTERSECT(x, y, 1, 1, info[i]) != 0) break; - int x_offset = info[i].x_org; - int y_offset = info[i].y_org; + x_offset = info[i].x_org; + y_offset = info[i].y_org; mh = info[i].height; mw = info[i].width; XFree(info); - - // Correct on monitor boundary - if (y - y_offset + height > mh) - y -= MAX((y-y_offset+height)-mh,0); - if (x - x_offset + width > mw) - x -= MAX((x-x_offset+width)-mw, 0); - } + } else #endif + { + x_offset = 0; + y_offset = 0; + mw = DisplayWidth(dpy, screen); + mh = DisplayHeight(dpy, screen); + } + + // Correct on monitor boundary + int by = y - y_offset + height + border_size + MIN_BORDER_DISTANCE; + int rx = x - x_offset + width + border_size + MIN_BORDER_DISTANCE; + if (by > mh) + y = MAX(y-(by-mh), 0); + if (rx > mw) + x = MAX(x-(rx-mw), 0); + window = XCreateWindow(dpy, RootWindow(dpy, screen), x, y, width, height, border_size, DefaultDepth(dpy, screen), CopyFromParent, visual, CWOverrideRedirect | CWBackPixel | CWBorderPixel, &attributes); @@ -202,9 +191,6 @@ int main(int argc, char *argv[]) XSelectInput(dpy, window, ExposureMask | ButtonPress); XMapWindow(dpy, window); - sigaction(SIGUSR1, &act_expire, 0); - sigaction(SIGUSR2, &act_expire, 0); - for (;;) { XEvent event; @@ -217,14 +203,18 @@ int main(int argc, char *argv[]) XftDrawStringUtf8(draw, &color, font, padding, line_spacing * i + text_height * (i + 1) + padding, (FcChar8 *)lines[i], strlen(lines[i])); } - /* else if (event.type == ButtonPress) */ else if (event.type == ButtonPress) { if (event.xbutton.button == DISMISS_BUTTON) break; - else if (event.xbutton.button == ACTION_BUTTON) + else if (event.xbutton.button == ACTION1_BUTTON) { - exit_code = EXIT_ACTION; + exit_code = EXIT_ACTION1; + break; + } + else if (event.xbutton.button == ACTION2_BUTTON) + { + exit_code = EXIT_ACTION2; break; } }