summaryrefslogblamecommitdiffstats
path: root/src/input/inputEvent.cpp
blob: cff1ac8660650e4bb2d42d8de4a2bdf962c200d5 (plain) (tree)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
  












                                                                               



                     
                           







                                                         


                                                  

                                                                   
                                                      


                     


                                               

                                                             
                                                      


                     




                                                                  









                                                       



                                                                 




























                                                                                      
                                                                       
 

                                                         














                                                          
                                                       
 
                                   


                                                   
                                                         
 
                                   

                                                     
 
                                                        
                                                                                                         



                                                                                         






















                                                                                                   

                    
/*
 # Copyright (c) 2009 - OpenSLX Project, Computer Center University of Freiburg
 #
 # This program is free software distributed under the GPL version 2.
 # See http://openslx.org/COPYING
 #
 # If you have any feedback please consult http://openslx.org/feedback and
 # send your suggestions, praise, or complaints to feedback@openslx.org
 #
 # General information about OpenSLX can be found at http://openslx.org/
 # --------------------------------------------------------------------------
 # inputEvent.h:
 #  - Definition of an input event - utility routines
 # --------------------------------------------------------------------------
 */

#include <QBuffer>
#include <QByteArray>
#include <QCoreApplication>
#include <QDataStream>
#include <QKeyEvent>
#include <QMouseEvent>
#include <QString>
#include "inputEvent.h"
#include <src/util/consoleLogger.h>

// We implement operators to serialize and load an event:
/**
 * Serialize an input event to a \ref QDataStream.
 */
QDataStream& operator <<(QDataStream& ostrm, InputEvent const& evt)
{
	ostrm << evt._type << evt._code << evt._value;
	return ostrm;
}

/**
 * Load an input event from a \ref QDataStream.
 */
QDataStream& operator >>(QDataStream& istrm, InputEvent& evt)
{
	istrm >> evt._type >> evt._code >> evt._value;
	return istrm;
}

/**
 * Produce a string-encodeable representation of an input event.
 * This consists of a base64-encoded binary representation as
 * generated by \ref{operator<<(QDataStream&, InputEvent const&)}.
 */
void eventToString(InputEvent const& evt, QString& str)
{
	QByteArray ba;
	QBuffer buf(&ba);
	buf.open(QIODevice::WriteOnly);
	QDataStream out(&buf);
	out << evt;
	str = QString::fromAscii(ba.toBase64());
}

/**
 * Decode the string representation of an input-event produced by
 * \ref eventToString.
 */
bool eventFromString(QString const& str, InputEvent& evt)
{
	// TODO This does not do proper error checking. Only use from trusted sources!
	QByteArray ba = QByteArray::fromBase64(str.toAscii());
	QBuffer buf(&ba);
	buf.open(QIODevice::ReadOnly);
	QDataStream in(&buf);
	in >> evt;
	return true;
}

quint16 InputEvent::mouseButtonsFromQt(int b)
{
	quint16 ret = 0;
	if(b & Qt::LeftButton)
	{
		ret |= EB_LEFT;
	}
	if(b & Qt::RightButton)
	{
		ret |= EB_RIGHT;
	}
	if(b & Qt::MidButton)
	{
		ret |= EB_MIDDLE;
	}
	return ret;
}

InputEvent InputEvent::mousePressRelease(int qt_button, int qt_buttons)
{
	quint16 button = mouseButtonsFromQt(qt_button);
	quint16 buttons = mouseButtonsFromQt(qt_buttons);
	quint16 code;

	if(buttons & button)
	{
		code = EC_PRESS;
	}
	else
	{
		code = EC_RELEASE;
	}

	quint32 value = ((quint32)button << 16) | buttons;
	return InputEvent(ET_BUTTON, code, value);
}

InputEvent InputEvent::keyboardPress(int key, int mods)
{
	quint32 value = key | mods;
	return InputEvent(ET_KEY, EC_PRESS, value);
}

InputEvent InputEvent::keyboardRelease(int key, int mods)
{
	quint32 value = key | mods;
	return InputEvent(ET_KEY, EC_RELEASE, value);
}

#define describe(list, description, type, code, value) \
	list.append(SpecialInputEventDescription(InputEvent::type, InputEvent::code, value, description))

QList<SpecialInputEventDescription> SpecialInputEventDescription::describeSpecialEvents()
{
	QList<SpecialInputEventDescription> list;
	describe(list, InputEvent::tr("Say Hello"), ET_SPECIAL, EC_SAY_HELLO, 0);
	describe(list, InputEvent::tr("Reboot"), ET_SPECIAL, EC_REBOOT, 0);
	describe(list, InputEvent::tr("Kill X Server"), ET_SPECIAL, EC_KILL_X, 0);
	describe(list, InputEvent::tr("Reboot immediately"), ET_SPECIAL, EC_SYSRQ, 'b');
	describe(list, InputEvent::tr("Power off immediately"), ET_SPECIAL, EC_SYSRQ, 'o');
	describe(list, InputEvent::tr("Crash System"), ET_SPECIAL, EC_SYSRQ, 'c');
	describe(list, InputEvent::tr("Turn off raw keyboard mode"), ET_SPECIAL, EC_SYSRQ, 'r');
	describe(list, InputEvent::tr("Send SIGTERM to all"), ET_SPECIAL, EC_SYSRQ, 'e');
	describe(list, InputEvent::tr("Send SIGKILL to all"), ET_SPECIAL, EC_SYSRQ, 'i');
	describe(list, InputEvent::tr("Kill all on this terminal"), ET_SPECIAL, EC_SYSRQ, 'k');
	describe(list, InputEvent::tr("Activate OOM killer"), ET_SPECIAL, EC_SYSRQ, 'f');
	describe(list, InputEvent::tr("Make real-time tasks niceable"), ET_SPECIAL, EC_SYSRQ, 'n');
	describe(list, InputEvent::tr("Force-thaw filesystems"), ET_SPECIAL, EC_SYSRQ, 'j');
	describe(list, InputEvent::tr("Sync all mounted filesystems"), ET_SPECIAL, EC_SYSRQ, 's');
	describe(list, InputEvent::tr("Remount all readonly"), ET_SPECIAL, EC_SYSRQ, 'u');
	describe(list, InputEvent::tr("Show all held locks"), ET_SPECIAL, EC_SYSRQ, 'd');
	describe(list, InputEvent::tr("Show stack traces"), ET_SPECIAL, EC_SYSRQ, 'l');
	describe(list, InputEvent::tr("Dump memory info"), ET_SPECIAL, EC_SYSRQ, 'm');
	describe(list, InputEvent::tr("Dump registers and flags"), ET_SPECIAL, EC_SYSRQ, 'p');
	describe(list, InputEvent::tr("Dump timers and clockevents"), ET_SPECIAL, EC_SYSRQ, 'q');
	describe(list, InputEvent::tr("Dump task list"), ET_SPECIAL, EC_SYSRQ, 't');
	describe(list, InputEvent::tr("Dump uninterruptible tasks"), ET_SPECIAL, EC_SYSRQ, 'w');
	describe(list, InputEvent::tr("Dump ftrace buffer"), ET_SPECIAL, EC_SYSRQ, 'z');
	return list;
}