Skip to content

Commit

Permalink
swaybg: split into standalone project
Browse files Browse the repository at this point in the history
The new upstream is https://github.com/swaywm/swaybg

This commit also refactors our use of gdk-pixbuf a bit, since the only
remaining reverse dependency is swaybar tray support.
  • Loading branch information
ddevault authored and emersion committed Apr 25, 2019
1 parent f62c755 commit 236ca63
Show file tree
Hide file tree
Showing 17 changed files with 107 additions and 738 deletions.
4 changes: 1 addition & 3 deletions README.es.md
Expand Up @@ -34,14 +34,12 @@ Instale las dependencias:
* json-c
* pango
* cairo
* gdk-pixbuf2 \*\*
* gdk-pixbuf2 (optional: system tray)
* [scdoc](https://git.sr.ht/~sircmpwn/scdoc) (optional: man pages) \*
* git \*

_\*Compile-time dep_

_\*\*opcional: necesario para swaybg_

Desde su consola, ejecute las órdenes:

meson build
Expand Down
4 changes: 1 addition & 3 deletions README.fr.md
Expand Up @@ -41,14 +41,12 @@ Installez les dépendances :
* json-c
* pango
* cairo
* gdk-pixbuf2 \*\*
* gdk-pixbuf2 (optionnel: system tray)
* [scdoc](https://git.sr.ht/~sircmpwn/scdoc) (optionnel: requis pour les pages man) \*
* git \*

_\*Requis uniquement pour la compilation_

_\*\*Optionnel: requis uniquement pour swaybg_

Exécutez ces commandes :

meson build
Expand Down
4 changes: 1 addition & 3 deletions README.ja.md
Expand Up @@ -36,14 +36,12 @@ Swayは沢山のディストリビューションで提供されています。"
* json-c
* pango
* cairo
* gdk-pixbuf2 \*\*
* gdk-pixbuf2 (システムイコンで必要です)
* [scdoc](https://git.sr.ht/~sircmpwn/scdoc) (manで必要です) \*
* git \*

_\*コンパイルの時_

_\*\*オプション: swaybgでのみ必要です_

次のコマンドを実行してください:

meson build
Expand Down
4 changes: 1 addition & 3 deletions README.md
Expand Up @@ -38,14 +38,12 @@ Install dependencies:
* json-c
* pango
* cairo
* gdk-pixbuf2 \*\*
* gdk-pixbuf2 (optional: system tray)
* [scdoc](https://git.sr.ht/~sircmpwn/scdoc) (optional: man pages) \*
* git \*

_\*Compile-time dep_

_\*\*optional: required for swaybg_

Run these commands:

meson build
Expand Down
4 changes: 1 addition & 3 deletions README.pl.md
Expand Up @@ -34,14 +34,12 @@ Zainstaluj zależności:
* json-c
* pango
* cairo
* gdk-pixbuf2 \*\*
* gdk-pixbuf2 (opcjonalnie: system tray)
* [scdoc](https://git.sr.ht/~sircmpwn/scdoc) (opcjonalnie: strony pomocy man) \*
* git \*

_\*zależności kompilacji_

_\*\*opcjonalnie: wymagane dla swaybg_

Wykonaj następujące polecenia:

meson build
Expand Down
4 changes: 1 addition & 3 deletions README.uk.md
Expand Up @@ -46,14 +46,12 @@ Sway доступний у багатьох дистрибутивах Linux (а
* json-c
* pango
* cairo
* gdk-pixbuf2 \*\*
* gdk-pixbuf2 (optional: system tray)
* [scdoc](https://git.sr.ht/~sircmpwn/scdoc) (необов'язково, необхідно для сторінок man) \*
* git \*

_\*Лише для компіляції_

_\*\*Необов'язково, необхідно для swaybg_

Виконайте ці команди:

meson build
Expand Down
4 changes: 1 addition & 3 deletions README.zh-CN.md
Expand Up @@ -35,14 +35,12 @@ Sway 在很多发行版中可用. 尝试在你的发行版中安装 "sway" 包.
* json-c
* pango
* cairo
* gdk-pixbuf2 \*\*
* gdk-pixbuf2 (可选的: system tray)
* [scdoc](https://git.sr.ht/~sircmpwn/scdoc) (可选的: man pages) \*
* git \*

_\*编译时依赖_

_\*\*可选的: swaybg 依赖_

运行这些命令:

meson build
Expand Down
1 change: 0 additions & 1 deletion client/meson.build
Expand Up @@ -5,7 +5,6 @@ lib_sway_client = static_library(
),
dependencies: [
cairo,
gdk_pixbuf,
pango,
pangocairo,
wayland_client
Expand Down
100 changes: 100 additions & 0 deletions common/background-image.c
Expand Up @@ -2,6 +2,9 @@
#include "background-image.h"
#include "cairo.h"
#include "log.h"
#if HAVE_GDK_PIXBUF
#include <gdk-pixbuf/gdk-pixbuf.h>
#endif

enum background_mode parse_background_mode(const char *mode) {
if (strcmp(mode, "stretch") == 0) {
Expand All @@ -21,6 +24,103 @@ enum background_mode parse_background_mode(const char *mode) {
return BACKGROUND_MODE_INVALID;
}

#if HAVE_GDK_PIXBUF
static cairo_surface_t* gdk_cairo_image_surface_create_from_pixbuf(
const GdkPixbuf *gdkbuf) {
int chan = gdk_pixbuf_get_n_channels(gdkbuf);
if (chan < 3) {
return NULL;
}

const guint8* gdkpix = gdk_pixbuf_read_pixels(gdkbuf);
if (!gdkpix) {
return NULL;
}
gint w = gdk_pixbuf_get_width(gdkbuf);
gint h = gdk_pixbuf_get_height(gdkbuf);
int stride = gdk_pixbuf_get_rowstride(gdkbuf);

cairo_format_t fmt = (chan == 3) ? CAIRO_FORMAT_RGB24 : CAIRO_FORMAT_ARGB32;
cairo_surface_t * cs = cairo_image_surface_create (fmt, w, h);
cairo_surface_flush (cs);
if ( !cs || cairo_surface_status(cs) != CAIRO_STATUS_SUCCESS) {
return NULL;
}

int cstride = cairo_image_surface_get_stride(cs);
unsigned char * cpix = cairo_image_surface_get_data(cs);

if (chan == 3) {
int i;
for (i = h; i; --i) {
const guint8 *gp = gdkpix;
unsigned char *cp = cpix;
const guint8* end = gp + 3*w;
while (gp < end) {
#if G_BYTE_ORDER == G_LITTLE_ENDIAN
cp[0] = gp[2];
cp[1] = gp[1];
cp[2] = gp[0];
#else
cp[1] = gp[0];
cp[2] = gp[1];
cp[3] = gp[2];
#endif
gp += 3;
cp += 4;
}
gdkpix += stride;
cpix += cstride;
}
} else {
/* premul-color = alpha/255 * color/255 * 255 = (alpha*color)/255
* (z/255) = z/256 * 256/255 = z/256 (1 + 1/255)
* = z/256 + (z/256)/255 = (z + z/255)/256
* # recurse once
* = (z + (z + z/255)/256)/256
* = (z + z/256 + z/256/255) / 256
* # only use 16bit uint operations, loose some precision,
* # result is floored.
* -> (z + z>>8)>>8
* # add 0x80/255 = 0.5 to convert floor to round
* => (z+0x80 + (z+0x80)>>8 ) >> 8
* ------
* tested as equal to lround(z/255.0) for uint z in [0..0xfe02]
*/
#define PREMUL_ALPHA(x,a,b,z) \
G_STMT_START { z = a * b + 0x80; x = (z + (z >> 8)) >> 8; } \
G_STMT_END
int i;
for (i = h; i; --i) {
const guint8 *gp = gdkpix;
unsigned char *cp = cpix;
const guint8* end = gp + 4*w;
guint z1, z2, z3;
while (gp < end) {
#if G_BYTE_ORDER == G_LITTLE_ENDIAN
PREMUL_ALPHA(cp[0], gp[2], gp[3], z1);
PREMUL_ALPHA(cp[1], gp[1], gp[3], z2);
PREMUL_ALPHA(cp[2], gp[0], gp[3], z3);
cp[3] = gp[3];
#else
PREMUL_ALPHA(cp[1], gp[0], gp[3], z1);
PREMUL_ALPHA(cp[2], gp[1], gp[3], z2);
PREMUL_ALPHA(cp[3], gp[2], gp[3], z3);
cp[0] = gp[3];
#endif
gp += 4;
cp += 4;
}
gdkpix += stride;
cpix += cstride;
}
#undef PREMUL_ALPHA
}
cairo_surface_mark_dirty(cs);
return cs;
}
#endif // HAVE_GDK_PIXBUF

cairo_surface_t *load_background_image(const char *path) {
cairo_surface_t *image;
#if HAVE_GDK_PIXBUF
Expand Down
99 changes: 0 additions & 99 deletions common/cairo.c
@@ -1,9 +1,6 @@
#include <stdint.h>
#include <cairo/cairo.h>
#include "cairo.h"
#if HAVE_GDK_PIXBUF
#include <gdk-pixbuf/gdk-pixbuf.h>
#endif

void cairo_set_source_u32(cairo_t *cairo, uint32_t color) {
cairo_set_source_rgba(cairo,
Expand Down Expand Up @@ -45,99 +42,3 @@ cairo_surface_t *cairo_image_surface_scale(cairo_surface_t *image,
cairo_destroy(cairo);
return new;
}

#if HAVE_GDK_PIXBUF
cairo_surface_t* gdk_cairo_image_surface_create_from_pixbuf(const GdkPixbuf *gdkbuf) {
int chan = gdk_pixbuf_get_n_channels(gdkbuf);
if (chan < 3) {
return NULL;
}

const guint8* gdkpix = gdk_pixbuf_read_pixels(gdkbuf);
if (!gdkpix) {
return NULL;
}
gint w = gdk_pixbuf_get_width(gdkbuf);
gint h = gdk_pixbuf_get_height(gdkbuf);
int stride = gdk_pixbuf_get_rowstride(gdkbuf);

cairo_format_t fmt = (chan == 3) ? CAIRO_FORMAT_RGB24 : CAIRO_FORMAT_ARGB32;
cairo_surface_t * cs = cairo_image_surface_create (fmt, w, h);
cairo_surface_flush (cs);
if ( !cs || cairo_surface_status(cs) != CAIRO_STATUS_SUCCESS) {
return NULL;
}

int cstride = cairo_image_surface_get_stride(cs);
unsigned char * cpix = cairo_image_surface_get_data(cs);

if (chan == 3) {
int i;
for (i = h; i; --i) {
const guint8 *gp = gdkpix;
unsigned char *cp = cpix;
const guint8* end = gp + 3*w;
while (gp < end) {
#if G_BYTE_ORDER == G_LITTLE_ENDIAN
cp[0] = gp[2];
cp[1] = gp[1];
cp[2] = gp[0];
#else
cp[1] = gp[0];
cp[2] = gp[1];
cp[3] = gp[2];
#endif
gp += 3;
cp += 4;
}
gdkpix += stride;
cpix += cstride;
}
} else {
/* premul-color = alpha/255 * color/255 * 255 = (alpha*color)/255
* (z/255) = z/256 * 256/255 = z/256 (1 + 1/255)
* = z/256 + (z/256)/255 = (z + z/255)/256
* # recurse once
* = (z + (z + z/255)/256)/256
* = (z + z/256 + z/256/255) / 256
* # only use 16bit uint operations, loose some precision,
* # result is floored.
* -> (z + z>>8)>>8
* # add 0x80/255 = 0.5 to convert floor to round
* => (z+0x80 + (z+0x80)>>8 ) >> 8
* ------
* tested as equal to lround(z/255.0) for uint z in [0..0xfe02]
*/
#define PREMUL_ALPHA(x,a,b,z) \
G_STMT_START { z = a * b + 0x80; x = (z + (z >> 8)) >> 8; } \
G_STMT_END
int i;
for (i = h; i; --i) {
const guint8 *gp = gdkpix;
unsigned char *cp = cpix;
const guint8* end = gp + 4*w;
guint z1, z2, z3;
while (gp < end) {
#if G_BYTE_ORDER == G_LITTLE_ENDIAN
PREMUL_ALPHA(cp[0], gp[2], gp[3], z1);
PREMUL_ALPHA(cp[1], gp[1], gp[3], z2);
PREMUL_ALPHA(cp[2], gp[0], gp[3], z3);
cp[3] = gp[3];
#else
PREMUL_ALPHA(cp[1], gp[0], gp[3], z1);
PREMUL_ALPHA(cp[2], gp[1], gp[3], z2);
PREMUL_ALPHA(cp[3], gp[2], gp[3], z3);
cp[0] = gp[3];
#endif
gp += 4;
cp += 4;
}
gdkpix += stride;
cpix += cstride;
}
#undef PREMUL_ALPHA
}
cairo_surface_mark_dirty(cs);
return cs;
}
#endif // HAVE_GDK_PIXBUF
11 changes: 0 additions & 11 deletions include/cairo.h
@@ -1,25 +1,14 @@
#ifndef _SWAY_CAIRO_H
#define _SWAY_CAIRO_H

#include "config.h"
#include <stdint.h>
#include <cairo/cairo.h>
#include <wayland-client-protocol.h>
#if HAVE_GDK_PIXBUF
#include <gdk-pixbuf/gdk-pixbuf.h>
#endif

void cairo_set_source_u32(cairo_t *cairo, uint32_t color);
cairo_subpixel_order_t to_cairo_subpixel_order(enum wl_output_subpixel subpixel);

cairo_surface_t *cairo_image_surface_scale(cairo_surface_t *image,
int width, int height);

#if HAVE_GDK_PIXBUF

cairo_surface_t* gdk_cairo_image_surface_create_from_pixbuf(
const GdkPixbuf *gdkbuf);

#endif // HAVE_GDK_PIXBUF

#endif
2 changes: 0 additions & 2 deletions meson.build
Expand Up @@ -107,7 +107,6 @@ if scdoc.found()
'sway/sway-input.5.scd',
'sway/sway-ipc.7.scd',
'sway/sway-output.5.scd',
'swaybg/swaybg.1.scd',
'swaymsg/swaymsg.1.scd',
'swaynag/swaynag.1.scd',
'swaynag/swaynag.5.scd',
Expand Down Expand Up @@ -151,7 +150,6 @@ subdir('sway')
subdir('swaymsg')

subdir('client')
subdir('swaybg')
subdir('swaybar')
subdir('swaynag')

Expand Down
1 change: 0 additions & 1 deletion sway/meson.build
Expand Up @@ -186,7 +186,6 @@ sway_sources = files(

sway_deps = [
cairo,
gdk_pixbuf,
jsonc,
libevdev,
libinput,
Expand Down

0 comments on commit 236ca63

Please sign in to comment.