~mc-return/compiz/compiz.merge-src-screen.cpp-improvements

« back to all changes in this revision

Viewing changes to plugins/inotify/src/inotify.cpp

  • Committer: Dennis kasprzyk
  • Author(s): Dennis Kasprzyk
  • Date: 2009-03-15 05:09:18 UTC
  • Revision ID: git-v1:163f6b6f3c3b7764987cbdf8e03cc355edeaa499
New generalized build system.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright © 2006 Novell, Inc.
 
3
 *
 
4
 * Permission to use, copy, modify, distribute, and sell this software
 
5
 * and its documentation for any purpose is hereby granted without
 
6
 * fee, provided that the above copyright notice appear in all copies
 
7
 * and that both that copyright notice and this permission notice
 
8
 * appear in supporting documentation, and that the name of
 
9
 * Novell, Inc. not be used in advertising or publicity pertaining to
 
10
 * distribution of the software without specific, written prior permission.
 
11
 * Novell, Inc. makes no representations about the suitability of this
 
12
 * software for any purpose. It is provided "as is" without express or
 
13
 * implied warranty.
 
14
 *
 
15
 * NOVELL, INC. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
 
16
 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN
 
17
 * NO EVENT SHALL NOVELL, INC. BE LIABLE FOR ANY SPECIAL, INDIRECT OR
 
18
 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
 
19
 * OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
 
20
 * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
 
21
 * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 
22
 *
 
23
 * Author: David Reveman <davidr@novell.com>
 
24
 */
 
25
 
 
26
#include "inotify.h"
 
27
 
 
28
COMPIZ_PLUGIN_20081216 (inotify, InotifyPluginVTable)
 
29
 
 
30
InotifyScreen::InotifyScreen (CompScreen *screen) :
 
31
    PrivateHandler<InotifyScreen, CompScreen> (screen)
 
32
{
 
33
    fd = inotify_init ();
 
34
 
 
35
    fdHandle = screen->addWatchFd (fd,
 
36
                                   POLLIN | POLLPRI | POLLHUP | POLLERR,
 
37
                                   boost::bind (&InotifyScreen::processEvents,
 
38
                                                this));
 
39
 
 
40
    ScreenInterface::setHandler (screen, true);
 
41
 
 
42
    const CompFileWatchList           &watchList = screen->getFileWatches ();
 
43
    CompFileWatchList::const_iterator iter;
 
44
 
 
45
    for (iter = watchList.begin (); iter != watchList.end (); ++iter)
 
46
        fileWatchAdded (*iter);
 
47
}
 
48
 
 
49
InotifyScreen::~InotifyScreen ()
 
50
{
 
51
    const CompFileWatchList           &watchList = screen->getFileWatches ();
 
52
    CompFileWatchList::const_iterator iter;
 
53
 
 
54
    for (iter = watchList.begin (); iter != watchList.end (); ++iter)
 
55
        fileWatchRemoved (*iter);
 
56
 
 
57
    screen->removeWatchFd (fdHandle);
 
58
 
 
59
    close (fd);
 
60
}
 
61
 
 
62
void
 
63
InotifyScreen::processEvents ()
 
64
{
 
65
    char buf[256 * (sizeof (struct inotify_event) + 16)];
 
66
    int  len;
 
67
 
 
68
    len = read (fd, buf, sizeof (buf));
 
69
    if (len < 0)
 
70
    {
 
71
        perror ("read");
 
72
    }
 
73
    else
 
74
    {
 
75
        struct inotify_event              *event;
 
76
        int                               i = 0;
 
77
        WatchList::iterator               iter;
 
78
        const CompFileWatchList           &list = screen->getFileWatches ();
 
79
        CompFileWatchList::const_iterator wIter;
 
80
 
 
81
        while (i < len)
 
82
        {
 
83
            event = (struct inotify_event *) &buf[i];
 
84
 
 
85
            for (iter = watches.begin (); iter != watches.end (); ++iter)
 
86
                if ((*iter).wd = event->wd)
 
87
                    break;
 
88
 
 
89
            if (iter != watches.end ())
 
90
            {
 
91
                for (wIter = list.begin (); wIter != list.end (); ++iter)
 
92
                    if ((*iter).handle == (*wIter)->handle)
 
93
                        break;
 
94
 
 
95
                if (wIter != list.end ())
 
96
                {
 
97
                    const char *name = (event->len) ? event->name : NULL;
 
98
                    (*wIter)->callBack (name);
 
99
                }
 
100
            }
 
101
 
 
102
            i += sizeof (*event) + event->len;
 
103
        }
 
104
    }
 
105
}
 
106
 
 
107
static unsigned int
 
108
inotifyMask (CompFileWatch *watch)
 
109
{
 
110
    unsigned int mask = 0;
 
111
 
 
112
    if (watch->mask & NOTIFY_CREATE_MASK)
 
113
        mask |= IN_CREATE;
 
114
 
 
115
    if (watch->mask & NOTIFY_DELETE_MASK)
 
116
        mask |= IN_DELETE;
 
117
 
 
118
    if (watch->mask & NOTIFY_MOVE_MASK)
 
119
        mask |= IN_MOVE;
 
120
 
 
121
    if (watch->mask & NOTIFY_MODIFY_MASK)
 
122
        mask |= IN_MODIFY;
 
123
 
 
124
    return mask;
 
125
}
 
126
 
 
127
void
 
128
InotifyScreen::fileWatchAdded (CompFileWatch *fileWatch)
 
129
{
 
130
    InotifyWatch iw;
 
131
 
 
132
    iw.handle = fileWatch->handle;
 
133
    iw.wd     = inotify_add_watch (fd, fileWatch->path.c_str (),
 
134
                                   inotifyMask (fileWatch));
 
135
    if (iw.wd < 0)
 
136
    {
 
137
        perror ("inotify_add_watch");
 
138
        return;
 
139
    }
 
140
 
 
141
    watches.push_back (iw);
 
142
}
 
143
 
 
144
void
 
145
InotifyScreen::fileWatchRemoved (CompFileWatch *fileWatch)
 
146
{
 
147
    WatchList::iterator iter;
 
148
 
 
149
    for (iter = watches.begin (); iter != watches.end (); ++iter)
 
150
    {
 
151
        if ((*iter).handle == fileWatch->handle)
 
152
        {
 
153
            if (inotify_rm_watch (fd, (*iter).wd))
 
154
                perror ("inotify_rm_watch");
 
155
            watches.erase (iter);
 
156
            break;
 
157
        }
 
158
    }
 
159
}
 
160
 
 
161
bool
 
162
InotifyPluginVTable::init ()
 
163
{
 
164
    if (!CompPlugin::checkPluginABI ("core", CORE_ABIVERSION))
 
165
        return false;
 
166
 
 
167
    getMetadata ()->addFromFile (name ());
 
168
 
 
169
    return true;
 
170
}
 
171