GLib - main event loop

383 views
Skip to first unread message

Lukas Adamec

unread,
Sep 23, 2010, 12:03:27 AM9/23/10
to android-ndk
Hello,
I need to implement my own main event loop in C++ which will be based
on GLib library. I don't know where to begin. I studied some materials
about GLib, but it doesn't help me to know, how implement event loop.
Could somebody give me some advise about it or give me some source
code? I basically need to implement GSource and GSourceFuncs from
GLib. I can't find how event loop is implemented in Android, could
somebody explain it to me too? Thanks a lot.

Lukas

Dianne Hackborn

unread,
Sep 23, 2010, 6:37:44 PM9/23/10
to andro...@googlegroups.com
There are no event loops in the NDK; you need to implement them yourself from the POSIX primitives just as GLib and others do.


--
You received this message because you are subscribed to the Google Groups "android-ndk" group.
To post to this group, send email to andro...@googlegroups.com.
To unsubscribe from this group, send email to android-ndk...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/android-ndk?hl=en.




--
Dianne Hackborn
Android framework engineer
hac...@android.com

Note: please don't send private questions to me, as I don't have time to provide private support, and so won't reply to such e-mails.  All such questions should be posted on public forums, where I and others can see and answer them.

Lukas Adamec

unread,
Sep 24, 2010, 10:03:00 AM9/24/10
to android-ndk
I have another question: Where to find how event loop is implemented
on Android? Is there any documentation about it?
Thanks

On 24 zář, 00:37, Dianne Hackborn <hack...@android.com> wrote:
> There are no event loops in the NDK; you need to implement them yourself
> from the POSIX primitives just as GLib and others do.
>
>
>
> On Wed, Sep 22, 2010 at 9:03 PM, Lukas Adamec <luk.ada...@gmail.com> wrote:
> > Hello,
> > I need to implement my own main event loop in C++ which will be based
> > on GLib library. I don't know where to begin. I studied some materials
> > about GLib, but it doesn't help me to know, how implement event loop.
> > Could somebody give me some advise about it or give me some source
> > code? I basically need to implement GSource and GSourceFuncs from
> > GLib. I can't find how event loop is implemented in Android, could
> > somebody explain it to me too? Thanks a lot.
>
> > Lukas
>
> > --
> > You received this message because you are subscribed to the Google Groups
> > "android-ndk" group.
> > To post to this group, send email to andro...@googlegroups.com.
> > To unsubscribe from this group, send email to
> > android-ndk...@googlegroups.com<android-ndk%2Bunsu...@googlegroups.com>
> > .
> > For more options, visit this group at
> >http://groups.google.com/group/android-ndk?hl=en.
>
> --
> Dianne Hackborn
> Android framework engineer
> hack...@android.com

Prakash Iyer

unread,
Sep 24, 2010, 10:13:25 AM9/24/10
to andro...@googlegroups.com
This is a very open question. If you are talking about messages and delivery in Java you should read up the SDK - it's actually decently documented and I might recommend understanding Handler class well. If you are into UI, then knowing how that is rendered in the main thread only would be quite useful.

In the NDK there is no concept of event loop as Dianner pointed out. So if you want to pass events between threads/processes, use pipes, sockets etc. POSIX documentation is your friend here. You might want to read the OVERVIEW in the lib directory of the NDK to make sure you understand some limitations (nothing relevant here but you might want to know)...

To unsubscribe from this group, send email to android-ndk...@googlegroups.com.

Lukas Adamec

unread,
Sep 26, 2010, 1:30:30 AM9/26/10
to android-ndk
I'd like to know, how the main event loop is implemented in Android
source code, because I need to implement my own event loop in C++ and
Android implementation could be useful for me to help with C++
implementation.

On 24 zář, 16:13, Prakash Iyer <thei...@gmail.com> wrote:
> This is a very open question. If you are talking about messages and delivery
> in Java you should read up the SDK - it's actually decently documented and I
> might recommend understanding Handler class well. If you are into UI, then
> knowing how that is rendered in the main thread only would be quite useful.
>
> In the NDK there is no concept of event loop as Dianner pointed out. So if
> you want to pass events between threads/processes, use pipes, sockets etc.
> POSIX documentation is your friend here. You might want to read the OVERVIEW
> in the lib directory of the NDK to make sure you understand some limitations
> (nothing relevant here but you might want to know)...
>
> > <android-ndk%2Bunsu...@googlegroups.com<android-ndk%252Buns...@googlegroups.com>

Tim Mensch

unread,
Sep 26, 2010, 11:19:15 PM9/26/10
to andro...@googlegroups.com
On 9/25/2010 11:30 PM, Lukas Adamec wrote:
> I'd like to know, how the main event loop is implemented in Android
> source code, because I need to implement my own event loop in C++ and
> Android implementation could be useful for me to help with C++
> implementation.
I think you're expecting a certain programming paradigm (the Windows
message pump) that doesn't exist on Android. The "events" are dispatched
directly to classes like android.view.View(), or to listeners that they
register.

Tim

Lukas Adamec

unread,
Sep 27, 2010, 12:51:45 AM9/27/10
to android-ndk
You are right, I meant something like Windows pump, peek messages. I
wanted to implement GLib's functions prepare(), check(), dispatch() in
C++ to working with events. So is there any possibility to do this?

Tim Mensch

unread,
Sep 27, 2010, 1:29:22 AM9/27/10
to andro...@googlegroups.com
On 9/26/2010 10:51 PM, Lukas Adamec wrote:
> You are right, I meant something like Windows pump, peek messages. I
> wanted to implement GLib's functions prepare(), check(), dispatch() in
> C++ to working with events. So is there any possibility to do this?
You'd need to collect anything you'd want to handle as an "event" and
pass it to the NDK through JNI. You can ask in the Android SDK forum, or
someone can comment here, but as far as I know there is no equivalent of
an "event" on Android that encompasses key presses, touch events, sensor
events, window creation/update/destruction/activation, etc. Each of
those requires its own listener or callback.

And each of those listeners would live at the Java level. You could
easily create a JNI "OnEvent()" function that your Java wrapper could
use to pass each event type to C++. And you COULD even create a thread
in C++ that could call "PeekEvent" to get an event from the queue (that
you're filling with your JNI callbacks), if you really had to. I'd hope
you're familiar with thread synchronization, though, since there are a
hundred ways to get it wrong. :)

Don't know if GLib requires that the C++ loop never exit, or if it can
be itself event driven; if the latter is true, then you'd be better off
writing it that way. In other words, if you can simply have Android/Java
call in to your C++ code for each event, including a screen draw, and
every time your C++ code exits back to Java (it doesn't get stuck in a
C++ "event loop" while the app runs), you'll have a much easier go of
it, not only because threaded code can be hard, but also because
threaded debugging on Android is broken at present, so you wouldn't be
able to use GDB to debug any of your code. :(

Tim

Reply all
Reply to author
Forward
0 new messages