Skip to content

Commit

Permalink
Add allow-input and crop area options
Browse files Browse the repository at this point in the history
  • Loading branch information
phoboslab committed Dec 21, 2016
1 parent f3b7fdf commit e609bfd
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 15 deletions.
13 changes: 9 additions & 4 deletions source/app.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,13 @@ void on_close(server_t *server, libwebsocket *socket) { app_on_close((app_t *)se



app_t *app_create(HWND window, int port, int bit_rate, int out_width, int out_height) {
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) {
app_t *self = (app_t *)malloc(sizeof(app_t));
memset(self, 0, sizeof(app_t));

self->mouse_speed = APP_MOUSE_SPEED;
self->grabber = grabber_create(window);
self->grabber = grabber_create(window, crop);
self->allow_input = allow_input;

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

void app_on_message(app_t *self, libwebsocket *socket, void *data, size_t len) {
if (!self->allow_input) {
return;
}

input_type_t type = (input_type_t)((unsigned short *)data)[0];

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

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

//printf("mouse absolute %d, %d\n", x, y);
SetCursorPos(x, y);
Expand Down
3 changes: 2 additions & 1 deletion source/app.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@ typedef struct {
encoder_t *encoder;
grabber_t *grabber;
server_t *server;
int allow_input;

float mouse_speed;
} app_t;


app_t *app_create(HWND window, int port, int bit_rate, int out_width, int out_height);
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);
void app_destroy(app_t *self);
void app_run(app_t *self, int targt_fps);

Expand Down
13 changes: 11 additions & 2 deletions source/grabber.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

#include "grabber.h"

grabber_t *grabber_create(HWND window) {
grabber_t *grabber_create(HWND window, grabber_crop_area_t crop) {
grabber_t *self = (grabber_t *)malloc(sizeof(grabber_t));
memset(self, 0, sizeof(grabber_t));

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

self->width = rect.right-rect.left;
self->height = rect.bottom-rect.top;

self->crop = crop;
if( crop.width == 0 || crop.height == 0 ) {
self->crop.width = self->width - crop.x;
self->crop.height = self->height - crop.y;
}

self->width = self->crop.width;
self->height = self->crop.height;

self->windowDC = GetDC(window);
self->memoryDC = CreateCompatibleDC(self->windowDC);
Expand Down Expand Up @@ -47,7 +56,7 @@ void grabber_destroy(grabber_t *self) {

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

return self->pixels;
Expand Down
8 changes: 6 additions & 2 deletions source/grabber.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>

typedef struct {
int x, y, width, height;
} grabber_crop_area_t;

typedef struct {
HWND window;

Expand All @@ -16,10 +20,10 @@ typedef struct {
int height;

void *pixels;
grabber_crop_area_t crop;
} grabber_t;


grabber_t *grabber_create(HWND window);
grabber_t *grabber_create(HWND window, grabber_crop_area_t crop);
void grabber_destroy(grabber_t *self);
void *grabber_grab(grabber_t *self);

Expand Down
19 changes: 13 additions & 6 deletions source/jsmpeg-vnc.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,12 @@ void exit_usage(char *self_name) {
"Usage: %s [options] <window name>\n\n"

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

"Use \"desktop\" as the window name to capture the whole Desktop. Use \"cursor\"\n"
"to capture the window at the current cursor position.\n\n"
Expand All @@ -57,7 +59,10 @@ int main(int argc, char* argv[]) {
fps = 60,
port = 8080,
width = 0,
height = 0;
height = 0,
allow_input = 1;

grabber_crop_area_t crop = {0, 0, 0, 0};

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

// Start the app
app_t *app = app_create(window, port, bit_rate, width, height);
app_t *app = app_create(window, port, bit_rate, width, height, allow_input, crop);

if( !app ) {
return 1;
Expand Down

0 comments on commit e609bfd

Please sign in to comment.