summaryrefslogtreecommitdiff
path: root/libopie2/opiecore/ofilenotify.cpp
blob: 11d4f876045e2ac50b9706b5bcb4ad0acd776354 (plain)
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/*
                            This file is part of the Opie Project
             =.             Copyright (C) 2004-2005 Michael 'Mickey' Lauer <mickey@Vanille.de>
           .=l.             Copyright (C) The Opie Team <opie-devel@handhelds.org>
          .>+-=
_;:,     .>    :=|.         This program is free software; you can
.> <`_,   >  .   <=         redistribute it and/or  modify it under
:`=1 )Y*s>-.--   :          the terms of the GNU Library General Public
.="- .-=="i,     .._        License as published by the Free Software
- .   .-<_>     .<>         Foundation; version 2 of the License.
    ._= =}       :
   .%`+i>       _;_.
   .i_,=:_.      -<s.       This program 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
..}^=.=       =       ;     Library General Public License for more
++=   -.     .`     .:      details.
:     =  ...= . :.=-
-.   .:....=;==+<;          You should have received a copy of the GNU
 -_. . .   )=.  =           Library General Public License along with
   --        :-=`           this library; see the file COPYING.LIB.
                            If not, write to the Free Software Foundation,
                            Inc., 59 Temple Place - Suite 330,
                            Boston, MA 02111-1307, USA.
*/

#include "ofilenotify.h"
using namespace Opie::Core;

/* OPIE */

/* QT */
#include <qobject.h>
#include <qsocketnotifier.h>
#include <qsignal.h>
#include <qintdict.h>
#include <qdir.h>

/* STD */
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <assert.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>

static QIntDict<OFileNotification> notification_list;

QSocketNotifier* OFileNotification::_sn;
int OFileNotification::_fd = -1;

#define INOTIFY_DEVICE "/dev/inotify"

namespace Opie {
namespace Core {

OFileNotification::OFileNotification( QObject* parent, const char* name )
                  :QObject( parent, name ), _active( false ), _multi( true )
{
    qDebug( "OFileNotification::OFileNotification()" );
}


OFileNotification::~OFileNotification()
{
    stop();
    qDebug( "OFileNotification::~OFileNotification()" );
}


bool OFileNotification::isActive() const
{
    return _active;
}


int OFileNotification::watch( const QString& path, bool sshot, OFileNotificationType type )
{
    // check if path exists and is a regular file
    struct stat s;
    if ( ::stat( (const char*) path, &s ) == -1 )
    {
        qWarning( "OFileNotification::watch(): Can't watch '%s': %s.", (const char*) path, strerror( errno ) );
        return -1;
    }
    if ( !S_ISREG( s.st_mode ) )
    {
        qWarning( "OFileNotification::watch(): Can't watch '%s': %s.", (const char*) path, "not a regular file" );
        return -1;
    }

    return startWatching( path, sshot, type );
}


int OFileNotification::startWatching( const QString& path, bool sshot, OFileNotificationType type )
{
    if ( notification_list.isEmpty() )
    {
        OFileNotification::registerEventHandler();
    }

    struct inotify_watch_request iwr;
    ::memset( &iwr, 0, sizeof iwr );
    iwr.name = const_cast<char*>( (const char*) path );
    iwr.mask = type;

    _wd = ::ioctl( OFileNotification::_fd, INOTIFY_WATCH, &iwr );

    if ( _wd < 0 )
    {
        qWarning( "OFileNotification::watch(): inotify can't watch '%s': %s.", (const char*) path, strerror( errno ) );
        return -1;
    }

    notification_list.insert( _wd, this );
    _path = path;
    _multi = !sshot;
    _type = type;
    _active = true;
    qDebug( "OFileNotification::watch(): watching '%s' [wd=%d].", (const char*) path, _wd );
    return _wd;
}


void OFileNotification::stop()
{
    notification_list.remove( _wd );
    _path = QString::null;
    _wd = 0;
    _active = false;
    if ( notification_list.isEmpty() )
    {
        OFileNotification::unregisterEventHandler();
    }
}


OFileNotificationType OFileNotification::type() const
{
    return _type;
}


QString OFileNotification::path() const
{
    return _path;
}


bool OFileNotification::activate()
{
    emit triggered( _path );
    _signal.activate();
    if ( !_multi ) stop();
    return true;
}


bool OFileNotification::singleShot( const QString& path, QObject* receiver, const char* member, OFileNotificationType type )
{
    OFileNotification* ofn = new OFileNotification();
    ofn->_signal.connect( receiver, member );
    return ofn->watch( path, true, type ) != -1;
}


void OFileNotification::inotifyEventHandler()
{
    qWarning( "OFileNotification::__eventHandler(): reached." );

    char buffer[16384];
    size_t buffer_i;
    struct inotify_event *pevent, *event;
    ssize_t r;
    size_t event_size;
    int count = 0;

    r = ::read(_fd, buffer, 16384);

    if ( r <= 0 )
        return;

    buffer_i = 0;
    while ( buffer_i < r )
    {
            /* Parse events and queue them ! */
            pevent = (struct inotify_event *)&buffer[buffer_i];
            event_size = sizeof(struct inotify_event) + pevent->len;
            qDebug( "pevent->len = %d\n", pevent->len);

            OFileNotification* fn = notification_list[ pevent->wd ];
            if ( fn )
                fn->activate();
            else
                assert( false );

            //event = malloc(event_size);
            //memmove(event, pevent, event_size);
            //queue_enqueue(event, q);
            buffer_i += event_size;
            count++;
    }

    qDebug( "received %d events...", count );
    return;
}


bool OFileNotification::registerEventHandler()
{
    OFileNotification::_fd = ::open( INOTIFY_DEVICE, O_RDONLY );
    if ( OFileNotification::_fd < 0 )
    {
        qWarning( "OFileNotification::registerEventHandler(): couldn't register event handler: %s", strerror( errno ) );
        return false;
    }

    OFileNotification::_sn = new QSocketNotifier( _fd, QSocketNotifier::Read, this, "inotify event" );
    connect( OFileNotification::_sn, SIGNAL( activated(int) ), this, SLOT( inotifyEventHandler() ) );

    qDebug( "OFileNotification::registerEventHandler(): done" );
    return true;
}


void OFileNotification::unregisterEventHandler()
{
    if ( _sn ) delete _sn;
    if ( OFileNotification::_fd )
    ::close( OFileNotification::_fd );
    qDebug( "OFileNotification::unregisterEventHandler(): done" );
}

//=================================================================================================
// ODirNotification
//=================================================================================================
ODirNotification::ODirNotification( QObject* parent, const char* name )
                  :QObject( parent, name )
{
    qDebug( "ODirNotification::ODirNotification()" );
}


ODirNotification::~ODirNotification()
{
    qDebug( "ODirNotification::~ODirNotification()" );
}


int ODirNotification::watch( const QString& path, bool sshot, OFileNotificationType type, int recurse )
{
    qDebug( "ODirNotification::watch( %s, %d, 0x%08x, %d )", (const char*) path, sshot, type, recurse );

    if ( recurse == 0 )
    {
        OFileNotification* fn = new OFileNotification( this, "ODirNotification delegate" );
        int result = fn->startWatching( path, sshot, type );
        if ( result != -1 )
        {
            connect( fn, SIGNAL( triggered( const QString& ) ), this, SIGNAL( triggered( const QString& ) ) );
            return result;
        }
    }
    else
    {
        qDebug( "ODirNotification::watch(), recursion not yet implemented... :)" );
        return -1;
    }
}

} // namespace Ui

} // namespace Opie