예제 #1
0
파일: vncviewer.c 프로젝트: LucidOne/Rovio
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;
}
예제 #2
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;
}