mirror of
https://github.com/Cateners/tiny_computer.git
synced 2026-05-20 16:35:47 +08:00
Update code to v1.0.14 (10)
This commit is contained in:
551
android/extern/libvncserver/examples/client/SDLvncviewer.c
vendored
Normal file
551
android/extern/libvncserver/examples/client/SDLvncviewer.c
vendored
Normal file
@@ -0,0 +1,551 @@
|
||||
/**
|
||||
* @example SDLvncviewer.c
|
||||
* Once built, you can run it via `SDLvncviewer <remote-host>`.
|
||||
*/
|
||||
|
||||
#include <SDL.h>
|
||||
#include <signal.h>
|
||||
#include <rfb/rfbclient.h>
|
||||
|
||||
struct { int sdl; int rfb; } buttonMapping[]={
|
||||
{1, rfbButton1Mask},
|
||||
{2, rfbButton2Mask},
|
||||
{3, rfbButton3Mask},
|
||||
{4, rfbButton4Mask},
|
||||
{5, rfbButton5Mask},
|
||||
{0,0}
|
||||
};
|
||||
|
||||
struct { char mask; int bits_stored; } utf8Mapping[]= {
|
||||
{0b00111111, 6},
|
||||
{0b01111111, 7},
|
||||
{0b00011111, 5},
|
||||
{0b00001111, 4},
|
||||
{0b00000111, 3},
|
||||
{0,0}
|
||||
};
|
||||
|
||||
static int enableResizable = 1, viewOnly, listenLoop, buttonMask;
|
||||
int sdlFlags;
|
||||
SDL_Texture *sdlTexture;
|
||||
SDL_Renderer *sdlRenderer;
|
||||
SDL_Window *sdlWindow;
|
||||
/* client's pointer position */
|
||||
int x,y;
|
||||
|
||||
static int rightAltKeyDown, leftAltKeyDown;
|
||||
|
||||
static rfbBool resize(rfbClient* client) {
|
||||
int width=client->width,height=client->height,
|
||||
depth=client->format.bitsPerPixel;
|
||||
|
||||
if (enableResizable)
|
||||
sdlFlags |= SDL_WINDOW_RESIZABLE;
|
||||
|
||||
client->updateRect.x = client->updateRect.y = 0;
|
||||
client->updateRect.w = width; client->updateRect.h = height;
|
||||
|
||||
/* (re)create the surface used as the client's framebuffer */
|
||||
SDL_FreeSurface(rfbClientGetClientData(client, SDL_Init));
|
||||
SDL_Surface* sdl=SDL_CreateRGBSurface(0,
|
||||
width,
|
||||
height,
|
||||
depth,
|
||||
0,0,0,0);
|
||||
if(!sdl)
|
||||
rfbClientErr("resize: error creating surface: %s\n", SDL_GetError());
|
||||
|
||||
rfbClientSetClientData(client, SDL_Init, sdl);
|
||||
client->width = sdl->pitch / (depth / 8);
|
||||
client->frameBuffer=sdl->pixels;
|
||||
|
||||
client->format.bitsPerPixel=depth;
|
||||
client->format.redShift=sdl->format->Rshift;
|
||||
client->format.greenShift=sdl->format->Gshift;
|
||||
client->format.blueShift=sdl->format->Bshift;
|
||||
client->format.redMax=sdl->format->Rmask>>client->format.redShift;
|
||||
client->format.greenMax=sdl->format->Gmask>>client->format.greenShift;
|
||||
client->format.blueMax=sdl->format->Bmask>>client->format.blueShift;
|
||||
SetFormatAndEncodings(client);
|
||||
|
||||
/* create or resize the window */
|
||||
if(!sdlWindow) {
|
||||
sdlWindow = SDL_CreateWindow(client->desktopName,
|
||||
SDL_WINDOWPOS_UNDEFINED,
|
||||
SDL_WINDOWPOS_UNDEFINED,
|
||||
width,
|
||||
height,
|
||||
sdlFlags);
|
||||
if(!sdlWindow)
|
||||
rfbClientErr("resize: error creating window: %s\n", SDL_GetError());
|
||||
} else {
|
||||
SDL_SetWindowSize(sdlWindow, width, height);
|
||||
}
|
||||
|
||||
/* create the renderer if it does not already exist */
|
||||
if(!sdlRenderer) {
|
||||
sdlRenderer = SDL_CreateRenderer(sdlWindow, -1, 0);
|
||||
if(!sdlRenderer)
|
||||
rfbClientErr("resize: error creating renderer: %s\n", SDL_GetError());
|
||||
SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "linear"); /* make the scaled rendering look smoother. */
|
||||
}
|
||||
SDL_RenderSetLogicalSize(sdlRenderer, width, height); /* this is a departure from the SDL1.2-based version, but more in the sense of a VNC viewer in keeeping aspect ratio */
|
||||
|
||||
/* (re)create the texture that sits in between the surface->pixels and the renderer */
|
||||
if(sdlTexture)
|
||||
SDL_DestroyTexture(sdlTexture);
|
||||
sdlTexture = SDL_CreateTexture(sdlRenderer,
|
||||
SDL_PIXELFORMAT_ARGB8888,
|
||||
SDL_TEXTUREACCESS_STREAMING,
|
||||
width, height);
|
||||
if(!sdlTexture)
|
||||
rfbClientErr("resize: error creating texture: %s\n", SDL_GetError());
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
static rfbKeySym SDL_key2rfbKeySym(SDL_KeyboardEvent* e) {
|
||||
rfbKeySym k = 0;
|
||||
SDL_Keycode sym = e->keysym.sym;
|
||||
|
||||
switch (sym) {
|
||||
case SDLK_BACKSPACE: k = XK_BackSpace; break;
|
||||
case SDLK_TAB: k = XK_Tab; break;
|
||||
case SDLK_CLEAR: k = XK_Clear; break;
|
||||
case SDLK_RETURN: k = XK_Return; break;
|
||||
case SDLK_PAUSE: k = XK_Pause; break;
|
||||
case SDLK_ESCAPE: k = XK_Escape; break;
|
||||
case SDLK_DELETE: k = XK_Delete; break;
|
||||
case SDLK_KP_0: k = XK_KP_0; break;
|
||||
case SDLK_KP_1: k = XK_KP_1; break;
|
||||
case SDLK_KP_2: k = XK_KP_2; break;
|
||||
case SDLK_KP_3: k = XK_KP_3; break;
|
||||
case SDLK_KP_4: k = XK_KP_4; break;
|
||||
case SDLK_KP_5: k = XK_KP_5; break;
|
||||
case SDLK_KP_6: k = XK_KP_6; break;
|
||||
case SDLK_KP_7: k = XK_KP_7; break;
|
||||
case SDLK_KP_8: k = XK_KP_8; break;
|
||||
case SDLK_KP_9: k = XK_KP_9; break;
|
||||
case SDLK_KP_PERIOD: k = XK_KP_Decimal; break;
|
||||
case SDLK_KP_DIVIDE: k = XK_KP_Divide; break;
|
||||
case SDLK_KP_MULTIPLY: k = XK_KP_Multiply; break;
|
||||
case SDLK_KP_MINUS: k = XK_KP_Subtract; break;
|
||||
case SDLK_KP_PLUS: k = XK_KP_Add; break;
|
||||
case SDLK_KP_ENTER: k = XK_KP_Enter; break;
|
||||
case SDLK_KP_EQUALS: k = XK_KP_Equal; break;
|
||||
case SDLK_UP: k = XK_Up; break;
|
||||
case SDLK_DOWN: k = XK_Down; break;
|
||||
case SDLK_RIGHT: k = XK_Right; break;
|
||||
case SDLK_LEFT: k = XK_Left; break;
|
||||
case SDLK_INSERT: k = XK_Insert; break;
|
||||
case SDLK_HOME: k = XK_Home; break;
|
||||
case SDLK_END: k = XK_End; break;
|
||||
case SDLK_PAGEUP: k = XK_Page_Up; break;
|
||||
case SDLK_PAGEDOWN: k = XK_Page_Down; break;
|
||||
case SDLK_F1: k = XK_F1; break;
|
||||
case SDLK_F2: k = XK_F2; break;
|
||||
case SDLK_F3: k = XK_F3; break;
|
||||
case SDLK_F4: k = XK_F4; break;
|
||||
case SDLK_F5: k = XK_F5; break;
|
||||
case SDLK_F6: k = XK_F6; break;
|
||||
case SDLK_F7: k = XK_F7; break;
|
||||
case SDLK_F8: k = XK_F8; break;
|
||||
case SDLK_F9: k = XK_F9; break;
|
||||
case SDLK_F10: k = XK_F10; break;
|
||||
case SDLK_F11: k = XK_F11; break;
|
||||
case SDLK_F12: k = XK_F12; break;
|
||||
case SDLK_F13: k = XK_F13; break;
|
||||
case SDLK_F14: k = XK_F14; break;
|
||||
case SDLK_F15: k = XK_F15; break;
|
||||
case SDLK_NUMLOCKCLEAR: k = XK_Num_Lock; break;
|
||||
case SDLK_CAPSLOCK: k = XK_Caps_Lock; break;
|
||||
case SDLK_SCROLLLOCK: k = XK_Scroll_Lock; break;
|
||||
case SDLK_RSHIFT: k = XK_Shift_R; break;
|
||||
case SDLK_LSHIFT: k = XK_Shift_L; break;
|
||||
case SDLK_RCTRL: k = XK_Control_R; break;
|
||||
case SDLK_LCTRL: k = XK_Control_L; break;
|
||||
case SDLK_RALT: k = XK_Alt_R; break;
|
||||
case SDLK_LALT: k = XK_Alt_L; break;
|
||||
case SDLK_LGUI: k = XK_Super_L; break;
|
||||
case SDLK_RGUI: k = XK_Super_R; break;
|
||||
#if 0
|
||||
case SDLK_COMPOSE: k = XK_Compose; break;
|
||||
#endif
|
||||
case SDLK_MODE: k = XK_Mode_switch; break;
|
||||
case SDLK_HELP: k = XK_Help; break;
|
||||
case SDLK_PRINTSCREEN: k = XK_Print; break;
|
||||
case SDLK_SYSREQ: k = XK_Sys_Req; break;
|
||||
default: break;
|
||||
}
|
||||
/* SDL_TEXTINPUT does not generate characters if ctrl is down, so handle those here */
|
||||
if (k == 0 && sym > 0x0 && sym < 0x100 && e->keysym.mod & KMOD_CTRL)
|
||||
k = sym;
|
||||
|
||||
return k;
|
||||
}
|
||||
|
||||
/* UTF-8 decoding is from https://rosettacode.org/wiki/UTF-8_encode_and_decode which is under GFDL 1.2 */
|
||||
static rfbKeySym utf8char2rfbKeySym(const char chr[4]) {
|
||||
int bytes = strlen(chr);
|
||||
int shift = utf8Mapping[0].bits_stored * (bytes - 1);
|
||||
rfbKeySym codep = (*chr++ & utf8Mapping[bytes].mask) << shift;
|
||||
int i;
|
||||
for(i = 1; i < bytes; ++i, ++chr) {
|
||||
shift -= utf8Mapping[0].bits_stored;
|
||||
codep |= ((char)*chr & utf8Mapping[0].mask) << shift;
|
||||
}
|
||||
return codep;
|
||||
}
|
||||
|
||||
static void update(rfbClient* cl,int x,int y,int w,int h) {
|
||||
SDL_Surface *sdl = rfbClientGetClientData(cl, SDL_Init);
|
||||
/* update texture from surface->pixels */
|
||||
SDL_Rect r = {x,y,w,h};
|
||||
if(SDL_UpdateTexture(sdlTexture, &r, sdl->pixels + y*sdl->pitch + x*4, sdl->pitch) < 0)
|
||||
rfbClientErr("update: failed to update texture: %s\n", SDL_GetError());
|
||||
/* copy texture to renderer and show */
|
||||
if(SDL_RenderClear(sdlRenderer) < 0)
|
||||
rfbClientErr("update: failed to clear renderer: %s\n", SDL_GetError());
|
||||
if(SDL_RenderCopy(sdlRenderer, sdlTexture, NULL, NULL) < 0)
|
||||
rfbClientErr("update: failed to copy texture to renderer: %s\n", SDL_GetError());
|
||||
SDL_RenderPresent(sdlRenderer);
|
||||
}
|
||||
|
||||
static void kbd_leds(rfbClient* cl, int value, int pad) {
|
||||
/* note: pad is for future expansion 0=unused */
|
||||
fprintf(stderr,"Led State= 0x%02X\n", value);
|
||||
fflush(stderr);
|
||||
}
|
||||
|
||||
/* trivial support for textchat */
|
||||
static void text_chat(rfbClient* cl, int value, char *text) {
|
||||
switch(value) {
|
||||
case rfbTextChatOpen:
|
||||
fprintf(stderr,"TextChat: We should open a textchat window!\n");
|
||||
TextChatOpen(cl);
|
||||
break;
|
||||
case rfbTextChatClose:
|
||||
fprintf(stderr,"TextChat: We should close our window!\n");
|
||||
break;
|
||||
case rfbTextChatFinished:
|
||||
fprintf(stderr,"TextChat: We should close our window!\n");
|
||||
break;
|
||||
default:
|
||||
fprintf(stderr,"TextChat: Received \"%s\"\n", text);
|
||||
break;
|
||||
}
|
||||
fflush(stderr);
|
||||
}
|
||||
|
||||
#ifdef __MINGW32__
|
||||
#define LOG_TO_FILE
|
||||
#endif
|
||||
|
||||
#ifdef LOG_TO_FILE
|
||||
#include <stdarg.h>
|
||||
static void
|
||||
log_to_file(const char *format, ...)
|
||||
{
|
||||
FILE* logfile;
|
||||
static char* logfile_str=0;
|
||||
va_list args;
|
||||
char buf[256];
|
||||
time_t log_clock;
|
||||
|
||||
if(!rfbEnableClientLogging)
|
||||
return;
|
||||
|
||||
if(logfile_str==0) {
|
||||
logfile_str=getenv("VNCLOG");
|
||||
if(logfile_str==0)
|
||||
logfile_str="vnc.log";
|
||||
}
|
||||
|
||||
logfile=fopen(logfile_str,"a");
|
||||
|
||||
va_start(args, format);
|
||||
|
||||
time(&log_clock);
|
||||
strftime(buf, 255, "%d/%m/%Y %X ", localtime(&log_clock));
|
||||
fprintf(logfile,buf);
|
||||
|
||||
vfprintf(logfile, format, args);
|
||||
fflush(logfile);
|
||||
|
||||
va_end(args);
|
||||
fclose(logfile);
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
static void cleanup(rfbClient* cl)
|
||||
{
|
||||
/*
|
||||
just in case we're running in listenLoop:
|
||||
close viewer window by restarting SDL video subsystem
|
||||
*/
|
||||
SDL_QuitSubSystem(SDL_INIT_VIDEO);
|
||||
SDL_InitSubSystem(SDL_INIT_VIDEO);
|
||||
if(cl)
|
||||
rfbClientCleanup(cl);
|
||||
}
|
||||
|
||||
|
||||
static rfbBool handleSDLEvent(rfbClient *cl, SDL_Event *e)
|
||||
{
|
||||
switch(e->type) {
|
||||
case SDL_WINDOWEVENT:
|
||||
switch (e->window.event) {
|
||||
case SDL_WINDOWEVENT_EXPOSED:
|
||||
SendFramebufferUpdateRequest(cl, 0, 0,
|
||||
cl->width, cl->height, FALSE);
|
||||
break;
|
||||
case SDL_WINDOWEVENT_RESIZED:
|
||||
SendExtDesktopSize(cl, e->window.data1, e->window.data2);
|
||||
break;
|
||||
case SDL_WINDOWEVENT_FOCUS_GAINED:
|
||||
if (SDL_HasClipboardText()) {
|
||||
char *text = SDL_GetClipboardText();
|
||||
if(text) {
|
||||
rfbClientLog("sending clipboard text '%s'\n", text);
|
||||
if(!SendClientCutTextUTF8(cl, text, strlen(text)))
|
||||
SendClientCutText(cl, text, strlen(text));
|
||||
}
|
||||
}
|
||||
|
||||
break;
|
||||
case SDL_WINDOWEVENT_FOCUS_LOST:
|
||||
if (rightAltKeyDown) {
|
||||
SendKeyEvent(cl, XK_Alt_R, FALSE);
|
||||
rightAltKeyDown = FALSE;
|
||||
rfbClientLog("released right Alt key\n");
|
||||
}
|
||||
if (leftAltKeyDown) {
|
||||
SendKeyEvent(cl, XK_Alt_L, FALSE);
|
||||
leftAltKeyDown = FALSE;
|
||||
rfbClientLog("released left Alt key\n");
|
||||
}
|
||||
break;
|
||||
}
|
||||
break;
|
||||
case SDL_MOUSEWHEEL:
|
||||
{
|
||||
int steps;
|
||||
if (viewOnly)
|
||||
break;
|
||||
|
||||
if(e->wheel.y > 0)
|
||||
for(steps = 0; steps < e->wheel.y; ++steps) {
|
||||
SendPointerEvent(cl, x, y, rfbButton4Mask);
|
||||
SendPointerEvent(cl, x, y, 0);
|
||||
}
|
||||
if(e->wheel.y < 0)
|
||||
for(steps = 0; steps > e->wheel.y; --steps) {
|
||||
SendPointerEvent(cl, x, y, rfbButton5Mask);
|
||||
SendPointerEvent(cl, x, y, 0);
|
||||
}
|
||||
if(e->wheel.x > 0)
|
||||
for(steps = 0; steps < e->wheel.x; ++steps) {
|
||||
SendPointerEvent(cl, x, y, 0b01000000);
|
||||
SendPointerEvent(cl, x, y, 0);
|
||||
}
|
||||
if(e->wheel.x < 0)
|
||||
for(steps = 0; steps > e->wheel.x; --steps) {
|
||||
SendPointerEvent(cl, x, y, 0b00100000);
|
||||
SendPointerEvent(cl, x, y, 0);
|
||||
}
|
||||
break;
|
||||
}
|
||||
case SDL_MOUSEBUTTONUP:
|
||||
case SDL_MOUSEBUTTONDOWN:
|
||||
case SDL_MOUSEMOTION:
|
||||
{
|
||||
int state, i;
|
||||
if (viewOnly)
|
||||
break;
|
||||
|
||||
if (e->type == SDL_MOUSEMOTION) {
|
||||
x = e->motion.x;
|
||||
y = e->motion.y;
|
||||
state = e->motion.state;
|
||||
}
|
||||
else {
|
||||
x = e->button.x;
|
||||
y = e->button.y;
|
||||
state = e->button.button;
|
||||
for (i = 0; buttonMapping[i].sdl; i++)
|
||||
if (state == buttonMapping[i].sdl) {
|
||||
state = buttonMapping[i].rfb;
|
||||
if (e->type == SDL_MOUSEBUTTONDOWN)
|
||||
buttonMask |= state;
|
||||
else
|
||||
buttonMask &= ~state;
|
||||
break;
|
||||
}
|
||||
}
|
||||
SendPointerEvent(cl, x, y, buttonMask);
|
||||
buttonMask &= ~(rfbButton4Mask | rfbButton5Mask);
|
||||
break;
|
||||
}
|
||||
case SDL_KEYUP:
|
||||
case SDL_KEYDOWN:
|
||||
if (viewOnly)
|
||||
break;
|
||||
SendKeyEvent(cl, SDL_key2rfbKeySym(&e->key),
|
||||
e->type == SDL_KEYDOWN ? TRUE : FALSE);
|
||||
if (e->key.keysym.sym == SDLK_RALT)
|
||||
rightAltKeyDown = e->type == SDL_KEYDOWN;
|
||||
if (e->key.keysym.sym == SDLK_LALT)
|
||||
leftAltKeyDown = e->type == SDL_KEYDOWN;
|
||||
break;
|
||||
case SDL_TEXTINPUT:
|
||||
if (viewOnly)
|
||||
break;
|
||||
rfbKeySym sym = utf8char2rfbKeySym(e->text.text);
|
||||
SendKeyEvent(cl, sym, TRUE);
|
||||
SendKeyEvent(cl, sym, FALSE);
|
||||
break;
|
||||
case SDL_QUIT:
|
||||
if(listenLoop)
|
||||
{
|
||||
cleanup(cl);
|
||||
return FALSE;
|
||||
}
|
||||
else
|
||||
{
|
||||
rfbClientCleanup(cl);
|
||||
exit(0);
|
||||
}
|
||||
default:
|
||||
rfbClientLog("ignore SDL event: 0x%x\n", e->type);
|
||||
}
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
static void got_selection_latin1(rfbClient *cl, const char *text, int len)
|
||||
{
|
||||
rfbClientLog("received latin1 clipboard text '%s'\n", text);
|
||||
if(SDL_SetClipboardText(text) != 0)
|
||||
rfbClientErr("could not set received latin1 clipboard text: %s\n", SDL_GetError());
|
||||
}
|
||||
|
||||
static void got_selection_utf8(rfbClient *cl, const char *buf, int len)
|
||||
{
|
||||
rfbClientLog("received utf8 clipboard text '%s'\n", buf);
|
||||
if(SDL_SetClipboardText(buf) != 0)
|
||||
rfbClientErr("could not set received utf8 clipboard text: %s\n", SDL_GetError());
|
||||
}
|
||||
|
||||
|
||||
static rfbCredential* get_credential(rfbClient* cl, int credentialType){
|
||||
rfbCredential *c = malloc(sizeof(rfbCredential));
|
||||
c->userCredential.username = malloc(RFB_BUF_SIZE);
|
||||
c->userCredential.password = malloc(RFB_BUF_SIZE);
|
||||
|
||||
if(credentialType != rfbCredentialTypeUser) {
|
||||
rfbClientErr("something else than username and password required for authentication\n");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
rfbClientLog("username and password required for authentication!\n");
|
||||
printf("user: ");
|
||||
fgets(c->userCredential.username, RFB_BUF_SIZE, stdin);
|
||||
printf("pass: ");
|
||||
fgets(c->userCredential.password, RFB_BUF_SIZE, stdin);
|
||||
|
||||
/* remove trailing newlines */
|
||||
c->userCredential.username[strcspn(c->userCredential.username, "\n")] = 0;
|
||||
c->userCredential.password[strcspn(c->userCredential.password, "\n")] = 0;
|
||||
|
||||
return c;
|
||||
}
|
||||
|
||||
|
||||
#ifdef mac
|
||||
#define main SDLmain
|
||||
#endif
|
||||
|
||||
int main(int argc,char** argv) {
|
||||
rfbClient* cl;
|
||||
int i, j;
|
||||
SDL_Event e;
|
||||
|
||||
#ifdef LOG_TO_FILE
|
||||
rfbClientLog=rfbClientErr=log_to_file;
|
||||
#endif
|
||||
|
||||
for (i = 1, j = 1; i < argc; i++)
|
||||
if (!strcmp(argv[i], "-viewonly"))
|
||||
viewOnly = 1;
|
||||
else if (!strcmp(argv[i], "-resizable"))
|
||||
enableResizable = 1;
|
||||
else if (!strcmp(argv[i], "-no-resizable"))
|
||||
enableResizable = 0;
|
||||
else if (!strcmp(argv[i], "-listen")) {
|
||||
listenLoop = 1;
|
||||
argv[i] = "-listennofork";
|
||||
++j;
|
||||
}
|
||||
else {
|
||||
if (i != j)
|
||||
argv[j] = argv[i];
|
||||
j++;
|
||||
}
|
||||
argc = j;
|
||||
|
||||
SDL_Init(SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE);
|
||||
atexit(SDL_Quit);
|
||||
signal(SIGINT, exit);
|
||||
|
||||
do {
|
||||
/* 16-bit: cl=rfbGetClient(5,3,2); */
|
||||
cl=rfbGetClient(8,3,4);
|
||||
cl->MallocFrameBuffer=resize;
|
||||
cl->canHandleNewFBSize = TRUE;
|
||||
cl->GotFrameBufferUpdate=update;
|
||||
cl->HandleKeyboardLedState=kbd_leds;
|
||||
cl->HandleTextChat=text_chat;
|
||||
/* two different cut text handlers here for demo purposes, you
|
||||
might as well use the same callback for both if it doesn't
|
||||
matter for your application */
|
||||
cl->GotXCutText = got_selection_latin1;
|
||||
cl->GotXCutTextUTF8 = got_selection_utf8;
|
||||
cl->GetCredential = get_credential;
|
||||
cl->listenPort = LISTEN_PORT_OFFSET;
|
||||
cl->listen6Port = LISTEN_PORT_OFFSET;
|
||||
if(!rfbInitClient(cl,&argc,argv))
|
||||
{
|
||||
cl = NULL; /* rfbInitClient has already freed the client struct */
|
||||
cleanup(cl);
|
||||
break;
|
||||
}
|
||||
|
||||
while(1) {
|
||||
if(SDL_PollEvent(&e)) {
|
||||
/*
|
||||
handleSDLEvent() return 0 if user requested window close.
|
||||
In this case, handleSDLEvent() will have called cleanup().
|
||||
*/
|
||||
if(!handleSDLEvent(cl, &e))
|
||||
break;
|
||||
}
|
||||
else {
|
||||
i=WaitForMessage(cl,500);
|
||||
if(i<0)
|
||||
{
|
||||
cleanup(cl);
|
||||
break;
|
||||
}
|
||||
if(i)
|
||||
if(!HandleRFBServerMessage(cl))
|
||||
{
|
||||
cleanup(cl);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
while(listenLoop);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
104
android/extern/libvncserver/examples/client/backchannel.c
vendored
Normal file
104
android/extern/libvncserver/examples/client/backchannel.c
vendored
Normal file
@@ -0,0 +1,104 @@
|
||||
/**
|
||||
* @example backchannel-client.c
|
||||
* A simple example of an RFB client
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include <errno.h>
|
||||
#include <rfb/rfbclient.h>
|
||||
|
||||
static void HandleRect(rfbClient* client, int x, int y, int w, int h) {
|
||||
}
|
||||
|
||||
/*
|
||||
* The client part of the back channel extension example.
|
||||
*
|
||||
*/
|
||||
|
||||
#define rfbBackChannel 155
|
||||
|
||||
typedef struct backChannelMsg {
|
||||
uint8_t type;
|
||||
uint8_t pad1;
|
||||
uint16_t pad2;
|
||||
uint32_t size;
|
||||
} backChannelMsg;
|
||||
|
||||
static void sendMessage(rfbClient* client, char* text)
|
||||
{
|
||||
backChannelMsg msg;
|
||||
uint32_t length = strlen(text)+1;
|
||||
|
||||
msg.type = rfbBackChannel;
|
||||
msg.size = rfbClientSwap32IfLE(length);
|
||||
if(!WriteToRFBServer(client, (char*)&msg, sizeof(msg)) ||
|
||||
!WriteToRFBServer(client, text, length)) {
|
||||
rfbClientLog("enableBackChannel: write error (%d: %s)", errno, strerror(errno));
|
||||
}
|
||||
}
|
||||
|
||||
static rfbBool handleBackChannelMessage(rfbClient* client,
|
||||
rfbServerToClientMsg* message)
|
||||
{
|
||||
backChannelMsg msg;
|
||||
char* text;
|
||||
|
||||
if(message->type != rfbBackChannel)
|
||||
return FALSE;
|
||||
|
||||
rfbClientSetClientData(client, sendMessage, sendMessage);
|
||||
|
||||
if(!ReadFromRFBServer(client, ((char*)&msg)+1, sizeof(msg)-1))
|
||||
return TRUE;
|
||||
msg.size = rfbClientSwap32IfLE(msg.size);
|
||||
text = malloc(msg.size);
|
||||
if(!ReadFromRFBServer(client, text, msg.size)) {
|
||||
free(text);
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
rfbClientLog("got back channel message: %s\n", text);
|
||||
free(text);
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
static int backChannelEncodings[] = { rfbBackChannel, 0 };
|
||||
|
||||
static rfbClientProtocolExtension backChannel = {
|
||||
backChannelEncodings, /* encodings */
|
||||
NULL, /* handleEncoding */
|
||||
handleBackChannelMessage, /* handleMessage */
|
||||
NULL, /* next extension */
|
||||
NULL, /* securityTypes */
|
||||
NULL /* handleAuthentication */
|
||||
};
|
||||
|
||||
int
|
||||
main(int argc, char **argv)
|
||||
{
|
||||
rfbClient* client = rfbGetClient(8,3,4);
|
||||
|
||||
client->GotFrameBufferUpdate = HandleRect;
|
||||
rfbClientRegisterExtension(&backChannel);
|
||||
|
||||
if (!rfbInitClient(client,&argc,argv))
|
||||
return 1;
|
||||
|
||||
while (1) {
|
||||
/* After each idle second, send a message */
|
||||
if(WaitForMessage(client,1000000)>0)
|
||||
HandleRFBServerMessage(client);
|
||||
else if(rfbClientGetClientData(client, sendMessage))
|
||||
sendMessage(client, "Dear Server,\n"
|
||||
"thank you for understanding "
|
||||
"back channel messages!");
|
||||
}
|
||||
|
||||
rfbClientCleanup(client);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
677
android/extern/libvncserver/examples/client/gtkvncviewer.c
vendored
Normal file
677
android/extern/libvncserver/examples/client/gtkvncviewer.c
vendored
Normal file
@@ -0,0 +1,677 @@
|
||||
|
||||
/*
|
||||
* Copyright (C) 2007 - Mateus Cesar Groess
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 59 Temple Place, Suite 330,
|
||||
* Boston, MA 02111-1307, USA.
|
||||
*/
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <gtk/gtk.h>
|
||||
#include <gdk/gdkkeysyms.h>
|
||||
#include <rfb/rfbclient.h>
|
||||
|
||||
static rfbClient *cl;
|
||||
static gchar *server_cut_text = NULL;
|
||||
static gboolean framebuffer_allocated = FALSE;
|
||||
static GtkWidget *window;
|
||||
static GtkWidget *dialog_connecting = NULL;
|
||||
|
||||
/* Redraw the screen from the backing pixmap */
|
||||
static gboolean expose_event (GtkWidget *widget,
|
||||
GdkEventExpose *event)
|
||||
{
|
||||
static GdkImage *image = NULL;
|
||||
|
||||
if (framebuffer_allocated == FALSE) {
|
||||
|
||||
rfbClientSetClientData (cl, gtk_init, widget);
|
||||
|
||||
image = gdk_drawable_get_image (widget->window, 0, 0,
|
||||
widget->allocation.width,
|
||||
widget->allocation.height);
|
||||
|
||||
cl->frameBuffer= image->mem;
|
||||
|
||||
cl->width = widget->allocation.width;
|
||||
cl->height = widget->allocation.height;
|
||||
|
||||
cl->format.bitsPerPixel = image->bits_per_pixel;
|
||||
cl->format.redShift = image->visual->red_shift;
|
||||
cl->format.greenShift = image->visual->green_shift;
|
||||
cl->format.blueShift = image->visual->blue_shift;
|
||||
|
||||
cl->format.redMax = (1 << image->visual->red_prec) - 1;
|
||||
cl->format.greenMax = (1 << image->visual->green_prec) - 1;
|
||||
cl->format.blueMax = (1 << image->visual->blue_prec) - 1;
|
||||
|
||||
SetFormatAndEncodings (cl);
|
||||
|
||||
framebuffer_allocated = TRUE;
|
||||
|
||||
/* Also disable local cursor */
|
||||
GdkCursor* cur = gdk_cursor_new( GDK_BLANK_CURSOR );
|
||||
gdk_window_set_cursor (gtk_widget_get_window(GTK_WIDGET(window)), cur);
|
||||
gdk_cursor_unref( cur );
|
||||
}
|
||||
|
||||
gdk_draw_image (GDK_DRAWABLE (widget->window),
|
||||
widget->style->fg_gc[gtk_widget_get_state(widget)],
|
||||
image,
|
||||
event->area.x, event->area.y,
|
||||
event->area.x, event->area.y,
|
||||
event->area.width, event->area.height);
|
||||
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
struct { int gdk; int rfb; } buttonMapping[] = {
|
||||
{ GDK_BUTTON1_MASK, rfbButton1Mask },
|
||||
{ GDK_BUTTON2_MASK, rfbButton2Mask },
|
||||
{ GDK_BUTTON3_MASK, rfbButton3Mask },
|
||||
{ 0, 0 }
|
||||
};
|
||||
|
||||
static gboolean button_event (GtkWidget *widget,
|
||||
GdkEventButton *event)
|
||||
{
|
||||
int x, y;
|
||||
GdkModifierType state;
|
||||
int i, buttonMask;
|
||||
|
||||
gdk_window_get_pointer (event->window, &x, &y, &state);
|
||||
|
||||
for (buttonMask = 0, i = 0; buttonMapping[i].gdk; i++)
|
||||
if (state & buttonMapping[i].gdk)
|
||||
buttonMask |= buttonMapping[i].rfb;
|
||||
SendPointerEvent (cl, x, y, buttonMask);
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
static gboolean motion_notify_event (GtkWidget *widget,
|
||||
GdkEventMotion *event)
|
||||
{
|
||||
int x, y;
|
||||
GdkModifierType state;
|
||||
int i, buttonMask;
|
||||
|
||||
if (event->is_hint)
|
||||
gdk_window_get_pointer (event->window, &x, &y, &state);
|
||||
else {
|
||||
x = event->x;
|
||||
y = event->y;
|
||||
state = event->state;
|
||||
}
|
||||
|
||||
for (buttonMask = 0, i = 0; buttonMapping[i].gdk; i++)
|
||||
if (state & buttonMapping[i].gdk)
|
||||
buttonMask |= buttonMapping[i].rfb;
|
||||
SendPointerEvent (cl, x, y, buttonMask);
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
static void got_cut_text (rfbClient *cl, const char *text, int textlen)
|
||||
{
|
||||
if (server_cut_text != NULL) {
|
||||
g_free (server_cut_text);
|
||||
server_cut_text = NULL;
|
||||
}
|
||||
|
||||
server_cut_text = g_strdup (text);
|
||||
}
|
||||
|
||||
void received_text_from_clipboard (GtkClipboard *clipboard,
|
||||
const gchar *text,
|
||||
gpointer data)
|
||||
{
|
||||
if (text)
|
||||
SendClientCutText (cl, (char *) text, strlen (text));
|
||||
}
|
||||
|
||||
static void clipboard_local_to_remote (GtkMenuItem *menuitem,
|
||||
gpointer user_data)
|
||||
{
|
||||
GtkClipboard *clipboard;
|
||||
|
||||
clipboard = gtk_widget_get_clipboard (GTK_WIDGET (menuitem),
|
||||
GDK_SELECTION_CLIPBOARD);
|
||||
gtk_clipboard_request_text (clipboard, received_text_from_clipboard,
|
||||
NULL);
|
||||
}
|
||||
|
||||
static void clipboard_remote_to_local (GtkMenuItem *menuitem,
|
||||
gpointer user_data)
|
||||
{
|
||||
GtkClipboard *clipboard;
|
||||
|
||||
clipboard = gtk_widget_get_clipboard (GTK_WIDGET (menuitem),
|
||||
GDK_SELECTION_CLIPBOARD);
|
||||
|
||||
gtk_clipboard_set_text (clipboard, server_cut_text,
|
||||
strlen (server_cut_text));
|
||||
}
|
||||
|
||||
static void request_screen_refresh (GtkMenuItem *menuitem,
|
||||
gpointer user_data)
|
||||
{
|
||||
SendFramebufferUpdateRequest (cl, 0, 0, cl->width, cl->height, FALSE);
|
||||
}
|
||||
|
||||
static void send_f8 (GtkMenuItem *menuitem,
|
||||
gpointer user_data)
|
||||
{
|
||||
SendKeyEvent(cl, XK_F8, TRUE);
|
||||
SendKeyEvent(cl, XK_F8, FALSE);
|
||||
}
|
||||
|
||||
static void send_crtl_alt_del (GtkMenuItem *menuitem,
|
||||
gpointer user_data)
|
||||
{
|
||||
SendKeyEvent(cl, XK_Control_L, TRUE);
|
||||
SendKeyEvent(cl, XK_Alt_L, TRUE);
|
||||
SendKeyEvent(cl, XK_Delete, TRUE);
|
||||
SendKeyEvent(cl, XK_Alt_L, FALSE);
|
||||
SendKeyEvent(cl, XK_Control_L, FALSE);
|
||||
SendKeyEvent(cl, XK_Delete, FALSE);
|
||||
}
|
||||
|
||||
static void show_connect_window(int argc, char **argv)
|
||||
{
|
||||
GtkWidget *label;
|
||||
char buf[256];
|
||||
|
||||
dialog_connecting = gtk_dialog_new_with_buttons ("VNC Viewer",
|
||||
NULL,
|
||||
GTK_DIALOG_DESTROY_WITH_PARENT,
|
||||
/*GTK_STOCK_CANCEL,
|
||||
GTK_RESPONSE_CANCEL,*/
|
||||
NULL);
|
||||
|
||||
/* FIXME: this works only when address[:port] is at end of arg list */
|
||||
char *server;
|
||||
if(argc==1)
|
||||
server = "localhost";
|
||||
else
|
||||
server = argv[argc-1];
|
||||
snprintf(buf, 255, "Connecting to %s...", server);
|
||||
|
||||
label = gtk_label_new (buf);
|
||||
gtk_widget_show (label);
|
||||
|
||||
gtk_container_add (GTK_CONTAINER (GTK_DIALOG(dialog_connecting)->vbox),
|
||||
label);
|
||||
|
||||
gtk_widget_show (dialog_connecting);
|
||||
|
||||
while (gtk_events_pending ())
|
||||
gtk_main_iteration ();
|
||||
}
|
||||
|
||||
static void show_popup_menu()
|
||||
{
|
||||
GtkWidget *popup_menu;
|
||||
GtkWidget *menu_item;
|
||||
|
||||
popup_menu = gtk_menu_new ();
|
||||
|
||||
menu_item = gtk_menu_item_new_with_label ("Dismiss popup");
|
||||
gtk_widget_show (menu_item);
|
||||
gtk_menu_shell_append (GTK_MENU_SHELL (popup_menu), menu_item);
|
||||
|
||||
menu_item = gtk_menu_item_new_with_label ("Clipboard: local -> remote");
|
||||
g_signal_connect (G_OBJECT (menu_item), "activate",
|
||||
G_CALLBACK (clipboard_local_to_remote), NULL);
|
||||
gtk_widget_show (menu_item);
|
||||
gtk_menu_shell_append (GTK_MENU_SHELL (popup_menu), menu_item);
|
||||
|
||||
menu_item = gtk_menu_item_new_with_label ("Clipboard: local <- remote");
|
||||
g_signal_connect (G_OBJECT (menu_item), "activate",
|
||||
G_CALLBACK (clipboard_remote_to_local), NULL);
|
||||
gtk_widget_show (menu_item);
|
||||
gtk_menu_shell_append (GTK_MENU_SHELL (popup_menu), menu_item);
|
||||
|
||||
menu_item = gtk_menu_item_new_with_label ("Request refresh");
|
||||
g_signal_connect (G_OBJECT (menu_item), "activate",
|
||||
G_CALLBACK (request_screen_refresh), NULL);
|
||||
gtk_widget_show (menu_item);
|
||||
gtk_menu_shell_append (GTK_MENU_SHELL (popup_menu), menu_item);
|
||||
|
||||
menu_item = gtk_menu_item_new_with_label ("Send ctrl-alt-del");
|
||||
g_signal_connect (G_OBJECT (menu_item), "activate",
|
||||
G_CALLBACK (send_crtl_alt_del), NULL);
|
||||
gtk_widget_show (menu_item);
|
||||
gtk_menu_shell_append (GTK_MENU_SHELL (popup_menu), menu_item);
|
||||
|
||||
menu_item = gtk_menu_item_new_with_label ("Send F8");
|
||||
g_signal_connect (G_OBJECT (menu_item), "activate",
|
||||
G_CALLBACK (send_f8), NULL);
|
||||
gtk_widget_show (menu_item);
|
||||
gtk_menu_shell_append (GTK_MENU_SHELL (popup_menu), menu_item);
|
||||
|
||||
gtk_menu_popup (GTK_MENU (popup_menu), NULL, NULL, NULL, NULL, 0,
|
||||
gtk_get_current_event_time());
|
||||
}
|
||||
|
||||
static rfbKeySym gdkKey2rfbKeySym(guint keyval)
|
||||
{
|
||||
rfbKeySym k = 0;
|
||||
switch(keyval) {
|
||||
case GDK_BackSpace: k = XK_BackSpace; break;
|
||||
case GDK_Tab: k = XK_Tab; break;
|
||||
case GDK_Clear: k = XK_Clear; break;
|
||||
case GDK_Return: k = XK_Return; break;
|
||||
case GDK_Pause: k = XK_Pause; break;
|
||||
case GDK_Escape: k = XK_Escape; break;
|
||||
case GDK_space: k = XK_space; break;
|
||||
case GDK_Delete: k = XK_Delete; break;
|
||||
case GDK_KP_0: k = XK_KP_0; break;
|
||||
case GDK_KP_1: k = XK_KP_1; break;
|
||||
case GDK_KP_2: k = XK_KP_2; break;
|
||||
case GDK_KP_3: k = XK_KP_3; break;
|
||||
case GDK_KP_4: k = XK_KP_4; break;
|
||||
case GDK_KP_5: k = XK_KP_5; break;
|
||||
case GDK_KP_6: k = XK_KP_6; break;
|
||||
case GDK_KP_7: k = XK_KP_7; break;
|
||||
case GDK_KP_8: k = XK_KP_8; break;
|
||||
case GDK_KP_9: k = XK_KP_9; break;
|
||||
case GDK_KP_Decimal: k = XK_KP_Decimal; break;
|
||||
case GDK_KP_Divide: k = XK_KP_Divide; break;
|
||||
case GDK_KP_Multiply: k = XK_KP_Multiply; break;
|
||||
case GDK_KP_Subtract: k = XK_KP_Subtract; break;
|
||||
case GDK_KP_Add: k = XK_KP_Add; break;
|
||||
case GDK_KP_Enter: k = XK_KP_Enter; break;
|
||||
case GDK_KP_Equal: k = XK_KP_Equal; break;
|
||||
case GDK_Up: k = XK_Up; break;
|
||||
case GDK_Down: k = XK_Down; break;
|
||||
case GDK_Right: k = XK_Right; break;
|
||||
case GDK_Left: k = XK_Left; break;
|
||||
case GDK_Insert: k = XK_Insert; break;
|
||||
case GDK_Home: k = XK_Home; break;
|
||||
case GDK_End: k = XK_End; break;
|
||||
case GDK_Page_Up: k = XK_Page_Up; break;
|
||||
case GDK_Page_Down: k = XK_Page_Down; break;
|
||||
case GDK_F1: k = XK_F1; break;
|
||||
case GDK_F2: k = XK_F2; break;
|
||||
case GDK_F3: k = XK_F3; break;
|
||||
case GDK_F4: k = XK_F4; break;
|
||||
case GDK_F5: k = XK_F5; break;
|
||||
case GDK_F6: k = XK_F6; break;
|
||||
case GDK_F7: k = XK_F7; break;
|
||||
case GDK_F8: k = XK_F8; break;
|
||||
case GDK_F9: k = XK_F9; break;
|
||||
case GDK_F10: k = XK_F10; break;
|
||||
case GDK_F11: k = XK_F11; break;
|
||||
case GDK_F12: k = XK_F12; break;
|
||||
case GDK_F13: k = XK_F13; break;
|
||||
case GDK_F14: k = XK_F14; break;
|
||||
case GDK_F15: k = XK_F15; break;
|
||||
case GDK_Num_Lock: k = XK_Num_Lock; break;
|
||||
case GDK_Caps_Lock: k = XK_Caps_Lock; break;
|
||||
case GDK_Scroll_Lock: k = XK_Scroll_Lock; break;
|
||||
case GDK_Shift_R: k = XK_Shift_R; break;
|
||||
case GDK_Shift_L: k = XK_Shift_L; break;
|
||||
case GDK_Control_R: k = XK_Control_R; break;
|
||||
case GDK_Control_L: k = XK_Control_L; break;
|
||||
case GDK_Alt_R: k = XK_Alt_R; break;
|
||||
case GDK_Alt_L: k = XK_Alt_L; break;
|
||||
case GDK_Meta_R: k = XK_Meta_R; break;
|
||||
case GDK_Meta_L: k = XK_Meta_L; break;
|
||||
#if 0
|
||||
/* TODO: find out keysyms */
|
||||
case GDK_Super_L: k = XK_LSuper; break; /* left "windows" key */
|
||||
case GDK_Super_R: k = XK_RSuper; break; /* right "windows" key */
|
||||
case GDK_Multi_key: k = XK_Compose; break;
|
||||
#endif
|
||||
case GDK_Mode_switch: k = XK_Mode_switch; break;
|
||||
case GDK_Help: k = XK_Help; break;
|
||||
case GDK_Print: k = XK_Print; break;
|
||||
case GDK_Sys_Req: k = XK_Sys_Req; break;
|
||||
case GDK_Break: k = XK_Break; break;
|
||||
default: break;
|
||||
}
|
||||
if (k == 0) {
|
||||
if (keyval < 0x100)
|
||||
k = keyval;
|
||||
else
|
||||
rfbClientLog ("Unknown keysym: %d\n", keyval);
|
||||
}
|
||||
|
||||
return k;
|
||||
}
|
||||
|
||||
static gboolean key_event (GtkWidget *widget, GdkEventKey *event,
|
||||
gpointer user_data)
|
||||
{
|
||||
if ((event->type == GDK_KEY_PRESS) && (event->keyval == GDK_F8))
|
||||
show_popup_menu();
|
||||
else
|
||||
SendKeyEvent(cl, gdkKey2rfbKeySym (event->keyval),
|
||||
(event->type == GDK_KEY_PRESS) ? TRUE : FALSE);
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
void quit ()
|
||||
{
|
||||
exit (0);
|
||||
}
|
||||
|
||||
static rfbBool resize (rfbClient *client) {
|
||||
GtkWidget *scrolled_window;
|
||||
GtkWidget *drawing_area=NULL;
|
||||
static char first=TRUE;
|
||||
int tmp_width, tmp_height;
|
||||
|
||||
if (first) {
|
||||
first=FALSE;
|
||||
|
||||
/* Create the drawing area */
|
||||
|
||||
drawing_area = gtk_drawing_area_new ();
|
||||
gtk_widget_set_size_request (GTK_WIDGET (drawing_area),
|
||||
client->width, client->height);
|
||||
|
||||
/* Signals used to handle backing pixmap */
|
||||
|
||||
g_signal_connect (G_OBJECT (drawing_area), "expose_event",
|
||||
G_CALLBACK (expose_event), NULL);
|
||||
|
||||
/* Event signals */
|
||||
|
||||
g_signal_connect (G_OBJECT (drawing_area),
|
||||
"motion-notify-event",
|
||||
G_CALLBACK (motion_notify_event), NULL);
|
||||
g_signal_connect (G_OBJECT (drawing_area),
|
||||
"button-press-event",
|
||||
G_CALLBACK (button_event), NULL);
|
||||
g_signal_connect (G_OBJECT (drawing_area),
|
||||
"button-release-event",
|
||||
G_CALLBACK (button_event), NULL);
|
||||
|
||||
gtk_widget_set_events (drawing_area, GDK_EXPOSURE_MASK
|
||||
| GDK_LEAVE_NOTIFY_MASK
|
||||
| GDK_BUTTON_PRESS_MASK
|
||||
| GDK_BUTTON_RELEASE_MASK
|
||||
| GDK_POINTER_MOTION_MASK
|
||||
| GDK_POINTER_MOTION_HINT_MASK);
|
||||
|
||||
gtk_widget_show (drawing_area);
|
||||
|
||||
scrolled_window = gtk_scrolled_window_new (NULL, NULL);
|
||||
gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_window),
|
||||
GTK_POLICY_AUTOMATIC,
|
||||
GTK_POLICY_AUTOMATIC);
|
||||
gtk_scrolled_window_add_with_viewport (
|
||||
GTK_SCROLLED_WINDOW (scrolled_window),
|
||||
drawing_area);
|
||||
g_signal_connect (G_OBJECT (scrolled_window),
|
||||
"key-press-event", G_CALLBACK (key_event),
|
||||
NULL);
|
||||
g_signal_connect (G_OBJECT (scrolled_window),
|
||||
"key-release-event", G_CALLBACK (key_event),
|
||||
NULL);
|
||||
gtk_widget_show (scrolled_window);
|
||||
|
||||
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
|
||||
gtk_window_set_title (GTK_WINDOW (window), client->desktopName);
|
||||
gtk_container_add (GTK_CONTAINER (window), scrolled_window);
|
||||
tmp_width = (int) (
|
||||
gdk_screen_get_width (gdk_screen_get_default ())
|
||||
* 0.85);
|
||||
if (client->width > tmp_width) {
|
||||
tmp_height = (int) (
|
||||
gdk_screen_get_height (
|
||||
gdk_screen_get_default ())
|
||||
* 0.85);
|
||||
gtk_widget_set_size_request (window,
|
||||
tmp_width, tmp_height);
|
||||
} else {
|
||||
gtk_widget_set_size_request (window,
|
||||
client->width + 2,
|
||||
client->height + 2);
|
||||
}
|
||||
|
||||
g_signal_connect (G_OBJECT (window), "destroy",
|
||||
G_CALLBACK (quit), NULL);
|
||||
|
||||
gtk_widget_show (window);
|
||||
} else {
|
||||
gtk_widget_set_size_request (GTK_WIDGET (drawing_area),
|
||||
client->width, client->height);
|
||||
}
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
static void update (rfbClient *cl, int x, int y, int w, int h) {
|
||||
if (dialog_connecting != NULL) {
|
||||
gtk_widget_destroy (dialog_connecting);
|
||||
dialog_connecting = NULL;
|
||||
}
|
||||
|
||||
GtkWidget *drawing_area = rfbClientGetClientData (cl, gtk_init);
|
||||
|
||||
if (drawing_area != NULL)
|
||||
gtk_widget_queue_draw_area (drawing_area, x, y, w, h);
|
||||
}
|
||||
|
||||
static void kbd_leds (rfbClient *cl, int value, int pad) {
|
||||
/* note: pad is for future expansion 0=unused */
|
||||
fprintf (stderr, "Led State= 0x%02X\n", value);
|
||||
fflush (stderr);
|
||||
}
|
||||
|
||||
/* trivial support for textchat */
|
||||
static void text_chat (rfbClient *cl, int value, char *text) {
|
||||
switch (value) {
|
||||
case rfbTextChatOpen:
|
||||
fprintf (stderr, "TextChat: We should open a textchat window!\n");
|
||||
TextChatOpen (cl);
|
||||
break;
|
||||
case rfbTextChatClose:
|
||||
fprintf (stderr, "TextChat: We should close our window!\n");
|
||||
break;
|
||||
case rfbTextChatFinished:
|
||||
fprintf (stderr, "TextChat: We should close our window!\n");
|
||||
break;
|
||||
default:
|
||||
fprintf (stderr, "TextChat: Received \"%s\"\n", text);
|
||||
break;
|
||||
}
|
||||
fflush (stderr);
|
||||
}
|
||||
|
||||
static gboolean on_entry_key_press_event (GtkWidget *widget, GdkEventKey *event,
|
||||
gpointer user_data)
|
||||
{
|
||||
if (event->keyval == GDK_Escape)
|
||||
gtk_dialog_response (GTK_DIALOG(user_data), GTK_RESPONSE_REJECT);
|
||||
else if (event->keyval == GDK_Return)
|
||||
gtk_dialog_response (GTK_DIALOG(user_data), GTK_RESPONSE_ACCEPT);
|
||||
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
static void GtkErrorLog (const char *format, ...)
|
||||
{
|
||||
GtkWidget *dialog, *label;
|
||||
va_list args;
|
||||
char buf[256];
|
||||
|
||||
if (dialog_connecting != NULL) {
|
||||
gtk_widget_destroy (dialog_connecting);
|
||||
dialog_connecting = NULL;
|
||||
}
|
||||
|
||||
va_start (args, format);
|
||||
vsnprintf (buf, 255, format, args);
|
||||
va_end (args);
|
||||
|
||||
if (g_utf8_validate (buf, strlen (buf), NULL)) {
|
||||
label = gtk_label_new (buf);
|
||||
} else {
|
||||
const gchar *charset;
|
||||
gchar *utf8;
|
||||
|
||||
(void) g_get_charset (&charset);
|
||||
utf8 = g_convert_with_fallback (buf, strlen (buf), "UTF-8",
|
||||
charset, NULL, NULL, NULL, NULL);
|
||||
|
||||
if (utf8) {
|
||||
label = gtk_label_new (utf8);
|
||||
g_free (utf8);
|
||||
} else {
|
||||
label = gtk_label_new (buf);
|
||||
g_warning ("Message Output is not in UTF-8"
|
||||
"nor in locale charset.\n");
|
||||
}
|
||||
}
|
||||
|
||||
dialog = gtk_dialog_new_with_buttons ("Error",
|
||||
NULL,
|
||||
GTK_DIALOG_DESTROY_WITH_PARENT,
|
||||
GTK_STOCK_OK,
|
||||
GTK_RESPONSE_ACCEPT,
|
||||
NULL);
|
||||
label = gtk_label_new (buf);
|
||||
gtk_widget_show (label);
|
||||
|
||||
gtk_container_add (GTK_CONTAINER (GTK_DIALOG(dialog)->vbox),
|
||||
label);
|
||||
gtk_widget_show (dialog);
|
||||
|
||||
switch (gtk_dialog_run (GTK_DIALOG (dialog))) {
|
||||
case GTK_RESPONSE_ACCEPT:
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
gtk_widget_destroy (dialog);
|
||||
}
|
||||
|
||||
static void GtkDefaultLog (const char *format, ...)
|
||||
{
|
||||
va_list args;
|
||||
char buf[256];
|
||||
time_t log_clock;
|
||||
|
||||
va_start (args, format);
|
||||
|
||||
time (&log_clock);
|
||||
strftime (buf, 255, "%d/%m/%Y %X ", localtime (&log_clock));
|
||||
fprintf (stdout, "%s", buf);
|
||||
|
||||
vfprintf (stdout, format, args);
|
||||
fflush (stdout);
|
||||
|
||||
va_end (args);
|
||||
}
|
||||
|
||||
static char * get_password (rfbClient *client)
|
||||
{
|
||||
GtkWidget *dialog, *entry;
|
||||
char *password;
|
||||
|
||||
gtk_widget_destroy (dialog_connecting);
|
||||
dialog_connecting = NULL;
|
||||
|
||||
dialog = gtk_dialog_new_with_buttons ("Password",
|
||||
NULL,
|
||||
GTK_DIALOG_DESTROY_WITH_PARENT,
|
||||
GTK_STOCK_CANCEL,
|
||||
GTK_RESPONSE_REJECT,
|
||||
GTK_STOCK_OK,
|
||||
GTK_RESPONSE_ACCEPT,
|
||||
NULL);
|
||||
entry = gtk_entry_new ();
|
||||
gtk_entry_set_visibility (GTK_ENTRY (entry),
|
||||
FALSE);
|
||||
g_signal_connect (GTK_OBJECT(entry), "key-press-event",
|
||||
G_CALLBACK(on_entry_key_press_event),
|
||||
GTK_OBJECT (dialog));
|
||||
gtk_widget_show (entry);
|
||||
|
||||
gtk_container_add (GTK_CONTAINER (GTK_DIALOG(dialog)->vbox),
|
||||
entry);
|
||||
gtk_widget_show (dialog);
|
||||
|
||||
switch (gtk_dialog_run (GTK_DIALOG (dialog))) {
|
||||
case GTK_RESPONSE_ACCEPT:
|
||||
password = strdup (gtk_entry_get_text (GTK_ENTRY (entry)));
|
||||
break;
|
||||
default:
|
||||
password = NULL;
|
||||
break;
|
||||
}
|
||||
gtk_widget_destroy (dialog);
|
||||
return password;
|
||||
}
|
||||
|
||||
int main (int argc, char *argv[])
|
||||
{
|
||||
int i;
|
||||
GdkImage *image;
|
||||
|
||||
rfbClientLog = GtkDefaultLog;
|
||||
rfbClientErr = GtkErrorLog;
|
||||
|
||||
gtk_init (&argc, &argv);
|
||||
|
||||
/* create a dummy image just to make use of its properties */
|
||||
image = gdk_image_new (GDK_IMAGE_FASTEST, gdk_visual_get_system(),
|
||||
200, 100);
|
||||
|
||||
cl = rfbGetClient (image->depth / 3, 3, image->bpp);
|
||||
|
||||
cl->format.redShift = image->visual->red_shift;
|
||||
cl->format.greenShift = image->visual->green_shift;
|
||||
cl->format.blueShift = image->visual->blue_shift;
|
||||
|
||||
cl->format.redMax = (1 << image->visual->red_prec) - 1;
|
||||
cl->format.greenMax = (1 << image->visual->green_prec) - 1;
|
||||
cl->format.blueMax = (1 << image->visual->blue_prec) - 1;
|
||||
|
||||
g_object_unref (image);
|
||||
|
||||
cl->MallocFrameBuffer = resize;
|
||||
cl->canHandleNewFBSize = TRUE;
|
||||
cl->GotFrameBufferUpdate = update;
|
||||
cl->GotXCutText = got_cut_text;
|
||||
cl->HandleKeyboardLedState = kbd_leds;
|
||||
cl->HandleTextChat = text_chat;
|
||||
cl->GetPassword = get_password;
|
||||
|
||||
show_connect_window (argc, argv);
|
||||
|
||||
if (!rfbInitClient (cl, &argc, argv))
|
||||
return 1;
|
||||
|
||||
while (1) {
|
||||
while (gtk_events_pending ())
|
||||
gtk_main_iteration ();
|
||||
i = WaitForMessage (cl, 500);
|
||||
if (i < 0)
|
||||
return 0;
|
||||
if (i && framebuffer_allocated == TRUE)
|
||||
if (!HandleRFBServerMessage(cl))
|
||||
return 0;
|
||||
}
|
||||
|
||||
gtk_main ();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
117
android/extern/libvncserver/examples/client/ppmtest.c
vendored
Normal file
117
android/extern/libvncserver/examples/client/ppmtest.c
vendored
Normal file
@@ -0,0 +1,117 @@
|
||||
/**
|
||||
* @example ppmtest.c
|
||||
* A simple example of an RFB client
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include <errno.h>
|
||||
#include <rfb/rfbclient.h>
|
||||
|
||||
static void PrintRect(rfbClient* client, int x, int y, int w, int h) {
|
||||
rfbClientLog("Received an update for %d,%d,%d,%d.\n",x,y,w,h);
|
||||
}
|
||||
|
||||
static void SaveFramebufferAsPPM(rfbClient* client, int x, int y, int w, int h) {
|
||||
static time_t t=0,t1;
|
||||
FILE* f;
|
||||
int i,j;
|
||||
rfbPixelFormat* pf=&client->format;
|
||||
int bpp=pf->bitsPerPixel/8;
|
||||
int row_stride=client->width*bpp;
|
||||
|
||||
/* save one picture only if the last is older than 2 seconds */
|
||||
t1=time(NULL);
|
||||
if(t1-t>2)
|
||||
t=t1;
|
||||
else
|
||||
return;
|
||||
|
||||
/* assert bpp=4 */
|
||||
if(bpp!=4 && bpp!=2) {
|
||||
rfbClientLog("bpp = %d (!=4)\n",bpp);
|
||||
return;
|
||||
}
|
||||
|
||||
f=fopen("framebuffer.ppm","wb");
|
||||
if(!f) {
|
||||
rfbClientErr("Could not open framebuffer.ppm\n");
|
||||
return;
|
||||
}
|
||||
|
||||
fprintf(f,"P6\n# %s\n%d %d\n255\n",client->desktopName,client->width,client->height);
|
||||
for(j=0;j<client->height*row_stride;j+=row_stride)
|
||||
for(i=0;i<client->width*bpp;i+=bpp) {
|
||||
unsigned char* p=client->frameBuffer+j+i;
|
||||
unsigned int v;
|
||||
if(bpp==4)
|
||||
v=*(unsigned int*)p;
|
||||
else if(bpp==2)
|
||||
v=*(unsigned short*)p;
|
||||
else
|
||||
v=*(unsigned char*)p;
|
||||
fputc((v>>pf->redShift)*256/(pf->redMax+1),f);
|
||||
fputc((v>>pf->greenShift)*256/(pf->greenMax+1),f);
|
||||
fputc((v>>pf->blueShift)*256/(pf->blueMax+1),f);
|
||||
}
|
||||
fclose(f);
|
||||
}
|
||||
|
||||
char * getuser(rfbClient *client)
|
||||
{
|
||||
return strdup("testuser@test");
|
||||
}
|
||||
|
||||
char * getpassword(rfbClient *client)
|
||||
{
|
||||
return strdup("Password");
|
||||
}
|
||||
|
||||
int
|
||||
main(int argc, char **argv)
|
||||
{
|
||||
rfbClient* client = rfbGetClient(8,3,4);
|
||||
time_t t=time(NULL);
|
||||
|
||||
#ifdef LIBVNCSERVER_HAVE_SASL
|
||||
client->GetUser = getuser;
|
||||
client->GetPassword = getpassword;
|
||||
#endif
|
||||
|
||||
if(argc>1 && !strcmp("-print",argv[1])) {
|
||||
client->GotFrameBufferUpdate = PrintRect;
|
||||
argv[1]=argv[0]; argv++; argc--;
|
||||
} else
|
||||
client->GotFrameBufferUpdate = SaveFramebufferAsPPM;
|
||||
|
||||
/* The -listen option is used to make us a daemon process which listens for
|
||||
incoming connections from servers, rather than actively connecting to a
|
||||
given server. The -tunnel and -via options are useful to create
|
||||
connections tunneled via SSH port forwarding. We must test for the
|
||||
-listen option before invoking any Xt functions - this is because we use
|
||||
forking, and Xt doesn't seem to cope with forking very well. For -listen
|
||||
option, when a successful incoming connection has been accepted,
|
||||
listenForIncomingConnections() returns, setting the listenSpecified
|
||||
flag. */
|
||||
|
||||
if (!rfbInitClient(client,&argc,argv))
|
||||
return 1;
|
||||
|
||||
/* TODO: better wait for update completion */
|
||||
while (time(NULL)-t<5) {
|
||||
static int i=0;
|
||||
fprintf(stderr,"\r%d",i++);
|
||||
int n = WaitForMessage(client,50);
|
||||
if(n < 0)
|
||||
break;
|
||||
if(n)
|
||||
if(!HandleRFBServerMessage(client))
|
||||
break;
|
||||
}
|
||||
|
||||
rfbClientCleanup(client);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
445
android/extern/libvncserver/examples/client/sshtunnel.c
vendored
Normal file
445
android/extern/libvncserver/examples/client/sshtunnel.c
vendored
Normal file
@@ -0,0 +1,445 @@
|
||||
/**
|
||||
* @example sshtunnel.c
|
||||
* An example of an RFB client tunneled through SSH by using libssh2.
|
||||
* This is based on https://www.libssh2.org/examples/direct_tcpip.html
|
||||
* with the following changes:
|
||||
* - the listening is split out into a separate thread function
|
||||
* - the listener gets closed immediately once a connection was accepted
|
||||
* - the listening port is chosen by the OS, SO_REUSEADDR removed
|
||||
* - global variables moved into SshData helper structure
|
||||
* - added name resolution for the ssh host
|
||||
*/
|
||||
|
||||
#include <rfb/rfbclient.h>
|
||||
#include <libssh2.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <netdb.h>
|
||||
#include <signal.h>
|
||||
#include <errno.h>
|
||||
#ifdef LIBVNCSERVER_HAVE_SYS_TYPES_H
|
||||
#include <sys/types.h>
|
||||
#endif
|
||||
#ifdef LIBVNCSERVER_HAVE_SYS_SOCKET_H
|
||||
#include <sys/socket.h>
|
||||
#endif
|
||||
#ifdef LIBVNCSERVER_HAVE_UNISTD_H
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
|
||||
/* The one global bool that's global so we can set it via
|
||||
a signal handler... */
|
||||
int maintain_connection = 1;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
rfbClient *client;
|
||||
LIBSSH2_SESSION *session;
|
||||
#ifdef LIBVNCSERVER_HAVE_LIBPTHREAD
|
||||
pthread_t thread;
|
||||
#elif defined(LIBVNCSERVER_HAVE_WIN32THREADS)
|
||||
uintptr_t thread;
|
||||
#endif
|
||||
int ssh_sock;
|
||||
int local_listensock;
|
||||
int local_listenport;
|
||||
const char *remote_desthost;
|
||||
int remote_destport;
|
||||
} SshData;
|
||||
|
||||
|
||||
THREAD_ROUTINE_RETURN_TYPE ssh_proxy_loop(void *arg)
|
||||
{
|
||||
SshData *data = arg;
|
||||
int rc, i;
|
||||
struct sockaddr_in sin;
|
||||
socklen_t sinlen;
|
||||
LIBSSH2_CHANNEL *channel = NULL;
|
||||
const char *shost;
|
||||
int sport;
|
||||
fd_set fds;
|
||||
struct timeval tv;
|
||||
ssize_t len, wr;
|
||||
char buf[16384];
|
||||
int proxy_sock = RFB_INVALID_SOCKET;
|
||||
|
||||
proxy_sock = accept(data->local_listensock, (struct sockaddr *)&sin, &sinlen);
|
||||
if(proxy_sock == RFB_INVALID_SOCKET) {
|
||||
fprintf(stderr, "ssh_proxy_loop: accept: %s\n", strerror(errno));
|
||||
goto shutdown;
|
||||
}
|
||||
|
||||
/* Close listener once a connection got accepted */
|
||||
rfbCloseSocket(data->local_listensock);
|
||||
|
||||
shost = inet_ntoa(sin.sin_addr);
|
||||
sport = ntohs(sin.sin_port);
|
||||
|
||||
printf("ssh_proxy_loop: forwarding connection from %s:%d here to remote %s:%d\n",
|
||||
shost, sport, data->remote_desthost, data->remote_destport);
|
||||
|
||||
channel = libssh2_channel_direct_tcpip_ex(data->session, data->remote_desthost,
|
||||
data->remote_destport, shost, sport);
|
||||
if(!channel) {
|
||||
fprintf(stderr, "ssh_proxy_loop: Could not open the direct-tcpip channel!\n"
|
||||
"(Note that this can be a problem at the server!"
|
||||
" Please review the server logs.)\n");
|
||||
goto shutdown;
|
||||
}
|
||||
|
||||
/* Must use non-blocking IO hereafter due to the current libssh2 API */
|
||||
libssh2_session_set_blocking(data->session, 0);
|
||||
|
||||
while(1) {
|
||||
FD_ZERO(&fds);
|
||||
FD_SET(proxy_sock, &fds);
|
||||
tv.tv_sec = 0;
|
||||
tv.tv_usec = 100000;
|
||||
rc = select(proxy_sock + 1, &fds, NULL, NULL, &tv);
|
||||
if(-1 == rc) {
|
||||
fprintf(stderr, "ssh_proxy_loop: select: %s\n", strerror(errno));
|
||||
goto shutdown;
|
||||
}
|
||||
if(rc && FD_ISSET(proxy_sock, &fds)) {
|
||||
len = recv(proxy_sock, buf, sizeof(buf), 0);
|
||||
if(len < 0) {
|
||||
fprintf(stderr, "read: %s\n", strerror(errno));
|
||||
goto shutdown;
|
||||
}
|
||||
else if(0 == len) {
|
||||
fprintf(stderr, "ssh_proxy_loop: the client at %s:%d disconnected!\n", shost,
|
||||
sport);
|
||||
goto shutdown;
|
||||
}
|
||||
wr = 0;
|
||||
while(wr < len) {
|
||||
i = libssh2_channel_write(channel, buf + wr, len - wr);
|
||||
if(LIBSSH2_ERROR_EAGAIN == i) {
|
||||
continue;
|
||||
}
|
||||
if(i < 0) {
|
||||
fprintf(stderr, "ssh_proxy_loop: libssh2_channel_write: %d\n", i);
|
||||
goto shutdown;
|
||||
}
|
||||
wr += i;
|
||||
}
|
||||
}
|
||||
while(1) {
|
||||
len = libssh2_channel_read(channel, buf, sizeof(buf));
|
||||
if(LIBSSH2_ERROR_EAGAIN == len)
|
||||
break;
|
||||
else if(len < 0) {
|
||||
fprintf(stderr, "ssh_proxy_loop: libssh2_channel_read: %d\n", (int)len);
|
||||
goto shutdown;
|
||||
}
|
||||
wr = 0;
|
||||
while(wr < len) {
|
||||
i = send(proxy_sock, buf + wr, len - wr, 0);
|
||||
if(i <= 0) {
|
||||
fprintf(stderr, "ssh_proxy_loop: write: %s\n", strerror(errno));
|
||||
goto shutdown;
|
||||
}
|
||||
wr += i;
|
||||
}
|
||||
if(libssh2_channel_eof(channel)) {
|
||||
fprintf(stderr, "ssh_proxy_loop: the server at %s:%d disconnected!\n",
|
||||
data->remote_desthost, data->remote_destport);
|
||||
goto shutdown;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
shutdown:
|
||||
|
||||
printf("ssh_proxy_loop: shutting down\n");
|
||||
|
||||
rfbCloseSocket(proxy_sock);
|
||||
|
||||
if(channel)
|
||||
libssh2_channel_free(channel);
|
||||
|
||||
libssh2_session_disconnect(data->session, "Client disconnecting normally");
|
||||
libssh2_session_free(data->session);
|
||||
|
||||
rfbCloseSocket(data->ssh_sock);
|
||||
|
||||
return THREAD_ROUTINE_RETURN_VALUE;
|
||||
}
|
||||
|
||||
/**
|
||||
Decide whether or not the SSH tunnel setup should continue
|
||||
based on the current host and its fingerprint.
|
||||
Business logic is up to the implementer in a real app, i.e.
|
||||
compare keys, ask user etc...
|
||||
@return -1 if tunnel setup should be aborted
|
||||
0 if tunnel setup should continue
|
||||
*/
|
||||
int ssh_fingerprint_check(const char *fingerprint, size_t fingerprint_len,
|
||||
const char *host, rfbClient *client)
|
||||
{
|
||||
size_t i;
|
||||
fprintf(stderr, "ssh_fingerprint_check: host %s has ", host);
|
||||
for(i = 0; i < fingerprint_len; i++)
|
||||
printf("%02X ", (unsigned char)fingerprint[i]);
|
||||
printf("\n");
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
Creates an SSH tunnel and a local proxy and returns the port the proxy is listening on.
|
||||
@return A pointer to an SshData structure or NULL on error.
|
||||
*/
|
||||
SshData* ssh_tunnel_open(const char *ssh_host,
|
||||
const char *ssh_user,
|
||||
const char *ssh_password,
|
||||
const char *ssh_pub_key_path,
|
||||
const char *ssh_priv_key_path,
|
||||
const char *ssh_priv_key_password,
|
||||
const char *rfb_host,
|
||||
int rfb_port,
|
||||
rfbClient *client)
|
||||
{
|
||||
int rc, i;
|
||||
struct sockaddr_in sin;
|
||||
socklen_t sinlen;
|
||||
const char *fingerprint;
|
||||
char *userauthlist;
|
||||
struct addrinfo hints, *res;
|
||||
SshData *data;
|
||||
|
||||
/* Sanity checks */
|
||||
if(!ssh_host || !ssh_user || !rfb_host) /* these must be set */
|
||||
return NULL;
|
||||
|
||||
data = calloc(1, sizeof(SshData));
|
||||
|
||||
data->client = client;
|
||||
data->remote_desthost = rfb_host; /* resolved by the server */
|
||||
data->remote_destport = rfb_port;
|
||||
|
||||
/* Connect to SSH server */
|
||||
data->ssh_sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
|
||||
if(data->ssh_sock == RFB_INVALID_SOCKET) {
|
||||
fprintf(stderr, "ssh_tunnel_open: socket: %s\n", strerror(errno));
|
||||
goto error;
|
||||
}
|
||||
|
||||
memset(&hints, 0, sizeof(hints));
|
||||
hints.ai_family = AF_INET;
|
||||
hints.ai_socktype = SOCK_STREAM;
|
||||
|
||||
if ((rc = getaddrinfo(ssh_host, NULL, &hints, &res)) == 0) {
|
||||
sin.sin_family = AF_INET;
|
||||
sin.sin_addr.s_addr = (((struct sockaddr_in *)res->ai_addr)->sin_addr.s_addr);
|
||||
freeaddrinfo(res);
|
||||
} else {
|
||||
fprintf(stderr, "ssh_tunnel_open: getaddrinfo: %s\n", gai_strerror(rc));
|
||||
goto error;
|
||||
}
|
||||
|
||||
sin.sin_port = htons(22);
|
||||
if(connect(data->ssh_sock, (struct sockaddr*)(&sin), sizeof(struct sockaddr_in)) != 0) {
|
||||
fprintf(stderr, "ssh_tunnel_open: failed to connect to SSH server!\n");
|
||||
goto error;
|
||||
}
|
||||
|
||||
/* Create a session instance */
|
||||
data->session = libssh2_session_init();
|
||||
if(!data->session) {
|
||||
fprintf(stderr, "ssh_tunnel_open: could not initialize SSH session!\n");
|
||||
goto error;
|
||||
}
|
||||
|
||||
/* ... start it up. This will trade welcome banners, exchange keys,
|
||||
* and setup crypto, compression, and MAC layers
|
||||
*/
|
||||
rc = libssh2_session_handshake(data->session, data->ssh_sock);
|
||||
if(rc) {
|
||||
fprintf(stderr, "ssh_tunnel_open: error when starting up SSH session: %d\n", rc);
|
||||
goto error;
|
||||
}
|
||||
|
||||
/* At this point we havn't yet authenticated. The first thing to do
|
||||
* is check the hostkey's fingerprint against our known hosts Your app
|
||||
* may have it hard coded, may go to a file, may present it to the
|
||||
* user, that's your call
|
||||
*/
|
||||
fingerprint = libssh2_hostkey_hash(data->session, LIBSSH2_HOSTKEY_HASH_SHA256);
|
||||
if(ssh_fingerprint_check(fingerprint, 32, ssh_host, data->client) == -1) {
|
||||
fprintf(stderr, "ssh_tunnel_open: fingerprint check indicated tunnel setup stop\n");
|
||||
goto error;
|
||||
}
|
||||
|
||||
/* check what authentication methods are available */
|
||||
userauthlist = libssh2_userauth_list(data->session, ssh_user, strlen(ssh_user));
|
||||
printf("ssh_tunnel_open: authentication methods: %s\n", userauthlist);
|
||||
|
||||
if(ssh_password && strstr(userauthlist, "password")) {
|
||||
if(libssh2_userauth_password(data->session, ssh_user, ssh_password)) {
|
||||
fprintf(stderr, "ssh_tunnel_open: authentication by password failed.\n");
|
||||
goto error;
|
||||
}
|
||||
}
|
||||
else if(ssh_priv_key_path && ssh_priv_key_password && strstr(userauthlist, "publickey")) {
|
||||
if(libssh2_userauth_publickey_fromfile(data->session, ssh_user, ssh_pub_key_path,
|
||||
ssh_priv_key_path, ssh_priv_key_password)) {
|
||||
fprintf(stderr, "ssh_tunnel_open: authentication by public key failed!\n");
|
||||
goto error;
|
||||
}
|
||||
}
|
||||
else {
|
||||
fprintf(stderr, "ssh_tunnel_open: no supported authentication methods found!\n");
|
||||
goto error;
|
||||
}
|
||||
|
||||
/* Create and bind the local listening socket */
|
||||
data->local_listensock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
|
||||
if(data->local_listensock == RFB_INVALID_SOCKET) {
|
||||
fprintf(stderr, "ssh_tunnel_open: socket: %s\n", strerror(errno));
|
||||
return NULL;
|
||||
}
|
||||
sin.sin_family = AF_INET;
|
||||
sin.sin_port = htons(0); /* let the OS choose the port */
|
||||
sin.sin_addr.s_addr = inet_addr("127.0.0.1");
|
||||
if(INADDR_NONE == sin.sin_addr.s_addr) {
|
||||
fprintf(stderr, "ssh_tunnel_open: inet_addr: %s\n", strerror(errno));
|
||||
goto error;
|
||||
}
|
||||
sinlen = sizeof(sin);
|
||||
if(-1 == bind(data->local_listensock, (struct sockaddr *)&sin, sinlen)) {
|
||||
fprintf(stderr, "bind: %s\n", strerror(errno));
|
||||
goto error;
|
||||
}
|
||||
if(-1 == listen(data->local_listensock, 1)) {
|
||||
fprintf(stderr, "listen: %s\n", strerror(errno));
|
||||
goto error;
|
||||
}
|
||||
|
||||
/* get info back from OS */
|
||||
if (getsockname(data->local_listensock, (struct sockaddr *)&sin, &sinlen ) == -1){
|
||||
fprintf(stderr, "ssh_tunnel_open: getsockname: %s\n", strerror(errno));
|
||||
goto error;
|
||||
}
|
||||
|
||||
data->local_listenport = ntohs(sin.sin_port);
|
||||
|
||||
printf("ssh_tunnel_open: waiting for TCP connection on %s:%d...\n",
|
||||
inet_ntoa(sin.sin_addr), ntohs(sin.sin_port));
|
||||
|
||||
|
||||
/* Create the proxy thread */
|
||||
#if defined(LIBVNCSERVER_HAVE_LIBPTHREAD)
|
||||
if (pthread_create(&data->thread, NULL, ssh_proxy_loop, data) != 0) {
|
||||
#elif defined(LIBVNCSERVER_HAVE_WIN32THREADS)
|
||||
if(data->thread = _beginthread(proxy_loop, 0, data) == 0);
|
||||
#endif
|
||||
fprintf(stderr, "ssh_tunnel_open: proxy thread creation failed\n");
|
||||
goto error;
|
||||
}
|
||||
|
||||
return data;
|
||||
|
||||
error:
|
||||
if (data->session) {
|
||||
libssh2_session_disconnect(data->session, "Error in SSH tunnel setup");
|
||||
libssh2_session_free(data->session);
|
||||
}
|
||||
|
||||
rfbCloseSocket(data->local_listensock);
|
||||
rfbCloseSocket(data->ssh_sock);
|
||||
|
||||
free(data);
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
void ssh_tunnel_close(SshData *data) {
|
||||
if(!data)
|
||||
return;
|
||||
|
||||
/* the proxy thread does the internal cleanup as it can be
|
||||
ended due to external reasons */
|
||||
THREAD_JOIN(data->thread);
|
||||
|
||||
free(data);
|
||||
|
||||
printf("ssh_tunnel_close: done\n");
|
||||
}
|
||||
|
||||
|
||||
void intHandler(int dummy) {
|
||||
maintain_connection = 0;
|
||||
}
|
||||
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
rfbClient *client = rfbGetClient(8,3,4);
|
||||
|
||||
/*
|
||||
Get args and create SSH tunnel
|
||||
*/
|
||||
int rc = libssh2_init(0);
|
||||
if(rc) {
|
||||
fprintf(stderr, "libssh2 initialization failed (%d)\n", rc);
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
SshData *data;
|
||||
if (argc == 6) {
|
||||
/* SSH tunnel w/ password */
|
||||
data = ssh_tunnel_open(argv[1], argv[2], argv[3], NULL, NULL, NULL, argv[4], atoi(argv[5]), client);
|
||||
} else if (argc == 8) {
|
||||
/* SSH tunnel w/ privkey */
|
||||
data = ssh_tunnel_open(argv[1], argv[2], NULL, argv[3], argv[4], argv[5], argv[6], atoi(argv[7]), client);
|
||||
} else {
|
||||
fprintf(stderr,
|
||||
"Usage (w/ password): %s <ssh-server-IP> <ssh-server-username> <ssh-server-password> <rfb-host> <rfb-port>\n"
|
||||
"Usage (w/ privkey): %s <ssh-server-IP> <ssh-server-username> <pubkey_filename> <privkey_filename> <privkey_password> <rfb-host> <rfb-port>\n",
|
||||
argv[0], argv[0]);
|
||||
return(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
The actual VNC connection setup.
|
||||
*/
|
||||
client->serverHost = strdup("127.0.0.1");
|
||||
if(data) // might be NULL if ssh setup failed
|
||||
client->serverPort = data->local_listenport;
|
||||
rfbClientSetClientData(client, (void*)42, data);
|
||||
|
||||
if (!data || !rfbInitClient(client,NULL,NULL))
|
||||
return EXIT_FAILURE;
|
||||
|
||||
printf("Successfully connected to %s:%d - hit Ctrl-C to disconnect\n", client->serverHost, client->serverPort);
|
||||
|
||||
signal(SIGINT, intHandler);
|
||||
|
||||
while (maintain_connection) {
|
||||
int n = WaitForMessage(client,50);
|
||||
if(n < 0)
|
||||
break;
|
||||
if(n)
|
||||
if(!HandleRFBServerMessage(client))
|
||||
break;
|
||||
}
|
||||
|
||||
/* Disconnect client inside tunnel */
|
||||
if(client && client->sock != RFB_INVALID_SOCKET)
|
||||
rfbCloseSocket(client->sock);
|
||||
|
||||
/* Close the tunnel and clean up */
|
||||
ssh_tunnel_close(rfbClientGetClientData(client, (void*)42));
|
||||
|
||||
/* free client */
|
||||
rfbClientCleanup(client);
|
||||
|
||||
/* Teardown libssh2 */
|
||||
libssh2_exit();
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
481
android/extern/libvncserver/examples/client/vnc2mpg.c
vendored
Normal file
481
android/extern/libvncserver/examples/client/vnc2mpg.c
vendored
Normal file
@@ -0,0 +1,481 @@
|
||||
/**
|
||||
* @example vnc2mpg.c
|
||||
* Simple movie writer for vnc; based on Libavformat API example from FFMPEG
|
||||
*
|
||||
* Copyright (c) 2003 Fabrice Bellard, 2004 Johannes E. Schindelin
|
||||
* Updates copyright (c) 2017 Tyrel M. McQueen
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
||||
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <math.h>
|
||||
#include <signal.h>
|
||||
#include <sys/time.h>
|
||||
#include <libavcodec/avcodec.h>
|
||||
#include <libavformat/avformat.h>
|
||||
#include <libswscale/swscale.h>
|
||||
#include <rfb/rfbclient.h>
|
||||
|
||||
#define VNC_PIX_FMT AV_PIX_FMT_RGB565 /* pixel format generated by VNC client */
|
||||
#define OUTPUT_PIX_FMT AV_PIX_FMT_YUV420P /* default pix_fmt */
|
||||
|
||||
static int write_packet(AVFormatContext *oc, const AVRational *time_base, AVStream *st, AVPacket *pkt)
|
||||
{
|
||||
/* rescale output packet timestamp values from codec to stream timebase */
|
||||
av_packet_rescale_ts(pkt, *time_base, st->time_base);
|
||||
pkt->stream_index = st->index;
|
||||
/* Write the compressed frame to the media file. */
|
||||
return av_interleaved_write_frame(oc, pkt);
|
||||
}
|
||||
|
||||
/*************************************************/
|
||||
/* video functions */
|
||||
|
||||
/* a wrapper around a single output video stream */
|
||||
typedef struct {
|
||||
AVStream *st;
|
||||
AVCodec *codec;
|
||||
AVCodecContext *enc;
|
||||
int64_t pts;
|
||||
AVFrame *frame;
|
||||
AVFrame *tmp_frame;
|
||||
struct SwsContext *sws;
|
||||
} VideoOutputStream;
|
||||
|
||||
/* Add an output video stream. */
|
||||
int add_video_stream(VideoOutputStream *ost, AVFormatContext *oc,
|
||||
enum AVCodecID codec_id, int64_t br, int sr, int w, int h)
|
||||
{
|
||||
int i;
|
||||
|
||||
/* find the encoder */
|
||||
ost->codec = avcodec_find_encoder(codec_id);
|
||||
if (!(ost->codec)) {
|
||||
fprintf(stderr, "Could not find encoder for '%s'\n",
|
||||
avcodec_get_name(codec_id));
|
||||
return -1;
|
||||
} // no extra memory allocation from this call
|
||||
if (ost->codec->type != AVMEDIA_TYPE_VIDEO) {
|
||||
fprintf(stderr, "Encoder for '%s' does not seem to be for video.\n",
|
||||
avcodec_get_name(codec_id));
|
||||
return -2;
|
||||
}
|
||||
ost->enc = avcodec_alloc_context3(ost->codec);
|
||||
if (!(ost->enc)) {
|
||||
fprintf(stderr, "Could not alloc an encoding context\n");
|
||||
return -3;
|
||||
} // from now on need to call avcodec_free_context(&(ost->enc)) on error
|
||||
|
||||
/* Set codec parameters */
|
||||
ost->enc->codec_id = codec_id;
|
||||
ost->enc->bit_rate = br;
|
||||
/* Resolution must be a multiple of two (round up to avoid buffer overflow). */
|
||||
ost->enc->width = w + (w % 2);
|
||||
ost->enc->height = h + (h % 2);
|
||||
/* timebase: This is the fundamental unit of time (in seconds) in terms
|
||||
* of which frame timestamps are represented. For fixed-fps content,
|
||||
* timebase should be 1/framerate and timestamp increments should be
|
||||
* identical to 1. */
|
||||
ost->enc->time_base = (AVRational){ 1, sr };
|
||||
ost->enc->gop_size = 12; /* emit one intra frame every twelve frames at most */
|
||||
ost->enc->pix_fmt = OUTPUT_PIX_FMT;
|
||||
if (ost->enc->codec_id == AV_CODEC_ID_MPEG1VIDEO) {
|
||||
/* Needed to avoid using macroblocks in which some coeffs overflow.
|
||||
* This does not happen with normal video, it just happens here as
|
||||
* the motion of the chroma plane does not match the luma plane. */
|
||||
ost->enc->mb_decision = 2;
|
||||
}
|
||||
|
||||
ost->st = avformat_new_stream(oc, ost->codec);
|
||||
if (!ost->st) {
|
||||
fprintf(stderr, "Could not allocate stream\n");
|
||||
avcodec_free_context(&(ost->enc));
|
||||
return -4;
|
||||
} // stream memory cleared up when oc is freed, so no need to do so later in this function on error
|
||||
ost->st->id = oc->nb_streams-1;
|
||||
ost->st->time_base = ost->enc->time_base;
|
||||
ost->pts = 0;
|
||||
|
||||
/* Some formats want stream headers to be separate. */
|
||||
if (oc->oformat->flags & AVFMT_GLOBALHEADER)
|
||||
ost->enc->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
|
||||
|
||||
// must wait to allocate frame buffers until codec is opened (in case codec changes the PIX_FMT)
|
||||
return 0;
|
||||
}
|
||||
|
||||
AVFrame *alloc_picture(enum AVPixelFormat pix_fmt, int width, int height)
|
||||
{
|
||||
AVFrame *picture;
|
||||
int ret;
|
||||
picture = av_frame_alloc();
|
||||
if (!picture)
|
||||
return NULL;
|
||||
// from now on need to call av_frame_free(&picture) on error
|
||||
picture->format = pix_fmt;
|
||||
picture->width = width;
|
||||
picture->height = height;
|
||||
/* allocate the buffers for the frame data */
|
||||
ret = av_frame_get_buffer(picture, 64);
|
||||
if (ret < 0) {
|
||||
fprintf(stderr, "Could not allocate frame data.\n");
|
||||
av_frame_free(&picture);
|
||||
return NULL;
|
||||
}
|
||||
return picture;
|
||||
} // use av_frame_free(&picture) to free memory from this call
|
||||
|
||||
int open_video(AVFormatContext *oc, VideoOutputStream *ost)
|
||||
{
|
||||
int ret;
|
||||
/* open the codec */
|
||||
ret = avcodec_open2(ost->enc, ost->codec, NULL);
|
||||
if (ret < 0) {
|
||||
fprintf(stderr, "Could not open video codec: %s\n", av_err2str(ret));
|
||||
return ret;
|
||||
} // memory from this call freed when oc is freed, no need to do it on error in this call
|
||||
/* copy the stream parameters to the muxer */
|
||||
ret = avcodec_parameters_from_context(ost->st->codecpar, ost->enc);
|
||||
if (ret < 0) {
|
||||
fprintf(stderr, "Could not copy the stream parameters.\n");
|
||||
return ret;
|
||||
} // memory from this call is freed when oc (parent of ost->st) is freed, no need to do it on error in this call
|
||||
/* allocate and init a re-usable frame */
|
||||
ost->frame = alloc_picture(ost->enc->pix_fmt, ost->enc->width, ost->enc->height);
|
||||
if (!(ost->frame)) {
|
||||
fprintf(stderr, "Could not allocate video frame\n");
|
||||
return -1;
|
||||
} // from now on need to call av_frame_free(&(ost->frame)) on error
|
||||
/* If the output format is not the same as the VNC format, then a temporary VNC format
|
||||
* picture is needed too. It is then converted to the required
|
||||
* output format. */
|
||||
ost->tmp_frame = NULL;
|
||||
ost->sws = NULL;
|
||||
if (ost->enc->pix_fmt != VNC_PIX_FMT) {
|
||||
ost->tmp_frame = alloc_picture(VNC_PIX_FMT, ost->enc->width, ost->enc->height);
|
||||
if (!(ost->tmp_frame)) {
|
||||
fprintf(stderr, "Could not allocate temporary picture\n");
|
||||
av_frame_free(&(ost->frame));
|
||||
return -2;
|
||||
} // from now on need to call av_frame_free(&(ost->tmp_frame)) on error
|
||||
ost->sws = sws_getCachedContext(ost->sws, ost->enc->width, ost->enc->height, VNC_PIX_FMT, ost->enc->width, ost->enc->height, ost->enc->pix_fmt, 0, NULL, NULL, NULL);
|
||||
if (!(ost->sws)) {
|
||||
fprintf(stderr, "Could not get sws context\n");
|
||||
av_frame_free(&(ost->frame));
|
||||
av_frame_free(&(ost->tmp_frame));
|
||||
return -3;
|
||||
} // from now on need to call sws_freeContext(ost->sws); ost->sws = NULL; on error
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* encode current video frame and send it to the muxer
|
||||
* return 0 on success, negative on error
|
||||
*/
|
||||
int write_video_frame(AVFormatContext *oc, VideoOutputStream *ost, int64_t pts)
|
||||
{
|
||||
int ret, ret2;
|
||||
AVPacket pkt = { 0 };
|
||||
if (pts <= ost->pts) return 0; // nothing to do
|
||||
/* convert format if needed */
|
||||
if (ost->tmp_frame) {
|
||||
sws_scale(ost->sws, (const uint8_t * const *)ost->tmp_frame->data,
|
||||
ost->tmp_frame->linesize, 0, ost->enc->height, ost->frame->data, ost->frame->linesize);
|
||||
}
|
||||
|
||||
/* send the imager to encoder */
|
||||
ost->pts = pts;
|
||||
ost->frame->pts = ost->pts;
|
||||
ret = avcodec_send_frame(ost->enc, ost->frame);
|
||||
if (ret < 0) {
|
||||
fprintf(stderr, "Error sending video frame to encoder: %s\n", av_err2str(ret));
|
||||
return ret;
|
||||
}
|
||||
/* read all available packets */
|
||||
ret2 = 0;
|
||||
for (ret = avcodec_receive_packet(ost->enc, &pkt); ret == 0; ret = avcodec_receive_packet(ost->enc, &pkt)) {
|
||||
ret2 = write_packet(oc, &(ost->enc->time_base), ost->st, &pkt);
|
||||
if (ret2 < 0) {
|
||||
fprintf(stderr, "Error while writing video frame: %s\n", av_err2str(ret2));
|
||||
/* continue on this error to not gum up encoder */
|
||||
}
|
||||
}
|
||||
if (ret2 < 0) return ret2;
|
||||
if (!(ret == AVERROR(EAGAIN))) return ret; // if AVERROR(EAGAIN), means all available packets output, need more frames (i.e. success)
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* Write final video frame (i.e. drain codec).
|
||||
*/
|
||||
int write_final_video_frame(AVFormatContext *oc, VideoOutputStream *ost)
|
||||
{
|
||||
int ret, ret2;
|
||||
AVPacket pkt = { 0 };
|
||||
|
||||
/* send NULL image to encoder */
|
||||
ret = avcodec_send_frame(ost->enc, NULL);
|
||||
if (ret < 0) {
|
||||
fprintf(stderr, "Error sending final video frame to encoder: %s\n", av_err2str(ret));
|
||||
return ret;
|
||||
}
|
||||
/* read all available packets */
|
||||
ret2 = 0;
|
||||
for (ret = avcodec_receive_packet(ost->enc, &pkt); ret == 0; ret = avcodec_receive_packet(ost->enc, &pkt)) {
|
||||
ret2 = write_packet(oc, &(ost->enc->time_base), ost->st, &pkt);
|
||||
if (ret2 < 0) {
|
||||
fprintf(stderr, "Error while writing final video frame: %s\n", av_err2str(ret2));
|
||||
/* continue on this error to not gum up encoder */
|
||||
}
|
||||
}
|
||||
if (ret2 < 0) return ret2;
|
||||
if (!(ret == AVERROR(EOF))) return ret;
|
||||
return 0;
|
||||
}
|
||||
|
||||
void close_video_stream(VideoOutputStream *ost)
|
||||
{
|
||||
avcodec_free_context(&(ost->enc));
|
||||
av_frame_free(&(ost->frame));
|
||||
av_frame_free(&(ost->tmp_frame));
|
||||
sws_freeContext(ost->sws); ost->sws = NULL;
|
||||
ost->codec = NULL; /* codec not an allocated item */
|
||||
ost->st = NULL; /* freeing parent oc will free this memory */
|
||||
}
|
||||
|
||||
/**************************************************************/
|
||||
/* Output movie handling */
|
||||
AVFormatContext *movie_open(char *filename, VideoOutputStream *video_st, int br, int fr, int w, int h) {
|
||||
int ret;
|
||||
AVFormatContext *oc;
|
||||
|
||||
/* allocate the output media context. */
|
||||
ret = avformat_alloc_output_context2(&oc, NULL, NULL, filename);
|
||||
if (ret < 0) {
|
||||
fprintf(stderr, "Warning: Could not deduce output format from file extension: using MP4.\n");
|
||||
ret = avformat_alloc_output_context2(&oc, NULL, "mp4", filename);
|
||||
}
|
||||
if (ret < 0) {
|
||||
fprintf(stderr, "Error: Could not allocate media context: %s.\n", av_err2str(ret));
|
||||
return NULL;
|
||||
} // from now on, need to call avformat_free_context(oc); oc=NULL; to free memory on error
|
||||
|
||||
/* Add the video stream using the default format codec and initialize the codec. */
|
||||
if (oc->oformat->video_codec != AV_CODEC_ID_NONE) {
|
||||
ret = add_video_stream(video_st, oc, oc->oformat->video_codec, br, fr, w, h);
|
||||
} else {
|
||||
ret = -1;
|
||||
}
|
||||
if (ret < 0) {
|
||||
fprintf(stderr, "Error: chosen output format does not have a video codec, or error %i\n", ret);
|
||||
avformat_free_context(oc); oc = NULL;
|
||||
return NULL;
|
||||
} // from now on, need to call close_video_stream(video_st) to free memory on error
|
||||
|
||||
/* Now that all the parameters are set, we can open the codecs and allocate the necessary encode buffers. */
|
||||
ret = open_video(oc, video_st);
|
||||
if (ret < 0) {
|
||||
fprintf(stderr, "Error: error opening video codec, error %i\n", ret);
|
||||
close_video_stream(video_st);
|
||||
avformat_free_context(oc); oc = NULL;
|
||||
return NULL;
|
||||
} // no additional calls required to free memory, as close_video_stream(video_st) will do it
|
||||
|
||||
/* open the output file, if needed */
|
||||
if (!(oc->oformat->flags & AVFMT_NOFILE)) {
|
||||
ret = avio_open(&oc->pb, filename, AVIO_FLAG_WRITE);
|
||||
if (ret < 0) {
|
||||
fprintf(stderr, "Could not open '%s': %s\n", filename,
|
||||
av_err2str(ret));
|
||||
close_video_stream(video_st);
|
||||
avformat_free_context(oc); oc = NULL;
|
||||
return NULL;
|
||||
}
|
||||
} // will need to call avio_closep(&oc->pb) to free file handle on error
|
||||
|
||||
/* Write the stream header, if any. */
|
||||
ret = avformat_write_header(oc, NULL);
|
||||
if (ret < 0) {
|
||||
fprintf(stderr, "Error occurred when writing to output file: %s\n",
|
||||
av_err2str(ret));
|
||||
if (!(oc->oformat->flags & AVFMT_NOFILE))
|
||||
avio_closep(&oc->pb);
|
||||
close_video_stream(video_st);
|
||||
avformat_free_context(oc); oc = NULL;
|
||||
} // no additional items to free
|
||||
|
||||
return oc;
|
||||
}
|
||||
|
||||
void movie_close(AVFormatContext **ocp, VideoOutputStream *video_st) {
|
||||
AVFormatContext *oc = *ocp;
|
||||
/* Write the trailer, if any. The trailer must be written before you
|
||||
* close the CodecContexts open when you wrote the header; otherwise
|
||||
* av_write_trailer() may try to use memory that was freed on
|
||||
* av_codec_close(). */
|
||||
if (oc) {
|
||||
if (video_st)
|
||||
write_final_video_frame(oc, video_st);
|
||||
|
||||
av_write_trailer(oc);
|
||||
|
||||
/* Close the video codec. */
|
||||
close_video_stream(video_st);
|
||||
|
||||
if (!(oc->oformat->flags & AVFMT_NOFILE))
|
||||
/* Close the output file. */
|
||||
avio_closep(&oc->pb);
|
||||
|
||||
/* free the stream */
|
||||
avformat_free_context(oc);
|
||||
ocp = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
/**************************************************************/
|
||||
/* VNC globals */
|
||||
VideoOutputStream video_st = { 0 };
|
||||
rfbClient *client = NULL;
|
||||
rfbBool quit = FALSE;
|
||||
char *filename = NULL;
|
||||
AVFormatContext *oc = NULL;
|
||||
int bitrate = 1000000;
|
||||
int framerate = 5;
|
||||
long max_time = 0;
|
||||
struct timespec start_time, cur_time;
|
||||
|
||||
/* Signal handling */
|
||||
void signal_handler(int signal) {
|
||||
quit=TRUE;
|
||||
}
|
||||
|
||||
/* returns time since start in pts units */
|
||||
int64_t time_to_pts(int framerate, struct timespec *start_time, struct timespec *cur_time) {
|
||||
time_t ds = cur_time->tv_sec - start_time->tv_sec;
|
||||
long dns = cur_time->tv_nsec - start_time->tv_nsec;
|
||||
/* use usecs */
|
||||
int64_t dt = (int64_t)ds*(int64_t)1000000+(int64_t)dns/(int64_t)1000;
|
||||
/* compute rv in units of frame number (rounding to nearest, not truncating) */
|
||||
int64_t rv = (((int64_t)framerate)*dt + (int64_t)500000) / (int64_t)(1000000);
|
||||
|
||||
return rv;
|
||||
}
|
||||
|
||||
/* VNC callback functions */
|
||||
rfbBool vnc_malloc_fb(rfbClient* client) {
|
||||
movie_close(&oc, &video_st);
|
||||
oc = movie_open(filename, &video_st, bitrate, framerate, client->width, client->height);
|
||||
if (!oc)
|
||||
return FALSE;
|
||||
signal(SIGINT,signal_handler);
|
||||
signal(SIGTERM,signal_handler);
|
||||
#ifdef SIGQUIT
|
||||
signal(SIGQUIT,signal_handler);
|
||||
#endif
|
||||
signal(SIGABRT,signal_handler);
|
||||
/* These assignments assumes the AVFrame buffer is contigous. This is true in current ffmpeg versions for
|
||||
* most non-HW accelerated bits, but may not be true globally. */
|
||||
if(video_st.tmp_frame)
|
||||
client->frameBuffer=video_st.tmp_frame->data[0];
|
||||
else
|
||||
client->frameBuffer=video_st.frame->data[0];
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
void vnc_update(rfbClient* client,int x,int y,int w,int h) {
|
||||
}
|
||||
|
||||
/**************************************************************/
|
||||
/* media file output */
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
int i,j;
|
||||
|
||||
/* Initialize vnc client structure (don't connect yet). */
|
||||
client = rfbGetClient(5,3,2);
|
||||
client->format.redShift=11; client->format.redMax=31;
|
||||
client->format.greenShift=5; client->format.greenMax=63;
|
||||
client->format.blueShift=0; client->format.blueMax=31;
|
||||
|
||||
/* Initialize libavcodec, and register all codecs and formats. */
|
||||
#if LIBAVUTIL_VERSION_MAJOR < 56 /* deprecrated in FFMPEG 4.0 */
|
||||
av_register_all();
|
||||
#endif
|
||||
|
||||
/* Parse command line. */
|
||||
for(i=1;i<argc;i++) {
|
||||
j=i;
|
||||
if(argc>i+1 && !strcmp("-o",argv[i])) {
|
||||
filename=argv[i+1];
|
||||
j+=2;
|
||||
} else if(argc>i+1 && !strcmp("-t",argv[i])) {
|
||||
max_time=atol(argv[i+1]);
|
||||
if (max_time < 10 || max_time > 100000000) {
|
||||
fprintf(stderr, "Warning: Nonsensical time-per-file %li, resetting to default.\n", max_time);
|
||||
max_time = 0;
|
||||
}
|
||||
j+=2;
|
||||
}
|
||||
/* This is so that argc/argv are ready for passing to rfbInitClient */
|
||||
if(j>i) {
|
||||
argc-=j-i;
|
||||
memmove(argv+i,argv+j,(argc-i)*sizeof(char*));
|
||||
i--;
|
||||
}
|
||||
}
|
||||
|
||||
/* default filename. */
|
||||
if (!filename) {
|
||||
fprintf(stderr, "Warning: No filename specified. Using output.mp4\n");
|
||||
filename = "output.mp4";
|
||||
}
|
||||
|
||||
/* open VNC connection. */
|
||||
client->MallocFrameBuffer=vnc_malloc_fb;
|
||||
client->GotFrameBufferUpdate=vnc_update;
|
||||
if(!rfbInitClient(client,&argc,argv)) {
|
||||
printf("usage: %s [-o output_file] [-t seconds-per-file] server:port\n", argv[0]);
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* main loop */
|
||||
clock_gettime(CLOCK_MONOTONIC, &start_time);
|
||||
while(!quit) {
|
||||
int i=WaitForMessage(client,10000/framerate); /* useful for timeout to be no more than 10 msec per second (=10000/framerate usec) */
|
||||
if (i>0) {
|
||||
if(!HandleRFBServerMessage(client))
|
||||
quit=TRUE;
|
||||
} else if (i<0) {
|
||||
quit=TRUE;
|
||||
}
|
||||
if (!quit) {
|
||||
clock_gettime(CLOCK_MONOTONIC, &cur_time);
|
||||
write_video_frame(oc, &video_st, time_to_pts(framerate, &start_time, &cur_time));
|
||||
if ((cur_time.tv_sec - start_time.tv_sec) > max_time && max_time > 0) {
|
||||
quit = TRUE;
|
||||
}
|
||||
}
|
||||
}
|
||||
movie_close(&oc,&video_st);
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user