Skip to content

Commit

Permalink
Add a format option to print the output of the top-left corner
Browse files Browse the repository at this point in the history
  • Loading branch information
tmccombs committed Aug 24, 2020
1 parent 795f526 commit c1854a7
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 10 deletions.
53 changes: 43 additions & 10 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -449,12 +449,16 @@ static void xdg_output_handle_logical_size(void *data,
output->logical_geometry.width = width;
output->logical_geometry.height = height;
}
static void xdg_output_handle_name(void *data, struct zxdg_output_v1 *xdg_output, const char *name) {
struct slurp_output *output = data;
output->logical_geometry.label = strdup(name);
}

static const struct zxdg_output_v1_listener xdg_output_listener = {
.logical_position = xdg_output_handle_logical_position,
.logical_size = xdg_output_handle_logical_size,
.done = noop,
.name = noop,
.name = xdg_output_handle_name,
.description = noop,
};

Expand Down Expand Up @@ -490,6 +494,9 @@ static void destroy_output(struct slurp_output *output) {
wl_callback_destroy(output->frame_callback);
}
wl_output_destroy(output->wl_output);
if (output->logical_geometry.label) {
free(output->logical_geometry.label);
}
free(output);
}

Expand Down Expand Up @@ -613,7 +620,7 @@ static void handle_global(void *data, struct wl_registry *registry,
create_output(state, wl_output);
} else if (strcmp(interface, zxdg_output_manager_v1_interface.name) == 0) {
state->xdg_output_manager = wl_registry_bind(registry, name,
&zxdg_output_manager_v1_interface, 1);
&zxdg_output_manager_v1_interface, 2);
}
}

Expand Down Expand Up @@ -652,7 +659,23 @@ uint32_t parse_color(const char *color) {
return res;
}

static void print_formatted_result(const struct slurp_box *result,
static void print_output_name(const struct slurp_box *result, struct wl_list *outputs) {
struct slurp_output *output;
wl_list_for_each(output, outputs, link) {
// For now just use the top-left corner
struct slurp_box *geometry = &output->logical_geometry;
if (in_box(geometry, result->x, result->y)) {
if (geometry->label) {
printf("%s", geometry->label);
return;
}
break;
}
}
printf("<unknown>");
}

static void print_formatted_result(const struct slurp_box *result, struct wl_list *outputs,
const char *format) {
for (size_t i = 0; format[i] != '\0'; i++) {
char c = format[i];
Expand All @@ -678,6 +701,9 @@ static void print_formatted_result(const struct slurp_box *result,
printf("%s", result->label);
}
continue;
case 'o':
print_output_name(result, outputs);
continue;
default:
// If no case was executed, revert i back - we don't need to
// skip the next character.
Expand All @@ -701,6 +727,8 @@ static void add_choice_box(struct slurp_state *state,
}

int main(int argc, char *argv[]) {
int status = EXIT_SUCCESS;

struct slurp_state state = {
.colors = {
.background = 0xFFFFFF40,
Expand Down Expand Up @@ -884,6 +912,14 @@ int main(int argc, char *argv[]) {
// This space intentionally left blank
}


if (state.result.width == 0 && state.result.height == 0) {
fprintf(stderr, "selection cancelled\n");
status = EXIT_FAILURE;
} else {
print_formatted_result(&state.result, &state.outputs, format);
}

struct slurp_output *output_tmp;
wl_list_for_each_safe(output, output_tmp, &state.outputs, link) {
destroy_output(output);
Expand All @@ -909,14 +945,11 @@ int main(int argc, char *argv[]) {
struct slurp_box *box, *box_tmp;
wl_list_for_each_safe(box, box_tmp, &state.boxes, link) {
wl_list_remove(&box->link);
if (box->label) {
free(box->label);
}
free(box);
}

if (state.result.width == 0 && state.result.height == 0) {
fprintf(stderr, "selection cancelled\n");
return EXIT_FAILURE;
}

print_formatted_result(&state.result, format);
return EXIT_SUCCESS;
return status;
}
3 changes: 3 additions & 0 deletions slurp.1.scd
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ Interpreted sequences are:

%l Label included with region from stdin

%o The name of the output containing the top left corner, or "<unknown>" if not
known.

The default format is "%x,%y %wx%h".

# AUTHORS
Expand Down

0 comments on commit c1854a7

Please sign in to comment.