-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.c
121 lines (116 loc) · 2.87 KB
/
utils.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#define STD_IMAGE_WRITE_IMPLEMENTATION
#include "stb_image_write.h"
#ifdef __linux__
#include "X11/Xlib.h"
#elif _WIN32
#include <windows.h>
#include <stdlib.h>
#endif
int get_screen_width(void)
{
#ifdef __linux__
Display *display = XOpenDisplay(NULL);
if (!display)
return (0);
int width = XDisplayWidth(display, 0);
XCloseDisplay(display);
return (width);
#elif _WIN32
return (GetSystemMetrics(SM_CXSCREEN));
#endif
}
int get_screen_height(void)
{
#ifdef __linux__
Display *display = XOpenDisplay(NULL);
if (!display)
return (0);
int height = XDisplayHeight(display, 0);
XCloseDisplay(display);
return (height);
#elif _WIN32
return (GetSystemMetrics(SM_CYSCREEN));
#endif
}
void get_mouse_position(int *x, int *y)
{
#ifdef __linux
Display *display = XOpenDisplay(NULL);
if (!display)
return;
Window root = RootWindow(display, 0);
unsigned int mask;
XQueryPointer(display, root, &root, &root, x, y, x, y, &mask);
XCloseDisplay(display);
#elif _WIN32
POINT point;
GetCursorPos(&point);
*x = point.x;
*y = point.y;
#endif
}
void convert_to_rgba(unsigned char *data, const int width, const int height, const int stride)
{
for (int y = 0; y < height; ++y)
{
for (int x = 0; x < width; ++x)
{
int offset = y * stride + x * 4;
unsigned char r = data[offset + 2];
unsigned char g = data[offset + 1];
unsigned char b = data[offset + 0];
unsigned char a = 255;
data[offset + 0] = r;
data[offset + 1] = g;
data[offset + 2] = b;
data[offset + 3] = a;
}
}
}
int take_screenshot(const char *filename, const int width, const int height)
{
const int comps = 4;
const int stride = width * comps;
#ifdef __linux__
Display *display = XOpenDisplay(NULL);
if (!display)
return (1);
Window root = RootWindow(display, 0);
XImage *image = XGetImage(display, root, 0, 0, width, height, AllPlanes, ZPixmap);
if (!image)
{
XFree(image->data);
XFree(image);
XCloseDisplay(display);
return (1);
}
unsigned char *data = (unsigned char *)image->data;
convert_to_rgba(data, width, height, stride);
stbi_write_png(filename, width, height, comps, data, stride);
XFree(image->data);
XFree(image);
XCloseDisplay(display);
#elif _WIN32
unsigned char *data = (unsigned char *)malloc(stride * height);
HDC hdc = GetDC(NULL);
HDC memdc = CreateCompatibleDC(hdc);
HBITMAP bitmap = CreateCompatibleBitmap(hdc, width, height);
SelectObject(memdc, bitmap);
BitBlt(memdc, 0, 0, width, height, hdc, 0, 0, SRCCOPY);
BITMAPINFOHEADER bi = {0};
bi.biSize = sizeof(BITMAPINFOHEADER);
bi.biWidth = width;
bi.biHeight = -height;
bi.biPlanes = 1;
bi.biBitCount = 32;
bi.biCompression = BI_RGB;
GetDIBits(memdc, bitmap, 0, height, data, (BITMAPINFO *)&bi, DIB_RGB_COLORS);
convert_to_rgba(data, width, height, stride);
stbi_write_png(filename, width, height, comps, data, stride);
free(data);
DeleteObject(bitmap);
DeleteDC(memdc);
ReleaseDC(NULL, hdc);
#endif
return (0);
}