Skip to content

Commit e609bfd

Browse files
committed
Add allow-input and crop area options
1 parent f3b7fdf commit e609bfd

File tree

5 files changed

+41
-15
lines changed

5 files changed

+41
-15
lines changed

source/app.c

+9-4
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,13 @@ void on_close(server_t *server, libwebsocket *socket) { app_on_close((app_t *)se
6767

6868

6969

70-
app_t *app_create(HWND window, int port, int bit_rate, int out_width, int out_height) {
70+
app_t *app_create(HWND window, int port, int bit_rate, int out_width, int out_height, int allow_input, grabber_crop_area_t crop) {
7171
app_t *self = (app_t *)malloc(sizeof(app_t));
7272
memset(self, 0, sizeof(app_t));
7373

7474
self->mouse_speed = APP_MOUSE_SPEED;
75-
self->grabber = grabber_create(window);
75+
self->grabber = grabber_create(window, crop);
76+
self->allow_input = allow_input;
7677

7778
if( !out_width ) { out_width = self->grabber->width; }
7879
if( !out_height ) { out_height = self->grabber->height; }
@@ -140,6 +141,10 @@ void app_on_close(app_t *self, libwebsocket *socket) {
140141
}
141142

142143
void app_on_message(app_t *self, libwebsocket *socket, void *data, size_t len) {
144+
if (!self->allow_input) {
145+
return;
146+
}
147+
143148
input_type_t type = (input_type_t)((unsigned short *)data)[0];
144149

145150
if( type & input_type_key && len >= sizeof(input_key_t) ) {
@@ -174,8 +179,8 @@ void app_on_message(app_t *self, libwebsocket *socket, void *data, size_t len) {
174179
float scale_x = ((float)self->encoder->in_width / self->encoder->out_width),
175180
scale_y =((float)self->encoder->in_height / self->encoder->out_height);
176181

177-
int x = (int)(input->x * scale_x + window_pos.x),
178-
y = (int)(input->y * scale_y + window_pos.y);
182+
int x = (int)(input->x * scale_x + window_pos.x + self->grabber->crop.x),
183+
y = (int)(input->y * scale_y + window_pos.y + self->grabber->crop.y);
179184

180185
//printf("mouse absolute %d, %d\n", x, y);
181186
SetCursorPos(x, y);

source/app.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,13 @@ typedef struct {
1515
encoder_t *encoder;
1616
grabber_t *grabber;
1717
server_t *server;
18+
int allow_input;
1819

1920
float mouse_speed;
2021
} app_t;
2122

2223

23-
app_t *app_create(HWND window, int port, int bit_rate, int out_width, int out_height);
24+
app_t *app_create(HWND window, int port, int bit_rate, int out_width, int out_height, int allow_input, grabber_crop_area_t crop);
2425
void app_destroy(app_t *self);
2526
void app_run(app_t *self, int targt_fps);
2627

source/grabber.c

+11-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
#include "grabber.h"
77

8-
grabber_t *grabber_create(HWND window) {
8+
grabber_t *grabber_create(HWND window, grabber_crop_area_t crop) {
99
grabber_t *self = (grabber_t *)malloc(sizeof(grabber_t));
1010
memset(self, 0, sizeof(grabber_t));
1111

@@ -16,6 +16,15 @@ grabber_t *grabber_create(HWND window) {
1616

1717
self->width = rect.right-rect.left;
1818
self->height = rect.bottom-rect.top;
19+
20+
self->crop = crop;
21+
if( crop.width == 0 || crop.height == 0 ) {
22+
self->crop.width = self->width - crop.x;
23+
self->crop.height = self->height - crop.y;
24+
}
25+
26+
self->width = self->crop.width;
27+
self->height = self->crop.height;
1928

2029
self->windowDC = GetDC(window);
2130
self->memoryDC = CreateCompatibleDC(self->windowDC);
@@ -47,7 +56,7 @@ void grabber_destroy(grabber_t *self) {
4756

4857
void *grabber_grab(grabber_t *self) {
4958
SelectObject(self->memoryDC, self->bitmap);
50-
BitBlt(self->memoryDC, 0, 0, self->width, self->height, self->windowDC, 0, 0, SRCCOPY);
59+
BitBlt(self->memoryDC, 0, 0, self->width, self->height, self->windowDC, self->crop.x, self->crop.y, SRCCOPY);
5160
GetDIBits(self->memoryDC, self->bitmap, 0, self->height, self->pixels, (BITMAPINFO*)&(self->bitmapInfo), DIB_RGB_COLORS);
5261

5362
return self->pixels;

source/grabber.h

+6-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44
#define WIN32_LEAN_AND_MEAN
55
#include <Windows.h>
66

7+
typedef struct {
8+
int x, y, width, height;
9+
} grabber_crop_area_t;
10+
711
typedef struct {
812
HWND window;
913

@@ -16,10 +20,10 @@ typedef struct {
1620
int height;
1721

1822
void *pixels;
23+
grabber_crop_area_t crop;
1924
} grabber_t;
2025

21-
22-
grabber_t *grabber_create(HWND window);
26+
grabber_t *grabber_create(HWND window, grabber_crop_area_t crop);
2327
void grabber_destroy(grabber_t *self);
2428
void *grabber_grab(grabber_t *self);
2529

source/jsmpeg-vnc.c

+13-6
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,12 @@ void exit_usage(char *self_name) {
3232
"Usage: %s [options] <window name>\n\n"
3333

3434
"Options:\n"
35-
" -b bitrate in kilobit/s (default: estimated by output size)\n"
36-
" -s output size as WxH. E.g: -s 640x480 (default: same as window size)\n"
37-
" -f target framerate (default: 60)\n"
38-
" -p port (default: 8080)\n\n"
35+
" -b bitrate in kilobit/s (default: estimated by output size)\n"
36+
" -s output size as WxH. E.g: -s 640x480 (default: same as window size)\n"
37+
" -f target framerate (default: 60)\n"
38+
" -p port (default: 8080)\n"
39+
" -c crop area in the captured window as X,Y,W,H. E.g.: -c 200,300,640,480\n"
40+
" -i enable/disable remote input. E.g. -i 0 (default: 1)\n\n"
3941

4042
"Use \"desktop\" as the window name to capture the whole Desktop. Use \"cursor\"\n"
4143
"to capture the window at the current cursor position.\n\n"
@@ -57,7 +59,10 @@ int main(int argc, char* argv[]) {
5759
fps = 60,
5860
port = 8080,
5961
width = 0,
60-
height = 0;
62+
height = 0,
63+
allow_input = 1;
64+
65+
grabber_crop_area_t crop = {0, 0, 0, 0};
6166

6267
// Parse command line options
6368
for( int i = 1; i < argc-1; i+=2 ) {
@@ -70,6 +75,8 @@ int main(int argc, char* argv[]) {
7075
case 'p': port = atoi(argv[i+1]); break;
7176
case 's': sscanf(argv[i+1], "%dx%d", &width, &height); break;
7277
case 'f': fps = atoi(argv[i+1]); break;
78+
case 'i': allow_input = atoi(argv[i+1]); break;
79+
case 'c': sscanf(argv[i+1], "%d,%d,%d,%d", &crop.x, &crop.y, &crop.width, &crop.height); break;
7380
default: exit_usage(argv[0]);
7481
}
7582
}
@@ -95,7 +102,7 @@ int main(int argc, char* argv[]) {
95102
}
96103

97104
// Start the app
98-
app_t *app = app_create(window, port, bit_rate, width, height);
105+
app_t *app = app_create(window, port, bit_rate, width, height, allow_input, crop);
99106

100107
if( !app ) {
101108
return 1;

0 commit comments

Comments
 (0)