https://bitbucket.org/hudson/magic-lantern
Raw File
Tip revision: c2a705dfc4c616078fedc047363a0c162a20d560 authored by alex@thinkpad on 21 December 2019, 10:21:34 UTC
raw_diag: fix BUSY screen with 2-shot analyses in still photo mode
Tip revision: c2a705d
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"

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

static int notify_box_timeout = 0;
static int notify_box_stop_request = 0;
static int notify_box_dirty = 0;
static char notify_box_msg[100];
static char notify_box_msg_tmp[100];

static void NotifyBox_task(void* priv)
{
    TASK_LOOP
    {
        // wait until some other task asks for a notification
        int err = take_semaphore(notify_box_show_sem, 500);
        if (err) continue;
        
        if (!notify_box_timeout) 
            continue;
        
        // show notification for a while, then redraw to erase it
        notify_box_stop_request = 0;
        notify_box_dirty = 0;
        for ( ; notify_box_timeout > 0 ; notify_box_timeout -= 50)
        {
            if (notify_box_dirty)
            {
                /* clear old message */
                redraw();
                notify_box_dirty = 0;
            }
            bmp_printf(FONT_LARGE,  50,  50, notify_box_msg);
            msleep(50);
            if (notify_box_stop_request) break;
        }
        notify_box_timeout = 0;
        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);
    
    va_list ap;
    va_start( ap, fmt );
    vsnprintf( notify_box_msg_tmp, sizeof(notify_box_msg_tmp)-1, fmt, ap );
    va_end( ap );
    
    if (notify_box_timeout && streq(notify_box_msg_tmp, notify_box_msg)) 
    {
        // same message: do not redraw, just re-set the timeout
        notify_box_timeout = MAX(timeout, 100);
        goto end;
    }

    // new message
    qprint("[NotifyBox] "); qprint(notify_box_msg_tmp); qprint("\n");
    memcpy(notify_box_msg, notify_box_msg_tmp, sizeof(notify_box_msg));
    notify_box_msg[sizeof(notify_box_msg)-1] = '\0';
    notify_box_timeout = MAX(timeout, 100);
    if (notify_box_timeout) notify_box_dirty = 1; // ask for a redraw, message changed

    give_semaphore(notify_box_show_sem); // request displaying the notification box

end:
    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