int main(int argc, char **argv) { #ifndef NANOX fd_set fds; struct timeval tv, *tvp; int msWait; #endif processArgs(argc, argv); if (listenSpecified) { #ifndef NANOX listenForIncomingConnections(); /* returns only with a succesful connection */ #endif } else { if (!ConnectToRFBServer(hostname, port)) exit(1); } if (!InitialiseRFBConnection(rfbsock)) exit(1); if (!CreateXWindow()) exit(1); if (!SetFormatAndEncodings()) { ShutdownX(); exit(1); } if (!SendFramebufferUpdateRequest(updateRequestX, updateRequestY, updateRequestW, updateRequestH, False)) { ShutdownX(); exit(1); } printf("nanox fd = %d, rfbsock = %d\n", ConnectionNumber(dpy), rfbsock); #ifdef NANOX /* register the RFB socket */ GrRegisterInput(rfbsock); /* call the nanox main loop to wait for all events */ while (True) { GrMainLoop(HandleEvents); } #else while (True) { /* * Always handle all X events before doing select. This is the * simplest way of ensuring that we don't block in select while * Xlib has some events on its queue. */ if (!HandleXEvents()) { ShutdownX(); exit(1); } tvp = NULL; if (sendUpdateRequest) { gettimeofday(&tv, NULL); msWait = (updateRequestPeriodms + ((updateRequestTime.tv_sec - tv.tv_sec) * 1000) + ((updateRequestTime.tv_usec - tv.tv_usec) / 1000)); if (msWait > 0) { tv.tv_sec = msWait / 1000; tv.tv_usec = (msWait % 1000) * 1000; tvp = &tv; } else { if (!SendIncrementalFramebufferUpdateRequest()) { ShutdownX(); exit(1); } } } FD_ZERO(&fds); FD_SET(ConnectionNumber(dpy),&fds); FD_SET(rfbsock,&fds); if (select(FD_SETSIZE, &fds, NULL, NULL, tvp) < 0) { perror("select"); ShutdownX(); exit(1); } if (FD_ISSET(rfbsock, &fds)) { if (!HandleRFBServerMessage()) { ShutdownX(); exit(1); } } } #endif /* NANOX */ return 0; }
int main(int argc, char **argv) { fd_set fds; struct timeval tv, *tvp; int msWait; processArgs(argc, argv); if (listenSpecified) { listenForIncomingConnections(); /* returns only with a succesful connection */ } else { if(reconnect) { long last_fork=0; char *tmpdir="/tmp"; char tmpfile[1024]; if(getenv("TMPDIR") && strlen(tmpdir) < 900) tmpdir=getenv("TMPDIR"); sprintf(tmpfile, "%s/x2vnc-%d-%d", tmpdir, getpid(), time(0)); temp_file_fd=open(tmpfile, O_RDWR | O_CREAT | O_EXCL, 0600); unlink(tmpfile); while(1) { int status, pid; /* limit how often we restart */ if(time(0) - last_fork < 1) sleep(2); last_fork=time(0); switch (pid=fork()) { case -1: perror("fork"); exit(1); case 0: break; default: while(waitpid(pid, &status, 0) < 0 && errno==EINTR); if(debug) fprintf(stderr,"Child exited with status %d\n",status); continue; } break; } } if (!ConnectToRFBServer(hostname, port)) exit(1); } if (!InitialiseRFBConnection(rfbsock)) exit(1); if (!CreateXWindow()) exit(1); if (!SetFormatAndEncodings()) { exit(1); } while (1) { /* * Always handle all X events before doing select. This is the * simplest way of ensuring that we don't block in select while * Xlib has some events on its queue. */ if (!HandleXEvents()) { exit(1); } tv.tv_sec = 5; tv.tv_usec = 0; FD_ZERO(&fds); FD_SET(ConnectionNumber(dpy),&fds); FD_SET(rfbsock,&fds); if (select(FD_SETSIZE, &fds, NULL, NULL, &tv) < 0) { perror("select"); exit(1); } if (FD_ISSET(rfbsock, &fds)) { if (!HandleRFBServerMessage()) { exit(1); } } } return 0; }
rfbBool rfbInitClient(rfbClient* client,int* argc,char** argv) { int i,j; if(argv && argc && *argc) { if(client->programName==0) client->programName=argv[0]; for (i = 1; i < *argc; i++) { j = i; if (strcmp(argv[i], "-listen") == 0) { listenForIncomingConnections(client); break; } else if (strcmp(argv[i], "-listennofork") == 0) { listenForIncomingConnectionsNoFork(client, -1); break; } else if (strcmp(argv[i], "-play") == 0) { client->serverPort = -1; j++; } else if (i+1<*argc && strcmp(argv[i], "-encodings") == 0) { client->appData.encodingsString = argv[i+1]; j+=2; } else if (i+1<*argc && strcmp(argv[i], "-compress") == 0) { client->appData.compressLevel = atoi(argv[i+1]); j+=2; } else if (i+1<*argc && strcmp(argv[i], "-quality") == 0) { client->appData.qualityLevel = atoi(argv[i+1]); j+=2; } else if (i+1<*argc && strcmp(argv[i], "-scale") == 0) { client->appData.scaleSetting = atoi(argv[i+1]); j+=2; } else if (i+1<*argc && strcmp(argv[i], "-qosdscp") == 0) { client->QoS_DSCP = atoi(argv[i+1]); j+=2; } else if (i+1<*argc && strcmp(argv[i], "-repeaterdest") == 0) { char* colon=strchr(argv[i+1],':'); if(client->destHost) free(client->destHost); client->destPort = 5900; client->destHost = strdup(argv[i+1]); if(colon) { client->destHost[(int)(colon-argv[i+1])] = '\0'; client->destPort = atoi(colon+1); } j+=2; } else { char* colon=strchr(argv[i],':'); if(client->serverHost) free(client->serverHost); if(colon) { client->serverHost = strdup(argv[i]); client->serverHost[(int)(colon-argv[i])] = '\0'; client->serverPort = atoi(colon+1); } else { client->serverHost = strdup(argv[i]); } if(client->serverPort >= 0 && client->serverPort < 5900) client->serverPort += 5900; } /* purge arguments */ if (j>i) { *argc-=j-i; memmove(argv+i,argv+j,(*argc-i)*sizeof(char*)); i--; } } } if(!rfbInitConnection(client)) { rfbClientCleanup(client); return FALSE; } return TRUE; }
int main(int argc, char **argv) { int i; programName = argv[0]; /* 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. */ for (i = 1; i < argc; i++) { if (strcmp(argv[i], "-listen") == 0) { listenForIncomingConnections(&argc, argv, i); break; } if (strcmp(argv[i], "-tunnel") == 0 || strcmp(argv[i], "-via") == 0) { if (!createTunnel(&argc, argv, i)) exit(1); break; } } /* Call the main Xt initialisation function. It parses command-line options, generating appropriate resource specs, and makes a connection to the X display. */ toplevel = XtVaAppInitialize(&appContext, "Vncviewer", cmdLineOptions, numCmdLineOptions, &argc, argv, fallback_resources, XtNborderWidth, 0, NULL); dpy = XtDisplay(toplevel); /* Interpret resource specs and process any remaining command-line arguments (i.e. the VNC server name). If the server name isn't specified on the command line, getArgsAndResources() will pop up a dialog box and wait for one to be entered. */ GetArgsAndResources(argc, argv); /* Unless we accepted an incoming connection, make a TCP connection to the given VNC server */ if (!listenSpecified) { if (!ConnectToRFBServer(vncServerHost, vncServerPort)) exit(1); } /* Initialise the VNC connection, including reading the password */ if (!InitialiseRFBConnection()) exit(1); /* Create the "popup" widget - this won't actually appear on the screen until some user-defined event causes the "ShowPopup" action to be invoked */ CreatePopup(); /* Find the best pixel format and X visual/colormap to use */ SetVisualAndCmap(); /* Create the "desktop" widget, and perform initialisation which needs doing before the widgets are realized */ ToplevelInitBeforeRealization(); DesktopInitBeforeRealization(); /* "Realize" all the widgets, i.e. actually create and map their X windows */ XtRealizeWidget(toplevel); /* Perform initialisation that needs doing after realization, now that the X windows exist */ InitialiseSelection(); ToplevelInitAfterRealization(); DesktopInitAfterRealization(); /* Spawn daemon thread to listen to vmgl libs */ if (appData.glStubPort == 7000) appData.glStubPort += (vncServerPort-5900); daemonStart(desktopWin, (unsigned short) appData.glStubPort, NULL); /* Tell the VNC server which pixel format and encodings we want to use */ SetFormatAndEncodings(); /* Now enter the main loop, processing VNC messages. X events will automatically be processed whenever the VNC connection is idle. */ while (1) { if (!HandleRFBServerMessage()) break; } Cleanup(); return 0; }