summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorSam Spilsbury <sam.spilsbury@canonical.com>2010-11-20 10:07:46 +0800
committerSam Spilsbury <sam.spilsbury@canonical.com>2010-11-20 10:07:46 +0800
commitd31d55fa24835dde6c4949adf223dc83ab4c2b6f (patch)
tree6eb886da06217529c4f371426767899e1e83890e /src
parentc26f860f5e70c20e32a6b2e7b9c611c4f288f89e (diff)
downloadcompiz-with-glib-mainloop-d31d55fa24835dde6c4949adf223dc83ab4c2b6f.tar.gz
compiz-with-glib-mainloop-d31d55fa24835dde6c4949adf223dc83ab4c2b6f.tar.bz2
Port C based glib to C++ glibmm
Diffstat (limited to 'src')
-rw-r--r--src/privatescreen.h35
-rw-r--r--src/screen.cpp184
-rw-r--r--src/timer.cpp12
3 files changed, 122 insertions, 109 deletions
diff --git a/src/privatescreen.h b/src/privatescreen.h
index 79b5442..ad95351 100644
--- a/src/privatescreen.h
+++ b/src/privatescreen.h
@@ -35,7 +35,7 @@
#include <core/timer.h>
#include <core/plugin.h>
-#include <glib.h>
+#include <glibmm/main.h>
#include "core_options.h"
@@ -104,6 +104,33 @@ struct CompStartupSequence {
unsigned int viewportY;
};
+class CompEventSource:
+ public Glib::Source
+{
+ public:
+
+ static
+ Glib::RefPtr <CompEventSource> create ();
+
+ sigc::connection connect (const sigc::slot <bool> &slot);
+
+ protected:
+
+ bool prepare (int &timeout);
+ bool check ();
+ bool dispatch (sigc::slot_base *slot);
+ bool callback ();
+
+ explicit CompEventSource ();
+ virtual ~CompEventSource ();
+
+ private:
+
+ Display *mDpy;
+ Glib::PollFD mPollFD;
+ int mConnectionFD;
+};
+
class PrivateScreen : public CoreOptions {
public:
@@ -303,8 +330,10 @@ class PrivateScreen : public CoreOptions {
public:
PrivateScreen *priv;
-
- GMainLoop *loop;
+
+ Glib::RefPtr <Glib::MainLoop> mainloop;
+ Glib::RefPtr <CompEventSource> source;
+ Glib::RefPtr <Glib::MainContext> ctx;
CompFileWatchList fileWatch;
CompFileWatchHandle lastFileWatchHandle;
diff --git a/src/screen.cpp b/src/screen.cpp
index 76c2ba1..b9d59b5 100644
--- a/src/screen.cpp
+++ b/src/screen.cpp
@@ -41,9 +41,6 @@
#include <poll.h>
#include <algorithm>
-#include <glib.h>
-#include <glib-object.h>
-
#include <boost/bind.hpp>
#include <boost/foreach.hpp>
#define foreach BOOST_FOREACH
@@ -113,115 +110,86 @@ CompScreen::freePluginClassIndex (unsigned int index)
screen->pluginClasses.resize (screenPluginClassIndices.size ());
}
-
-struct CompizEventQueue
+Glib::RefPtr <CompEventSource>
+CompEventSource::create ()
{
- GSource source;
-
- Display *display;
- GPollFD pollFd;
- int connectionFd;
-};
-
-/* XXX:
- * We should use GlibMM here and avoid mixing C and C++ linkage
- */
+ return Glib::RefPtr <CompEventSource> (new CompEventSource ());
+}
-extern "C"
+sigc::connection
+CompEventSource::connect (const sigc::slot <bool> &slot)
{
- static gboolean
- processCallback (CompScreen *screen)
- {
- screen->processEvents ();
- return TRUE;
- }
+ return connect_generic (slot);
}
-static gboolean
-gsourcePrepare (GSource *source, gint *timeout)
+CompEventSource::CompEventSource () :
+ Glib::Source (),
+ mDpy (screen->dpy ()),
+ mConnectionFD (ConnectionNumber (screen->dpy ()))
{
- CompizEventQueue *ceq = (CompizEventQueue *) source;
+ mPollFD.set_fd (mConnectionFD);
+ mPollFD.set_events (Glib::IO_IN);
+
+ set_priority (G_PRIORITY_DEFAULT);
+ add_poll (mPollFD);
+ set_can_recurse (true);
- *timeout = -1;
- return XPending (ceq->display);
+ connect (sigc::mem_fun <bool, CompEventSource> (this, &CompEventSource::callback));
}
-static gboolean
-gsourceCheck (GSource *source)
+CompEventSource::~CompEventSource ()
{
- CompizEventQueue *ceq;
-
- ceq = (CompizEventQueue *) source;
+}
- if (ceq->pollFd.revents & G_IO_IN)
- return XPending (ceq->display);
+bool
+CompEventSource::callback ()
+{
+ if (restartSignal || shutDown)
+ {
+ screen->priv->source->destroy ();
+ screen->priv->source.reset ();
+ screen->priv->mainloop->quit ();
+ }
else
- return false;
+ screen->priv->processEvents ();
+ return true;
}
-static gboolean
-gsourceDispatch (GSource *source, GSourceFunc callback, gpointer data)
+bool
+CompEventSource::prepare (int &timeout)
{
- return callback (data);
+ timeout = -1;
+ return XPending (mDpy);
}
-static void
-gsourceDestroy (GSource *source)
+bool
+CompEventSource::check ()
{
-}
+ if (mPollFD.get_revents () & Glib::IO_IN)
+ return XPending (mDpy);
-static GSourceFuncs gsourceFuncs = {
- gsourcePrepare,
- gsourceCheck,
- gsourceDispatch,
- gsourceDestroy
-};
+ return false;
+}
-void
-CompScreen::processEvents ()
+bool
+CompEventSource::dispatch (sigc::slot_base *slot)
{
- if (restartSignal || shutDown)
- g_main_loop_quit (priv->loop);
- else
- priv->processEvents ();
+ return (*static_cast <sigc::slot <bool> *> (slot)) ();
}
void
CompScreen::eventLoop ()
{
- int fd;
- GSource *source;
- GMainContext *ctx;
- CompizEventQueue *ceq;
-
- g_type_init ();
-
- ctx = g_main_context_default ();
-
- priv->loop = g_main_loop_new (ctx, false);
-
- source = g_source_new (&gsourceFuncs, sizeof (CompizEventQueue));
- ceq = (CompizEventQueue*) source;
-
- fd = ConnectionNumber (priv->dpy);
- ceq->connectionFd = fd;
- ceq->pollFd.fd = fd;
- ceq->pollFd.events = G_IO_IN;
- ceq->display = priv->dpy;
+ priv->ctx = Glib::MainContext::get_default ();
+ priv->mainloop = Glib::MainLoop::create (priv->ctx, false);
+ priv->source = CompEventSource::create ();
- g_source_set_priority (source, G_PRIORITY_DEFAULT);
- g_source_add_poll (source, &ceq->pollFd);
- g_source_set_can_recurse (source, TRUE);
-
- g_source_set_callback (source, (GSourceFunc) processCallback, this, NULL);
-
- g_source_attach (source, NULL);
- g_source_unref (source);
+ priv->source->attach (priv->ctx);
/* Kick the event loop */
- g_main_context_iteration (ctx, false);
+ priv->ctx->iteration (false);
- g_main_loop_run (priv->loop);
+ priv->mainloop->run ();
}
CompFileWatchHandle
@@ -275,33 +243,36 @@ CompScreen::getFileWatches () const
return priv->fileWatch;
}
-static unsigned int executingId = 0;
-static bool forceFail = false;
-
-gboolean
-onTimerTimeout (CompTimer *timer)
+bool
+CompTimer::internalCallback ()
{
bool result;
- if (!timer->active ())
- return true;
+ if (!mActive)
+ return false;
+
+ mForceFail = false;
+ mExecuting = true;
- forceFail = false;
- executingId = timer->mId;
+ result = mCallBack ();
- result = timer->mCallBack ();
+ mExecuting = false;
- if (forceFail)
+ if (mForceFail)
return false;
if (result)
{
- timer->tick ();
+ tick ();
return true;
}
else
{
- timer->mId = 0;
+ if (mSource)
+ {
+ mSource->destroy ();
+ mSource.reset ();
+ }
return false;
}
}
@@ -309,27 +280,34 @@ onTimerTimeout (CompTimer *timer)
void
PrivateScreen::addTimer (CompTimer *timer)
{
- if (timer->mId != 0)
+ if (timer->mSource)
return;
unsigned int time = timer->mMinTime;
- timer->mId = g_timeout_add (time, (GSourceFunc) onTimerTimeout, timer);
+ timer->mSource = Glib::TimeoutSource::create (time);
- timer->tick ();
+ if (timer->mSource)
+ {
+ timer->mSource->attach (priv->ctx);
+ timer->mSource->connect (sigc::mem_fun <bool, CompTimer> (timer, &CompTimer::internalCallback));
+ timer->tick ();
+ }
+ else
+ timer->mActive = false;
}
void
PrivateScreen::removeTimer (CompTimer *timer)
{
- if (timer->mId == 0)
+ if (!timer->mSource)
return;
- if (executingId == timer->mId)
- forceFail = true;
+ if (timer->mExecuting)
+ timer->mForceFail = true;
- g_source_remove (timer->mId);
- timer->mId = 0;
+ timer->mSource->destroy ();
+ timer->mSource.reset (); /* This will NULL the pointer */
}
CompWatchFdHandle
diff --git a/src/timer.cpp b/src/timer.cpp
index 964f932..793dad4 100644
--- a/src/timer.cpp
+++ b/src/timer.cpp
@@ -34,7 +34,9 @@ CompTimer::CompTimer () :
mMinLeft (0),
mMaxLeft (0),
mCallBack (NULL),
- mId (0)
+ mForceFail (false),
+ mExecuting (false),
+ mSource (NULL)
{
}
@@ -81,8 +83,10 @@ CompTimer::start ()
return;
}
+ if (!mActive)
+ screen->priv->addTimer (this);
+
mActive = true;
- screen->priv->addTimer (this);
}
void
@@ -106,8 +110,10 @@ CompTimer::start (CompTimer::CallBack callback,
void
CompTimer::stop ()
{
+ if (mActive)
+ screen->priv->removeTimer (this);
+
mActive = false;
- screen->priv->removeTimer (this);
}
unsigned int