From cfae750b72263b285af0cc42c29aca99c1d1f876 Mon Sep 17 00:00:00 2001 From: Maik Broemme Date: Fri, 12 Dec 2025 12:08:07 +0100 Subject: [PATCH] feat: add -O to print focused output geometry immediately Add a new -O option that queries the output currently containing the pointer and prints its box using the existing format string machinery, then exits without entering region selection. Reject -O when combined with -p or -r to avoid ambiguous behavior. This enables grabbing the active display dimensions instantly (e.g. for scripts) without requiring a click/drag. --- README.md | 6 ++++++ include/slurp.h | 1 + main.c | 18 +++++++++++++++++- slurp.1.scd | 4 ++++ 4 files changed, 28 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index cdfe76e..3109095 100644 --- a/README.md +++ b/README.md @@ -45,6 +45,12 @@ Select an output and print its name: slurp -o -f "%o" ``` +Print the geometry of the output currently containing the pointer: + +```sh +slurp -O +``` + Select a window under Sway, using `swaymsg` and `jq`: ```sh diff --git a/include/slurp.h b/include/slurp.h index 711f7ed..11a7f78 100644 --- a/include/slurp.h +++ b/include/slurp.h @@ -51,6 +51,7 @@ struct slurp_state { bool single_point; bool restrict_selection; bool crosshairs; + bool print_focused_output; // Print focused output geometry and exit bool resizing_selection; struct wl_list boxes; // slurp_box::link bool fixed_aspect_ratio; diff --git a/main.c b/main.c index 84a2e63..cbc242e 100644 --- a/main.c +++ b/main.c @@ -129,6 +129,12 @@ static void pointer_handle_enter(void *data, struct wl_pointer *wl_pointer, // TODO: handle multiple overlapping outputs seat->pointer_selection.current_output = output; + if (seat->state->print_focused_output) { + seat->state->result = output->logical_geometry; + seat->state->running = false; + return; + } + move_seat(seat, surface_x, surface_y, &seat->pointer_selection); switch (seat->button_state) { @@ -728,6 +734,7 @@ static const char usage[] = " -w n Set border weight.\n" " -f s Set output format.\n" " -o Select a display output.\n" + " -O Print the focused display output geometry and exit.\n" " -p Select a single point.\n" " -r Restrict selection to predefined boxes.\n" " -a w:h Force aspect ratio.\n" @@ -897,6 +904,7 @@ int main(int argc, char *argv[]) { .border_weight = 2, .display_dimensions = false, .restrict_selection = false, + .print_focused_output = false, .resizing_selection = false, .fixed_aspect_ratio = false, .aspect_ratio = 0, @@ -907,7 +915,7 @@ int main(int argc, char *argv[]) { char *format = "%x,%y %wx%h\n"; bool output_boxes = false; int w, h; - while ((opt = getopt(argc, argv, "hdb:c:s:B:w:proa:f:F:x")) != -1) { + while ((opt = getopt(argc, argv, "hdb:c:s:B:w:proa:f:F:xO")) != -1) { switch (opt) { case 'h': printf("%s", usage); @@ -949,6 +957,9 @@ int main(int argc, char *argv[]) { case 'o': output_boxes = true; break; + case 'O': + state.print_focused_output = true; + break; case 'r': state.restrict_selection = true; break; @@ -978,6 +989,11 @@ int main(int argc, char *argv[]) { return EXIT_FAILURE; } + if (state.print_focused_output && (state.single_point || state.restrict_selection)) { + fprintf(stderr, "-O cannot be used with -p or -r\n"); + return EXIT_FAILURE; + } + if (!acquire_lock()) { // acquire_lock prints an appropriate error message itself return EXIT_FAILURE; diff --git a/slurp.1.scd b/slurp.1.scd index e79aaf6..1d81960 100644 --- a/slurp.1.scd +++ b/slurp.1.scd @@ -64,6 +64,10 @@ held, the selection is moved instead of being resized. Add predefined rectangles for all outputs, as if provided on standard input. The label will be the name of the output. +*-O* + Immediately print the geometry of the output currently containing the pointer + and exit. + *-r* Require the user to select one of the predefined rectangles. These can come from standard input, if *-o* is used, the rectangles of all display outputs.