diff options
Diffstat (limited to 'common')
-rw-r--r-- | common/ipc-client.c | 32 | ||||
-rw-r--r-- | common/meson.build | 3 | ||||
-rw-r--r-- | common/pango.c | 65 |
3 files changed, 93 insertions, 7 deletions
diff --git a/common/ipc-client.c b/common/ipc-client.c index 582c5e86..117e9910 100644 --- a/common/ipc-client.c +++ b/common/ipc-client.c | |||
@@ -1,4 +1,4 @@ | |||
1 | #define _POSIX_C_SOURCE 2 | 1 | #define _POSIX_C_SOURCE 200809L |
2 | #include <stdio.h> | 2 | #include <stdio.h> |
3 | #include <stdint.h> | 3 | #include <stdint.h> |
4 | #include <stdlib.h> | 4 | #include <stdlib.h> |
@@ -14,13 +14,31 @@ static const char ipc_magic[] = {'i', '3', '-', 'i', 'p', 'c'}; | |||
14 | static const size_t ipc_header_size = sizeof(ipc_magic)+8; | 14 | static const size_t ipc_header_size = sizeof(ipc_magic)+8; |
15 | 15 | ||
16 | char *get_socketpath(void) { | 16 | char *get_socketpath(void) { |
17 | FILE *fp = popen("sway --get-socketpath", "r"); | 17 | const char *swaysock = getenv("SWAYSOCK"); |
18 | if (!fp) { | 18 | if (swaysock) { |
19 | return NULL; | 19 | return strdup(swaysock); |
20 | } | 20 | } |
21 | char *line = read_line(fp); | 21 | FILE *fp = popen("sway --get-socketpath 2>/dev/null", "r"); |
22 | pclose(fp); | 22 | if (fp) { |
23 | return line; | 23 | char *line = read_line(fp); |
24 | pclose(fp); | ||
25 | if (line && *line) { | ||
26 | return line; | ||
27 | } | ||
28 | } | ||
29 | const char *i3sock = getenv("I3SOCK"); | ||
30 | if (i3sock) { | ||
31 | return strdup(i3sock); | ||
32 | } | ||
33 | fp = popen("i3 --get-socketpath 2>/dev/null", "r"); | ||
34 | if (fp) { | ||
35 | char *line = read_line(fp); | ||
36 | pclose(fp); | ||
37 | if (line && *line) { | ||
38 | return line; | ||
39 | } | ||
40 | } | ||
41 | return NULL; | ||
24 | } | 42 | } |
25 | 43 | ||
26 | int ipc_open_socket(const char *socket_path) { | 44 | int ipc_open_socket(const char *socket_path) { |
diff --git a/common/meson.build b/common/meson.build index 01736ca6..4ad47077 100644 --- a/common/meson.build +++ b/common/meson.build | |||
@@ -1,5 +1,7 @@ | |||
1 | deps = [ | 1 | deps = [ |
2 | cairo, | 2 | cairo, |
3 | pango, | ||
4 | pangocairo, | ||
3 | wlroots | 5 | wlroots |
4 | ] | 6 | ] |
5 | 7 | ||
@@ -14,6 +16,7 @@ lib_sway_common = static_library( | |||
14 | 'ipc-client.c', | 16 | 'ipc-client.c', |
15 | 'log.c', | 17 | 'log.c', |
16 | 'list.c', | 18 | 'list.c', |
19 | 'pango.c', | ||
17 | 'readline.c', | 20 | 'readline.c', |
18 | 'stringop.c', | 21 | 'stringop.c', |
19 | 'util.c' | 22 | 'util.c' |
diff --git a/common/pango.c b/common/pango.c new file mode 100644 index 00000000..2ae7883c --- /dev/null +++ b/common/pango.c | |||
@@ -0,0 +1,65 @@ | |||
1 | #include <cairo/cairo.h> | ||
2 | #include <pango/pangocairo.h> | ||
3 | #include <stdarg.h> | ||
4 | #include <stdbool.h> | ||
5 | #include <stdint.h> | ||
6 | #include <stdio.h> | ||
7 | #include <stdlib.h> | ||
8 | #include <string.h> | ||
9 | |||
10 | PangoLayout *get_pango_layout(cairo_t *cairo, const char *font, | ||
11 | const char *text, int32_t scale, bool markup) { | ||
12 | PangoLayout *layout = pango_cairo_create_layout(cairo); | ||
13 | PangoAttrList *attrs; | ||
14 | if (markup) { | ||
15 | char *buf; | ||
16 | pango_parse_markup(text, -1, 0, &attrs, &buf, NULL, NULL); | ||
17 | pango_layout_set_markup(layout, buf, -1); | ||
18 | free(buf); | ||
19 | } else { | ||
20 | attrs = pango_attr_list_new(); | ||
21 | pango_layout_set_text(layout, text, -1); | ||
22 | } | ||
23 | pango_attr_list_insert(attrs, pango_attr_scale_new(scale)); | ||
24 | PangoFontDescription *desc = pango_font_description_from_string(font); | ||
25 | pango_layout_set_font_description(layout, desc); | ||
26 | pango_layout_set_single_paragraph_mode(layout, 1); | ||
27 | pango_layout_set_attributes(layout, attrs); | ||
28 | pango_attr_list_unref(attrs); | ||
29 | pango_font_description_free(desc); | ||
30 | return layout; | ||
31 | } | ||
32 | |||
33 | void get_text_size(cairo_t *cairo, const char *font, int *width, int *height, | ||
34 | int32_t scale, bool markup, const char *fmt, ...) { | ||
35 | static char buf[2048]; | ||
36 | |||
37 | va_list args; | ||
38 | va_start(args, fmt); | ||
39 | if (vsnprintf(buf, 2048, fmt, args) >= 2048) { | ||
40 | strcpy(buf, "[buffer overflow]"); | ||
41 | } | ||
42 | va_end(args); | ||
43 | |||
44 | PangoLayout *layout = get_pango_layout(cairo, font, buf, scale, markup); | ||
45 | pango_cairo_update_layout(cairo, layout); | ||
46 | pango_layout_get_pixel_size(layout, width, height); | ||
47 | g_object_unref(layout); | ||
48 | } | ||
49 | |||
50 | void pango_printf(cairo_t *cairo, const char *font, | ||
51 | int32_t scale, bool markup, const char *fmt, ...) { | ||
52 | static char buf[2048]; | ||
53 | |||
54 | va_list args; | ||
55 | va_start(args, fmt); | ||
56 | if (vsnprintf(buf, 2048, fmt, args) >= 2048) { | ||
57 | strcpy(buf, "[buffer overflow]"); | ||
58 | } | ||
59 | va_end(args); | ||
60 | |||
61 | PangoLayout *layout = get_pango_layout(cairo, font, buf, scale, markup); | ||
62 | pango_cairo_update_layout(cairo, layout); | ||
63 | pango_cairo_show_layout(cairo, layout); | ||
64 | g_object_unref(layout); | ||
65 | } | ||