Index ¦ Archives  ¦ Atom  ¦ RSS

PDK - Minimum Event Loop

I recently worked on re-creating this week's Weekly App Hack's hybrid plugin, sdltts. It was compiled for the device and basically closed-source, only mentioning the library it used. I'm going to post the necessary code to make it later, but first I'm posting what I found to be the minimal amount of code necessary to have an SDL event loop without taking up any extra CPU:

void main(int argc, char ** argv) {
    if (SDL_Init(SDL_INIT_VIDEO) {
        exit(1);
    }
    PDL_Init(0);

    atexit(SDL_Quit);
    atexit(PDL_Quit);

    PDL_RegisterJSHandler("doSomething", doSomething);
    PDL_JSRegistrationComplete();
    PDL_CallJS("ready",NULL,0);

    SDL_Event Event;
    while (1) {
        SDL_WaitEvent(&Event);
        if(Event.type == SDL_QUIT) exit(0);
    }
}

You need to initialize SDL and then PDL, registering them for atexit in the proper order. This allows your code to clean up properly as well. ~~Lastly, set the video mode to the equivalent of nothing. This step is necessary even if your code has nothing to do with video, this took me a while to find since it's not really documented and there are so few non-visible hybrid apps out there.~~ Thanks to pcworld for setting me straight on that one.

Once you're initialized, you can register your javascript handlers as you wish. Call the javascript 'ready' function for enyo.Hybrid kinds that are listening to the onPluginReady event. I think you only have to do this for enyo, but mojo might have a similar issue.

The next piece is the SDL event loop. Since we're just waiting for javascript calls and ignoring all other things, our loop is short and sweet, just wait for quit events and ignore anything else. There's no faster way to filter events, SDL_SetEventFilter just registers a filtering callback.

That's it, that's all you need for a javascript-only hybrid plugin. I actually like this PDK stuff considerably, it opens a lot of doors to Enyo apps that can't handle certain non-video tasks, such as audio or web events. This might, with other arguments to SDL_SetVideoMode, even be able to add tiny bits of video processing to Enyo apps too.

© Fahrzin Hemmati. Built using Pelican. Theme by Giulio Fidente on github.