├── .gitignore
├── LICENSE
├── Makefile
├── README.md
├── config.def.h
└── herbe.c
/.gitignore:
--------------------------------------------------------------------------------
1 | herbe
2 | config.h
3 | .ccls-cache
4 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2020 Samuel Dudík
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/Makefile:
--------------------------------------------------------------------------------
1 | CFLAGS = -Wall -Wextra -pedantic -lX11 -lXft -I/usr/include/freetype2 -pthread
2 |
3 | PREFIX ?= /usr/local
4 | CC ?= cc
5 |
6 | all: herbe
7 |
8 | config.h: config.def.h
9 | cp config.def.h config.h
10 |
11 | herbe: herbe.c config.h
12 | $(CC) herbe.c $(CFLAGS) -o herbe
13 |
14 | install: herbe
15 | mkdir -p ${DESTDIR}${PREFIX}/bin
16 | cp -f herbe ${DESTDIR}${PREFIX}/bin
17 |
18 | uninstall:
19 | rm -f ${DESTDIR}${PREFIX}/bin/herbe
20 |
21 | clean:
22 | rm -f herbe
23 |
24 | .PHONY: all install uninstall clean
25 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # 🌱 herbe
2 | > Daemon-less notifications without D-Bus. Minimal and lightweight.
3 |
4 |
5 |
6 |
7 |
8 | ## Features
9 | * Under 200 lines of code
10 | * Doesn't run in the background, just displays the notification and exits
11 | * No external dependencies except Xlib and Xft
12 | * Configurable through `config.h` or Xresources ([using this patch](https://github.com/dudik/herbe/pull/11))
13 | * [Actions support](#actions)
14 | * Extensible through [patches](https://github.com/dudik/herbe/pulls?q=is%3Aopen+is%3Apr+label%3Apatch)
15 |
16 | ## Table of contents
17 |
18 | * [Usage](#usage)
19 | * [Patches](#patches)
20 | * [Dismiss a notification](#dismiss-a-notification)
21 | * [Actions](#actions)
22 | * [Newlines](#newlines)
23 | * [Multiple notifications](#multiple-notifications)
24 | * [Notifications don't show up](#notifications-dont-show-up)
25 | * [Installation](#installation)
26 | * [Packages](#packages)
27 | * [Dependencies](#dependencies)
28 | * [Build](#build)
29 | * [Configuration](#configuration)
30 | * [Contribute](#contribute)
31 |
32 | ## Usage
33 |
34 | ### Patches
35 | [List of available patches](https://github.com/dudik/herbe/pulls?q=is%3Aopen+is%3Apr+label%3Apatch)
36 |
37 | To create a new patch you'll have to open a pull request with your changes. Append `.diff` to the pull request URL to get a downloadable diff file. Don't forget to prefix the title with `patch:` and to apply the `patch` label to it. For inspiration, look at [my Xresources patch](https://github.com/dudik/herbe/pull/11). Thank you.
38 |
39 | _Note: This patching method was heavily inspired by [dylan's sowm](https://github.com/dylanaraps/sowm)._
40 |
41 | ### Dismiss a notification
42 | A notification can be dismissed either by clicking on it with `DISMISS_BUTTON` (set in config.h, defaults to left mouse button) or sending a `SIGUSR1` signal to it:
43 | ```shell
44 | $ pkill -SIGUSR1 herbe
45 | ```
46 | Dismissed notifications return exit code 2.
47 |
48 | ### Actions
49 | Action is a piece of shell code that runs when a notification gets accepted. Accepting a notification is the same as dismissing it, but you have to use either `ACTION_BUTTON` (defaults to right mouse button) or the `SIGUSR2` signal.
50 | An accepted notification always returns exit code 0. To specify an action:
51 | ```shell
52 | $ herbe "Notification body" && echo "This is an action"
53 | ```
54 | Where everything after `&&` is the action and will get executed after the notification gets accepted.
55 |
56 | ### Newlines
57 | Every command line argument gets printed on a separate line by default e.g.:
58 | ```shell
59 | $ herbe "First line" "Second line" "Third line" ...
60 | ```
61 | You can also use `\n` e.g. in `bash`:
62 | ```shell
63 | $ herbe $'First line\nSecond line\nThird line'
64 | ```
65 | But by default `herbe` prints `\n` literally:
66 | ```shell
67 | $ herbe "First line\nStill the first line"
68 | ```
69 | Output of other programs will get printed correctly, just make sure to escape it (so you don't end up with every word on a separate line):
70 | ```shell
71 | $ herbe "$(ps axch -o cmd:15,%cpu --sort=-%cpu | head)"
72 | ```
73 |
74 | ### Multiple notifications
75 | Notifications are put in a queue and shown one after another in order of creation (first in, first out). They don't overlap and each one is shown for its entire duration.
76 |
77 | ### Notifications don't show up
78 | Most likely a running notification got terminated forcefully (SIGKILL or any uncaught signal) which caused the semaphore not getting unlocked. First, kill any `herbe` instance that is stuck:
79 | ```shell
80 | $ pkill -SIGKILL herbe
81 | ```
82 | Then just call `herbe` without any arguments:
83 | ```shell
84 | $ herbe
85 | ```
86 | Notifications should now show up as expected.
87 |
88 | Don't ever send any signals to `herbe` except these:
89 | ```shell
90 | # same as pkill -SIGTERM herbe, terminates every running herbe process
91 | $ pkill herbe
92 |
93 | $ pkill -SIGUSR1 herbe
94 | $ pkill -SIGUSR2 herbe
95 | ```
96 | And you should be fine. That's all you really need to interact with `herbe`.
97 |
98 | ## Installation
99 | ### Packages
100 | [](https://repology.org/project/herbe/versions)
101 |
102 | [OpenBSD patch](https://github.com/dudik/herbe/pull/4)
103 |
104 | [FreeBSD patch](https://github.com/dudik/herbe/pull/16)
105 |
106 | [Wayland port](https://github.com/muevoid/Wayherb) by [muevoid](https://github.com/muevoid)
107 |
108 | **Only the [herbe-git AUR package](https://aur.archlinux.org/packages/herbe-git/) is maintained by me.**
109 |
110 | ### Dependencies
111 | * X11 (Xlib)
112 | * Xft
113 |
114 | The names of packages are different depending on which distribution you use.
115 | For example, if you use [Void Linux](https://voidlinux.org/) you will have to install these dependencies:
116 | ```shell
117 | sudo xbps-install base-devel libX11-devel libXft-devel
118 | ```
119 |
120 | ### Build
121 | ```shell
122 | git clone https://github.com/dudik/herbe
123 | cd herbe
124 | sudo make install
125 | ```
126 | `make install` requires root privileges because it copies the resulting binary to `/usr/local/bin`. This makes `herbe` accessible globally.
127 |
128 | You can also use `make clean` to remove the binary from the build folder, `sudo make uninstall` to remove the binary from `/usr/local/bin` or just `make` to build the binary locally.
129 |
130 | ## Configuration
131 | herbe is configured at compile-time by editing `config.h`. Every option should be self-explanatory. There is no `height` option because height is determined by font size and text padding.
132 |
133 | [Xresources patch](https://github.com/dudik/herbe/pull/11)
134 |
135 | ## Contribute
136 | If you want to report a bug or you have a feature request, feel free to [open an issue](https://github.com/dudik/herbe/issues).
137 |
138 | ## Projects with herbe integration
139 | - [qutebrowser](https://qutebrowser.org/) supports showing web notifications via herbe, via the `content.notifications.presenter` setting.
140 |
--------------------------------------------------------------------------------
/config.def.h:
--------------------------------------------------------------------------------
1 | static const char *background_color = "#3e3e3e";
2 | static const char *border_color = "#ececec";
3 | static const char *font_color = "#ececec";
4 | static const char *font_pattern = "monospace:size=10";
5 | static const unsigned line_spacing = 5;
6 | static const unsigned int padding = 15;
7 |
8 | static const unsigned int width = 450;
9 | static const unsigned int border_size = 2;
10 | static const unsigned int pos_x = 30;
11 | static const unsigned int pos_y = 60;
12 |
13 | enum corners { TOP_LEFT, TOP_RIGHT, BOTTOM_LEFT, BOTTOM_RIGHT };
14 | enum corners corner = TOP_RIGHT;
15 |
16 | static const unsigned int duration = 5; /* in seconds */
17 |
18 | #define DISMISS_BUTTON Button1
19 | #define ACTION_BUTTON Button3
20 |
--------------------------------------------------------------------------------
/herbe.c:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | #include
4 | #include
5 | #include
6 | #include
7 | #include
8 | #include
9 | #include
10 | #include
11 |
12 | #include "config.h"
13 |
14 | #define EXIT_ACTION 0
15 | #define EXIT_FAIL 1
16 | #define EXIT_DISMISS 2
17 |
18 | Display *display;
19 | Window window;
20 | int exit_code = EXIT_DISMISS;
21 |
22 | static void die(const char *format, ...)
23 | {
24 | va_list ap;
25 | va_start(ap, format);
26 | vfprintf(stderr, format, ap);
27 | fprintf(stderr, "\n");
28 | va_end(ap);
29 | exit(EXIT_FAIL);
30 | }
31 |
32 | int get_max_len(char *string, XftFont *font, int max_text_width)
33 | {
34 | int eol = strlen(string);
35 | XGlyphInfo info;
36 | XftTextExtentsUtf8(display, font, (FcChar8 *)string, eol, &info);
37 |
38 | if (info.width > max_text_width)
39 | {
40 | eol = max_text_width / font->max_advance_width;
41 | info.width = 0;
42 |
43 | while (info.width < max_text_width)
44 | {
45 | eol++;
46 | XftTextExtentsUtf8(display, font, (FcChar8 *)string, eol, &info);
47 | }
48 |
49 | eol--;
50 | }
51 |
52 | for (int i = 0; i < eol; i++)
53 | if (string[i] == '\n')
54 | {
55 | string[i] = ' ';
56 | return ++i;
57 | }
58 |
59 | if (info.width <= max_text_width)
60 | return eol;
61 |
62 | int temp = eol;
63 |
64 | while (string[eol] != ' ' && eol)
65 | --eol;
66 |
67 | if (eol == 0)
68 | return temp;
69 | else
70 | return ++eol;
71 | }
72 |
73 | void expire(int sig)
74 | {
75 | XEvent event;
76 | event.type = ButtonPress;
77 | event.xbutton.button = (sig == SIGUSR2) ? (ACTION_BUTTON) : (DISMISS_BUTTON);
78 | XSendEvent(display, window, 0, 0, &event);
79 | XFlush(display);
80 | }
81 |
82 | int main(int argc, char *argv[])
83 | {
84 | if (argc == 1)
85 | {
86 | sem_unlink("/herbe");
87 | die("Usage: %s body", argv[0]);
88 | }
89 |
90 | struct sigaction act_expire, act_ignore;
91 |
92 | act_expire.sa_handler = expire;
93 | act_expire.sa_flags = SA_RESTART;
94 | sigemptyset(&act_expire.sa_mask);
95 |
96 | act_ignore.sa_handler = SIG_IGN;
97 | act_ignore.sa_flags = 0;
98 | sigemptyset(&act_ignore.sa_mask);
99 |
100 | sigaction(SIGALRM, &act_expire, 0);
101 | sigaction(SIGTERM, &act_expire, 0);
102 | sigaction(SIGINT, &act_expire, 0);
103 |
104 | sigaction(SIGUSR1, &act_ignore, 0);
105 | sigaction(SIGUSR2, &act_ignore, 0);
106 |
107 | if (!(display = XOpenDisplay(0)))
108 | die("Cannot open display");
109 |
110 | int screen = DefaultScreen(display);
111 | Visual *visual = DefaultVisual(display, screen);
112 | Colormap colormap = DefaultColormap(display, screen);
113 |
114 | int screen_width = DisplayWidth(display, screen);
115 | int screen_height = DisplayHeight(display, screen);
116 |
117 | XSetWindowAttributes attributes;
118 | attributes.override_redirect = True;
119 | XftColor color;
120 | XftColorAllocName(display, visual, colormap, background_color, &color);
121 | attributes.background_pixel = color.pixel;
122 | XftColorAllocName(display, visual, colormap, border_color, &color);
123 | attributes.border_pixel = color.pixel;
124 |
125 | int num_of_lines = 0;
126 | int max_text_width = width - 2 * padding;
127 | int lines_size = 5;
128 | char **lines = malloc(lines_size * sizeof(char *));
129 | if (!lines)
130 | die("malloc failed");
131 |
132 | XftFont *font = XftFontOpenName(display, screen, font_pattern);
133 |
134 | for (int i = 1; i < argc; i++)
135 | {
136 | 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))
137 | {
138 | if (lines_size <= num_of_lines)
139 | {
140 | lines = realloc(lines, (lines_size += 5) * sizeof(char *));
141 | if (!lines)
142 | die("realloc failed");
143 | }
144 |
145 | lines[num_of_lines] = malloc((eol + 1) * sizeof(char));
146 | if (!lines[num_of_lines])
147 | die("malloc failed");
148 |
149 | strncpy(lines[num_of_lines], argv[i], eol);
150 | lines[num_of_lines][eol] = '\0';
151 | }
152 | }
153 |
154 | unsigned int x = pos_x;
155 | unsigned int y = pos_y;
156 | unsigned int text_height = font->ascent - font->descent;
157 | unsigned int height = (num_of_lines - 1) * line_spacing + num_of_lines * text_height + 2 * padding;
158 |
159 | if (corner == TOP_RIGHT || corner == BOTTOM_RIGHT)
160 | x = screen_width - width - border_size * 2 - pos_x;
161 |
162 | if (corner == BOTTOM_LEFT || corner == BOTTOM_RIGHT)
163 | y = screen_height - height - border_size * 2 - pos_y;
164 |
165 | window = XCreateWindow(display, RootWindow(display, screen), x, y, width, height, border_size, DefaultDepth(display, screen),
166 | CopyFromParent, visual, CWOverrideRedirect | CWBackPixel | CWBorderPixel, &attributes);
167 |
168 | XftDraw *draw = XftDrawCreate(display, window, visual, colormap);
169 | XftColorAllocName(display, visual, colormap, font_color, &color);
170 |
171 | XSelectInput(display, window, ExposureMask | ButtonPress);
172 | XMapWindow(display, window);
173 |
174 | sem_t *mutex = sem_open("/herbe", O_CREAT, 0644, 1);
175 | sem_wait(mutex);
176 |
177 | sigaction(SIGUSR1, &act_expire, 0);
178 | sigaction(SIGUSR2, &act_expire, 0);
179 |
180 | if (duration != 0)
181 | alarm(duration);
182 |
183 | for (;;)
184 | {
185 | XEvent event;
186 | XNextEvent(display, &event);
187 |
188 | if (event.type == Expose)
189 | {
190 | XClearWindow(display, window);
191 | for (int i = 0; i < num_of_lines; i++)
192 | XftDrawStringUtf8(draw, &color, font, padding, line_spacing * i + text_height * (i + 1) + padding,
193 | (FcChar8 *)lines[i], strlen(lines[i]));
194 | }
195 | else if (event.type == ButtonPress)
196 | {
197 | if (event.xbutton.button == DISMISS_BUTTON)
198 | break;
199 | else if (event.xbutton.button == ACTION_BUTTON)
200 | {
201 | exit_code = EXIT_ACTION;
202 | break;
203 | }
204 | }
205 | }
206 |
207 | sem_post(mutex);
208 | sem_close(mutex);
209 |
210 | for (int i = 0; i < num_of_lines; i++)
211 | free(lines[i]);
212 |
213 | free(lines);
214 | XftDrawDestroy(draw);
215 | XftColorFree(display, visual, colormap, &color);
216 | XftFontClose(display, font);
217 | XCloseDisplay(display);
218 |
219 | return exit_code;
220 | }
--------------------------------------------------------------------------------