Merge pull request #805 from zandrmartin/swaybg-solid-color

implement solid color rendering for swaybg
master
Drew DeVault 8 years ago committed by GitHub
commit 44a4905466

@ -12,6 +12,8 @@ add_library(sway-common STATIC
stringop.c
)
target_link_libraries(sway-common m)
if(Backtrace_FOUND)
set_target_properties(sway-common
PROPERTIES

@ -97,3 +97,16 @@ pid_t get_parent_pid(pid_t child) {
return -1;
}
uint32_t parse_color(const char *color) {
int len = strlen(color);
if (color[0] != '#' || (len != 7 && len != 9)) {
sway_log(L_DEBUG, "Invalid color %s, defaulting to color 0xFFFFFFFF", color);
return 0xFFFFFFFF;
}
uint32_t res = (uint32_t)strtol(color + 1, NULL, 16);
if (strlen(color) == 7) {
res = (res << 8) | 0xFF;
}
return res;
}

@ -5,6 +5,7 @@
#include <stdbool.h>
#include "list.h"
#include "util.h"
/**
* Colors for a box with background, border and text colors.
@ -47,11 +48,6 @@ struct config {
} colors;
};
/**
* Parse colors defined as hex string to uint32_t.
*/
uint32_t parse_color(const char *color);
/**
* Parse position top|bottom|left|right.
*/

@ -44,4 +44,10 @@ int get_modifier_names(const char **names, uint32_t modifier_masks);
*/
pid_t get_parent_pid(pid_t pid);
/**
* Given a string that represents an RGB(A) color, return a uint32_t
* version of the color.
*/
uint32_t parse_color(const char *color);
#endif

@ -1651,11 +1651,15 @@ static struct cmd_results *cmd_output(int argc, char **argv) {
} else if (strcasecmp(command, "background") == 0 || strcasecmp(command, "bg") == 0) {
wordexp_t p;
if (++i >= argc) {
return cmd_results_new(CMD_INVALID, "output", "Missing background file.");
return cmd_results_new(CMD_INVALID, "output", "Missing background file or color specification.");
}
if (i + 1 >= argc) {
return cmd_results_new(CMD_INVALID, "output", "Missing background scaling mode.");
return cmd_results_new(CMD_INVALID, "output", "Missing background scaling mode or `solid_color`.");
}
if (strcasecmp(argv[argc - 1], "solid_color") == 0) {
output->background = strdup(argv[argc - 2]);
output->background_option = strdup("solid_color");
} else {
char *src = join_args(argv + i, argc - i - 1);
char *mode = argv[argc - 1];
if (wordexp(src, &p, 0) != 0 || p.we_wordv[0] == NULL) {
@ -1694,6 +1698,7 @@ static struct cmd_results *cmd_output(int argc, char **argv) {
wordfree(&p);
}
}
}
i = list_seq_find(config->output_configs, output_name_cmp, name);
if (i >= 0) {

@ -308,6 +308,10 @@ The default colors are:
Sets the wallpaper for the given output to the specified file, using the given
scaling mode (one of "stretch", "fill", "fit", "center", "tile").
**output** <name> <background|bg> <color> solid_color::
Sets the background of the given output to the specified color. _color_ should
be specified as an _#rrggbb_ (no alpha) color.
**output** <name> disable::
Disables the specified output.

@ -5,19 +5,6 @@
#include "log.h"
#include "bar/config.h"
uint32_t parse_color(const char *color) {
if (color[0] != '#') {
sway_log(L_DEBUG, "Invalid color %s, defaulting to color 0xFFFFFFFF", color);
return 0xFFFFFFFF;
}
char *end;
uint32_t res = (uint32_t)strtol(color + 1, &end, 16);
if (strlen(color) == 7) {
res = (res << 8) | 0xFF;
}
return res;
}
uint32_t parse_position(const char *position) {
if (strcmp("top", position) == 0) {
return DESKTOP_SHELL_PANEL_POSITION_TOP;

@ -6,6 +6,7 @@
#include "log.h"
#include "bar/config.h"
#include "bar/status_line.h"
#include "util.h"
#define I3JSON_MAXDEPTH 4
#define I3JSON_UNKNOWN 0

@ -1,6 +1,8 @@
#include "wayland-desktop-shell-client-protocol.h"
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <wayland-client.h>
#include <time.h>
#include <string.h>
@ -9,6 +11,7 @@
#include "client/cairo.h"
#include "log.h"
#include "list.h"
#include "util.h"
list_t *surfaces;
struct registry *registry;
@ -32,6 +35,23 @@ void sway_terminate(int exit_code) {
exit(exit_code);
}
bool is_valid_color(const char *color) {
int len = strlen(color);
if (len != 7 || color[0] != '#') {
sway_log(L_ERROR, "%s is not a valid color for swaybg. Color should be specified as #rrggbb (no alpha).", color);
return false;
}
int i;
for (i = 1; i < len; ++i) {
if (!isxdigit(color[i])) {
return false;
}
}
return true;
}
int main(int argc, const char **argv) {
init_log(L_INFO);
surfaces = create_list();
@ -57,6 +77,11 @@ int main(int argc, const char **argv) {
window_make_shell(window);
list_add(surfaces, window);
if (strcmp(argv[3], "solid_color") == 0 && is_valid_color(argv[2])) {
cairo_set_source_u32(window->cairo, parse_color(argv[2]));
cairo_paint(window->cairo);
window_render(window);
} else {
#ifdef WITH_GDK_PIXBUF
GError *err = NULL;
GdkPixbuf *pixbuf = gdk_pixbuf_new_from_file(argv[2], &err);
@ -163,6 +188,7 @@ int main(int argc, const char **argv) {
}
cairo_surface_destroy(image);
}
while (wl_display_dispatch(registry->display) != -1);

Loading…
Cancel
Save