void X11KeyFaker::connect()
{
    // Open the display.
    dpy = XOpenDisplay(displayName.toLatin1().data());
    if (!dpy) {
	// Try again in a few milliseconds.  Xnest may not be alive yet.
	// Give up after 10 seconds.
	if (++retryCount < 50)
	    QTimer::singleShot(200, this, SLOT(connect()));
	else
	    QTimer::singleShot(0, this, SIGNAL(couldNotConnect()));
	return;
    }

    // Query the XTest extension, which we need to fake the key events.
    int event_base, error_base, major, minor;
    if (!XTestQueryExtension
	    (dpy, &event_base, &error_base, &major, &minor)) {
	XCloseDisplay(dpy);
	dpy = 0;
	QTimer::singleShot(0, this, SIGNAL(couldNotConnect()));
	return;
    }

    // Modify the Xnest's keyboard mappings to add Qtopia's special keysyms.
    int min_keycode = 1, max_keycode = 255;
    XDisplayKeycodes(dpy, &min_keycode, &max_keycode);
    bool ok = true;
    for (KeySym key = QTOPIAXK_Max; key >= QTOPIAXK_Min; --key) {
	// This is an extension keysym, not part of the standard X11 set.
	if (!allocateSpecialKeysym(dpy, min_keycode, max_keycode, key)) {
	    ok = false;
	    break;
	}
    }
    static const KeySym specials[] = {
	XF86XK_Back,		    // Qt::Key_Back
        XF86XK_AudioLowerVolume,    // Qt::Key_VolumeUp
        XF86XK_AudioRaiseVolume,    // Qt::Key_VolumeDown
	XK_F28,			    // Qt::Key_F28
	NoSymbol
    };
    int index = 0;
    while (ok && specials[index] != NoSymbol) {
	// This is a standard X11/XFree86 keysym that Qtopia uses,
	// but it may not be on the user's physical keyboard.
	if (!allocateSpecialKeysym
		(dpy, min_keycode, max_keycode, specials[index]))
	    ok = false;
	++index;
    }
    if (!ok)
	qWarning() << "There are insufficient spare X11 keycodes to allocate the special Qtopia keys";

    // Change the root cursor to something more reasonable than "X".
    Cursor cursor = XCreateFontCursor(dpy, XC_left_ptr);
    XDefineCursor(dpy, RootWindow(dpy, DefaultScreen(dpy)), cursor);

    // Look up the shift keys.
    shiftKeycode = XKeysymToKeycode(dpy, XK_Shift_L);
    if (shiftKeycode == NoSymbol)
	shiftKeycode = XKeysymToKeycode(dpy, XK_Shift_R);
    modeSwitchKeycode = XKeysymToKeycode(dpy, XK_Mode_switch);

    // Make sure all of the above changes are flushed.
    XFlush(dpy);

    // Set up event handling for the display.
    QSocketNotifier *notifier = new QSocketNotifier
	(ConnectionNumber(dpy), QSocketNotifier::Read, this);
    QObject::connect(notifier, SIGNAL(activated(int)), this, SLOT(readyRead()));

    // Make sure the file descriptor is not inherited across exec's.
    fcntl(ConnectionNumber(dpy), F_SETFD, 1);

    // Notify interested parties that we are now connected to the X display.
    QTimer::singleShot(0, this, SIGNAL(connected()));
}
Exemplo n.º 2
0
void NetClientCom::startCommunication()
{
    bool autoReconnect = true;

    // Connect to Host
    socket=new QTcpSocket;
    socket->connectToHost(host, port);

    connect(socket, SIGNAL(disconnected()), this, SLOT(closeSocket()));

    if (!socket->waitForConnected(5000)) {
        cout << "Error: Could not connect to " << host.toStdString() << ":" << port << endl;
        emit couldNotConnect();
        return;
    }

    cout << "Connected to " << host.toStdString() << ":" << port << endl;
    isconnected=true;

    int i=0;
    while (comActive && socket->isOpen()) {

        /*if (autoReconnect && (!socket->isOpen()))
        {
            socket->connectToHost(host, port);
            cout << "RECONNECTING..." << endl;

            if (!socket->waitForConnected())
            {
                isconnected=false;
                comActive=false;
                cout << "Error: Could not reconnect to " << host.toStdString() << ":" << port << endl;
            }
        }*/

        //read queued commands and send to host
        if (writeCmd.size()>0)
        {
            NetCommand cmd = writeCmd.dequeue();
            QString m = cmd.toString().append("\n");
            socket->write(m.toAscii());
            socket->flush();
            cout << "CLIENT CMD OUT: " << cmd.toString().toStdString() << endl;
        }

        //read command from host
        if (socket->waitForReadyRead(100))
        {
            QByteArray data = socket->readAll();
            cout << "----" << endl << QString(data.constData()).toStdString() << "----" << endl;
            QStringList list = QString(data.constData()).trimmed().split("\n");
            for (int i=0; i<list.size(); i++)
            {
                NetCommand cmd = NetCommand(list.at(i));
                cout << "CLIENT CMD IN : " << cmd.toString().toStdString() << endl;
                emit commandReceived(cmd);
            }

        }
    }

    isconnected=false;
    socket->disconnect();
    cout << "client: " << "socket->disconnect();" << endl;
    delete socket;
    emit finished();
    emit lostConnection(comActive);
    cout << "client: " << "delete socket, emit finished()" << endl;
}