libsaria: Notify UI when the idle queue size has changed

When the first task is queued, I trigger a callback so the UI can start
processing when idle.  I return the size of the queue from both
run_task() and queue_task() so the UI knows when to stop processing
callbacks.
This commit is contained in:
Bryan Schumaker 2011-09-13 17:27:46 -04:00
parent e94ca6b9d6
commit 59fbd0c696
5 changed files with 22 additions and 11 deletions

View File

@ -5,6 +5,7 @@ enum callback_t {
PLAY,
PAUSE,
VOLUME,
IDLE_TASK_QUEUED,
};
void register_callback(callback_t, void (*)());

View File

@ -9,7 +9,7 @@ class IdleTask
virtual void run_task() = 0;
};
void libsaria_idle_task();
void libsaria_queue_task(IdleTask *);
int libsaria_idle_task();
int libsaria_queue_task(IdleTask *);
#endif /* LIBSARIA_IDLE_H */

View File

@ -1,19 +1,27 @@
#include <libsaria/callback.h>
#include "idle/idle.h"
static Idle idle;
void libsaria_idle_task()
int libsaria_idle_task()
{
idle.run_task();
return idle.run_task();
}
/*
* This will queue a pre-constructed idle task
* It should be constructed through `new` before calling
* and will be deleted by the idle queue once run.
*
* IDLE_TASK_QUEUED will only be triggered when the queue
* goes from size is increased from 0 to 1. This will tell
* the UI that idle tasks have been queued.
*/
void libsaria_queue_task(IdleTask *task)
int libsaria_queue_task(IdleTask *task)
{
idle.queue_task(task);
int queue_size = idle.queue_task(task);
if (queue_size == 1)
trigger_callback(IDLE_TASK_QUEUED);
return queue_size;
}

View File

@ -10,18 +10,20 @@ Idle::~Idle()
{
}
void Idle::run_task()
int Idle::run_task()
{
IdleTask *task;
if (idle_queue.size() == 0)
return;
return idle_queue.size();
task = idle_queue.front();
idle_queue.pop();
task->run_task();
delete task;
return idle_queue.size();
}
void Idle::queue_task(IdleTask *task)
int Idle::queue_task(IdleTask *task)
{
idle_queue.push(task);
return idle_queue.size();
}

View File

@ -15,8 +15,8 @@ class Idle
Idle();
~Idle();
void run_task();
void queue_task(IdleTask *);
int run_task();
int queue_task(IdleTask *);
};
#endif /* LIBSARIA_IDLE_COMPONENT_H */