neatvnc/examples/draw.c

102 lines
2.8 KiB
C
Raw Normal View History

2019-09-07 16:51:07 +00:00
/*
* Copyright (c) 2019 Andri Yngvason
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
* REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
* INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
* LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
* OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
* PERFORMANCE OF THIS SOFTWARE.
*/
2019-08-31 20:45:10 +00:00
#include "neatvnc.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <uv.h>
#include <assert.h>
#include <pixman.h>
#include <libdrm/drm_fourcc.h>
struct draw {
2019-10-07 17:39:54 +00:00
struct nvnc_fb* fb;
2019-08-31 20:45:10 +00:00
};
void on_fb_req(struct nvnc_client *client, bool incremental, uint16_t x, uint16_t y,
2019-08-31 20:45:10 +00:00
uint16_t width, uint16_t height)
{
if (incremental)
return;
struct nvnc* server = nvnc_get_server(client);
assert(server);
struct draw *draw = nvnc_get_userdata(server);
2019-08-31 20:45:10 +00:00
assert(draw);
2019-10-07 17:39:54 +00:00
int fbwidth = nvnc_fb_get_width(draw->fb);
int fbheight = nvnc_fb_get_height(draw->fb);
2019-08-31 20:45:10 +00:00
struct pixman_region16 region;
2019-10-07 17:39:54 +00:00
pixman_region_init_rect(&region, 0, 0, fbwidth, fbheight);
nvnc_update_fb(server, draw->fb, &region, NULL);
2019-08-31 20:45:10 +00:00
pixman_region_fini(&region);
}
void on_pointer_event(struct nvnc_client *client, uint16_t x, uint16_t y,
2019-08-31 20:45:10 +00:00
enum nvnc_button_mask buttons)
{
if (!(buttons & NVNC_BUTTON_LEFT))
return;
struct nvnc *server = nvnc_get_server(client);
assert(server);
struct draw *draw = nvnc_get_userdata(server);
2019-08-31 20:45:10 +00:00
assert(draw);
2019-10-07 17:39:54 +00:00
uint32_t *image = nvnc_fb_get_addr(draw->fb);
int width = nvnc_fb_get_width(draw->fb);
int height = nvnc_fb_get_height(draw->fb);
2019-08-31 20:45:10 +00:00
2019-10-07 17:39:54 +00:00
image[x + y * width] = 0;
2019-08-31 20:45:10 +00:00
struct pixman_region16 region;
2019-10-07 17:39:54 +00:00
pixman_region_init_rect(&region, 0, 0, width, height);
pixman_region_intersect_rect(&region, &region, x, y, 1, 1);
2019-10-07 17:39:54 +00:00
nvnc_update_fb(server, draw->fb, &region, NULL);
2019-08-31 20:45:10 +00:00
pixman_region_fini(&region);
}
int main(int argc, char *argv[])
{
struct draw draw;
2019-10-07 17:39:54 +00:00
int width = 500, height = 500;
uint32_t format = DRM_FORMAT_RGBX8888;
draw.fb = nvnc_fb_new(width, height, format);
assert(draw.fb);
2019-08-31 20:45:10 +00:00
2019-10-07 17:39:54 +00:00
void* addr = nvnc_fb_get_addr(draw.fb);
2019-08-31 20:45:10 +00:00
2019-10-07 17:39:54 +00:00
memset(addr, 0xff, width * height * 4);
2019-08-31 20:45:10 +00:00
struct nvnc *server = nvnc_open("127.0.0.1", 5900);
2019-10-07 17:39:54 +00:00
nvnc_set_dimensions(server, width, height, format);
2019-08-31 20:45:10 +00:00
nvnc_set_name(server, "Draw");
nvnc_set_fb_req_fn(server, on_fb_req);
nvnc_set_pointer_fn(server, on_pointer_event);
nvnc_set_userdata(server, &draw);
uv_run(uv_default_loop(), UV_RUN_DEFAULT);
nvnc_close(server);
}