From 194a76df115db3b6fe62adbccfe4181ed46d2c52 Mon Sep 17 00:00:00 2001 From: dscho Date: Thu, 6 Oct 2005 19:26:41 +0000 Subject: add an extension mechanism for LibVNCClient, modify the client data handling so that more than one data structure can be attached, and add an example to speak the client part of the back channel. --- client_examples/Makefile.am | 2 +- client_examples/SDLvncviewer.c | 6 +-- client_examples/backchannel.c | 99 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 103 insertions(+), 4 deletions(-) create mode 100644 client_examples/backchannel.c (limited to 'client_examples') diff --git a/client_examples/Makefile.am b/client_examples/Makefile.am index 6ffcf51..77998f0 100644 --- a/client_examples/Makefile.am +++ b/client_examples/Makefile.am @@ -23,7 +23,7 @@ SDLvncviewer_CFLAGS=$(SDL_CFLAGS) SDLvncviewer_LDADD=$(LDADD) $(SDL_LIBS) endif -noinst_PROGRAMS=ppmtest $(SDLVIEWER) $(FFMPEG_CLIENT) +noinst_PROGRAMS=ppmtest $(SDLVIEWER) $(FFMPEG_CLIENT) backchannel diff --git a/client_examples/SDLvncviewer.c b/client_examples/SDLvncviewer.c index 07d3869..8375ea6 100644 --- a/client_examples/SDLvncviewer.c +++ b/client_examples/SDLvncviewer.c @@ -16,7 +16,7 @@ static rfbBool resize(rfbClient* client) { okay=SDL_VideoModeOK(width,height,depth,flags); if(okay) { SDL_Surface* sdl=SDL_SetVideoMode(width,height,depth,flags); - client->clientData=sdl; + rfbClientSetClientData(client, SDL_Init, sdl); client->frameBuffer=sdl->pixels; if(first || depth!=client->format.bitsPerPixel) { first=FALSE; @@ -30,7 +30,7 @@ static rfbBool resize(rfbClient* client) { SetFormatAndEncodings(client); } } else { - SDL_Surface* sdl=client->clientData; + SDL_Surface* sdl=rfbClientGetClientData(client, SDL_Init); rfbClientLog("Could not set resolution %dx%d!\n", client->width,client->height); if(sdl) { @@ -188,7 +188,7 @@ static rfbKeySym SDL_keysym2rfbKeySym(int keysym) { } static void update(rfbClient* cl,int x,int y,int w,int h) { - SDL_UpdateRect(cl->clientData, x, y, w, h); + SDL_UpdateRect(rfbClientGetClientData(cl, SDL_Init), x, y, w, h); } #ifdef __MINGW32__ diff --git a/client_examples/backchannel.c b/client_examples/backchannel.c new file mode 100644 index 0000000..643754e --- /dev/null +++ b/client_examples/backchannel.c @@ -0,0 +1,99 @@ +/* A simple example of an RFB client */ + +#include +#include +#include +#include +#include + +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 */ +}; + +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; +} + -- cgit v1.2.3