summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLibravatar Drew DeVault <sir@cmpwn.com>2015-11-29 15:27:28 -0500
committerLibravatar Drew DeVault <sir@cmpwn.com>2015-11-29 15:27:28 -0500
commit82d464bb90af7fed57a36aacc2dce22994849bac (patch)
tree471b8eba3fb838115b163980b558cb151c6dbefc
parentOnly strip comments at the start of a line (diff)
downloadsway-82d464bb90af7fed57a36aacc2dce22994849bac.tar.gz
sway-82d464bb90af7fed57a36aacc2dce22994849bac.tar.zst
sway-82d464bb90af7fed57a36aacc2dce22994849bac.zip
Add text rendering support to wayland clients
-rw-r--r--include/client/pango.h12
-rw-r--r--include/client/window.h1
-rw-r--r--swaybar/main.c13
-rw-r--r--wayland/pango.c59
-rw-r--r--wayland/window.c4
5 files changed, 87 insertions, 2 deletions
diff --git a/include/client/pango.h b/include/client/pango.h
new file mode 100644
index 00000000..e25a2211
--- /dev/null
+++ b/include/client/pango.h
@@ -0,0 +1,12 @@
1#ifndef _SWAY_CLIENT_PANGO_H
2#define _SWAY_CLIENT_PANGO_H
3
4#include "client/window.h"
5#include "client/buffer.h"
6#include <stdarg.h>
7
8PangoLayout *get_pango_layout(struct window *window, struct buffer *buffer, const char *text);
9void get_text_size(struct window *window, int *width, int *height, const char *fmt, ...);
10void pango_printf(struct window *window, const char *fmt, ...);
11
12#endif
diff --git a/include/client/window.h b/include/client/window.h
index af954003..eff9032d 100644
--- a/include/client/window.h
+++ b/include/client/window.h
@@ -34,6 +34,7 @@ struct window {
34 struct wl_callback *frame_cb; 34 struct wl_callback *frame_cb;
35 struct cursor cursor; 35 struct cursor cursor;
36 uint32_t width, height; 36 uint32_t width, height;
37 char *font;
37 cairo_t *cairo; 38 cairo_t *cairo;
38}; 39};
39 40
diff --git a/swaybar/main.c b/swaybar/main.c
index b25d8252..7a54f74b 100644
--- a/swaybar/main.c
+++ b/swaybar/main.c
@@ -2,8 +2,11 @@
2#include <stdlib.h> 2#include <stdlib.h>
3#include "client/registry.h" 3#include "client/registry.h"
4#include "client/window.h" 4#include "client/window.h"
5#include "client/pango.h"
5#include "log.h" 6#include "log.h"
6 7
8#define MARGIN 5
9
7struct box_colors { 10struct box_colors {
8 uint32_t border; 11 uint32_t border;
9 uint32_t background; 12 uint32_t background;
@@ -71,15 +74,17 @@ void cairo_set_source_u32(cairo_t *cairo, uint32_t color) {
71} 74}
72 75
73void render() { 76void render() {
74 // Reset buffer
75 cairo_save(window->cairo); 77 cairo_save(window->cairo);
76 cairo_set_operator(window->cairo, CAIRO_OPERATOR_CLEAR); 78 cairo_set_operator(window->cairo, CAIRO_OPERATOR_CLEAR);
77 cairo_paint(window->cairo); 79 cairo_paint(window->cairo);
78 cairo_restore(window->cairo); 80 cairo_restore(window->cairo);
79 81
80 // Draw bar
81 cairo_set_source_u32(window->cairo, colors.background); 82 cairo_set_source_u32(window->cairo, colors.background);
82 cairo_paint(window->cairo); 83 cairo_paint(window->cairo);
84
85 cairo_move_to(window->cairo, MARGIN, MARGIN);
86 cairo_set_source_u32(window->cairo, colors.statusline);
87 pango_printf(window, "TODO: finish bar");
83} 88}
84 89
85int main(int argc, char **argv) { 90int main(int argc, char **argv) {
@@ -101,6 +106,10 @@ int main(int argc, char **argv) {
101 desktop_shell_set_panel(registry->desktop_shell, output->output, window->surface); 106 desktop_shell_set_panel(registry->desktop_shell, output->output, window->surface);
102 desktop_shell_set_panel_position(registry->desktop_shell, DESKTOP_SHELL_PANEL_POSITION_BOTTOM); 107 desktop_shell_set_panel_position(registry->desktop_shell, DESKTOP_SHELL_PANEL_POSITION_BOTTOM);
103 108
109 int width, height;
110 get_text_size(window, &width, &height, "Test string for measuring purposes");
111 window->height = height + MARGIN * 2;
112
104 do { 113 do {
105 if (window_prerender(window) && window->cairo) { 114 if (window_prerender(window) && window->cairo) {
106 render(); 115 render();
diff --git a/wayland/pango.c b/wayland/pango.c
new file mode 100644
index 00000000..9766be6a
--- /dev/null
+++ b/wayland/pango.c
@@ -0,0 +1,59 @@
1#include <cairo/cairo.h>
2#include <pango/pangocairo.h>
3#include <stdarg.h>
4#include <stdlib.h>
5#include <string.h>
6#include <stdio.h>
7#include "client/window.h"
8#include "client/buffer.h"
9#include "log.h"
10
11PangoLayout *get_pango_layout(struct window *window, const char *text) {
12 PangoLayout *layout = pango_cairo_create_layout(window->cairo);
13 pango_layout_set_text(layout, text, -1);
14 PangoFontDescription *desc = pango_font_description_from_string(window->font);
15 pango_layout_set_font_description(layout, desc);
16 pango_layout_set_single_paragraph_mode(layout, 1);
17 pango_font_description_free(desc);
18 return layout;
19}
20
21void get_text_size(struct window *window, int *width, int *height, const char *fmt, ...) {
22 char *buf = malloc(2048);
23
24 va_list args;
25 va_start(args, fmt);
26 if (vsnprintf(buf, 2048, fmt, args) >= 2048) {
27 strcpy(buf, "[buffer overflow]");
28 }
29 va_end(args);
30
31 PangoLayout *layout = get_pango_layout(window, buf);
32 pango_cairo_update_layout(window->cairo, layout);
33
34 pango_layout_get_pixel_size(layout, width, height);
35
36 g_object_unref(layout);
37
38 free(buf);
39}
40
41void pango_printf(struct window *window, const char *fmt, ...) {
42 char *buf = malloc(2048);
43
44 va_list args;
45 va_start(args, fmt);
46 if (vsnprintf(buf, 2048, fmt, args) >= 2048) {
47 strcpy(buf, "[buffer overflow]");
48 }
49 va_end(args);
50
51 PangoLayout *layout = get_pango_layout(window, buf);
52 pango_cairo_update_layout(window->cairo, layout);
53
54 pango_cairo_show_layout(window->cairo, layout);
55
56 g_object_unref(layout);
57
58 free(buf);
59}
diff --git a/wayland/window.c b/wayland/window.c
index 13d4c7b2..916e3b57 100644
--- a/wayland/window.c
+++ b/wayland/window.c
@@ -63,6 +63,7 @@ struct window *window_setup(struct registry *registry, uint32_t width, uint32_t
63 window->width = width; 63 window->width = width;
64 window->height = height; 64 window->height = height;
65 window->registry = registry; 65 window->registry = registry;
66 window->font = "monospace 10";
66 67
67 window->surface = wl_compositor_create_surface(registry->compositor); 68 window->surface = wl_compositor_create_surface(registry->compositor);
68 if (shell_surface) { 69 if (shell_surface) {
@@ -74,6 +75,8 @@ struct window *window_setup(struct registry *registry, uint32_t width, uint32_t
74 wl_pointer_add_listener(registry->pointer, &pointer_listener, window); 75 wl_pointer_add_listener(registry->pointer, &pointer_listener, window);
75 } 76 }
76 77
78 get_next_buffer(window);
79
77 window->cursor.cursor_theme = wl_cursor_theme_load("default", 32, registry->shm); // TODO: let you customize this 80 window->cursor.cursor_theme = wl_cursor_theme_load("default", 32, registry->shm); // TODO: let you customize this
78 window->cursor.cursor = wl_cursor_theme_get_cursor(window->cursor.cursor_theme, "left_ptr"); 81 window->cursor.cursor = wl_cursor_theme_get_cursor(window->cursor.cursor_theme, "left_ptr");
79 window->cursor.surface = wl_compositor_create_surface(registry->compositor); 82 window->cursor.surface = wl_compositor_create_surface(registry->compositor);
@@ -118,4 +121,5 @@ int window_render(struct window *window) {
118} 121}
119 122
120void window_teardown(struct window *window) { 123void window_teardown(struct window *window) {
124 // TODO
121} 125}