├── screenshot.png ├── .gitmodules ├── .gitignore ├── mauncher-win.h ├── mauncher-ipc.h ├── sysutil.h ├── README.md ├── Makefile ├── mauncher-ipc.c ├── sysutil.c ├── mauncher-win.c ├── mauncher.c ├── mauncher-launcher.c └── LICENSE /screenshot.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mortie/mauncher/HEAD/screenshot.png -------------------------------------------------------------------------------- /.gitmodules: -------------------------------------------------------------------------------- 1 | [submodule "gtk-layer-shell"] 2 | path = gtk-layer-shell 3 | url = https://github.com/wmww/gtk-layer-shell.git 4 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /mauncher 2 | /mauncher-launcher 3 | /gtk-layer-shell 4 | /mauncher.o 5 | /mauncher-win.o 6 | /mauncher-ipc.o 7 | /mauncher-launcher.o 8 | /sysutil.o 9 | -------------------------------------------------------------------------------- /mauncher-win.h: -------------------------------------------------------------------------------- 1 | #ifndef MAUNCHER_WIN 2 | #define MAUNCHER_WIN 3 | 4 | #include 5 | 6 | struct mauncher_win_opts { 7 | gchar *prompt; 8 | gboolean insensitive; 9 | }; 10 | 11 | void mauncher_win_run( 12 | GtkApplication *app, char *input, struct mauncher_win_opts opts, 13 | void (*callback)(const char *output, int status, void *data), void *data); 14 | 15 | #endif 16 | -------------------------------------------------------------------------------- /mauncher-ipc.h: -------------------------------------------------------------------------------- 1 | #ifndef MAUNCHER_IPC_H 2 | #define MAUNCHER_IPC_H 3 | 4 | #include "mauncher-win.h" 5 | 6 | struct daemon_message { 7 | char *payload; 8 | struct mauncher_win_opts opts; 9 | }; 10 | 11 | struct daemon_reply { 12 | char *reply; 13 | int status; 14 | }; 15 | 16 | int daemon_message_read(int fd, struct daemon_message *msg); 17 | int daemon_reply_read(int fd, struct daemon_reply *reply); 18 | int daemon_message_write(int fd, struct daemon_message *msg); 19 | int daemon_reply_write(int fd, struct daemon_reply *reply); 20 | 21 | #endif 22 | -------------------------------------------------------------------------------- /sysutil.h: -------------------------------------------------------------------------------- 1 | #ifndef SYSUTIL_H 2 | #define SYSUTIL_H 3 | 4 | #include 5 | #include 6 | 7 | char *string_concat(char **strs); 8 | char **string_split(char *str, char c, size_t *len); 9 | char **bs_lookup(const char *prefix, char **strs, size_t len, int (*cmp)(const char *a, const char *b, size_t n)); 10 | char *read_all(int fd, size_t *len); 11 | char *read_until(int fd, char c, size_t *len); 12 | 13 | uint64_t read_uint64(uint8_t buf[8]); 14 | void write_uint64(uint8_t buf[8], uint64_t num); 15 | uint32_t read_uint32(uint8_t buf[4]); 16 | void write_uint32(uint8_t buf[4], uint32_t num); 17 | 18 | char *xdg_runtime_dir(); 19 | char *xdg_data_dirs(); 20 | char *xdg_config_dirs(); 21 | char *xdg_config_home(); 22 | char *xdg_cache_home(); 23 | char *xdg_data_home(); 24 | 25 | #endif 26 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Mauncher 2 | 3 | Mauncher is a GTK-based alternative to dmenu for Wayland which supports display 4 | scaling. 5 | 6 | ![Screenshot](https://raw.githubusercontent.com/mortie/mauncher/master/screenshot.png) 7 | 8 | ## Installation 9 | 10 | ### From Package 11 | 12 | The [mauncher-git](https://aur.archlinux.org/packages/mauncher-git/) package is 13 | available for Arch Linux. 14 | 15 | ### Compiling From Source 16 | 17 | Run `make` to compile, `sudo make install` to install, and `sudo make uninstall` 18 | to uninstall. 19 | 20 | Dependencies: 21 | 22 | * meson 23 | * git 24 | * gtk3 25 | * gobject-introspection 26 | 27 | ## Usage 28 | 29 | Mauncher comes with a launcher called `mauncher-launcher`, 30 | so running `mauncher-launcher` will start a launcher which lists desktop files, 31 | supports math (through a python interpreter), running shell commands by 32 | prefixing the string with a `$`, etc. 33 | 34 | Otherwise, mauncher works like dmenu; give it a newline-separated list of strings on 35 | stdin, the user selects an item, and that item is printed to stdout. 36 | 37 | Just as you would use `dmenu_path | dmenu | sh` to use dmenu as a launcher, you 38 | can use `dmenu_path | mauncher | sh` to use mauncher as a launcher. 39 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | CFLAGS += \ 2 | -Wall -D_GNU_SOURCE -O2 -Igtk-layer-shell/usr/include \ 3 | $(shell pkg-config --cflags gtk+-3.0 wayland-client) 4 | LDFLAGS += \ 5 | -Wl,--no-as-needed -lpthread \ 6 | $(shell pkg-config --libs gtk+-3.0 wayland-client) 7 | PREFIX ?= /usr/local 8 | 9 | all: mauncher mauncher-launcher 10 | 11 | mauncher: mauncher.o mauncher-win.o mauncher-ipc.o sysutil.o gtk-layer-shell/usr/lib/libgtk-layer-shell.a 12 | mauncher.o: sysutil.h mauncher-win.h mauncher-ipc.h 13 | mauncher-win.o: mauncher-win.h sysutil.h gtk-layer-shell/usr/lib/libgtk-layer-shell.a 14 | mauncher-ipc.o: mauncher-ipc.h mauncher-win.h 15 | 16 | mauncher-launcher: mauncher-launcher.o sysutil.o 17 | mauncher-launcher.o: sysutil.h 18 | 19 | sysutil.o: sysutil.h 20 | 21 | gtk-layer-shell/usr/lib/libgtk-layer-shell.a: 22 | [ -f gtk-layer-shell/.git ] || git submodule update --init gtk-layer-shell 23 | (cd gtk-layer-shell && \ 24 | meson build --prefix /usr -Ddefault_library=static && \ 25 | ninja -C build && \ 26 | DESTDIR=`pwd` ninja -C build install) 27 | 28 | .PHONY: clean 29 | clean: 30 | rm -f mauncher mauncher-launcher *.o 31 | 32 | .PHONY: cleanall 33 | cleanall: clean 34 | rm -rf gtk-layer-shell 35 | 36 | .PHONY: install 37 | install: mauncher mauncher-launcher 38 | mkdir -p $(DESTDIR)$(PREFIX)/bin 39 | cp -f $^ $(DESTDIR)$(PREFIX)/bin 40 | chmod 755 $(DESTDIR)$(PREFIX)/bin/mauncher 41 | chmod 755 $(DESTDIR)$(PREFIX)/bin/mauncher-launcher 42 | 43 | .PHONY: uninstall 44 | uninstall: 45 | rm -f $(DESTDIR)$(PREFIX)/bin/mauncher 46 | rm -f $(DESTDIR)$(PREFIX)/bin/mauncher-launcher 47 | -------------------------------------------------------------------------------- /mauncher-ipc.c: -------------------------------------------------------------------------------- 1 | #include "mauncher-ipc.h" 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | #include "sysutil.h" 9 | 10 | #define checklen(buf, len, l) do { \ 11 | len -= l; \ 12 | if (len < 0) { \ 13 | fprintf(stderr, "Received malformed message.\n"); \ 14 | free(buf); \ 15 | return -1; \ 16 | } \ 17 | } while (0) 18 | 19 | int daemon_message_read(int fd, struct daemon_message *msg) { 20 | uint8_t total_len_buf[8]; 21 | ssize_t n = read(fd, total_len_buf, sizeof(total_len_buf)); 22 | if (n < 0) { 23 | perror("read"); 24 | return -1; 25 | } else if (n < sizeof(total_len_buf)) { 26 | fprintf(stderr, "Short read: %zu/%zu\n", n, sizeof(total_len_buf)); 27 | return -1; 28 | } 29 | 30 | ssize_t len = (ssize_t)read_uint64(total_len_buf); 31 | 32 | uint8_t *buf = malloc(len); 33 | uint8_t *b = buf; 34 | if (buf == NULL) { 35 | perror("malloc"); 36 | return -1; 37 | } 38 | 39 | n = read(fd, buf, len); 40 | if (n < 0) { 41 | perror("read"); 42 | free(buf); 43 | return FALSE; 44 | } else if (n < len) { 45 | fprintf(stderr, "Short read: %zu/%zu\n", n, len); 46 | free(buf); 47 | return -1; 48 | } 49 | 50 | // uint64 payload length 51 | checklen(buf, len, 8); 52 | uint64_t payload_len = read_uint64(b); 53 | b += 8; 54 | 55 | // payload 56 | checklen(buf, len, payload_len); 57 | char *payload = (char *)b; 58 | b += payload_len; 59 | 60 | // uint32 prompt length 61 | checklen(buf, len, 4); 62 | uint32_t prompt_len = read_uint32(b); 63 | b += 4; 64 | 65 | // prompt 66 | checklen(buf, len, prompt_len); 67 | char *prompt = (char *)b; 68 | b += prompt_len; 69 | 70 | // flags 71 | checklen(buf, len, 1); 72 | uint8_t flags = b[0]; 73 | b += 1; 74 | 75 | msg->payload = strndup(payload, payload_len); 76 | msg->opts.prompt = strndup(prompt, prompt_len); 77 | msg->opts.insensitive = flags & (1 << 0); 78 | free(buf); 79 | 80 | return 0; 81 | } 82 | 83 | int daemon_reply_read(int fd, struct daemon_reply *reply) { 84 | uint8_t total_len_buf[8]; 85 | ssize_t n = read(fd, total_len_buf, sizeof(total_len_buf)); 86 | if (n < 0) { 87 | perror("read"); 88 | return -1; 89 | } else if (n == 0) { 90 | fprintf(stderr, "Short read: %zu/%zu\n", n, sizeof(total_len_buf)); 91 | return -1; 92 | } 93 | 94 | ssize_t len = (ssize_t)read_uint64(total_len_buf); 95 | 96 | uint8_t *buf = malloc(len); 97 | uint8_t *b = buf; 98 | if (buf == NULL) { 99 | perror("malloc"); 100 | return -1; 101 | } 102 | 103 | n = read(fd, buf, len); 104 | if (n < 0) { 105 | perror("read"); 106 | free(buf); 107 | return FALSE; 108 | } else if (n < len) { 109 | fprintf(stderr, "Short read: %zu/%zu\n", n, len); 110 | free(buf); 111 | return -1; 112 | } 113 | 114 | // uint64 reply length 115 | checklen(buf, len, 8); 116 | uint64_t reply_len = read_uint64(b); 117 | b += 8; 118 | 119 | // reply 120 | checklen(buf, len, reply_len); 121 | char *reply_str = (char *)b; 122 | b += reply_len; 123 | 124 | // status 125 | int status = (int)read_uint32(b); 126 | 127 | reply->reply = strndup(reply_str, reply_len); 128 | reply->status = status; 129 | 130 | free(buf); 131 | return 0; 132 | } 133 | 134 | #undef checklen 135 | 136 | int daemon_message_write(int fd, struct daemon_message *msg) { 137 | uint64_t payload_len = msg->payload == NULL ? 0 : strlen(msg->payload); 138 | uint32_t prompt_len = msg->opts.prompt == NULL ? 0 : (uint32_t)strlen(msg->opts.prompt); 139 | uint64_t len = 8 + payload_len + 4 + prompt_len + 1; 140 | 141 | uint8_t *buf = malloc(len); 142 | uint8_t *b = buf; 143 | if (buf == NULL) { 144 | perror("malloc"); 145 | return -1; 146 | } 147 | 148 | uint8_t len_buf[8]; 149 | write_uint64(len_buf, len); 150 | if (write(fd, len_buf, 8) < 0) { 151 | perror("write"); 152 | free(buf); 153 | return -1; 154 | } 155 | 156 | write_uint64(b, payload_len); 157 | b += 8; 158 | 159 | memcpy(b, msg->payload, payload_len); 160 | b += payload_len; 161 | 162 | write_uint32(b, prompt_len); 163 | b += 4; 164 | 165 | memcpy(b, msg->opts.prompt, prompt_len); 166 | b += prompt_len; 167 | 168 | b[0] = !!msg->opts.insensitive << 0; 169 | 170 | if (write(fd, buf, len) < 0) { 171 | perror("write"); 172 | free(buf); 173 | return -1; 174 | } 175 | 176 | free(buf); 177 | return 0; 178 | } 179 | 180 | int daemon_reply_write(int fd, struct daemon_reply *reply) { 181 | uint64_t reply_len = reply->reply == NULL ? 0 : strlen(reply->reply); 182 | uint64_t len = 8 + reply_len + 4; 183 | 184 | uint8_t *buf = malloc(len); 185 | uint8_t *b = buf; 186 | if (buf == NULL) { 187 | perror("malloc"); 188 | return -1; 189 | } 190 | 191 | uint8_t len_buf[8]; 192 | write_uint64(len_buf, len); 193 | if (write(fd, len_buf, 8) < 0) { 194 | perror("write"); 195 | free(buf); 196 | return -1; 197 | } 198 | 199 | write_uint64(b, reply_len); 200 | b += 8; 201 | 202 | memcpy(b, reply->reply, reply_len); 203 | b += reply_len; 204 | 205 | write_uint32(b, (uint32_t)reply->status); 206 | b += 4; 207 | 208 | if (write(fd, buf, len) < 0) { 209 | perror("write"); 210 | free(buf); 211 | return -1; 212 | } 213 | 214 | free(buf); 215 | return 0; 216 | } 217 | -------------------------------------------------------------------------------- /sysutil.c: -------------------------------------------------------------------------------- 1 | #include "sysutil.h" 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | char *string_concat(char **strs) { 9 | size_t len = 0; 10 | for (char **s = strs; *s != NULL; ++s) { 11 | len += strlen(*s); 12 | } 13 | 14 | char *str = malloc(len + 1); 15 | char *strptr = str; 16 | for (char **s = strs; *s != NULL; ++s) { 17 | strcpy(strptr, *s); 18 | strptr += strlen(*s); 19 | } 20 | 21 | *strptr = '\0'; 22 | return str; 23 | } 24 | 25 | char **string_split(char *str, char c, size_t *len) { 26 | *len = 0; 27 | size_t size = 512; 28 | char **strs = malloc(size * sizeof(*strs)); 29 | 30 | while (1) { 31 | char *end = strchr(str, c); 32 | if (end == NULL) 33 | break; 34 | 35 | if (*len >= size - 2) { 36 | size *= 2; 37 | strs = realloc(strs, size * sizeof(*strs)); 38 | } 39 | 40 | strs[(*len)++] = str; 41 | *end = '\0'; 42 | str = end + 1; 43 | } 44 | 45 | strs[*len] = NULL; 46 | 47 | return strs; 48 | } 49 | 50 | char *read_until(int fd, char c, size_t *len) { 51 | char buf[1024]; 52 | size_t size = 1024; 53 | *len = 0; 54 | char *mem = malloc(size); 55 | 56 | while (1) { 57 | ssize_t l = read(fd, buf, sizeof(buf)); 58 | if (l < 0) { 59 | perror("read"); 60 | free(mem); 61 | return NULL; 62 | } else if (l == 0) { 63 | break; 64 | } 65 | 66 | char *found = memchr(buf, c, l); 67 | if (found) 68 | l = found - buf; 69 | 70 | if (*len + l >= size - 2) { 71 | size *= 2; 72 | mem = realloc(mem, size); 73 | } 74 | 75 | memcpy(mem + *len, buf, l); 76 | *len += l; 77 | 78 | if (found) 79 | break; 80 | } 81 | 82 | mem[*len] = '\0'; 83 | 84 | return mem; 85 | } 86 | 87 | char *read_all(int fd, size_t *len) { 88 | char buf[1024]; 89 | size_t size = 1024; 90 | *len = 0; 91 | char *mem = malloc(size); 92 | 93 | while (1) { 94 | ssize_t l = read(fd, buf, sizeof(buf)); 95 | if (l < 0) { 96 | perror("read"); 97 | free(mem); 98 | return NULL; 99 | } else if (l == 0) { 100 | break; 101 | } 102 | 103 | if (*len + l >= size - 2) { 104 | size *= 2; 105 | mem = realloc(mem, size); 106 | } 107 | 108 | memcpy(mem + *len, buf, l); 109 | *len += l; 110 | } 111 | 112 | mem[*len] = '\0'; 113 | 114 | return mem; 115 | } 116 | 117 | char **bs_lookup( 118 | const char *prefix, char **strs, size_t len, 119 | int (*cmp)(const char *a, const char *b, size_t n)) { 120 | if (prefix[0] == '\0') 121 | return strs; 122 | if (len == 0) 123 | return NULL; 124 | 125 | ssize_t pfxlen = strlen(prefix); 126 | ssize_t start = 0; 127 | ssize_t end = len - 1; 128 | ssize_t index; 129 | int logcount = 0; 130 | while (1) { 131 | logcount += 1; 132 | index = start + (end - start) / 2; 133 | char *str = strs[index]; 134 | 135 | int ret = cmp(str, prefix, pfxlen); 136 | if (ret == 0) { 137 | if (index > 0 && cmp(strs[index - 1], prefix, pfxlen) == 0) 138 | end = index; 139 | else 140 | break; 141 | } else if (ret < 0) { 142 | start = index + 1; 143 | } else { 144 | end = index - 1; 145 | } 146 | 147 | if (end < start || end == 0) 148 | return NULL; 149 | } 150 | 151 | return strs + index; 152 | } 153 | 154 | uint64_t read_uint64(uint8_t buf[8]) { 155 | return 0 | 156 | (uint64_t)buf[0] << 56 | 157 | (uint64_t)buf[1] << 48 | 158 | (uint64_t)buf[2] << 40 | 159 | (uint64_t)buf[3] << 32 | 160 | (uint64_t)buf[4] << 24 | 161 | (uint64_t)buf[5] << 16 | 162 | (uint64_t)buf[6] << 8 | 163 | (uint64_t)buf[7] << 0; 164 | } 165 | 166 | void write_uint64(uint8_t buf[8], uint64_t num) { 167 | buf[0] = num >> 56; 168 | buf[1] = num >> 48; 169 | buf[2] = num >> 40; 170 | buf[3] = num >> 32; 171 | buf[4] = num >> 24; 172 | buf[5] = num >> 16; 173 | buf[6] = num >> 8; 174 | buf[7] = num >> 0; 175 | } 176 | 177 | uint32_t read_uint32(uint8_t buf[4]) { 178 | return 0 | 179 | (uint32_t)buf[0] << 24 | 180 | (uint32_t)buf[1] << 16 | 181 | (uint32_t)buf[2] << 8 | 182 | (uint32_t)buf[3] << 0; 183 | } 184 | 185 | void write_uint32(uint8_t buf[4], uint32_t num) { 186 | buf[0] = num >> 24; 187 | buf[1] = num >> 16; 188 | buf[2] = num >> 8; 189 | buf[3] = num >> 0; 190 | } 191 | 192 | char *xdg_runtime_dir() { 193 | static char *path = NULL; 194 | if (path != NULL) 195 | return path; 196 | 197 | path = getenv("XDG_RUNTIME_DIR"); 198 | if (path != NULL) 199 | return path; 200 | 201 | path = getenv("TMPDIR"); 202 | if (path != NULL) 203 | return path; 204 | 205 | return path = "/tmp"; 206 | } 207 | 208 | char *xdg_data_dirs() { 209 | static char *path = NULL; 210 | if (path != NULL) 211 | return path; 212 | 213 | path = getenv("XDG_DATA_DIRS"); 214 | if (path != NULL) 215 | return path; 216 | 217 | return path = "/usr/local/share:/usr/share"; 218 | } 219 | 220 | char *xdg_config_dirs() { 221 | static char *path = NULL; 222 | if (path != NULL) 223 | return path; 224 | 225 | path = getenv("XDG_CONFIG_DIRS"); 226 | if (path != NULL) 227 | return path; 228 | 229 | return path = "/etc/xdg"; 230 | } 231 | 232 | char *xdg_config_home() { 233 | static char *path = NULL; 234 | if (path != NULL) 235 | return path; 236 | 237 | path = getenv("XDG_CONFIG_HOME"); 238 | if (path != NULL) 239 | return path; 240 | 241 | return path = string_concat((char *[]) { getenv("HOME"), "/.config", NULL }); 242 | } 243 | 244 | char *xdg_cache_home() { 245 | static char *path = NULL; 246 | if (path != NULL) 247 | return path; 248 | 249 | path = getenv("XDG_CACHE_HOME"); 250 | if (path != NULL) 251 | return path; 252 | 253 | return path = string_concat((char *[]) { getenv("HOME"), "/.cache", NULL }); 254 | } 255 | 256 | char *xdg_data_home() { 257 | static char *path = NULL; 258 | if (path != NULL) 259 | return path; 260 | 261 | path = getenv("XDG_DATA_HOME"); 262 | if (path != NULL) 263 | return path; 264 | 265 | return path = string_concat((char *[]) { getenv("HOME"), "/.local/share", NULL }); 266 | } 267 | -------------------------------------------------------------------------------- /mauncher-win.c: -------------------------------------------------------------------------------- 1 | #include "mauncher-win.h" 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | #include "sysutil.h" 9 | 10 | struct win { 11 | GtkApplication *app; 12 | char *input; 13 | void *data; 14 | void (*callback)(const char *output, int status, void *data); 15 | 16 | GtkWidget *win; 17 | GtkWidget *container; 18 | int status; 19 | char **strs; 20 | size_t strs_len; 21 | char **cursor; 22 | char **view; 23 | 24 | gint entry_padding; 25 | gint entry_min_width; 26 | 27 | struct mauncher_win_opts opts; 28 | }; 29 | 30 | static int str_compare(const void *a, const void *b) { 31 | return strcmp(*(const char **)a, *(const char **)b); 32 | } 33 | 34 | static int str_case_compare(const void *a, const void *b) { 35 | return strcasecmp(*(const char **)a, *(const char **)b); 36 | } 37 | 38 | static void draw_list(struct win *win) { 39 | // Clear out existing children 40 | GList *iter, *children = gtk_container_get_children(GTK_CONTAINER(win->container)); 41 | for(iter = children; iter != NULL; iter = g_list_next(iter)) 42 | gtk_widget_destroy(GTK_WIDGET(iter->data)); 43 | g_list_free(children); 44 | 45 | // Draw new children 46 | if (win->cursor != NULL) { 47 | for (char **i = win->view; i < win->view + 30 && *i != NULL; ++i) { 48 | GtkWidget *label; 49 | if (i == win->cursor) { 50 | char *str = g_markup_printf_escaped( 51 | "%s", *i); 52 | label = gtk_label_new(""); 53 | gtk_label_set_markup(GTK_LABEL(label), str); 54 | } else { 55 | label = gtk_label_new(*i); 56 | } 57 | gtk_container_add(GTK_CONTAINER(win->container), label); 58 | } 59 | } 60 | 61 | gtk_widget_show_all(win->container); 62 | } 63 | 64 | static void cleanup(struct win *win) { 65 | free(win->strs); 66 | gtk_widget_destroy(win->win); 67 | free(win); 68 | } 69 | 70 | static gboolean on_enter(GtkEntry *entry, void *data) { 71 | struct win *win = (struct win *)data; 72 | 73 | if (win->cursor != NULL) 74 | win->callback(*win->cursor, win->status, win->data); 75 | else 76 | win->callback(gtk_entry_get_text(entry), win->status, win->data); 77 | 78 | cleanup(win); 79 | 80 | return FALSE; 81 | } 82 | 83 | static gboolean on_keyboard(GtkWidget *widget, GdkEventKey *event, void *data) { 84 | struct win *win = (struct win *)data; 85 | 86 | if (event->keyval == GDK_KEY_Escape) { 87 | win->status = EXIT_FAILURE; 88 | win->callback(NULL, win->status, win->data); 89 | cleanup(win); 90 | } else if (event->keyval == GDK_KEY_Left && win->cursor > win->view) { 91 | if (win->cursor > win->strs) 92 | win->cursor -= 1; 93 | draw_list(win); 94 | } else if (event->keyval == GDK_KEY_Right && win->cursor >= win->strs) { 95 | if (win->cursor[1] != NULL) 96 | win->cursor += 1; 97 | draw_list(win); 98 | } 99 | return FALSE; 100 | } 101 | 102 | static gboolean on_change(GtkEditable *editable, void *data) { 103 | struct win *win = (struct win *)data; 104 | 105 | const gchar *text = gtk_entry_get_text(GTK_ENTRY(editable)); 106 | 107 | PangoLayout *layout = gtk_widget_create_pango_layout(GTK_WIDGET(editable), text); 108 | PangoRectangle rect; 109 | pango_layout_get_extents(layout, NULL, &rect); 110 | pango_extents_to_pixels(NULL, &rect); 111 | g_object_unref(layout); 112 | if (rect.width + win->entry_padding > win->entry_min_width) 113 | gtk_widget_set_size_request(GTK_WIDGET(editable), rect.width + win->entry_padding, -1); 114 | else 115 | gtk_widget_set_size_request(GTK_WIDGET(editable), win->entry_min_width, -1); 116 | 117 | int (*cmp)(const char *a, const char *b, size_t n) = 118 | win->opts.insensitive ? &strncasecmp : &strncmp; 119 | win->cursor = bs_lookup(text, win->strs, win->strs_len, cmp); 120 | win->view = win->cursor; 121 | draw_list(win); 122 | return FALSE; 123 | } 124 | 125 | void mauncher_win_run( 126 | GtkApplication *app, char *input, struct mauncher_win_opts opts, 127 | void (*callback)(const char *output, int status, void *data), void *data) { 128 | 129 | struct win *win = malloc(sizeof(*win)); 130 | win->app = app; 131 | win->input = input; 132 | win->data = data; 133 | win->callback = callback; 134 | win->status = EXIT_SUCCESS; 135 | win->opts = opts; 136 | 137 | win->strs = string_split(win->input, '\n', &win->strs_len); 138 | if (win->opts.insensitive) 139 | qsort(win->strs, win->strs_len, sizeof(*win->strs), &str_case_compare); 140 | else 141 | qsort(win->strs, win->strs_len, sizeof(*win->strs), &str_compare); 142 | 143 | win->cursor = win->strs; 144 | win->view = win->strs; 145 | 146 | win->win = gtk_application_window_new(win->app); 147 | 148 | gtk_window_set_title(GTK_WINDOW(win->win), "Mauncher"); 149 | gtk_window_set_decorated(GTK_WINDOW(win->win), FALSE); 150 | 151 | gtk_layer_init_for_window(GTK_WINDOW(win->win)); 152 | gtk_layer_set_layer(GTK_WINDOW(win->win), GTK_LAYER_SHELL_LAYER_OVERLAY); 153 | gtk_layer_set_keyboard_interactivity(GTK_WINDOW(win->win), TRUE); 154 | 155 | gtk_layer_set_anchor(GTK_WINDOW(win->win), GTK_LAYER_SHELL_EDGE_TOP, TRUE); 156 | gtk_layer_set_anchor(GTK_WINDOW(win->win), GTK_LAYER_SHELL_EDGE_LEFT, TRUE); 157 | gtk_layer_set_anchor(GTK_WINDOW(win->win), GTK_LAYER_SHELL_EDGE_RIGHT, TRUE); 158 | 159 | /* 160 | * Populate window 161 | */ 162 | 163 | GtkWidget *box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 8); 164 | gtk_container_add(GTK_CONTAINER(win->win), box); 165 | 166 | if (win->opts.prompt && win->opts.prompt[0] != '\0') { 167 | gtk_widget_set_margin_start(box, 8); 168 | 169 | // Only show one line 170 | char *c = win->opts.prompt; 171 | while (*c) { 172 | if (*c == '\n') { 173 | *c = '\0'; 174 | break; 175 | } 176 | c += 1; 177 | } 178 | 179 | GtkWidget *prompt = gtk_label_new(win->opts.prompt); 180 | gtk_container_add(GTK_CONTAINER(box), prompt); 181 | } 182 | 183 | GtkWidget *in = gtk_entry_new(); 184 | gtk_entry_set_placeholder_text(GTK_ENTRY(in), "Search..."); 185 | g_signal_connect(in, "activate", G_CALLBACK(on_enter), win); 186 | g_signal_connect(in, "key-press-event", G_CALLBACK(on_keyboard), win); 187 | g_signal_connect(in, "changed", G_CALLBACK(on_change), win); 188 | gtk_widget_grab_focus(in); 189 | gtk_container_add(GTK_CONTAINER(box), in); 190 | 191 | win->container = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 6); 192 | gtk_container_add(GTK_CONTAINER(box), win->container); 193 | 194 | gtk_widget_show_all(win->win); 195 | gtk_window_present(GTK_WINDOW(win->win)); 196 | 197 | win->entry_padding = 24; 198 | gtk_widget_get_preferred_width(in, NULL, &win->entry_min_width); 199 | 200 | draw_list(win); 201 | } 202 | -------------------------------------------------------------------------------- /mauncher.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | #include "mauncher-win.h" 9 | #include "mauncher-ipc.h" 10 | #include "sysutil.h" 11 | 12 | struct daemon_conn; 13 | 14 | struct daemon_ctx { 15 | GtkApplication *app; 16 | int sockfd; 17 | struct sockaddr_un addr; 18 | GIOChannel *channels[16]; 19 | }; 20 | 21 | struct daemon_conn { 22 | struct daemon_ctx *ctx; 23 | GIOChannel **channel; 24 | GSource *source; 25 | }; 26 | 27 | struct daemon_invocation { 28 | struct daemon_conn *conn; 29 | struct daemon_message msg; 30 | }; 31 | 32 | //static void daemon_free_channel(struct daemon_ctx *ctx, GIOChannel *channel) { 33 | static void daemon_free_conn(struct daemon_conn *conn) { 34 | GError *err = NULL; 35 | g_io_channel_shutdown(*conn->channel, TRUE, &err); 36 | if (err != NULL) { 37 | fprintf(stderr, "Closing IO channel failed: %s\n", err->message); 38 | g_error_free(err); 39 | } 40 | 41 | g_io_channel_unref(*conn->channel); 42 | g_source_destroy(conn->source); 43 | g_source_unref(conn->source); 44 | 45 | *conn->channel = NULL; 46 | } 47 | 48 | static void win_callback(const char *output, int status, void *data) { 49 | struct daemon_invocation *invocation = (struct daemon_invocation *)data; 50 | 51 | struct daemon_reply reply = { 52 | .reply = (char *)output, 53 | .status = status, 54 | }; 55 | 56 | daemon_reply_write(g_io_channel_unix_get_fd(*invocation->conn->channel), &reply); 57 | daemon_free_conn(invocation->conn); 58 | free(invocation->msg.payload); 59 | free(invocation->msg.opts.prompt); 60 | free(invocation->conn); 61 | free(invocation); 62 | } 63 | 64 | static gboolean on_data(GIOChannel *source, GIOCondition condition, void *data) { 65 | struct daemon_conn *conn = (struct daemon_conn *)data; 66 | 67 | if (condition & G_IO_IN) { 68 | struct daemon_invocation *invocation = malloc(sizeof(struct daemon_invocation)); 69 | invocation->conn = conn; 70 | 71 | int fd = g_io_channel_unix_get_fd(source); 72 | 73 | if (daemon_message_read(fd, &invocation->msg) < 0) { 74 | daemon_free_conn(conn); 75 | return FALSE; 76 | } 77 | 78 | mauncher_win_run( 79 | conn->ctx->app, invocation->msg.payload, invocation->msg.opts, 80 | &win_callback, invocation); 81 | } 82 | 83 | if (condition & G_IO_HUP) { 84 | daemon_free_conn(conn); 85 | return FALSE; 86 | } 87 | 88 | return TRUE; 89 | } 90 | 91 | static gboolean on_connect(GIOChannel *source, GIOCondition condition, gpointer data) { 92 | struct daemon_ctx *ctx = (struct daemon_ctx *)data; 93 | 94 | socklen_t socklen = sizeof(ctx->addr); 95 | int fd = accept(ctx->sockfd, &ctx->addr, &socklen); 96 | if (fd < 0) { 97 | perror("accept"); 98 | return TRUE; 99 | } 100 | 101 | GIOChannel **channel = NULL; 102 | for (size_t i = 0; i < sizeof(ctx->channels) / sizeof(*ctx->channels); ++i) { 103 | if (ctx->channels[i] == NULL) { 104 | channel = &ctx->channels[i]; 105 | break; 106 | } 107 | } 108 | 109 | if (channel == NULL) { 110 | printf("Client attempted connection, but we're full!\n"); 111 | close(fd); 112 | return TRUE; 113 | } 114 | 115 | *channel = g_io_channel_unix_new(fd); 116 | GSource *gsource = g_io_create_watch(*channel, G_IO_IN | G_IO_HUP); 117 | 118 | struct daemon_conn *conn = malloc(sizeof(*conn)); 119 | conn->channel = channel; 120 | conn->source = gsource; 121 | conn->ctx = ctx; 122 | 123 | g_source_set_callback(gsource, (GSourceFunc)&on_data, conn, NULL); 124 | g_source_attach(conn->source, g_main_context_default()); 125 | 126 | return TRUE; 127 | } 128 | 129 | static void activate(GtkApplication *app, gpointer data) { 130 | struct daemon_ctx *ctx = (struct daemon_ctx *)data; 131 | 132 | ctx->app = app; 133 | g_io_add_watch(g_io_channel_unix_new(ctx->sockfd), G_IO_IN, &on_connect, ctx); 134 | 135 | gtk_main(); 136 | } 137 | 138 | static int daemon_main(int closefd) { 139 | chdir(g_get_home_dir()); 140 | 141 | struct daemon_ctx ctx = { 0 }; 142 | 143 | ctx.addr.sun_family = AF_UNIX; 144 | char *sockpath = string_concat((char *[]) { xdg_runtime_dir(), "/mauncher-daemon.sock", NULL }); 145 | if (strlen(sockpath) >= sizeof(ctx.addr.sun_path)) { 146 | fprintf(stderr, "Unix socket path too long: %s\n", sockpath); 147 | free(sockpath); 148 | return EXIT_FAILURE; 149 | } 150 | 151 | strcpy(ctx.addr.sun_path, sockpath); 152 | free(sockpath); 153 | 154 | ctx.sockfd = socket(AF_UNIX, SOCK_STREAM, 0); 155 | if (ctx.sockfd < 0) { 156 | perror(ctx.addr.sun_path); 157 | return EXIT_FAILURE; 158 | } 159 | 160 | if (bind(ctx.sockfd, (struct sockaddr *)&ctx.addr, sizeof(ctx.addr)) < 0) { 161 | if (errno == EADDRINUSE) { 162 | fprintf(stderr, "%s exists, removing it.\n", ctx.addr.sun_path); 163 | if (unlink(ctx.addr.sun_path) < 0) { 164 | perror(ctx.addr.sun_path); 165 | close(ctx.sockfd); 166 | return EXIT_FAILURE; 167 | } 168 | 169 | if (bind(ctx.sockfd, (struct sockaddr *)&ctx.addr, sizeof(ctx.addr)) < 0) { 170 | perror(ctx.addr.sun_path); 171 | close(ctx.sockfd); 172 | } 173 | } else { 174 | perror(ctx.addr.sun_path); 175 | close(ctx.sockfd); 176 | return EXIT_FAILURE; 177 | } 178 | } 179 | 180 | if (listen(ctx.sockfd, 2) < 0) { 181 | perror(ctx.addr.sun_path); 182 | close(ctx.sockfd); 183 | return EXIT_FAILURE; 184 | } 185 | 186 | if (closefd >= 0) 187 | close(closefd); 188 | 189 | GtkApplication *app = gtk_application_new("coffee.mort.mauncher", G_APPLICATION_NON_UNIQUE); 190 | g_signal_connect(app, "activate", G_CALLBACK(activate), &ctx); 191 | int status = g_application_run(G_APPLICATION(app), 0, NULL); 192 | g_object_unref(app); 193 | return status; 194 | } 195 | 196 | static int daemon_fork() { 197 | int fds[2]; 198 | if (pipe(fds) < 0) { 199 | perror("pipe"); 200 | return -1; 201 | } 202 | 203 | pid_t child = fork(); 204 | if (child < 0) { 205 | perror("fork"); 206 | close(fds[0]); 207 | close(fds[1]); 208 | return -1; 209 | } 210 | 211 | if (child == 0) { 212 | close(fds[0]); 213 | daemon(1, 0); 214 | exit(daemon_main(fds[1])); 215 | } else { 216 | close(fds[1]); 217 | char buf[1]; 218 | 219 | // Just wait for the child to close the pipe 220 | if (read(fds[0], buf, 1) < 0) { 221 | perror("read"); 222 | return -1; 223 | } 224 | } 225 | 226 | return 0; 227 | } 228 | 229 | struct opts { 230 | struct mauncher_win_opts winopts; 231 | gboolean daemon; 232 | }; 233 | 234 | int main(int argc, char **argv) { 235 | struct opts opts = { 0 }; 236 | 237 | GOptionEntry optents[] = { 238 | { 239 | "prompt", 'p', G_OPTION_FLAG_NONE, G_OPTION_ARG_STRING, &opts.winopts.prompt, 240 | "The prompt to be displayed left of the input field", NULL, 241 | }, { 242 | "insensitive", 'i', G_OPTION_FLAG_NONE, G_OPTION_ARG_NONE, &opts.winopts.insensitive, 243 | "Match case-insensitive", NULL, 244 | }, { 245 | "daemon", '\0', G_OPTION_FLAG_NONE, G_OPTION_ARG_NONE, &opts.daemon, 246 | "Spawn the mauncher daemon. This will be done automatically,\n" 247 | " " 248 | "but doing it explicitly on startup will make the first invocation faster.", NULL, 249 | }, 250 | { 0 }, 251 | }; 252 | 253 | GOptionContext *optctx = g_option_context_new(NULL); 254 | g_option_context_add_main_entries(optctx, optents, NULL); 255 | GError *err = NULL; 256 | g_option_context_parse(optctx, &argc, &argv, &err); 257 | g_option_context_free(optctx); 258 | if (err != NULL) { 259 | fprintf(stderr, "%s\n", err->message); 260 | g_error_free(err); 261 | return EXIT_FAILURE; 262 | } 263 | 264 | if (opts.daemon) 265 | return daemon_main(-1); 266 | 267 | struct sockaddr_un addr; 268 | addr.sun_family = AF_UNIX; 269 | char *sockpath = string_concat((char *[]) { xdg_runtime_dir(), "/mauncher-daemon.sock", NULL }); 270 | if (strlen(sockpath) >= sizeof(addr.sun_path)) { 271 | fprintf(stderr, "Unix socket path too long: %s\n", sockpath); 272 | free(sockpath); 273 | return EXIT_FAILURE; 274 | } 275 | 276 | strcpy(addr.sun_path, sockpath); 277 | free(sockpath); 278 | 279 | int sockfd = socket(AF_UNIX, SOCK_STREAM, 0); 280 | if (sockfd < 0) { 281 | perror(addr.sun_path); 282 | printf("socket\n"); 283 | return EXIT_FAILURE; 284 | } 285 | 286 | if (connect(sockfd, (struct sockaddr *)&addr, sizeof(addr)) < 0) { 287 | if (errno == ENOENT || errno == ECONNREFUSED) { 288 | if (daemon_fork() < 0) { 289 | close(sockfd); 290 | return EXIT_FAILURE; 291 | } 292 | 293 | if (connect(sockfd, (struct sockaddr *)&addr, sizeof(addr)) < 0) { 294 | perror(addr.sun_path); 295 | close(sockfd); 296 | return EXIT_FAILURE; 297 | } 298 | } else { 299 | perror(addr.sun_path); 300 | close(sockfd); 301 | return EXIT_FAILURE; 302 | } 303 | } 304 | 305 | struct daemon_message msg = { 0 }; 306 | size_t payload_len; 307 | msg.payload = read_all(STDIN_FILENO, &payload_len); 308 | memcpy(&msg.opts, &opts.winopts, sizeof(msg.opts)); 309 | 310 | if (daemon_message_write(sockfd, &msg) < 0) { 311 | free(msg.payload); 312 | close(sockfd); 313 | return EXIT_FAILURE; 314 | } 315 | 316 | free(msg.payload); 317 | 318 | struct daemon_reply reply; 319 | if (daemon_reply_read(sockfd, &reply) < 0) { 320 | close(sockfd); 321 | return EXIT_FAILURE; 322 | } 323 | 324 | close(sockfd); 325 | 326 | if (reply.status == EXIT_SUCCESS) 327 | puts(reply.reply); 328 | free(reply.reply); 329 | 330 | return reply.status; 331 | } 332 | -------------------------------------------------------------------------------- /mauncher-launcher.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | #include "sysutil.h" 11 | 12 | static char *default_cmd[] = { "mauncher", "-i" }; 13 | static char **dmenu_cmd = default_cmd; 14 | static int dmenu_cmd_len = sizeof(default_cmd) / sizeof(*default_cmd); 15 | 16 | // not const because execvp takes pointers to non-const 17 | static char *calcstring = 18 | "import sys, os, math\n" 19 | "from math import (ceil, floor, log, log10, pow, sqrt,\n" 20 | " cos, sin, tan, acos, asin, atan, atan2, hypot, degrees, radians,\n" 21 | " pi, e)\n" 22 | "" 23 | "ans = 0\n" 24 | "if os.getenv('PY_ANS') != None:\n" 25 | " try: ans = eval(os.getenv('PY_ANS'))\n" 26 | " except: pass\n" 27 | "" 28 | "def digit_to_char(digit):\n" 29 | " if digit < 10:\n" 30 | " return str(digit)\n" 31 | " return chr(ord('a') + digit - 10)\n" 32 | "" 33 | "def base(num, b=16):\n" 34 | " if num < 0:\n" 35 | " return '-' + base(-num, b)\n" 36 | " (d, m) = divmod(num, b)\n" 37 | " if d > 0:\n" 38 | " return base(d, b) + digit_to_char(m)\n" 39 | " return digit_to_char(m)\n" 40 | "" 41 | "def solve(s):\n" 42 | " try:\n" 43 | " from sympy.parsing.sympy_parser import (\n" 44 | " parse_expr, standard_transformations, implicit_multiplication)\n" 45 | " from sympy import Eq, solve\n" 46 | " except ImportError:\n" 47 | " return 'Missing sympy module.'\n" 48 | "" 49 | " transformations = (\n" 50 | " standard_transformations + (\n" 51 | " implicit_multiplication,))\n" 52 | "" 53 | " parts = s.split('=')\n" 54 | " part1 = parse_expr(parts[0], transformations=transformations)\n" 55 | " part2 = parse_expr(parts[1], transformations=transformations)\n" 56 | "" 57 | " r = (solve(Eq(part1, part2)))\n" 58 | " if len(r) == 1:\n" 59 | " return r[0]\n" 60 | " else:\n" 61 | " return r\n" 62 | "" 63 | "res = ''\n" 64 | "try:\n" 65 | " res = eval(os.getenv('PY_EXPR'))\n" 66 | "except Exception as e:\n" 67 | " res = 'Exception'\n" 68 | " sys.stderr.write(str(e)+'\\n')\n" 69 | "\n" 70 | "print('{!r}'.format(res))\n"; 71 | 72 | char *desktops_data = NULL; 73 | char **desktops = NULL; 74 | size_t desktops_len = 0; 75 | size_t desktops_size = 0; 76 | 77 | static int str_compare(const void *a, const void *b) { 78 | return strcmp(*(const char **)a, *(const char **)b); 79 | } 80 | 81 | static void read_desktop_file(char *fpath, char *entname) { 82 | FILE *f = fopen(fpath, "r"); 83 | if (f == NULL) { 84 | perror(fpath); 85 | return; 86 | } 87 | 88 | char *entry = NULL; 89 | int display = 1; 90 | 91 | char linebuf[1024]; 92 | while (1) { 93 | if (fgets(linebuf, sizeof(linebuf), f) == NULL) 94 | break; 95 | 96 | // Find the first name 97 | if ( 98 | entry == NULL && strncmp(linebuf, "Name", 4) == 0 && 99 | (linebuf[4] == ' ' || linebuf[4] == '=')) { 100 | size_t start = 4; 101 | while (linebuf[start] == ' ') start += 1; 102 | start += 1; 103 | while (linebuf[start] == ' ') start += 1; 104 | 105 | size_t end = start; 106 | while (linebuf[end] != '\n' && linebuf[end] != '\0') end += 1; 107 | linebuf[end] = '\0'; 108 | 109 | entry = string_concat( 110 | (char *[]) { linebuf + start, ";", entname, NULL }); 111 | 112 | // Ignore desktop files with NoDisplay=true 113 | } else if ( 114 | strncmp(linebuf, "NoDisplay", 9) == 0 && 115 | (linebuf[9] == ' ' || linebuf[9] == '=')) { 116 | size_t start = 9; 117 | while (linebuf[start] == ' ') start += 1; 118 | start += 1; 119 | while (linebuf[start] == ' ') start += 1; 120 | 121 | size_t end = start; 122 | while (linebuf[end] != '\n' && linebuf[end] != '\0') end += 1; 123 | linebuf[end] = '\0'; 124 | 125 | if (strcmp(linebuf + start, "true") == 0) { 126 | display = 0; 127 | break; 128 | } 129 | } 130 | } 131 | 132 | if (display && entry) { 133 | if (desktops_size == 0) { 134 | desktops_size = 32; 135 | desktops = realloc(desktops, desktops_size * sizeof(*desktops)); 136 | } 137 | 138 | if (desktops_len >= desktops_size - 1) { 139 | desktops_size *= 2; 140 | desktops = realloc(desktops, desktops_size * sizeof(*desktops)); 141 | } 142 | 143 | desktops[desktops_len++] = entry; 144 | } else { 145 | free(entry); 146 | } 147 | 148 | fclose(f); 149 | } 150 | 151 | static void find_desktop_files_in_dir(char *path) { 152 | DIR *dir = opendir(path); 153 | if (dir == NULL) { 154 | if (errno != ENOENT) 155 | perror(path); 156 | return; 157 | } 158 | 159 | while (1) { 160 | errno = 0; 161 | struct dirent *ent = readdir(dir); 162 | if (ent == NULL && errno == 0) { 163 | break; 164 | } else if (ent == NULL) { 165 | perror(path); 166 | break; 167 | } 168 | 169 | if (ent->d_type != DT_REG && ent->d_type != DT_LNK) 170 | continue; 171 | 172 | char *fpath = string_concat( 173 | (char *[]) { path, "/", ent->d_name, NULL }); 174 | read_desktop_file(fpath, ent->d_name); 175 | free(fpath); 176 | } 177 | 178 | closedir(dir); 179 | } 180 | 181 | static void find_desktop_files() { 182 | char *datadirs = string_concat( 183 | (char *[]) { xdg_data_dirs(), ":", xdg_data_home(), NULL }); 184 | 185 | size_t start = 0; 186 | size_t i = 0; 187 | while (1) { 188 | char c = datadirs[i]; 189 | if (c == ':' || c == '\0') { 190 | datadirs[i] = '\0'; 191 | 192 | char *str = string_concat( 193 | (char *[]) { datadirs + start, "/applications", NULL }); 194 | find_desktop_files_in_dir(str); 195 | free(str); 196 | 197 | start = i + 1; 198 | if (c == '\0') 199 | break; 200 | } 201 | 202 | i += 1; 203 | } 204 | 205 | free(datadirs); 206 | 207 | qsort(desktops, desktops_len, sizeof(*desktops), &str_compare); 208 | } 209 | 210 | static int exec_menu(char *prompt) { 211 | if (prompt == NULL) { 212 | char *argv[dmenu_cmd_len + 1]; 213 | memcpy(argv, dmenu_cmd, dmenu_cmd_len * sizeof(*dmenu_cmd)); 214 | argv[dmenu_cmd_len] = NULL; 215 | if (execvp(argv[0], argv) < 0) { 216 | perror(argv[0]); 217 | return -1; 218 | } 219 | } else { 220 | char *argv[dmenu_cmd_len + 3]; 221 | memcpy(argv, dmenu_cmd, dmenu_cmd_len * sizeof(*dmenu_cmd)); 222 | argv[dmenu_cmd_len] = "-p"; 223 | argv[dmenu_cmd_len + 1] = prompt; 224 | argv[dmenu_cmd_len + 2] = NULL; 225 | if (execvp(argv[0], argv) < 0) { 226 | perror(argv[0]); 227 | return -1; 228 | } 229 | } 230 | 231 | return 0; 232 | } 233 | 234 | static int calculator(char *str, char *ans); 235 | 236 | static int calculator_menu(char *answer) { 237 | int infds[2]; 238 | int outfds[2]; 239 | 240 | if (pipe(infds) < 0) { 241 | perror("pipe"); 242 | return EXIT_FAILURE; 243 | } 244 | 245 | if (pipe(outfds) < 0) { 246 | close(infds[0]); 247 | close(outfds[0]); 248 | perror("pipe"); 249 | return EXIT_FAILURE; 250 | } 251 | 252 | pid_t child = fork(); 253 | if (child < 0) { 254 | perror("fork"); 255 | close(infds[0]); 256 | close(outfds[0]); 257 | return EXIT_FAILURE; 258 | } 259 | 260 | if (child == 0) { 261 | close(infds[1]); 262 | close(outfds[0]); 263 | dup2(infds[0], STDIN_FILENO); 264 | dup2(outfds[1], STDOUT_FILENO); 265 | if (exec_menu(answer) < 0) 266 | exit(EXIT_FAILURE); 267 | } else { 268 | close(infds[0]); 269 | close(outfds[1]); 270 | 271 | write(infds[1], "$\n", 2); 272 | close(infds[1]); 273 | 274 | size_t len; 275 | char *expr = read_until(outfds[0], '\n', &len); 276 | close(outfds[0]); 277 | if (expr == NULL) 278 | return EXIT_FAILURE; 279 | 280 | int status; 281 | if (waitpid(child, &status, 0) < 0) { 282 | perror("wait"); 283 | free(expr); 284 | return EXIT_FAILURE; 285 | } 286 | 287 | if (WIFEXITED(status) && WEXITSTATUS(status) != EXIT_SUCCESS) { 288 | free(expr); 289 | return WEXITSTATUS(status); 290 | } else if (WIFSIGNALED(status)) { 291 | free(expr); 292 | return WSTOPSIG(status) + 128; 293 | } 294 | 295 | if (strcmp(expr, "$") == 0) { 296 | free(expr); 297 | return EXIT_SUCCESS; 298 | } else { 299 | calculator(expr, answer); 300 | free(expr); 301 | } 302 | } 303 | 304 | return EXIT_SUCCESS; 305 | } 306 | 307 | static int calculator(char *str, char *ans) { 308 | int fds[2]; 309 | if (pipe(fds) < 0) { 310 | perror("pipe"); 311 | return EXIT_FAILURE; 312 | } 313 | 314 | setenv("PY_EXPR", str, 1); 315 | if (ans != NULL) 316 | setenv("PY_ANS", ans, 1); 317 | 318 | pid_t child = fork(); 319 | if (child < 0) { 320 | perror("fork"); 321 | return EXIT_FAILURE; 322 | } 323 | 324 | if (child == 0) { 325 | close(fds[0]); 326 | dup2(fds[1], STDOUT_FILENO); 327 | if (execvp("python3", (char *const[]) { "python3", "-c", calcstring, NULL }) < 0) { 328 | perror("python3"); 329 | exit(EXIT_FAILURE); 330 | } 331 | } else { 332 | close(fds[1]); 333 | size_t len; 334 | char *output = read_until(fds[0], '\n', &len); 335 | close(fds[0]); 336 | if (output == NULL) 337 | return EXIT_FAILURE; 338 | 339 | int status; 340 | if (waitpid(child, &status, 0) < 0) { 341 | perror("wait"); 342 | free(output); 343 | return EXIT_FAILURE; 344 | } 345 | 346 | if (WIFEXITED(status) && WEXITSTATUS(status) != EXIT_SUCCESS) { 347 | free(output); 348 | return WEXITSTATUS(status); 349 | } else if (WIFSIGNALED(status)) { 350 | free(output); 351 | return WSTOPSIG(status) + 128; 352 | } 353 | 354 | int ret = calculator_menu(output); 355 | free(output); 356 | return ret; 357 | } 358 | 359 | return EXIT_SUCCESS; 360 | } 361 | 362 | static int shell(char *str) { 363 | int ret = system(str); 364 | if (ret < 0) { 365 | perror("system"); 366 | return EXIT_FAILURE; 367 | } else { 368 | return ret; 369 | } 370 | } 371 | 372 | static int launch(char *str) { 373 | char **key = bs_lookup(str, desktops, desktops_len, strncmp); 374 | if (key == NULL) 375 | return -1; 376 | 377 | char *val = strchr(*key, ';'); 378 | if (val == NULL) { 379 | fprintf(stderr, "Desktop file entry doesn't contain a value: %s\n", *key); 380 | return EXIT_FAILURE; 381 | } 382 | val += 1; 383 | 384 | if (execvp("gtk-launch", (char *[]) { "gtk-launch", val, NULL }) < 0) { 385 | perror("gtk-launch"); 386 | return EXIT_FAILURE; 387 | } 388 | 389 | return EXIT_SUCCESS; 390 | } 391 | 392 | int main(int argc, char **argv) { 393 | for (int i = 1; i < argc; ++i) { 394 | char *arg = argv[i]; 395 | if (strcmp(arg, "--help") == 0 || strcmp(arg, "-h") == 0) { 396 | printf("Usage: %s [--dmenu=\"mauncher\"]\n", argv[0]); 397 | printf("\n"); 398 | printf("Options:\n"); 399 | printf(" --help|-h: Show this help text.\n"); 400 | printf(" --dmenu|-d : Use a different dmenu command.\n"); 401 | printf(" --calculator|-c: Go straight to the calculator instead of launcher.\n"); 402 | printf(" --list|-l: Just print the found applications, don't run the dmenu command.\n"); 403 | return EXIT_SUCCESS; 404 | } else if (strcmp(arg, "--dmenu") == 0 || strcmp(arg, "-d") == 0) { 405 | dmenu_cmd = argv + i + 1; 406 | dmenu_cmd_len = argc - i - 1; 407 | break; 408 | } else if (strcmp(arg, "--calculator") == 0 || strcmp(arg, "-c") == 0) { 409 | return calculator_menu("="); 410 | } else if (strcmp(arg, "--list") == 0 || strcmp(arg, "-l") == 0) { 411 | find_desktop_files(); 412 | for (size_t i = 0; i < desktops_len; ++i) { 413 | printf("%s\n", desktops[i]); 414 | } 415 | return EXIT_SUCCESS; 416 | } 417 | } 418 | 419 | int infds[2]; 420 | if (pipe(infds) < 0) { 421 | perror("pipe"); 422 | return EXIT_FAILURE; 423 | } 424 | 425 | int outfds[2]; 426 | if (pipe(outfds) < 0) { 427 | perror("pipe"); 428 | return EXIT_FAILURE; 429 | } 430 | 431 | pid_t child = fork(); 432 | if (child < 0) { 433 | perror("fork"); 434 | return EXIT_FAILURE; 435 | } 436 | 437 | if (child == 0) { 438 | close(infds[1]); 439 | close(outfds[0]); 440 | dup2(infds[0], STDIN_FILENO); 441 | dup2(outfds[1], STDOUT_FILENO); 442 | if (exec_menu(NULL)) 443 | exit(EXIT_FAILURE); 444 | } else { 445 | close(infds[0]); 446 | close(outfds[1]); 447 | 448 | find_desktop_files(); 449 | 450 | for (size_t i = 0; i < desktops_len; ++i) { 451 | char *chr = strchr(desktops[i], ';'); 452 | if (chr == NULL) 453 | break; 454 | 455 | if (write(infds[1], desktops[i], chr - desktops[i]) < 0) { 456 | perror("write"); 457 | break; 458 | } 459 | 460 | if (write(infds[1], "\n", 1) < 0) { 461 | perror("write"); 462 | break; 463 | } 464 | } 465 | close(infds[1]); 466 | 467 | size_t len; 468 | char *output = read_until(outfds[0], '\n', &len); 469 | close(outfds[0]); 470 | if (output == NULL) 471 | return EXIT_FAILURE; 472 | 473 | int status; 474 | if (waitpid(child, &status, 0) < 0) { 475 | perror("wait"); 476 | free(output); 477 | return EXIT_FAILURE; 478 | } 479 | 480 | if (WIFEXITED(status) && WEXITSTATUS(status) != EXIT_SUCCESS) { 481 | free(output); 482 | return WEXITSTATUS(status); 483 | } else if (WIFSIGNALED(status)) { 484 | free(output); 485 | return WSTOPSIG(status) + 128; 486 | } 487 | 488 | int ret = launch(output); 489 | if (ret < 0) { 490 | if (strncmp(output, "$", 1) == 0) 491 | return shell(output + 1); 492 | else if (strncmp(output, "sh ", 3) == 0) 493 | return shell(output + 3); 494 | else 495 | return calculator(output, NULL); 496 | } else { 497 | return ret; 498 | } 499 | 500 | free(output); 501 | return ret; 502 | } 503 | 504 | return EXIT_SUCCESS; 505 | } 506 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU General Public License is a free, copyleft license for 11 | software and other kinds of works. 12 | 13 | The licenses for most software and other practical works are designed 14 | to take away your freedom to share and change the works. By contrast, 15 | the GNU General Public License is intended to guarantee your freedom to 16 | share and change all versions of a program--to make sure it remains free 17 | software for all its users. We, the Free Software Foundation, use the 18 | GNU General Public License for most of our software; it applies also to 19 | any other work released this way by its authors. You can apply it to 20 | your programs, too. 21 | 22 | When we speak of free software, we are referring to freedom, not 23 | price. Our General Public Licenses are designed to make sure that you 24 | have the freedom to distribute copies of free software (and charge for 25 | them if you wish), that you receive source code or can get it if you 26 | want it, that you can change the software or use pieces of it in new 27 | free programs, and that you know you can do these things. 28 | 29 | To protect your rights, we need to prevent others from denying you 30 | these rights or asking you to surrender the rights. Therefore, you have 31 | certain responsibilities if you distribute copies of the software, or if 32 | you modify it: responsibilities to respect the freedom of others. 33 | 34 | For example, if you distribute copies of such a program, whether 35 | gratis or for a fee, you must pass on to the recipients the same 36 | freedoms that you received. You must make sure that they, too, receive 37 | or can get the source code. And you must show them these terms so they 38 | know their rights. 39 | 40 | Developers that use the GNU GPL protect your rights with two steps: 41 | (1) assert copyright on the software, and (2) offer you this License 42 | giving you legal permission to copy, distribute and/or modify it. 43 | 44 | For the developers' and authors' protection, the GPL clearly explains 45 | that there is no warranty for this free software. For both users' and 46 | authors' sake, the GPL requires that modified versions be marked as 47 | changed, so that their problems will not be attributed erroneously to 48 | authors of previous versions. 49 | 50 | Some devices are designed to deny users access to install or run 51 | modified versions of the software inside them, although the manufacturer 52 | can do so. This is fundamentally incompatible with the aim of 53 | protecting users' freedom to change the software. The systematic 54 | pattern of such abuse occurs in the area of products for individuals to 55 | use, which is precisely where it is most unacceptable. Therefore, we 56 | have designed this version of the GPL to prohibit the practice for those 57 | products. If such problems arise substantially in other domains, we 58 | stand ready to extend this provision to those domains in future versions 59 | of the GPL, as needed to protect the freedom of users. 60 | 61 | Finally, every program is threatened constantly by software patents. 62 | States should not allow patents to restrict development and use of 63 | software on general-purpose computers, but in those that do, we wish to 64 | avoid the special danger that patents applied to a free program could 65 | make it effectively proprietary. To prevent this, the GPL assures that 66 | patents cannot be used to render the program non-free. 67 | 68 | The precise terms and conditions for copying, distribution and 69 | modification follow. 70 | 71 | TERMS AND CONDITIONS 72 | 73 | 0. Definitions. 74 | 75 | "This License" refers to version 3 of the GNU General Public License. 76 | 77 | "Copyright" also means copyright-like laws that apply to other kinds of 78 | works, such as semiconductor masks. 79 | 80 | "The Program" refers to any copyrightable work licensed under this 81 | License. Each licensee is addressed as "you". "Licensees" and 82 | "recipients" may be individuals or organizations. 83 | 84 | To "modify" a work means to copy from or adapt all or part of the work 85 | in a fashion requiring copyright permission, other than the making of an 86 | exact copy. The resulting work is called a "modified version" of the 87 | earlier work or a work "based on" the earlier work. 88 | 89 | A "covered work" means either the unmodified Program or a work based 90 | on the Program. 91 | 92 | To "propagate" a work means to do anything with it that, without 93 | permission, would make you directly or secondarily liable for 94 | infringement under applicable copyright law, except executing it on a 95 | computer or modifying a private copy. Propagation includes copying, 96 | distribution (with or without modification), making available to the 97 | public, and in some countries other activities as well. 98 | 99 | To "convey" a work means any kind of propagation that enables other 100 | parties to make or receive copies. Mere interaction with a user through 101 | a computer network, with no transfer of a copy, is not conveying. 102 | 103 | An interactive user interface displays "Appropriate Legal Notices" 104 | to the extent that it includes a convenient and prominently visible 105 | feature that (1) displays an appropriate copyright notice, and (2) 106 | tells the user that there is no warranty for the work (except to the 107 | extent that warranties are provided), that licensees may convey the 108 | work under this License, and how to view a copy of this License. If 109 | the interface presents a list of user commands or options, such as a 110 | menu, a prominent item in the list meets this criterion. 111 | 112 | 1. Source Code. 113 | 114 | The "source code" for a work means the preferred form of the work 115 | for making modifications to it. "Object code" means any non-source 116 | form of a work. 117 | 118 | A "Standard Interface" means an interface that either is an official 119 | standard defined by a recognized standards body, or, in the case of 120 | interfaces specified for a particular programming language, one that 121 | is widely used among developers working in that language. 122 | 123 | The "System Libraries" of an executable work include anything, other 124 | than the work as a whole, that (a) is included in the normal form of 125 | packaging a Major Component, but which is not part of that Major 126 | Component, and (b) serves only to enable use of the work with that 127 | Major Component, or to implement a Standard Interface for which an 128 | implementation is available to the public in source code form. A 129 | "Major Component", in this context, means a major essential component 130 | (kernel, window system, and so on) of the specific operating system 131 | (if any) on which the executable work runs, or a compiler used to 132 | produce the work, or an object code interpreter used to run it. 133 | 134 | The "Corresponding Source" for a work in object code form means all 135 | the source code needed to generate, install, and (for an executable 136 | work) run the object code and to modify the work, including scripts to 137 | control those activities. However, it does not include the work's 138 | System Libraries, or general-purpose tools or generally available free 139 | programs which are used unmodified in performing those activities but 140 | which are not part of the work. For example, Corresponding Source 141 | includes interface definition files associated with source files for 142 | the work, and the source code for shared libraries and dynamically 143 | linked subprograms that the work is specifically designed to require, 144 | such as by intimate data communication or control flow between those 145 | subprograms and other parts of the work. 146 | 147 | The Corresponding Source need not include anything that users 148 | can regenerate automatically from other parts of the Corresponding 149 | Source. 150 | 151 | The Corresponding Source for a work in source code form is that 152 | same work. 153 | 154 | 2. Basic Permissions. 155 | 156 | All rights granted under this License are granted for the term of 157 | copyright on the Program, and are irrevocable provided the stated 158 | conditions are met. This License explicitly affirms your unlimited 159 | permission to run the unmodified Program. The output from running a 160 | covered work is covered by this License only if the output, given its 161 | content, constitutes a covered work. This License acknowledges your 162 | rights of fair use or other equivalent, as provided by copyright law. 163 | 164 | You may make, run and propagate covered works that you do not 165 | convey, without conditions so long as your license otherwise remains 166 | in force. You may convey covered works to others for the sole purpose 167 | of having them make modifications exclusively for you, or provide you 168 | with facilities for running those works, provided that you comply with 169 | the terms of this License in conveying all material for which you do 170 | not control copyright. Those thus making or running the covered works 171 | for you must do so exclusively on your behalf, under your direction 172 | and control, on terms that prohibit them from making any copies of 173 | your copyrighted material outside their relationship with you. 174 | 175 | Conveying under any other circumstances is permitted solely under 176 | the conditions stated below. Sublicensing is not allowed; section 10 177 | makes it unnecessary. 178 | 179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 180 | 181 | No covered work shall be deemed part of an effective technological 182 | measure under any applicable law fulfilling obligations under article 183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 184 | similar laws prohibiting or restricting circumvention of such 185 | measures. 186 | 187 | When you convey a covered work, you waive any legal power to forbid 188 | circumvention of technological measures to the extent such circumvention 189 | is effected by exercising rights under this License with respect to 190 | the covered work, and you disclaim any intention to limit operation or 191 | modification of the work as a means of enforcing, against the work's 192 | users, your or third parties' legal rights to forbid circumvention of 193 | technological measures. 194 | 195 | 4. Conveying Verbatim Copies. 196 | 197 | You may convey verbatim copies of the Program's source code as you 198 | receive it, in any medium, provided that you conspicuously and 199 | appropriately publish on each copy an appropriate copyright notice; 200 | keep intact all notices stating that this License and any 201 | non-permissive terms added in accord with section 7 apply to the code; 202 | keep intact all notices of the absence of any warranty; and give all 203 | recipients a copy of this License along with the Program. 204 | 205 | You may charge any price or no price for each copy that you convey, 206 | and you may offer support or warranty protection for a fee. 207 | 208 | 5. Conveying Modified Source Versions. 209 | 210 | You may convey a work based on the Program, or the modifications to 211 | produce it from the Program, in the form of source code under the 212 | terms of section 4, provided that you also meet all of these conditions: 213 | 214 | a) The work must carry prominent notices stating that you modified 215 | it, and giving a relevant date. 216 | 217 | b) The work must carry prominent notices stating that it is 218 | released under this License and any conditions added under section 219 | 7. This requirement modifies the requirement in section 4 to 220 | "keep intact all notices". 221 | 222 | c) You must license the entire work, as a whole, under this 223 | License to anyone who comes into possession of a copy. This 224 | License will therefore apply, along with any applicable section 7 225 | additional terms, to the whole of the work, and all its parts, 226 | regardless of how they are packaged. This License gives no 227 | permission to license the work in any other way, but it does not 228 | invalidate such permission if you have separately received it. 229 | 230 | d) If the work has interactive user interfaces, each must display 231 | Appropriate Legal Notices; however, if the Program has interactive 232 | interfaces that do not display Appropriate Legal Notices, your 233 | work need not make them do so. 234 | 235 | A compilation of a covered work with other separate and independent 236 | works, which are not by their nature extensions of the covered work, 237 | and which are not combined with it such as to form a larger program, 238 | in or on a volume of a storage or distribution medium, is called an 239 | "aggregate" if the compilation and its resulting copyright are not 240 | used to limit the access or legal rights of the compilation's users 241 | beyond what the individual works permit. Inclusion of a covered work 242 | in an aggregate does not cause this License to apply to the other 243 | parts of the aggregate. 244 | 245 | 6. Conveying Non-Source Forms. 246 | 247 | You may convey a covered work in object code form under the terms 248 | of sections 4 and 5, provided that you also convey the 249 | machine-readable Corresponding Source under the terms of this License, 250 | in one of these ways: 251 | 252 | a) Convey the object code in, or embodied in, a physical product 253 | (including a physical distribution medium), accompanied by the 254 | Corresponding Source fixed on a durable physical medium 255 | customarily used for software interchange. 256 | 257 | b) Convey the object code in, or embodied in, a physical product 258 | (including a physical distribution medium), accompanied by a 259 | written offer, valid for at least three years and valid for as 260 | long as you offer spare parts or customer support for that product 261 | model, to give anyone who possesses the object code either (1) a 262 | copy of the Corresponding Source for all the software in the 263 | product that is covered by this License, on a durable physical 264 | medium customarily used for software interchange, for a price no 265 | more than your reasonable cost of physically performing this 266 | conveying of source, or (2) access to copy the 267 | Corresponding Source from a network server at no charge. 268 | 269 | c) Convey individual copies of the object code with a copy of the 270 | written offer to provide the Corresponding Source. This 271 | alternative is allowed only occasionally and noncommercially, and 272 | only if you received the object code with such an offer, in accord 273 | with subsection 6b. 274 | 275 | d) Convey the object code by offering access from a designated 276 | place (gratis or for a charge), and offer equivalent access to the 277 | Corresponding Source in the same way through the same place at no 278 | further charge. You need not require recipients to copy the 279 | Corresponding Source along with the object code. If the place to 280 | copy the object code is a network server, the Corresponding Source 281 | may be on a different server (operated by you or a third party) 282 | that supports equivalent copying facilities, provided you maintain 283 | clear directions next to the object code saying where to find the 284 | Corresponding Source. Regardless of what server hosts the 285 | Corresponding Source, you remain obligated to ensure that it is 286 | available for as long as needed to satisfy these requirements. 287 | 288 | e) Convey the object code using peer-to-peer transmission, provided 289 | you inform other peers where the object code and Corresponding 290 | Source of the work are being offered to the general public at no 291 | charge under subsection 6d. 292 | 293 | A separable portion of the object code, whose source code is excluded 294 | from the Corresponding Source as a System Library, need not be 295 | included in conveying the object code work. 296 | 297 | A "User Product" is either (1) a "consumer product", which means any 298 | tangible personal property which is normally used for personal, family, 299 | or household purposes, or (2) anything designed or sold for incorporation 300 | into a dwelling. In determining whether a product is a consumer product, 301 | doubtful cases shall be resolved in favor of coverage. For a particular 302 | product received by a particular user, "normally used" refers to a 303 | typical or common use of that class of product, regardless of the status 304 | of the particular user or of the way in which the particular user 305 | actually uses, or expects or is expected to use, the product. A product 306 | is a consumer product regardless of whether the product has substantial 307 | commercial, industrial or non-consumer uses, unless such uses represent 308 | the only significant mode of use of the product. 309 | 310 | "Installation Information" for a User Product means any methods, 311 | procedures, authorization keys, or other information required to install 312 | and execute modified versions of a covered work in that User Product from 313 | a modified version of its Corresponding Source. The information must 314 | suffice to ensure that the continued functioning of the modified object 315 | code is in no case prevented or interfered with solely because 316 | modification has been made. 317 | 318 | If you convey an object code work under this section in, or with, or 319 | specifically for use in, a User Product, and the conveying occurs as 320 | part of a transaction in which the right of possession and use of the 321 | User Product is transferred to the recipient in perpetuity or for a 322 | fixed term (regardless of how the transaction is characterized), the 323 | Corresponding Source conveyed under this section must be accompanied 324 | by the Installation Information. But this requirement does not apply 325 | if neither you nor any third party retains the ability to install 326 | modified object code on the User Product (for example, the work has 327 | been installed in ROM). 328 | 329 | The requirement to provide Installation Information does not include a 330 | requirement to continue to provide support service, warranty, or updates 331 | for a work that has been modified or installed by the recipient, or for 332 | the User Product in which it has been modified or installed. Access to a 333 | network may be denied when the modification itself materially and 334 | adversely affects the operation of the network or violates the rules and 335 | protocols for communication across the network. 336 | 337 | Corresponding Source conveyed, and Installation Information provided, 338 | in accord with this section must be in a format that is publicly 339 | documented (and with an implementation available to the public in 340 | source code form), and must require no special password or key for 341 | unpacking, reading or copying. 342 | 343 | 7. Additional Terms. 344 | 345 | "Additional permissions" are terms that supplement the terms of this 346 | License by making exceptions from one or more of its conditions. 347 | Additional permissions that are applicable to the entire Program shall 348 | be treated as though they were included in this License, to the extent 349 | that they are valid under applicable law. If additional permissions 350 | apply only to part of the Program, that part may be used separately 351 | under those permissions, but the entire Program remains governed by 352 | this License without regard to the additional permissions. 353 | 354 | When you convey a copy of a covered work, you may at your option 355 | remove any additional permissions from that copy, or from any part of 356 | it. (Additional permissions may be written to require their own 357 | removal in certain cases when you modify the work.) You may place 358 | additional permissions on material, added by you to a covered work, 359 | for which you have or can give appropriate copyright permission. 360 | 361 | Notwithstanding any other provision of this License, for material you 362 | add to a covered work, you may (if authorized by the copyright holders of 363 | that material) supplement the terms of this License with terms: 364 | 365 | a) Disclaiming warranty or limiting liability differently from the 366 | terms of sections 15 and 16 of this License; or 367 | 368 | b) Requiring preservation of specified reasonable legal notices or 369 | author attributions in that material or in the Appropriate Legal 370 | Notices displayed by works containing it; or 371 | 372 | c) Prohibiting misrepresentation of the origin of that material, or 373 | requiring that modified versions of such material be marked in 374 | reasonable ways as different from the original version; or 375 | 376 | d) Limiting the use for publicity purposes of names of licensors or 377 | authors of the material; or 378 | 379 | e) Declining to grant rights under trademark law for use of some 380 | trade names, trademarks, or service marks; or 381 | 382 | f) Requiring indemnification of licensors and authors of that 383 | material by anyone who conveys the material (or modified versions of 384 | it) with contractual assumptions of liability to the recipient, for 385 | any liability that these contractual assumptions directly impose on 386 | those licensors and authors. 387 | 388 | All other non-permissive additional terms are considered "further 389 | restrictions" within the meaning of section 10. If the Program as you 390 | received it, or any part of it, contains a notice stating that it is 391 | governed by this License along with a term that is a further 392 | restriction, you may remove that term. If a license document contains 393 | a further restriction but permits relicensing or conveying under this 394 | License, you may add to a covered work material governed by the terms 395 | of that license document, provided that the further restriction does 396 | not survive such relicensing or conveying. 397 | 398 | If you add terms to a covered work in accord with this section, you 399 | must place, in the relevant source files, a statement of the 400 | additional terms that apply to those files, or a notice indicating 401 | where to find the applicable terms. 402 | 403 | Additional terms, permissive or non-permissive, may be stated in the 404 | form of a separately written license, or stated as exceptions; 405 | the above requirements apply either way. 406 | 407 | 8. Termination. 408 | 409 | You may not propagate or modify a covered work except as expressly 410 | provided under this License. Any attempt otherwise to propagate or 411 | modify it is void, and will automatically terminate your rights under 412 | this License (including any patent licenses granted under the third 413 | paragraph of section 11). 414 | 415 | However, if you cease all violation of this License, then your 416 | license from a particular copyright holder is reinstated (a) 417 | provisionally, unless and until the copyright holder explicitly and 418 | finally terminates your license, and (b) permanently, if the copyright 419 | holder fails to notify you of the violation by some reasonable means 420 | prior to 60 days after the cessation. 421 | 422 | Moreover, your license from a particular copyright holder is 423 | reinstated permanently if the copyright holder notifies you of the 424 | violation by some reasonable means, this is the first time you have 425 | received notice of violation of this License (for any work) from that 426 | copyright holder, and you cure the violation prior to 30 days after 427 | your receipt of the notice. 428 | 429 | Termination of your rights under this section does not terminate the 430 | licenses of parties who have received copies or rights from you under 431 | this License. If your rights have been terminated and not permanently 432 | reinstated, you do not qualify to receive new licenses for the same 433 | material under section 10. 434 | 435 | 9. Acceptance Not Required for Having Copies. 436 | 437 | You are not required to accept this License in order to receive or 438 | run a copy of the Program. Ancillary propagation of a covered work 439 | occurring solely as a consequence of using peer-to-peer transmission 440 | to receive a copy likewise does not require acceptance. However, 441 | nothing other than this License grants you permission to propagate or 442 | modify any covered work. These actions infringe copyright if you do 443 | not accept this License. Therefore, by modifying or propagating a 444 | covered work, you indicate your acceptance of this License to do so. 445 | 446 | 10. Automatic Licensing of Downstream Recipients. 447 | 448 | Each time you convey a covered work, the recipient automatically 449 | receives a license from the original licensors, to run, modify and 450 | propagate that work, subject to this License. You are not responsible 451 | for enforcing compliance by third parties with this License. 452 | 453 | An "entity transaction" is a transaction transferring control of an 454 | organization, or substantially all assets of one, or subdividing an 455 | organization, or merging organizations. If propagation of a covered 456 | work results from an entity transaction, each party to that 457 | transaction who receives a copy of the work also receives whatever 458 | licenses to the work the party's predecessor in interest had or could 459 | give under the previous paragraph, plus a right to possession of the 460 | Corresponding Source of the work from the predecessor in interest, if 461 | the predecessor has it or can get it with reasonable efforts. 462 | 463 | You may not impose any further restrictions on the exercise of the 464 | rights granted or affirmed under this License. For example, you may 465 | not impose a license fee, royalty, or other charge for exercise of 466 | rights granted under this License, and you may not initiate litigation 467 | (including a cross-claim or counterclaim in a lawsuit) alleging that 468 | any patent claim is infringed by making, using, selling, offering for 469 | sale, or importing the Program or any portion of it. 470 | 471 | 11. Patents. 472 | 473 | A "contributor" is a copyright holder who authorizes use under this 474 | License of the Program or a work on which the Program is based. The 475 | work thus licensed is called the contributor's "contributor version". 476 | 477 | A contributor's "essential patent claims" are all patent claims 478 | owned or controlled by the contributor, whether already acquired or 479 | hereafter acquired, that would be infringed by some manner, permitted 480 | by this License, of making, using, or selling its contributor version, 481 | but do not include claims that would be infringed only as a 482 | consequence of further modification of the contributor version. For 483 | purposes of this definition, "control" includes the right to grant 484 | patent sublicenses in a manner consistent with the requirements of 485 | this License. 486 | 487 | Each contributor grants you a non-exclusive, worldwide, royalty-free 488 | patent license under the contributor's essential patent claims, to 489 | make, use, sell, offer for sale, import and otherwise run, modify and 490 | propagate the contents of its contributor version. 491 | 492 | In the following three paragraphs, a "patent license" is any express 493 | agreement or commitment, however denominated, not to enforce a patent 494 | (such as an express permission to practice a patent or covenant not to 495 | sue for patent infringement). To "grant" such a patent license to a 496 | party means to make such an agreement or commitment not to enforce a 497 | patent against the party. 498 | 499 | If you convey a covered work, knowingly relying on a patent license, 500 | and the Corresponding Source of the work is not available for anyone 501 | to copy, free of charge and under the terms of this License, through a 502 | publicly available network server or other readily accessible means, 503 | then you must either (1) cause the Corresponding Source to be so 504 | available, or (2) arrange to deprive yourself of the benefit of the 505 | patent license for this particular work, or (3) arrange, in a manner 506 | consistent with the requirements of this License, to extend the patent 507 | license to downstream recipients. "Knowingly relying" means you have 508 | actual knowledge that, but for the patent license, your conveying the 509 | covered work in a country, or your recipient's use of the covered work 510 | in a country, would infringe one or more identifiable patents in that 511 | country that you have reason to believe are valid. 512 | 513 | If, pursuant to or in connection with a single transaction or 514 | arrangement, you convey, or propagate by procuring conveyance of, a 515 | covered work, and grant a patent license to some of the parties 516 | receiving the covered work authorizing them to use, propagate, modify 517 | or convey a specific copy of the covered work, then the patent license 518 | you grant is automatically extended to all recipients of the covered 519 | work and works based on it. 520 | 521 | A patent license is "discriminatory" if it does not include within 522 | the scope of its coverage, prohibits the exercise of, or is 523 | conditioned on the non-exercise of one or more of the rights that are 524 | specifically granted under this License. You may not convey a covered 525 | work if you are a party to an arrangement with a third party that is 526 | in the business of distributing software, under which you make payment 527 | to the third party based on the extent of your activity of conveying 528 | the work, and under which the third party grants, to any of the 529 | parties who would receive the covered work from you, a discriminatory 530 | patent license (a) in connection with copies of the covered work 531 | conveyed by you (or copies made from those copies), or (b) primarily 532 | for and in connection with specific products or compilations that 533 | contain the covered work, unless you entered into that arrangement, 534 | or that patent license was granted, prior to 28 March 2007. 535 | 536 | Nothing in this License shall be construed as excluding or limiting 537 | any implied license or other defenses to infringement that may 538 | otherwise be available to you under applicable patent law. 539 | 540 | 12. No Surrender of Others' Freedom. 541 | 542 | If conditions are imposed on you (whether by court order, agreement or 543 | otherwise) that contradict the conditions of this License, they do not 544 | excuse you from the conditions of this License. If you cannot convey a 545 | covered work so as to satisfy simultaneously your obligations under this 546 | License and any other pertinent obligations, then as a consequence you may 547 | not convey it at all. For example, if you agree to terms that obligate you 548 | to collect a royalty for further conveying from those to whom you convey 549 | the Program, the only way you could satisfy both those terms and this 550 | License would be to refrain entirely from conveying the Program. 551 | 552 | 13. Use with the GNU Affero General Public License. 553 | 554 | Notwithstanding any other provision of this License, you have 555 | permission to link or combine any covered work with a work licensed 556 | under version 3 of the GNU Affero General Public License into a single 557 | combined work, and to convey the resulting work. The terms of this 558 | License will continue to apply to the part which is the covered work, 559 | but the special requirements of the GNU Affero General Public License, 560 | section 13, concerning interaction through a network will apply to the 561 | combination as such. 562 | 563 | 14. Revised Versions of this License. 564 | 565 | The Free Software Foundation may publish revised and/or new versions of 566 | the GNU General Public License from time to time. Such new versions will 567 | be similar in spirit to the present version, but may differ in detail to 568 | address new problems or concerns. 569 | 570 | Each version is given a distinguishing version number. If the 571 | Program specifies that a certain numbered version of the GNU General 572 | Public License "or any later version" applies to it, you have the 573 | option of following the terms and conditions either of that numbered 574 | version or of any later version published by the Free Software 575 | Foundation. If the Program does not specify a version number of the 576 | GNU General Public License, you may choose any version ever published 577 | by the Free Software Foundation. 578 | 579 | If the Program specifies that a proxy can decide which future 580 | versions of the GNU General Public License can be used, that proxy's 581 | public statement of acceptance of a version permanently authorizes you 582 | to choose that version for the Program. 583 | 584 | Later license versions may give you additional or different 585 | permissions. However, no additional obligations are imposed on any 586 | author or copyright holder as a result of your choosing to follow a 587 | later version. 588 | 589 | 15. Disclaimer of Warranty. 590 | 591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 599 | 600 | 16. Limitation of Liability. 601 | 602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 610 | SUCH DAMAGES. 611 | 612 | 17. Interpretation of Sections 15 and 16. 613 | 614 | If the disclaimer of warranty and limitation of liability provided 615 | above cannot be given local legal effect according to their terms, 616 | reviewing courts shall apply local law that most closely approximates 617 | an absolute waiver of all civil liability in connection with the 618 | Program, unless a warranty or assumption of liability accompanies a 619 | copy of the Program in return for a fee. 620 | 621 | END OF TERMS AND CONDITIONS 622 | 623 | How to Apply These Terms to Your New Programs 624 | 625 | If you develop a new program, and you want it to be of the greatest 626 | possible use to the public, the best way to achieve this is to make it 627 | free software which everyone can redistribute and change under these terms. 628 | 629 | To do so, attach the following notices to the program. It is safest 630 | to attach them to the start of each source file to most effectively 631 | state the exclusion of warranty; and each file should have at least 632 | the "copyright" line and a pointer to where the full notice is found. 633 | 634 | 635 | Copyright (C) 636 | 637 | This program is free software: you can redistribute it and/or modify 638 | it under the terms of the GNU General Public License as published by 639 | the Free Software Foundation, either version 3 of the License, or 640 | (at your option) any later version. 641 | 642 | This program is distributed in the hope that it will be useful, 643 | but WITHOUT ANY WARRANTY; without even the implied warranty of 644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 645 | GNU General Public License for more details. 646 | 647 | You should have received a copy of the GNU General Public License 648 | along with this program. If not, see . 649 | 650 | Also add information on how to contact you by electronic and paper mail. 651 | 652 | If the program does terminal interaction, make it output a short 653 | notice like this when it starts in an interactive mode: 654 | 655 | Copyright (C) 656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 657 | This is free software, and you are welcome to redistribute it 658 | under certain conditions; type `show c' for details. 659 | 660 | The hypothetical commands `show w' and `show c' should show the appropriate 661 | parts of the General Public License. Of course, your program's commands 662 | might be different; for a GUI interface, you would use an "about box". 663 | 664 | You should also get your employer (if you work as a programmer) or school, 665 | if any, to sign a "copyright disclaimer" for the program, if necessary. 666 | For more information on this, and how to apply and follow the GNU GPL, see 667 | . 668 | 669 | The GNU General Public License does not permit incorporating your program 670 | into proprietary programs. If your program is a subroutine library, you 671 | may consider it more useful to permit linking proprietary applications with 672 | the library. If this is what you want to do, use the GNU Lesser General 673 | Public License instead of this License. But first, please read 674 | . 675 | --------------------------------------------------------------------------------