allow using local config file for server

This commit is contained in:
lxsang 2018-09-23 14:09:43 +02:00
parent 32e2de083b
commit e05f22063d

75
wvnc.c
View File

@ -1,5 +1,6 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <unistd.h>
#include <rfb/rfbclient.h> #include <rfb/rfbclient.h>
#include <pthread.h> #include <pthread.h>
#ifdef USE_ZLIB #ifdef USE_ZLIB
@ -103,7 +104,7 @@ int jpeg_compress(uint8_t *buff, int w, int h, int components, int quality)
cinfo.image_width = w; cinfo.image_width = w;
cinfo.image_height = h; cinfo.image_height = h;
cinfo.input_components = components; cinfo.input_components = components;
cinfo.in_color_space = components==4?JCS_EXT_RGBA:JCS_RGB; cinfo.in_color_space = components == 4 ? JCS_EXT_RGBA : JCS_RGB;
jpeg_set_defaults(&cinfo); jpeg_set_defaults(&cinfo);
jpeg_set_quality(&cinfo, quality, true); jpeg_set_quality(&cinfo, quality, true);
jpeg_start_compress(&cinfo, true); jpeg_start_compress(&cinfo, true);
@ -117,7 +118,7 @@ int jpeg_compress(uint8_t *buff, int w, int h, int components, int quality)
jpeg_finish_compress(&cinfo); jpeg_finish_compress(&cinfo);
jpeg_destroy_compress(&cinfo); jpeg_destroy_compress(&cinfo);
//LOG("before %d after %d\n", w*h*bbp, ); //LOG("before %d after %d\n", w*h*bbp, );
if(outbuffer_size < w*h*components) if (outbuffer_size < w * h * components)
{ {
memcpy(buff, out, outbuffer_size); memcpy(buff, out, outbuffer_size);
} }
@ -257,7 +258,7 @@ static rfbBool resize(rfbClient *client)
free(client->frameBuffer); free(client->frameBuffer);
client->frameBuffer = (uint8_t *)malloc(width * height * user_data->bbp / 8); client->frameBuffer = (uint8_t *)malloc(width * height * user_data->bbp / 8);
wvnc_pixel_format_t pxf; wvnc_pixel_format_t pxf;
if (!get_pixel_format( user_data->bbp, &pxf)) if (!get_pixel_format(user_data->bbp, &pxf))
{ {
vnc_fatal(user_data, "Cannot get pixel format"); vnc_fatal(user_data, "Cannot get pixel format");
return FALSE; return FALSE;
@ -279,7 +280,7 @@ static rfbBool resize(rfbClient *client)
cmd[2] = (uint8_t)(width >> 8); cmd[2] = (uint8_t)(width >> 8);
cmd[3] = (uint8_t)(height & 0xFF); cmd[3] = (uint8_t)(height & 0xFF);
cmd[4] = (uint8_t)(height >> 8); cmd[4] = (uint8_t)(height >> 8);
cmd[5] = (uint8_t)( user_data->bbp); cmd[5] = (uint8_t)(user_data->bbp);
ws_b(user_data->client, cmd, 6); ws_b(user_data->client, cmd, 6);
uint8_t *ack = (uint8_t *)process(user_data, 1); uint8_t *ack = (uint8_t *)process(user_data, 1);
if (!ack || !(*ack)) if (!ack || !(*ack))
@ -336,10 +337,10 @@ static void update(rfbClient *client, int x, int y, int w, int h)
cmd[8] = (uint8_t)(h >> 8); cmd[8] = (uint8_t)(h >> 8);
#ifdef USE_JPEG #ifdef USE_JPEG
if((components == 3 || components == 4) && (user_data->flag ==1 || user_data->flag == 3)) if ((components == 3 || components == 4) && (user_data->flag == 1 || user_data->flag == 3))
{ {
int ret = jpeg_compress(tmp, w, h, components, user_data->quality); int ret = jpeg_compress(tmp, w, h, components, user_data->quality);
if(ret > 0) if (ret > 0)
{ {
flag |= 0x01; flag |= 0x01;
size = ret; size = ret;
@ -348,7 +349,7 @@ static void update(rfbClient *client, int x, int y, int w, int h)
#endif #endif
#ifdef USE_ZLIB #ifdef USE_ZLIB
if(user_data->flag >= 2) if (user_data->flag >= 2)
{ {
flag |= 0x02; flag |= 0x02;
size = zlib_compress(tmp, size); size = zlib_compress(tmp, size);
@ -422,24 +423,64 @@ static char *get_password(rfbClient *client)
void open_session(void *data, const char *addr) void open_session(void *data, const char *addr)
{ {
// main loop
int argc = 2; int argc = 2;
char *argv[2]; char *argv[2];
argv[0] = "-listennofork"; argv[0] = "-listennofork";
argv[1] = (char *)addr; int len = 0;
FILE *fp = NULL;
char *buffer = NULL;
char c;
wvnc_user_data_t *user_data = get_user_data(data); wvnc_user_data_t *user_data = get_user_data(data);
LOG("client.BBP: %d\n", user_data->bbp ); if (access(addr, F_OK) != -1)
LOG("client.flag: %d\n", user_data->flag ); {
LOG("client.JPEG.quality: %d\n", user_data->quality ); //open the file
LOG("Server: %s\n", addr); fp = fopen(addr, "r");
if (fp == NULL)
{
vnc_fatal(data, "Unable to read server file");
return;
}
// find length of first line
// lines end in "\n", but some malformed text files may not have this char at all
// and whole file contents will be considered as the first line
while ((c = fgetc(fp)) != EOF)
{
if (c == '\n')
{
break;
}
len++;
}
// allocate memory for size of first line (len)
buffer = (char *)malloc(sizeof(char) * len);
// seek to beginning of file
fseek(fp, 0, SEEK_SET);
fread(buffer, sizeof(char), len, fp);
fclose(fp);
argv[1] = buffer;
}
else
{
argv[1] = (char *)addr;
}
LOG("client.BBP: %d\n", user_data->bbp);
LOG("client.flag: %d\n", user_data->flag);
LOG("client.JPEG.quality: %d\n", user_data->quality);
LOG("Server: %s\n", argv[1]);
//LOG("Rate is %d\n", user_data->rate); //LOG("Rate is %d\n", user_data->rate);
if (!rfbInitClient(user_data->vncl, &argc, argv)) if (!rfbInitClient(user_data->vncl, &argc, argv))
{ {
user_data->vncl = NULL; /* rfbInitClient has already freed the client struct */ user_data->vncl = NULL; /* rfbInitClient has already freed the client struct */
//cleanup(vncl); //cleanup(vncl);
vnc_fatal(user_data, "Cannot connect to the server"); vnc_fatal(user_data, "Cannot connect to the server");
if(buffer) free(buffer);
return; return;
} }
if(buffer) free(buffer);
while (1) while (1)
{ {
if (!user_data->status) if (!user_data->status)
@ -530,7 +571,7 @@ void *consume_client(void *ptr, wvnc_cmd_t header)
break; break;
case 0x06: // key board event case 0x06: // key board event
//LOG("Key is %c\n", header.data[0]); //LOG("Key is %c\n", header.data[0]);
SendKeyEvent(user_data->vncl, header.data[0] | (header.data[1] << 8), header.data[2]?TRUE:FALSE); SendKeyEvent(user_data->vncl, header.data[0] | (header.data[1] << 8), header.data[2] ? TRUE : FALSE);
break; break;
case 0x07: case 0x07:
SendClientCutText(user_data->vncl, header.data, strlen(header.data)); SendClientCutText(user_data->vncl, header.data, strlen(header.data));
@ -546,10 +587,10 @@ static void got_clipboard(rfbClient *cl, const char *text, int len)
LOG("received clipboard text '%s'\n", text); LOG("received clipboard text '%s'\n", text);
void *data = rfbClientGetClientData(cl, identify()); void *data = rfbClientGetClientData(cl, identify());
wvnc_user_data_t *user_data = get_user_data(data); wvnc_user_data_t *user_data = get_user_data(data);
uint8_t* cmd = (uint8_t*) malloc(len+1); uint8_t *cmd = (uint8_t *)malloc(len + 1);
cmd[0] = 0x85; cmd[0] = 0x85;
memcpy(cmd+1, text, len); memcpy(cmd + 1, text, len);
ws_b(user_data->client,cmd,len+1); ws_b(user_data->client, cmd, len + 1);
free(cmd); free(cmd);
uint8_t *ack = (uint8_t *)process(user_data, 1); uint8_t *ack = (uint8_t *)process(user_data, 1);
if (!ack || !(*ack)) if (!ack || !(*ack))