aboutsummaryrefslogtreecommitdiff
path: root/vncc.c
blob: bc9c5338171749a0919a930ed042e17dfd309c48 (plain) (blame)
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
122
123
124
125
126
127
#include <SDL2/SDL.h>

#include "SDL2_vnc.h"

#define exit_error(ret, msg, ...) do { \
    fprintf(stderr, msg "\n", ## __VA_ARGS__); \
    exit(ret); \
} while (0)

void usage(char *name) {
    printf("usage:\n%s host:port\n", name);
    exit(1);
}

void exit_on_sdl_error(int res) {
    if (!res) {
        return;
    }

    exit_error(1, "SDL error: %s", SDL_GetError());
}

void exit_on_vnc_error(VNC_Result res) {
    if (!res) {
        return;
    }

    exit_error(res, "VNC error: %s", VNC_ErrorString(res));
}

int parse_address(char *address) {

    /*
     * expects address of format host:port
     */
    char *split = strchr(address, ':');
    *split = '\0';

    char *port_start = split + 1;
    int port = strtol(port_start, NULL, 10);

    return port;
}

int main(int argc, char **argv) {

    if (argc < 2) {
        usage(argv[0]);
    }

    char *host = argv[1];
    int port = parse_address(host);

    SDL_Init(SDL_INIT_VIDEO);
    VNC_Init();

    VNC_Connection vnc;
    int connection_result = VNC_InitConnection(&vnc, host, port, 60);
    exit_on_vnc_error(connection_result);

    SDL_Window *wind = VNC_CreateWindowForConnection(&vnc, NULL,
            SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 0);
    exit_on_sdl_error(!wind);

    SDL_Renderer *rend = SDL_CreateRenderer(wind, -1, 0);
    exit_on_sdl_error(!rend);

    SDL_bool running = SDL_TRUE;

    while (running) {
        SDL_Event e;
        while (SDL_PollEvent(&e)) {
            switch (e.type) {
                case SDL_QUIT:
                    running = SDL_FALSE;
                    break;

                case SDL_KEYUP:
                case SDL_KEYDOWN:
                    VNC_SendKeyEvent(&vnc, e.key.state == SDL_PRESSED,
                            e.key.keysym);
                    break;

                case SDL_MOUSEBUTTONUP:
                case SDL_MOUSEBUTTONDOWN:
                case SDL_MOUSEMOTION: {
                    int x;
                    int y;
                    uint32_t buttons = SDL_GetMouseState(&x, &y);
                    VNC_SendPointerEvent(&vnc, buttons, x, y, 0, 0);
                    break;
                }

                case SDL_MOUSEWHEEL: {
                    int x;
                    int y;
                    uint32_t buttons = SDL_GetMouseState(&x, &y);

                    VNC_SendPointerEvent(&vnc, buttons, x, y,
                            e.wheel.x, e.wheel.y);
                    VNC_SendPointerEvent(&vnc, buttons, x, y, 0, 0);
                    break;
                }

                default:
                    if (e.type == VNC_SHUTDOWN) {
                        exit_on_vnc_error(e.user.code);
                        running = SDL_FALSE;
                    }
                    break;
            }

        }

        SDL_Texture *text = SDL_CreateTextureFromSurface(rend, vnc.surface);
        SDL_RenderCopy(rend, text, NULL, NULL);
        SDL_DestroyTexture(text);

        SDL_RenderPresent(rend);

        SDL_Delay(1000/vnc.fps);
    }

    return 0;
}

/* vim: se ft=c tw=80 ts=4 sw=4 et : */