Taking screenshots now!

This commit is contained in:
Casey 2024-06-06 16:37:39 +03:00
parent cfa2af3d91
commit f921c61b23
Signed by: hkc
GPG Key ID: F0F6CFE11CDB0960
2 changed files with 58 additions and 18 deletions

View File

@ -1,6 +1,7 @@
// x-run: make run
#include <raylib.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <xcb/xcb.h>
#include <xcb/xproto.h>
@ -20,17 +21,42 @@ int main(int argc, char **argv) {
size_t n_windows;
struct window_info *windows = get_windows_list(xcb, &n_windows);
xcb_disconnect(xcb);
SetConfigFlags(FLAG_WINDOW_TRANSPARENT | FLAG_WINDOW_RESIZABLE);
InitWindow(0, 0, "img/scrall");
Font font = LoadFontEx("/usr/share/fonts/Unifont/Unifont.ttf", 16, 0, 1024);
Font font = LoadFontEx("/usr/share/fonts/Unifont/Unifont.ttf", 16, 0, 8192);
while (!WindowShouldClose()) {
struct window_display {
Vector2 offset;
Texture2D texture;
} *windows_snaps = calloc(n_windows, sizeof(struct window_display));
for (size_t i = 0; i < n_windows; i++) {
Image screenshot = get_screenshot(xcb, windows[i].wid);
if (screenshot.data) {
windows_snaps[i].texture = LoadTextureFromImage(screenshot);
ExportImage(screenshot, TextFormat("screen_%08x.png", windows[i].wid));
UnloadImage(screenshot);
} else {
windows_snaps[i].texture.id = -1;
}
windows_snaps[i].offset.x = windows[i].rect.x;
windows_snaps[i].offset.y = windows[i].rect.y;
}
for (int frame = 0; !WindowShouldClose(); frame++) {
BeginDrawing();
ClearBackground(Fade(BLACK, 0.9));
BeginBlendMode(BLEND_ALPHA);
for (size_t i = 0; i < n_windows; i++) {
if (windows_snaps[i].texture.id != (unsigned int)-1 && ((frame / 10) % n_windows) == i) {
DrawTextureV(windows_snaps[i].texture, windows_snaps[i].offset, WHITE);
}
}
for (size_t i = 0; i < n_windows; i++) {
DrawTextEx(font,
TextFormat("0x%08x %dx%d+%d+%d %s\n",
@ -40,8 +66,12 @@ int main(int argc, char **argv) {
windows[i].rect.x,
windows[i].rect.y,
windows[i].title),
(Vector2) { 8, 8 + 18 * i }, 16, 0, WHITE);
(Vector2) { 8, 8 + 18 * i }, 16, 0,
windows_snaps[i].texture.id == (unsigned int)-1 ? RED :WHITE);
}
EndBlendMode();
EndDrawing();
}

View File

@ -3,6 +3,7 @@
#include <stdlib.h>
#include <stdio.h>
#include <xcb/composite.h>
#include <xcb/xcb_errors.h>
Image get_screenshot(xcb_connection_t *conn, xcb_window_t wid) {
xcb_generic_error_t *err = NULL;
@ -12,17 +13,18 @@ Image get_screenshot(xcb_connection_t *conn, xcb_window_t wid) {
xcb_get_geometry_cookie_t gg_cookie = xcb_get_geometry(conn, wid);
xcb_get_geometry_reply_t *gg_reply = xcb_get_geometry_reply(conn, gg_cookie, &err);
if (!gg_reply) {
fprintf(stderr, "xcb: error: geometry: %d\n", err->error_code);
fprintf(stderr, "xcb: error: geometry: %d (wid: 0x%08x)\n", err->error_code, wid);
return (Image){
.data = err, .width = 0, .height = 0, .format = 0, .mipmaps = 0
.data = 0, .width = 0, .height = 0, .format = 0, .mipmaps = 0
};
}
int win_w = gg_reply->width;
int win_h = gg_reply->height;
assert(gg_reply->depth == 32);
fprintf(stderr, "width: %d\nheight: %d\n", win_w, win_h);
int win_d = gg_reply->depth;
assert(win_d == 32 || win_d == 24);
free(gg_reply);
@ -31,27 +33,35 @@ Image get_screenshot(xcb_connection_t *conn, xcb_window_t wid) {
xcb_get_image_cookie_t gi_cookie = xcb_get_image(conn, XCB_IMAGE_FORMAT_Z_PIXMAP, win_pixmap, 0, 0, win_w, win_h, (uint32_t)(~0UL));
xcb_get_image_reply_t *gi_reply = xcb_get_image_reply(conn, gi_cookie, &err);
if (!gi_reply) {
fprintf(stderr, "xcb: error: get_image_reply: %d\n", err->error_code);
fprintf(stderr, "xcb: error: get_image_reply: %d (wid: 0x%08x)\n", err->error_code, wid);
return (Image){
.data = err, .width = 0, .height = 0, .format = 0, .mipmaps = 0
.data = 0, .width = 0, .height = 0, .format = 0, .mipmaps = 0
};
}
int data_len = xcb_get_image_data_length(gi_reply);
fprintf(stderr, "data_len: %d\n", data_len);
uint8_t *data = xcb_get_image_data(gi_reply);
Image img = GenImageColor(win_w, win_h, BLANK);
for (int i = 0; i < data_len; i += 4) {
((uint8_t *)img.data)[i + 0] = data[i + 2];
((uint8_t *)img.data)[i + 1] = data[i + 1];
((uint8_t *)img.data)[i + 2] = data[i + 0];
((uint8_t *)img.data)[i + 3] = data[i + 3];
if (win_d == 24) {
for (int i = 0; i < data_len / 3; i++) {
((uint8_t *)img.data)[i * 4 + 0] = data[i * 3 + 2];
((uint8_t *)img.data)[i * 4 + 1] = data[i * 3 + 1];
((uint8_t *)img.data)[i * 4 + 2] = data[i * 3 + 0];
((uint8_t *)img.data)[i * 4 + 3] = 0xff;
}
} else {
for (int i = 0; i < data_len / 4; i++) {
((uint8_t *)img.data)[i * 4 + 0] = data[i * 4 + 2];
((uint8_t *)img.data)[i * 4 + 1] = data[i * 4 + 1];
((uint8_t *)img.data)[i * 4 + 2] = data[i * 4 + 0];
((uint8_t *)img.data)[i * 4 + 3] = data[i * 4 + 3];
}
}
free(gi_reply);
return img;