Skip to content

Commit

Permalink
feat(application): allow print surface to stdout
Browse files Browse the repository at this point in the history
This allows a user to output the final surface to `stdout` using:

- `-O`
- `--print-stdout`

Also adding another keybinding to exit the application `Ctrl+w` which is
pretty standard.

Closes #2
  • Loading branch information
jtheoof committed Jan 20, 2020
1 parent bea076b commit 3249075
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 4 deletions.
24 changes: 24 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,30 @@
}
]
},
{
"name": "swappy - file & stdout",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/build/swappy",
"args": ["-f", "docs/images/screenshot.png", "-O"],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [
{
"name": "G_MESSAGES_DEBUG",
"value": "all"
}
],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
},
{
"name": "swappy - stdin",
"type": "cppdbg",
Expand Down
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ Select a region and swappshot it:
swappy -g "$(slurp)"
```

Print final surface to stdout (useful to pipe with other tools):

```sh
grim -g "$(slurp)" - | swappy -O -f - | pngquant -
```

Grab a swappshot from a specific window under Sway, using `swaymsg` and `jq`:

```sh
Expand Down Expand Up @@ -92,7 +98,7 @@ The following lines can be used as swappy's default:
- `Ctrl+Shift+z` or `Ctrl+y`: Redo
- `Ctrl+s`: Save to file (see man page)
- `Ctrl+c`: Copy to clipboard
- `Escape` or `q`: Quit swappy
- `Escape` or `q` or `Ctrl+w`: Quit swappy

## Limitations

Expand Down
3 changes: 2 additions & 1 deletion include/pixbuf.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@

#include "swappy.h"

void pixbuf_save_to_file(struct swappy_state *state);
void pixbuf_save_to_file(struct swappy_state *state);
void pixbuf_save_to_stdout(struct swappy_state *state);
1 change: 1 addition & 0 deletions include/swappy.h
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ struct swappy_state {
/* Options */
char *geometry_str;
char *file_str;
gboolean print_stdout;

struct swappy_box *geometry;

Expand Down
13 changes: 13 additions & 0 deletions src/application.c
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,9 @@ void arrow_clicked_handler(GtkWidget *widget, struct swappy_state *state) {
}

void application_finish(struct swappy_state *state) {
if (state->print_stdout) {
pixbuf_save_to_stdout(state);
}
paint_free_all(state);
buffer_free_all(state);
cairo_surface_destroy(state->cairo_surface);
Expand Down Expand Up @@ -230,6 +233,9 @@ void window_keypress_handler(GtkWidget *widget, GdkEventKey *event,
case GDK_KEY_b:
action_toggle_painting_pane(state);
break;
case GDK_KEY_w:
gtk_main_quit();
break;
case GDK_KEY_z:
action_undo(state);
break;
Expand Down Expand Up @@ -652,6 +658,13 @@ bool application_init(struct swappy_state *state) {
.arg_data = &state->file_str,
.description = "Load a file at a specific path",
},
{
.long_name = "print-stdout",
.short_name = 'O',
.arg = G_OPTION_ARG_NONE,
.arg_data = &state->print_stdout,
.description = "Print the final surface to stdout when exiting",
},
{NULL}};

state->app = gtk_application_new("me.jtheoof.swappy",
Expand Down
32 changes: 31 additions & 1 deletion src/pixbuf.c
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
#include "pixbuf.h"

#include <gio/gunixoutputstream.h>

#include "notification.h"

void pixbuf_save_to_file(struct swappy_state *state) {
static GdkPixbuf *pixbuf_get_from_state(struct swappy_state *state) {
guint width = gtk_widget_get_allocated_width(state->ui->area);
guint height = gtk_widget_get_allocated_height(state->ui->area);
GdkPixbuf *pixbuf =
gdk_pixbuf_get_from_surface(state->cairo_surface, 0, 0, width, height);

return pixbuf;
}

void pixbuf_save_to_file(struct swappy_state *state) {
GdkPixbuf *pixbuf = pixbuf_get_from_state(state);
GError *error = NULL;

time_t current_time;
Expand Down Expand Up @@ -34,3 +42,25 @@ void pixbuf_save_to_file(struct swappy_state *state) {
g_free(message);
g_object_unref(pixbuf);
}

void pixbuf_save_to_stdout(struct swappy_state *state) {
GdkPixbuf *pixbuf = pixbuf_get_from_state(state);
GOutputStream *out;
GError *error = NULL;

out = g_unix_output_stream_new(STDOUT_FILENO, TRUE);

if (error != NULL) {
g_warning("unable to open stdout for readwrite: %s", error->message);
g_error_free(error);
return;
}

gdk_pixbuf_save_to_stream(pixbuf, out, "png", NULL, &error);

if (error != NULL) {
g_warning("unable to save surface to stdout: %s", error->message);
g_error_free(error);
return;
}
}
6 changes: 5 additions & 1 deletion swappy.1.scd
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ to: *$HOME/Desktop*.
If set to *-*, read the file from standard input instead. This is grim
friendly.

*-O*
Print the final surface to stdout. This is useful to pipe swappy with other
tools (eg: pngquant).

# CONFIG FILE

The config file is located at *$XDG\_CONFIG\_HOME/swappy/config* or at
Expand Down Expand Up @@ -93,7 +97,7 @@ The following lines can be used as swappy's default:
- *Ctrl+Shift+z* or *Ctrl+y*: Redo
- *Ctrl+s*: Save to file (see man page)
- *Ctrl+c*: Copy to clipboard
- *Escape* or *q*: Quit swappy
- *Escape* or *q* or *Ctrl+w*: Quit swappy

# AUTHORS

Expand Down

0 comments on commit 3249075

Please sign in to comment.