aboutsummaryrefslogtreecommitdiff
path: root/common
diff options
context:
space:
mode:
authorTony Crisci <tony@dubstepdish.com>2018-04-04 22:36:09 -0400
committerTony Crisci <tony@dubstepdish.com>2018-04-04 22:36:09 -0400
commit65f254f3fbc83d006d4ec29170ec8a8695345d6c (patch)
tree3044fb62120ca23499d31275076af50db09a9850 /common
parentdeda37469ad4e21ad86b7c83c7c8a966301b9d5e (diff)
parent21aedf15052df4e7f8ee72922fa0e214d690facc (diff)
Merge branch 'wlroots' into fix-focus-inactive
Diffstat (limited to 'common')
-rw-r--r--common/background-image.c119
-rw-r--r--common/meson.build21
-rw-r--r--common/unicode.c101
3 files changed, 229 insertions, 12 deletions
diff --git a/common/background-image.c b/common/background-image.c
new file mode 100644
index 00000000..e5fb4433
--- /dev/null
+++ b/common/background-image.c
@@ -0,0 +1,119 @@
+#include <assert.h>
+#include <stdbool.h>
+#include <wlr/util/log.h>
+#include "background-image.h"
+#include "cairo.h"
+
+enum background_mode parse_background_mode(const char *mode) {
+ if (strcmp(mode, "stretch") == 0) {
+ return BACKGROUND_MODE_STRETCH;
+ } else if (strcmp(mode, "fill") == 0) {
+ return BACKGROUND_MODE_FILL;
+ } else if (strcmp(mode, "fit") == 0) {
+ return BACKGROUND_MODE_FIT;
+ } else if (strcmp(mode, "center") == 0) {
+ return BACKGROUND_MODE_CENTER;
+ } else if (strcmp(mode, "tile") == 0) {
+ return BACKGROUND_MODE_TILE;
+ } else if (strcmp(mode, "solid_color") == 0) {
+ return BACKGROUND_MODE_SOLID_COLOR;
+ }
+ wlr_log(L_ERROR, "Unsupported background mode: %s", mode);
+ return BACKGROUND_MODE_INVALID;
+}
+
+cairo_surface_t *load_background_image(const char *path) {
+ cairo_surface_t *image;
+#ifdef HAVE_GDK_PIXBUF
+ GError *err = NULL;
+ GdkPixbuf *pixbuf = gdk_pixbuf_new_from_file(path, &err);
+ if (!pixbuf) {
+ wlr_log(L_ERROR, "Failed to load background image (%s).",
+ err->message);
+ return false;
+ }
+ image = gdk_cairo_image_surface_create_from_pixbuf(pixbuf);
+ g_object_unref(pixbuf);
+#else
+ image = cairo_image_surface_create_from_png(path);
+#endif //HAVE_GDK_PIXBUF
+ if (!image) {
+ wlr_log(L_ERROR, "Failed to read background image.");
+ return NULL;
+ }
+ if (cairo_surface_status(image) != CAIRO_STATUS_SUCCESS) {
+ wlr_log(L_ERROR, "Failed to read background image: %s."
+#ifndef HAVE_GDK_PIXBUF
+ "\nSway was compiled without gdk_pixbuf support, so only"
+ "\nPNG images can be loaded. This is the likely cause."
+#endif //HAVE_GDK_PIXBUF
+ , cairo_status_to_string(cairo_surface_status(image)));
+ return NULL;
+ }
+ return image;
+}
+
+void render_background_image(cairo_t *cairo, cairo_surface_t *image,
+ enum background_mode mode, int buffer_width, int buffer_height) {
+ double width = cairo_image_surface_get_width(image);
+ double height = cairo_image_surface_get_height(image);
+
+ switch (mode) {
+ case BACKGROUND_MODE_STRETCH:
+ cairo_scale(cairo,
+ (double)buffer_width / width,
+ (double)buffer_height / height);
+ cairo_set_source_surface(cairo, image, 0, 0);
+ break;
+ case BACKGROUND_MODE_FILL: {
+ double window_ratio = (double)buffer_width / buffer_height;
+ double bg_ratio = width / height;
+
+ if (window_ratio > bg_ratio) {
+ double scale = (double)buffer_width / width;
+ cairo_scale(cairo, scale, scale);
+ cairo_set_source_surface(cairo, image,
+ 0, (double)buffer_height / 2 / scale - height / 2);
+ } else {
+ double scale = (double)buffer_height / height;
+ cairo_scale(cairo, scale, scale);
+ cairo_set_source_surface(cairo, image,
+ (double)buffer_width / 2 / scale - width / 2, 0);
+ }
+ break;
+ }
+ case BACKGROUND_MODE_FIT: {
+ double window_ratio = (double)buffer_width / buffer_height;
+ double bg_ratio = width / height;
+
+ if (window_ratio > bg_ratio) {
+ double scale = (double)buffer_height / height;
+ cairo_scale(cairo, scale, scale);
+ cairo_set_source_surface(cairo, image,
+ (double)buffer_width / 2 / scale - width / 2, 0);
+ } else {
+ double scale = (double)buffer_width / width;
+ cairo_scale(cairo, scale, scale);
+ cairo_set_source_surface(cairo, image,
+ 0, (double)buffer_height / 2 / scale - height / 2);
+ }
+ break;
+ }
+ case BACKGROUND_MODE_CENTER:
+ cairo_set_source_surface(cairo, image,
+ (double)buffer_width / 2 - width / 2,
+ (double)buffer_height / 2 - height / 2);
+ break;
+ case BACKGROUND_MODE_TILE: {
+ cairo_pattern_t *pattern = cairo_pattern_create_for_surface(image);
+ cairo_pattern_set_extend(pattern, CAIRO_EXTEND_REPEAT);
+ cairo_set_source(cairo, pattern);
+ break;
+ }
+ case BACKGROUND_MODE_SOLID_COLOR:
+ case BACKGROUND_MODE_INVALID:
+ assert(0);
+ break;
+ }
+ cairo_paint(cairo);
+}
diff --git a/common/meson.build b/common/meson.build
index 4ad47077..44a29508 100644
--- a/common/meson.build
+++ b/common/meson.build
@@ -1,17 +1,7 @@
-deps = [
- cairo,
- pango,
- pangocairo,
- wlroots
-]
-
-if gdk_pixbuf.found()
- deps += [gdk_pixbuf]
-endif
-
lib_sway_common = static_library(
'sway-common',
files(
+ 'background-image.c',
'cairo.c',
'ipc-client.c',
'log.c',
@@ -19,8 +9,15 @@ lib_sway_common = static_library(
'pango.c',
'readline.c',
'stringop.c',
+ 'unicode.c',
'util.c'
),
- dependencies: deps,
+ dependencies: [
+ cairo,
+ gdk_pixbuf,
+ pango,
+ pangocairo,
+ wlroots
+ ],
include_directories: sway_inc
)
diff --git a/common/unicode.c b/common/unicode.c
new file mode 100644
index 00000000..38a9b48e
--- /dev/null
+++ b/common/unicode.c
@@ -0,0 +1,101 @@
+#include <stdint.h>
+#include <stddef.h>
+#include "unicode.h"
+
+size_t utf8_chsize(uint32_t ch) {
+ if (ch < 0x80) {
+ return 1;
+ } else if (ch < 0x800) {
+ return 2;
+ } else if (ch < 0x10000) {
+ return 3;
+ }
+ return 4;
+}
+
+static const uint8_t masks[] = {
+ 0x7F,
+ 0x1F,
+ 0x0F,
+ 0x07,
+ 0x03,
+ 0x01
+};
+
+uint32_t utf8_decode(const char **char_str) {
+ uint8_t **s = (uint8_t **)char_str;
+
+ uint32_t cp = 0;
+ if (**s < 128) {
+ // shortcut
+ cp = **s;
+ ++*s;
+ return cp;
+ }
+ int size = utf8_size((char *)*s);
+ if (size == -1) {
+ ++*s;
+ return UTF8_INVALID;
+ }
+ uint8_t mask = masks[size - 1];
+ cp = **s & mask;
+ ++*s;
+ while (--size) {
+ cp <<= 6;
+ cp |= **s & 0x3f;
+ ++*s;
+ }
+ return cp;
+}
+
+size_t utf8_encode(char *str, uint32_t ch) {
+ size_t len = 0;
+ uint8_t first;
+
+ if (ch < 0x80) {
+ first = 0;
+ len = 1;
+ } else if (ch < 0x800) {
+ first = 0xc0;
+ len = 2;
+ } else if (ch < 0x10000) {
+ first = 0xe0;
+ len = 3;
+ } else {
+ first = 0xf0;
+ len = 4;
+ }
+
+ for (size_t i = len - 1; i > 0; --i) {
+ str[i] = (ch & 0x3f) | 0x80;
+ ch >>= 6;
+ }
+
+ str[0] = ch | first;
+ return len;
+}
+
+
+static const struct {
+ uint8_t mask;
+ uint8_t result;
+ int octets;
+} sizes[] = {
+ { 0x80, 0x00, 1 },
+ { 0xE0, 0xC0, 2 },
+ { 0xF0, 0xE0, 3 },
+ { 0xF8, 0xF0, 4 },
+ { 0xFC, 0xF8, 5 },
+ { 0xFE, 0xF8, 6 },
+ { 0x80, 0x80, -1 },
+};
+
+int utf8_size(const char *s) {
+ uint8_t c = (uint8_t)*s;
+ for (size_t i = 0; i < sizeof(sizes) / 2; ++i) {
+ if ((c & sizes[i].mask) == sizes[i].result) {
+ return sizes[i].octets;
+ }
+ }
+ return -1;
+}