Skip to content

Commit

Permalink
Add appearance customization options
Browse files Browse the repository at this point in the history
  • Loading branch information
Drew DeVault authored and emersion committed Jun 26, 2018
1 parent d1c3b2e commit 8d97e28
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 12 deletions.
8 changes: 8 additions & 0 deletions include/slurp.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@ struct slurp_state {
struct wl_list outputs; // slurp_output::link
struct wl_list pointers; // slurp_pointer::link

struct {
uint32_t background;
uint32_t border;
uint32_t selection;
} colors;

uint32_t border_weight;

struct slurp_box result;
};

Expand Down
57 changes: 54 additions & 3 deletions main.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#define _POSIX_C_SOURCE 2
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
Expand Down Expand Up @@ -320,21 +321,71 @@ static const struct wl_registry_listener registry_listener = {
static const char usage[] =
"Usage: slurp [options...]\n"
"\n"
" -h Show help message and quit.\n";
" -h Show help message and quit.\n"
" -b #rrggbb Set background color.\n"
" -c #rrggbb Set border color.\n"
" -s #rrggbb Set selection color.\n"
" -w n Set border weight.\n";

uint32_t parse_color(const char *color) {
if (color[0] == '#') {
++color;
}

int len = strlen(color);
if (len != 6 && len != 8) {
fprintf(stderr, "Invalid color %s, "
"defaulting to color 0xFFFFFFFF\n", color);
return 0xFFFFFFFF;
}
uint32_t res = (uint32_t)strtoul(color, NULL, 16);
if (strlen(color) == 6) {
res = (res << 8) | 0xFF;
}
return res;
}

int main(int argc, char *argv[]) {
struct slurp_state state = {
.colors = {
.background = 0xFFFFFF40,
.border = 0x000000FF,
.selection = 0x00000000,
},
.border_weight = 2,
};

int opt;
while ((opt = getopt(argc, argv, "h")) != -1) {
while ((opt = getopt(argc, argv, "hb:c:s:w:")) != -1) {
switch (opt) {
case 'h':
printf("%s", usage);
return EXIT_SUCCESS;
case 'b':
state.colors.background = parse_color(optarg);
break;
case 'c':
state.colors.border = parse_color(optarg);
break;
case 's':
state.colors.selection = parse_color(optarg);
break;
case 'w': {
errno = 0;
char *endptr;
state.border_weight = strtol(optarg, &endptr, 10);
if (*endptr || errno) {
fprintf(stderr, "Error: expected numeric argument for -w\n");
exit(EXIT_FAILURE);
}
break;
}
default:
printf("%s", usage);
return EXIT_FAILURE;
}
}

struct slurp_state state = {0};
wl_list_init(&state.outputs);
wl_list_init(&state.pointers);

Expand Down
16 changes: 8 additions & 8 deletions render.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,10 @@ void render(struct slurp_output *output) {
cairo_t *cairo = buffer->cairo;
int32_t scale = output->scale;

uint32_t border_color = 0x000000FF;
int border_size = 2;

// Clear
cairo_save(cairo);
cairo_set_source_rgba(cairo, 0, 0, 0, 0);
cairo_set_operator(cairo, CAIRO_OPERATOR_SOURCE);
set_source_u32(cairo, state->colors.background);
cairo_paint(cairo);
cairo_restore(cairo);

struct slurp_pointer *pointer;
wl_list_for_each(pointer, &state->pointers, link) {
Expand All @@ -41,8 +36,13 @@ void render(struct slurp_output *output) {
pointer_get_box(pointer, &x, &y, &width, &height);

// Draw border
set_source_u32(cairo, border_color);
cairo_set_line_width(cairo, border_size * scale);
set_source_u32(cairo, state->colors.selection);
cairo_rectangle(cairo, x * scale, y * scale,
width * scale, height * scale);
cairo_fill(cairo);

set_source_u32(cairo, state->colors.border);
cairo_set_line_width(cairo, state->border_weight * scale);
cairo_rectangle(cairo, x * scale, y * scale,
width * scale, height * scale);
cairo_stroke(cairo);
Expand Down
18 changes: 17 additions & 1 deletion slurp.1.scd
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,30 @@ slurp - select a region in a Wayland compositor
# SYNOPSIS

slurp is a command-line utility to select a region from Wayland compositors
which support the layer-shell protocol. It lets the user hold his pointer to
which support the layer-shell protocol. It lets the user hold the pointer to
select, or click to cancel the selection.

# OPTIONS

*-h*
Show help message and quit.

*-b* _color_
Set background color. See *COLORS* for more detail.

*-c* _color_
Set border color. See *COLORS* for more detail.

*-s* _color_
Set selection color. See *COLORS* for more detail.

*-w* _weight_
Set border weight.

# COLORS

Colors may be specified in #RRGGBB or #RRGGBBAA format. The # is optional.

# AUTHORS

Maintained by Simon Ser <[email protected]>, who is assisted by other
Expand Down

0 comments on commit 8d97e28

Please sign in to comment.