~ubuntu-branches/ubuntu/karmic/touchfreeze/karmic

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/*
 *  SynDaemon.cpp
 *
 *  Copyright (C) 2007, 2008  Stefan Kombrink
 *
 *  This file is part of TouchFreeze.
 *
 *  TouchFreeze is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  TouchFreeze is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with touchfreeze; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
 *
 *  On Debian GNU/Linux systems, the complete text of the GNU General
 *  Public License can be found in the /usr/share/common-licenses/GPL file.
 */


#include "SynDaemon.h"
#include <QSettings>

SynDaemon::SynDaemon( QObject* parent ) : QObject( parent )
{
    qDebug() << "SynDaemon::SynDaemon" << endl;

    mBypassTimer.setSingleShot( true );
    connect( &mBypassTimer, SIGNAL( timeout() ), this, SLOT( onStopTyping() ) );
    connect( this, SIGNAL( triggerBypass() ), this, SLOT( onStartTyping() ) );

    // use last saved values    
    rollbackSettings();
    
    mDisplay = NULL;
    mDisplay = XOpenDisplay( NULL );

    mPollingTimer.setInterval( 20 );
    connect( &mPollingTimer, SIGNAL( timeout() ), this, SLOT( checkOnKeyboardActivity() ) );
    
    mPollingTimer.start();
}

void SynDaemon::checkOnKeyboardActivity()
{
    if ( hasKeyboardActivity() )
        emit triggerBypass();
}

void SynDaemon::onStartTyping()
{
    if ( !mBypassTimer.isActive() && isDaemonActive() && isTouchPadActive() )
        emit startTyping();

    mBypassTimer.start();
}

void SynDaemon::onStopTyping()
{
  if ( !hasKeyboardActivity() && isDaemonActive() && isTouchPadActive())
    emit stopTyping();
  else
    mBypassTimer.start();
}

void SynDaemon::setDelay( unsigned int delay )
{
  unsigned int min = 10;
  unsigned int max = 2000;
  mBypassTimer.setInterval( qBound( min, delay, max ) );
}

unsigned int SynDaemon::delay() const
{
  return mBypassTimer.interval();
}

void SynDaemon::setDaemonActive( bool active )
{
    qDebug() << "set daemon" << active;
    mDaemonActive = active;
}

bool SynDaemon::isDaemonActive() const
{
    return mDaemonActive;
}

void SynDaemon::rollbackSettings()
{
    QSettings settings("TouchFreeze");
    setDelay( settings.value( "syndaemon/delay", 500 ).toUInt() );
    setDaemonActive( settings.value( "syndaemon/daemonactive", true ).toBool() );
    setTouchPadActive( settings.value( "syndaemon/touchpadactive", true ).toBool() );  
}

void SynDaemon::commitSettings()
{
  QSettings settings("TouchFreeze");

  settings.setValue("syndaemon/delay",delay());
  settings.setValue("syndaemon/daemonactive",isDaemonActive());
  settings.setValue("syndaemon/touchpadactive",isTouchPadActive());
  
  settings.sync();
}

SynDaemon::~SynDaemon()
{
//  qDebug() << "Syndaemon::~Syndaemon";
  
  if ( !mDisplay )
    XCloseDisplay( mDisplay );
}

bool SynDaemon::hasKeyboardActivity() const
{
    const unsigned int KEYMAP_SIZE=32;
    static unsigned char oldKeyState[KEYMAP_SIZE];
    unsigned char keyState[KEYMAP_SIZE];

    bool result = false;

    XQueryKeymap( mDisplay, (char*)keyState );

    // find pressed keys
    for ( unsigned int i = 0; i < KEYMAP_SIZE; i++ )
    {
        if ( ( keyState[i] & ~oldKeyState[i] ) )
        {
            result = true;
            break;
        }
    }

    // back up key states...
    for ( unsigned int i = 0; i < KEYMAP_SIZE; i++ )
    {
        oldKeyState[i] = keyState[i];
    }

    return result;
}

void SynDaemon::setTouchPadActive( bool active )
{
  qDebug() << "set pad" << active;
  mTouchPadActive = active;
}

bool SynDaemon::isTouchPadActive() const
{
  return mTouchPadActive;
}