コード例 #1
0
ファイル: vncviewer.c プロジェクト: GavinPoulton/libvncserver
static rfbBool rfbInitConnection(rfbClient* client)
{
  /* Unless we accepted an incoming connection, make a TCP connection to the
     given VNC server */

  if (!client->listenSpecified) {
    if (!client->serverHost)
      return FALSE;
    if (client->destHost) {
      if (!ConnectToRFBRepeater(client,client->serverHost,client->serverPort,client->destHost,client->destPort))
        return FALSE;
    } else {
      if (!ConnectToRFBServer(client,client->serverHost,client->serverPort))
        return FALSE;
    }
  }

  /* Initialise the VNC connection, including reading the password */

  if (!InitialiseRFBConnection(client))
    return FALSE;

  client->width=client->si.framebufferWidth;
  client->height=client->si.framebufferHeight;
  if (!client->MallocFrameBuffer(client))
    return FALSE;

  if (!SetFormatAndEncodings(client))
    return FALSE;

  if (client->updateRect.x < 0) {
    client->updateRect.x = client->updateRect.y = 0;
    client->updateRect.w = client->width;
    client->updateRect.h = client->height;
  }

  if (client->appData.scaleSetting>1)
  {
      if (!SendScaleSetting(client, client->appData.scaleSetting))
          return FALSE;
      if (!SendFramebufferUpdateRequest(client,
			      client->updateRect.x / client->appData.scaleSetting,
			      client->updateRect.y / client->appData.scaleSetting,
			      client->updateRect.w / client->appData.scaleSetting,
			      client->updateRect.h / client->appData.scaleSetting,
			      FALSE))
	      return FALSE;
  }
  else
  {
      if (!SendFramebufferUpdateRequest(client,
			      client->updateRect.x, client->updateRect.y,
			      client->updateRect.w, client->updateRect.h,
			      FALSE))
      return FALSE;
  }

  return TRUE;
}
コード例 #2
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;
}
コード例 #3
0
ファイル: x2vnc.c プロジェクト: chrisdamato/dotfiles
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;
}
コード例 #4
0
void ConnectionWindow::doConnect()
{
    if (connected())
        return;
    switch ( surfaceType() ) {
    case QVNCVIEWER_SURFACE_RASTER:
    default:
        surfaceWidget()->setDefaultMessage(tr("Connecting..."));
        surfaceWidget()->update();
        break;
    }
    qApp->processEvents(QEventLoop::AllEvents, 100);
    // prepare RFB client structure
    m_rfbClient = rfbGetClient(QVNCVIEWER_BITS_PER_SAMPLE, QVNCVIEWER_SAMPLES_PER_PIXEL, QVNCVIEWER_BYTES_PER_PIXEL);
    m_rfbClient->MallocFrameBuffer = rfbResize;
    m_rfbClient->GotFrameBufferUpdate = rfbUpdate;
    /* FIXME: we currently don't need these functions
       m_rfbClient->FinishedFrameBufferUpdate = rfbUpdateFinished;
       m_rfbClient->GotCopyRect = rfbUpdateCopyRect;
       m_rfbClient->HandleCursorPos = rfbHandleCursorPos;
    */
    m_rfbClient->programName = QVNCVIEWER_APP_TITLE_CSTR;
    m_rfbClient->frameBuffer = 0;
    m_rfbClient->canHandleNewFBSize = true;
    m_rfbClient->canUseCoRRE = true;
    m_rfbClient->canUseHextile = true;
    m_rfbClient->appData.forceTrueColour = true;
    m_rfbClient->appData.useRemoteCursor = true;
    m_rfbClient->appData.enableJPEG = true;
    m_clientToWindowHash[m_rfbClient] = this;
    PollServerThread::setConnecting(true);
    if ( ConnectToRFBServer(m_rfbClient, mHostName.toLocal8Bit().constData(), mDisplayNumber + QVNCVIEWER_VNC_BASE_PORT) ) {
        PollServerThread::setConnecting(false);
        if ( InitialiseRFBConnection(m_rfbClient) ) {
            QString rfbDesktopName(m_rfbClient->desktopName);
            if ( !rfbDesktopName.isEmpty() )
                setWindowTitle(rfbDesktopName);
            VncMainWindow::log(tr("Setting encoding to '%1'").arg(m_currentEncoding));
            m_rfbClient->appData.encodingsString = (const char*)m_currentEncoding.constData();
            m_rfbClient->appData.qualityLevel = 95;
            m_rfbClient->appData.compressLevel = 9;
            if ( !SetFormatAndEncodings(m_rfbClient) )
                VncMainWindow::log(tr("WARNING: Failed sending formats and encondings to %1:%2").arg(mHostName).arg(mDisplayNumber));
            m_rfbClient->width = m_rfbClient->si.framebufferWidth;
            m_rfbClient->height = m_rfbClient->si.framebufferHeight;
            m_rfbClient->frameBuffer = (uint8_t *)malloc(m_rfbClient->width * m_rfbClient->height * QVNCVIEWER_BYTES_PER_PIXEL);
            setConnected(true);
        } else {
            PollServerThread::setConnecting(false);
            setConnected(false);
            ::close(m_rfbClient->sock);
            free(m_rfbClient->frameBuffer);
            m_rfbClient->frameBuffer = 0;
            rfbClientCleanup(m_rfbClient);
            m_clientToWindowHash.remove(m_rfbClient);
            m_rfbClient = 0;
            setWindowTitle(m_defaultWindowTitle);
        }
    } else {
        PollServerThread::setConnecting(false);
        setConnected(false);
        rfbClientCleanup(m_rfbClient);
        m_clientToWindowHash.remove(m_rfbClient);
        m_rfbClient = 0;
        setWindowTitle(m_defaultWindowTitle);
        VncMainWindow::log(tr("WARNING: Failed connection to %1:%2").arg(mHostName).arg(mDisplayNumber));
    }
}
コード例 #5
0
ファイル: vncviewer.c プロジェクト: L3oV1nc3/VMGL
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;
}