https://bitbucket.org/hudson/magic-lantern
Raw File
Tip revision: 284c98443573705c42002c179320a0bd13753e43 authored by a1ex on 12 December 2011, 20:27:12 UTC
Optimize for size
Tip revision: 284c984
notify_box.c
/** \file
 * Dialog test code
 * Based on http://code.google.com/p/400plus/source/browse/trunk/menu_developer.c
 */
#include "dryos.h"
#include "bmp.h"
#include "tasks.h"
#include "debug.h"
#include "menu.h"
#include "property.h"
#include "config.h"
#include "gui.h"
#include "lens.h"

struct semaphore * notify_box_show_sem = 0;
struct semaphore * notify_box_main_sem = 0;

int notify_box_timeout = 0;
int notify_box_stop_request = 0;
char notify_box_msg[100];

/*int handle_notifybox_bgmt(struct event * event)
{
    if (event->param == MLEV_NOTIFY_BOX_OPEN)
    {
        //~ BMP_LOCK ( bfnt_puts(notify_box_msg, 50, 50, COLOR_WHITE, COLOR_BLACK); )
        BMP_LOCK ( bmp_printf(FONT_LARGE, 50, 50, notify_box_msg); )
    }
    else if (event->param == MLEV_NOTIFY_BOX_CLOSE)
    {
        redraw();
        give_semaphore(notify_box_sem);
    }
    return 0;
}*/

void NotifyBox_task(void* priv)
{
    while (1)
    {
        // wait until some other task asks for a notification
        take_semaphore(notify_box_show_sem, 0);
        
        redraw();
        
        // show notification for a while, then redraw to erase it
        notify_box_stop_request = 0;
        int i;
        for (i = 0; i < notify_box_timeout/50; i++)
        {
            bmp_printf(FONT_LARGE, os.x0 + 50, os.y0 + 50, notify_box_msg);
            msleep(50);
            crop_set_dirty(10); // should match the value from redraw_do
            if (notify_box_stop_request) break;
        }
        redraw();
    }
}

TASK_CREATE( "notifybox_task", NotifyBox_task, 0, 0x1b, 0x1000 );

void NotifyBoxHide()
{
    notify_box_stop_request = 1;
}

void NotifyBox(int timeout, char* fmt, ...) 
{
    // make sure this is thread safe
    take_semaphore(notify_box_main_sem, 0);
    
    notify_box_timeout = MAX(timeout, 100);
    va_list ap;
    va_start( ap, fmt );
    vsnprintf( notify_box_msg, sizeof(notify_box_msg), fmt, ap );
    va_end( ap );

    give_semaphore(notify_box_show_sem); // request displaying the notification box
    give_semaphore(notify_box_main_sem); // done, other call can be made now
}

static void dlg_init()
{
    notify_box_show_sem = create_named_semaphore("nbox_show_sem", 0);
    notify_box_main_sem = create_named_semaphore("nbox_done_sem", 1);
}

INIT_FUNC(__FILE__, dlg_init);
back to top