20#include "tdeeventdevice.h" 
   23#include <linux/input.h> 
   25#include <tqsocketnotifier.h> 
   30#include "tdehardwaredevices.h" 
   34#define BITS_PER_LONG (sizeof(long) * 8) 
   35#define NUM_BITS(x) ((((x) - 1) / BITS_PER_LONG) + 1) 
   36#define OFF(x)  ((x) % BITS_PER_LONG) 
   37#define BIT(x)  (1UL << OFF(x)) 
   38#define LONG(x) ((x) / BITS_PER_LONG) 
   39#define BIT_IS_SET(array, bit)  ((array[LONG(bit)] >> OFF(bit)) & 1) 
   41#if defined(WITH_TDEHWLIB_DAEMONS) 
   42#include <tqdbusconnection.h> 
   43#include <tqdbusproxy.h> 
   44#include <tqdbusmessage.h> 
   45#include <tqdbusvariant.h> 
   46#include <tqdbusdata.h> 
   47#include <tqdbusdatalist.h> 
   50TDEEventDevice::TDEEventDevice(TDEGenericDeviceType::TDEGenericDeviceType dt, TQString dn) : TDEGenericDevice(dt, dn) {
 
   53    m_monitorActive = 
false;
 
   56TDEEventDevice::~TDEEventDevice() {
 
   66TDEEventDeviceType::TDEEventDeviceType TDEEventDevice::eventType() {
 
   70void TDEEventDevice::internalSetEventType(TDEEventDeviceType::TDEEventDeviceType et) {
 
   74TDESwitchType::TDESwitchType TDEEventDevice::providedSwitches() {
 
   75    if (!m_monitorActive) {
 
   76        internalReadProvidedSwitches();
 
   78    return m_providedSwitches;
 
   81void TDEEventDevice::internalReadProvidedSwitches() {
 
   82    unsigned long switches[NUM_BITS(EV_CNT)];
 
   86    TDESwitchType::TDESwitchType supportedSwitches = TDESwitchType::Null;
 
   88        r = ioctl(m_fd, EVIOCGBIT(EV_SW, EV_CNT), switches);
 
   90#ifdef WITH_TDEHWLIB_DAEMONS 
   92        TQT_DBusConnection dbusConn = TQT_DBusConnection::addConnection(TQT_DBusConnection::SystemBus);
 
   93        if (dbusConn.isConnected()) {
 
   94            TQT_DBusProxy switchesProxy(
"org.trinitydesktop.hardwarecontrol",
 
   95                "/org/trinitydesktop/hardwarecontrol",
 
   96                "org.trinitydesktop.hardwarecontrol.InputEvents",
 
   98            if (switchesProxy.canSend()) {
 
   99                TQValueList<TQT_DBusData> params;
 
  100                params << TQT_DBusData::fromString(deviceNode().ascii());
 
  101                TQT_DBusMessage reply = switchesProxy.sendWithReply(
"GetProvidedSwitches", params);
 
  102                if (reply.type() == TQT_DBusMessage::ReplyMessage && reply.count() == 1) {
 
  103                    TQValueList<TQ_UINT32> list = reply[0].toList().toUInt32List();
 
  104                    TQValueList<TQ_UINT32>::const_iterator it = list.begin();
 
  105                    for (r = 0; it != list.end(); ++it, r++) {
 
  114        if (BIT_IS_SET(switches, SW_LID)) {
 
  115            supportedSwitches = supportedSwitches | TDESwitchType::Lid;
 
  117        if (BIT_IS_SET(switches, SW_TABLET_MODE)) {
 
  118            supportedSwitches = supportedSwitches | TDESwitchType::TabletMode;
 
  120        if (BIT_IS_SET(switches, SW_RFKILL_ALL)) {
 
  121            supportedSwitches = supportedSwitches | TDESwitchType::RFKill;
 
  123#       if SW_RADIO != SW_RFKILL_ALL  
  124        if (BIT_IS_SET(switches, SW_RADIO)) {
 
  125            supportedSwitches = supportedSwitches | TDESwitchType::Radio;
 
  128        if (BIT_IS_SET(switches, SW_MICROPHONE_INSERT)) {
 
  129            supportedSwitches = supportedSwitches | TDESwitchType::MicrophoneInsert;
 
  131        if (BIT_IS_SET(switches, SW_DOCK)) {
 
  132            supportedSwitches = supportedSwitches | TDESwitchType::Dock;
 
  134        if (BIT_IS_SET(switches, SW_LINEOUT_INSERT)) {
 
  135            supportedSwitches = supportedSwitches | TDESwitchType::LineOutInsert;
 
  137        if (BIT_IS_SET(switches, SW_JACK_PHYSICAL_INSERT)) {
 
  138            supportedSwitches = supportedSwitches | TDESwitchType::JackPhysicalInsert;
 
  140        if (BIT_IS_SET(switches, SW_VIDEOOUT_INSERT)) {
 
  141            supportedSwitches = supportedSwitches | TDESwitchType::VideoOutInsert;
 
  143#       ifdef SW_CAMERA_LENS_COVER 
  144        if (BIT_IS_SET(switches, SW_CAMERA_LENS_COVER)) {
 
  145            supportedSwitches = supportedSwitches | TDESwitchType::CameraLensCover;
 
  148#       ifdef SW_KEYPAD_SLIDE 
  149        if (BIT_IS_SET(switches, SW_KEYPAD_SLIDE)) {
 
  150            supportedSwitches = supportedSwitches | TDESwitchType::KeypadSlide;
 
  153#       ifdef SW_FRONT_PROXIMITY 
  154        if (BIT_IS_SET(switches, SW_FRONT_PROXIMITY)) {
 
  155            supportedSwitches = supportedSwitches | TDESwitchType::FrontProximity;
 
  158#       ifdef SW_ROTATE_LOCK 
  159        if (BIT_IS_SET(switches, SW_ROTATE_LOCK)) {
 
  160            supportedSwitches = supportedSwitches | TDESwitchType::RotateLock;
 
  163#       ifdef SW_LINEIN_INSERT 
  164        if (BIT_IS_SET(switches, SW_LINEIN_INSERT)) {
 
  165            supportedSwitches = supportedSwitches | TDESwitchType::LineInInsert;
 
  169        if (systemPath().contains(
"PNP0C0D")) {
 
  170            supportedSwitches = supportedSwitches | TDESwitchType::Lid;
 
  172        if (systemPath().contains(
"PNP0C0E") || systemPath().contains(
"/LNXSLPBN")) {
 
  173            supportedSwitches = supportedSwitches | TDESwitchType::SleepButton;
 
  175        if (systemPath().contains(
"PNP0C0C") || systemPath().contains(
"/LNXPWRBN")) {
 
  176            supportedSwitches = supportedSwitches | TDESwitchType::PowerButton;
 
  179    m_providedSwitches = supportedSwitches;
 
  182void TDEEventDevice::internalSetProvidedSwitches(TDESwitchType::TDESwitchType sl) {
 
  183    m_providedSwitches = sl;
 
  186TDESwitchType::TDESwitchType TDEEventDevice::activeSwitches() {
 
  187    if (!m_monitorActive) {
 
  188        internalReadActiveSwitches();
 
  190    return m_switchActive;
 
  193void TDEEventDevice::internalReadActiveSwitches() {
 
  194    unsigned long switches[NUM_BITS(EV_CNT)];
 
  197    TDESwitchType::TDESwitchType activeSwitches = TDESwitchType::Null;
 
  199        r = ioctl(m_fd, EVIOCGSW(
sizeof(switches)), switches);
 
  201#ifdef WITH_TDEHWLIB_DAEMONS 
  203        TQT_DBusConnection dbusConn = TQT_DBusConnection::addConnection(TQT_DBusConnection::SystemBus);
 
  204        if (dbusConn.isConnected()) {
 
  205            TQT_DBusProxy switchesProxy(
"org.trinitydesktop.hardwarecontrol",
 
  206                "/org/trinitydesktop/hardwarecontrol",
 
  207                "org.trinitydesktop.hardwarecontrol.InputEvents",
 
  209            if (switchesProxy.canSend()) {
 
  210                TQValueList<TQT_DBusData> params;
 
  211                params << TQT_DBusData::fromString(deviceNode().ascii());
 
  212                TQT_DBusMessage reply = switchesProxy.sendWithReply(
"GetActiveSwitches", params);
 
  213                if (reply.type() == TQT_DBusMessage::ReplyMessage && reply.count() == 1) {
 
  214                    TQValueList<TQ_UINT32> list = reply[0].toList().toUInt32List();
 
  215                    TQValueList<TQ_UINT32>::const_iterator it = list.begin();
 
  216                    for (r = 0; it != list.end(); ++it, r++) {
 
  225        if (BIT_IS_SET(switches, SW_LID)) {
 
  226            activeSwitches = activeSwitches | TDESwitchType::Lid;
 
  228        if (BIT_IS_SET(switches, SW_TABLET_MODE)) {
 
  229            activeSwitches = activeSwitches | TDESwitchType::TabletMode;
 
  231        if (BIT_IS_SET(switches, SW_RFKILL_ALL)) {
 
  232            activeSwitches = activeSwitches | TDESwitchType::RFKill;
 
  234#       if SW_RADIO != SW_RFKILL_ALL  
  235        if (BIT_IS_SET(switches, SW_RADIO)) {
 
  236            activeSwitches = activeSwitches | TDESwitchType::Radio;
 
  239        if (BIT_IS_SET(switches, SW_MICROPHONE_INSERT)) {
 
  240            activeSwitches = activeSwitches | TDESwitchType::MicrophoneInsert;
 
  242        if (BIT_IS_SET(switches, SW_DOCK)) {
 
  243            activeSwitches = activeSwitches | TDESwitchType::Dock;
 
  245        if (BIT_IS_SET(switches, SW_LINEOUT_INSERT)) {
 
  246            activeSwitches = activeSwitches | TDESwitchType::LineOutInsert;
 
  248        if (BIT_IS_SET(switches, SW_JACK_PHYSICAL_INSERT)) {
 
  249            activeSwitches = activeSwitches | TDESwitchType::JackPhysicalInsert;
 
  251        if (BIT_IS_SET(switches, SW_VIDEOOUT_INSERT)) {
 
  252            activeSwitches = activeSwitches | TDESwitchType::VideoOutInsert;
 
  254#       ifdef SW_CAMERA_LENS_COVER 
  255        if (BIT_IS_SET(switches, SW_CAMERA_LENS_COVER)) {
 
  256            activeSwitches = activeSwitches | TDESwitchType::CameraLensCover;
 
  259#       ifdef SW_KEYPAD_SLIDE 
  260        if (BIT_IS_SET(switches, SW_KEYPAD_SLIDE)) {
 
  261            activeSwitches = activeSwitches | TDESwitchType::KeypadSlide;
 
  264#       ifdef SW_FRONT_PROXIMITY 
  265        if (BIT_IS_SET(switches, SW_FRONT_PROXIMITY)) {
 
  266            activeSwitches = activeSwitches | TDESwitchType::FrontProximity;
 
  269#       ifdef SW_ROTATE_LOCK 
  270        if (BIT_IS_SET(switches, SW_ROTATE_LOCK)) {
 
  271            activeSwitches = activeSwitches | TDESwitchType::RotateLock;
 
  274#       ifdef SW_LINEIN_INSERT 
  275        if (BIT_IS_SET(switches, SW_LINEIN_INSERT)) {
 
  276            activeSwitches = activeSwitches | TDESwitchType::LineInInsert;
 
  280    m_switchActive = activeSwitches;
 
  283void TDEEventDevice::internalSetActiveSwitches(TDESwitchType::TDESwitchType sl) {
 
  288TQStringList TDEEventDevice::friendlySwitchList(TDESwitchType::TDESwitchType switches) {
 
  291    if (switches & TDESwitchType::Lid) {
 
  292        ret.append(
i18n(
"Lid Switch"));
 
  294    if (switches & TDESwitchType::TabletMode) {
 
  295        ret.append(
i18n(
"Tablet Mode"));
 
  297    if (switches & TDESwitchType::HeadphoneInsert) {
 
  298        ret.append(
i18n(
"Headphone Inserted"));
 
  300    if (switches & TDESwitchType::RFKill) {
 
  301        ret.append(
i18n(
"Radio Frequency Device Kill Switch"));
 
  303    if (switches & TDESwitchType::Radio) {
 
  304        ret.append(
i18n(
"Enable Radio"));
 
  306    if (switches & TDESwitchType::MicrophoneInsert) {
 
  307        ret.append(
i18n(
"Microphone Inserted"));
 
  309    if (switches & TDESwitchType::Dock) {
 
  310        ret.append(
i18n(
"Docked"));
 
  312    if (switches & TDESwitchType::LineOutInsert) {
 
  313        ret.append(
i18n(
"Line Out Inserted"));
 
  315    if (switches & TDESwitchType::JackPhysicalInsert) {
 
  316        ret.append(
i18n(
"Physical Jack Inserted"));
 
  318    if (switches & TDESwitchType::VideoOutInsert) {
 
  319        ret.append(
i18n(
"Video Out Inserted"));
 
  321    if (switches & TDESwitchType::CameraLensCover) {
 
  322        ret.append(
i18n(
"Camera Lens Cover"));
 
  324    if (switches & TDESwitchType::KeypadSlide) {
 
  325        ret.append(
i18n(
"Keypad Slide"));
 
  327    if (switches & TDESwitchType::FrontProximity) {
 
  328        ret.append(
i18n(
"Front Proximity"));
 
  330    if (switches & TDESwitchType::RotateLock) {
 
  331        ret.append(
i18n(
"Rotate Lock"));
 
  333    if (switches & TDESwitchType::LineInInsert) {
 
  334        ret.append(
i18n(
"Line In Inserted"));
 
  336    if (switches & TDESwitchType::PowerButton) {
 
  337        ret.append(
i18n(
"Power Button"));
 
  339    if (switches & TDESwitchType::SleepButton) {
 
  340        ret.append(
i18n(
"Sleep Button"));
 
  346void TDEEventDevice::internalStartMonitoring(TDEHardwareDevices* hwmanager) {
 
  347    if (!m_monitorActive) {
 
  349        if (eventType() != TDEEventDeviceType::Unknown) {
 
  351                m_eventNotifier = 
new TQSocketNotifier(m_fd, TQSocketNotifier::Read, 
this);
 
  352                connect( m_eventNotifier, TQ_SIGNAL(activated(
int)), 
this, TQ_SLOT(eventReceived()) );
 
  353                m_monitorActive = 
true;
 
  356        if (m_monitorActive == 
true) {
 
  358            internalReadProvidedSwitches();
 
  359            internalReadActiveSwitches();
 
  360            connect( 
this, TQ_SIGNAL(keyPressed(
unsigned int, TDEEventDevice*)), hwmanager, TQ_SLOT(processEventDeviceKeyPressed(
unsigned int, TDEEventDevice*)) );
 
  365void TDEEventDevice::eventReceived() {
 
  366    struct input_event ev;
 
  368    r = read(m_fd, &ev, 
sizeof(
struct input_event));
 
  370        if ((ev.type == EV_KEY) && (ev.value == 1)) {   
 
  371            emit keyPressed(ev.code, 
this);
 
  373        if (ev.type == EV_SW) {
 
  374            internalReadActiveSwitches();
 
  375            emit switchChanged();
 
  380void TDEEventDevice::processActiveSwitches() {
 
  381    TDESwitchType::TDESwitchType previousSwitches = m_switchActive;
 
  382    internalReadActiveSwitches();
 
  384    if (previousSwitches != m_switchActive) {
 
  385        emit switchChanged();
 
  389void TDEEventDevice::connectNotify( 
const char* signal ) {
 
  390    if( !m_monitorActive && qstrcmp( signal, TQ_SIGNAL(switchChanged())) == 0 ) {
 
  391        m_watchTimer = 
new TQTimer(
this);
 
  392        connect( m_watchTimer, TQ_SIGNAL(timeout()), 
this, TQ_SLOT(processActiveSwitches()) );
 
  393        m_watchTimer->start( 2500, 
false );
 
  394        m_monitorActive = 
true;
 
  397        internalReadProvidedSwitches();
 
  398        internalReadActiveSwitches();
 
  400    TQObject::connectNotify( signal );
 
  403#include "tdeeventdevice.moc" 
TQString i18n(const char *text)
i18n is the function that does everything you need to translate a string.
 
TDEAction * close(const TQObject *recvr, const char *slot, TDEActionCollection *parent, const char *name=0)