2011-11-20 08:48:02 -08:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include <config.h>
|
|
|
|
#endif
|
|
|
|
|
2012-10-16 01:57:18 -07:00
|
|
|
#ifndef _MSC_VER
|
|
|
|
# include <unistd.h>
|
2011-11-20 07:17:29 -08:00
|
|
|
#endif
|
2012-10-16 01:57:18 -07:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <errno.h>
|
2011-11-20 07:17:29 -08:00
|
|
|
|
|
|
|
#include "evas_common.h"
|
|
|
|
#include "evas_private.h"
|
|
|
|
|
2008-04-30 05:21:31 -07:00
|
|
|
static int _fd_write = -1;
|
|
|
|
static int _fd_read = -1;
|
2012-08-13 01:52:23 -07:00
|
|
|
static pid_t _fd_pid = 0;
|
2008-04-30 05:21:31 -07:00
|
|
|
|
|
|
|
static int _init_evas_event = 0;
|
|
|
|
|
|
|
|
typedef struct _Evas_Event_Async Evas_Event_Async;
|
2009-09-27 20:51:21 -07:00
|
|
|
|
2008-04-30 05:21:31 -07:00
|
|
|
struct _Evas_Event_Async
|
|
|
|
{
|
2010-08-28 18:59:21 -07:00
|
|
|
const void *target;
|
|
|
|
void *event_info;
|
|
|
|
Evas_Async_Events_Put_Cb func;
|
|
|
|
Evas_Callback_Type type;
|
2009-02-12 09:05:17 -08:00
|
|
|
};
|
2008-04-30 05:21:31 -07:00
|
|
|
|
|
|
|
int
|
|
|
|
evas_async_events_init(void)
|
|
|
|
{
|
|
|
|
int filedes[2];
|
2011-10-22 23:59:40 -07:00
|
|
|
|
2008-04-30 05:21:31 -07:00
|
|
|
_init_evas_event++;
|
|
|
|
if (_init_evas_event > 1) return _init_evas_event;
|
2011-10-22 23:59:40 -07:00
|
|
|
|
2012-08-13 01:52:23 -07:00
|
|
|
_fd_pid = getpid();
|
|
|
|
|
2008-04-30 05:21:31 -07:00
|
|
|
if (pipe(filedes) == -1)
|
|
|
|
{
|
|
|
|
_init_evas_event = 0;
|
|
|
|
return 0;
|
|
|
|
}
|
2011-10-22 23:59:40 -07:00
|
|
|
|
2008-04-30 05:21:31 -07:00
|
|
|
_fd_read = filedes[0];
|
|
|
|
_fd_write = filedes[1];
|
2011-10-22 23:59:40 -07:00
|
|
|
|
2008-04-30 05:21:31 -07:00
|
|
|
fcntl(_fd_read, F_SETFL, O_NONBLOCK);
|
2011-10-22 23:59:40 -07:00
|
|
|
|
2008-04-30 05:21:31 -07:00
|
|
|
return _init_evas_event;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
evas_async_events_shutdown(void)
|
|
|
|
{
|
|
|
|
_init_evas_event--;
|
|
|
|
if (_init_evas_event > 0) return _init_evas_event;
|
2011-10-22 23:59:40 -07:00
|
|
|
|
2008-04-30 05:21:31 -07:00
|
|
|
close(_fd_read);
|
|
|
|
close(_fd_write);
|
|
|
|
_fd_read = -1;
|
|
|
|
_fd_write = -1;
|
2011-10-22 23:59:40 -07:00
|
|
|
|
2008-04-30 05:21:31 -07:00
|
|
|
return _init_evas_event;
|
|
|
|
}
|
|
|
|
|
2012-08-13 01:52:23 -07:00
|
|
|
static void
|
|
|
|
_evas_async_events_fork_handle(void)
|
|
|
|
{
|
|
|
|
int i, count = _init_evas_event;
|
|
|
|
|
|
|
|
if (getpid() == _fd_pid) return;
|
|
|
|
for (i = 0; i < count; i++) evas_async_events_shutdown();
|
|
|
|
for (i = 0; i < count; i++) evas_async_events_init();
|
|
|
|
}
|
|
|
|
|
2008-04-30 05:21:31 -07:00
|
|
|
EAPI int
|
|
|
|
evas_async_events_fd_get(void)
|
|
|
|
{
|
2012-08-13 01:52:23 -07:00
|
|
|
_evas_async_events_fork_handle();
|
2008-04-30 05:21:31 -07:00
|
|
|
return _fd_read;
|
|
|
|
}
|
|
|
|
|
|
|
|
EAPI int
|
|
|
|
evas_async_events_process(void)
|
|
|
|
{
|
2009-09-27 20:51:21 -07:00
|
|
|
Evas_Event_Async *ev;
|
2008-04-30 05:21:31 -07:00
|
|
|
int check;
|
|
|
|
int count = 0;
|
2011-10-22 23:59:40 -07:00
|
|
|
|
2008-04-30 05:21:31 -07:00
|
|
|
if (_fd_read == -1) return 0;
|
2011-10-22 23:59:40 -07:00
|
|
|
|
2012-08-13 01:52:23 -07:00
|
|
|
_evas_async_events_fork_handle();
|
|
|
|
|
2011-10-22 20:51:41 -07:00
|
|
|
do
|
2011-10-20 22:50:05 -07:00
|
|
|
{
|
2009-12-18 03:03:31 -08:00
|
|
|
check = read(_fd_read, &ev, sizeof (Evas_Event_Async *));
|
2011-10-22 23:59:40 -07:00
|
|
|
|
2009-12-18 03:03:31 -08:00
|
|
|
if (check == sizeof (Evas_Event_Async *))
|
|
|
|
{
|
2009-09-27 20:51:21 -07:00
|
|
|
if (ev->func) ev->func((void *)ev->target, ev->type, ev->event_info);
|
2009-12-18 03:03:31 -08:00
|
|
|
free(ev);
|
|
|
|
count++;
|
|
|
|
}
|
2011-10-22 20:51:41 -07:00
|
|
|
}
|
2011-10-20 22:50:05 -07:00
|
|
|
while (check > 0);
|
2011-10-22 23:59:40 -07:00
|
|
|
|
2009-12-18 03:03:31 -08:00
|
|
|
evas_cache_image_wakeup();
|
2011-10-22 23:59:40 -07:00
|
|
|
|
2008-04-30 05:21:31 -07:00
|
|
|
if (check < 0)
|
2011-10-20 22:50:05 -07:00
|
|
|
{
|
|
|
|
switch (errno)
|
|
|
|
{
|
|
|
|
case EBADF:
|
|
|
|
case EINVAL:
|
|
|
|
case EIO:
|
|
|
|
case EISDIR:
|
|
|
|
_fd_read = -1;
|
|
|
|
}
|
|
|
|
}
|
2011-10-22 23:59:40 -07:00
|
|
|
|
2008-04-30 05:21:31 -07:00
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2009-06-17 03:01:52 -07:00
|
|
|
EAPI Eina_Bool
|
2010-08-28 18:59:21 -07:00
|
|
|
evas_async_events_put(const void *target, Evas_Callback_Type type, void *event_info, Evas_Async_Events_Put_Cb func)
|
2008-04-30 05:21:31 -07:00
|
|
|
{
|
2009-09-27 20:51:21 -07:00
|
|
|
Evas_Event_Async *ev;
|
2008-04-30 05:21:31 -07:00
|
|
|
ssize_t check;
|
2009-06-17 03:01:52 -07:00
|
|
|
Eina_Bool result = EINA_FALSE;
|
2011-10-22 23:59:40 -07:00
|
|
|
|
2008-04-30 05:21:31 -07:00
|
|
|
if (!func) return 0;
|
|
|
|
if (_fd_write == -1) return 0;
|
2011-10-22 23:59:40 -07:00
|
|
|
|
2012-08-13 01:52:23 -07:00
|
|
|
_evas_async_events_fork_handle();
|
|
|
|
|
2009-12-18 03:03:31 -08:00
|
|
|
ev = calloc(1, sizeof (Evas_Event_Async));
|
|
|
|
if (!ev) return 0;
|
2011-10-22 23:59:40 -07:00
|
|
|
|
2009-09-27 20:51:21 -07:00
|
|
|
ev->func = func;
|
|
|
|
ev->target = target;
|
|
|
|
ev->type = type;
|
|
|
|
ev->event_info = event_info;
|
2011-10-22 23:59:40 -07:00
|
|
|
|
2011-10-22 20:51:41 -07:00
|
|
|
do
|
2011-10-20 22:50:05 -07:00
|
|
|
{
|
|
|
|
check = write(_fd_write, &ev, sizeof (Evas_Event_Async*));
|
|
|
|
}
|
2011-10-22 20:51:41 -07:00
|
|
|
while ((check != sizeof (Evas_Event_Async*)) &&
|
2011-10-20 22:50:05 -07:00
|
|
|
((errno == EINTR) || (errno == EAGAIN)));
|
2011-10-22 23:59:40 -07:00
|
|
|
|
2009-12-18 03:03:31 -08:00
|
|
|
evas_cache_image_wakeup();
|
2011-10-22 23:59:40 -07:00
|
|
|
|
2009-12-18 03:03:31 -08:00
|
|
|
if (check == sizeof (Evas_Event_Async*))
|
2009-06-17 03:01:52 -07:00
|
|
|
result = EINA_TRUE;
|
2008-04-30 05:21:31 -07:00
|
|
|
else
|
2011-10-20 22:50:05 -07:00
|
|
|
{
|
|
|
|
switch (errno)
|
|
|
|
{
|
|
|
|
case EBADF:
|
|
|
|
case EINVAL:
|
|
|
|
case EIO:
|
|
|
|
case EPIPE:
|
|
|
|
_fd_write = -1;
|
|
|
|
}
|
|
|
|
}
|
2011-10-22 23:59:40 -07:00
|
|
|
|
2008-04-30 05:21:31 -07:00
|
|
|
return result;
|
|
|
|
}
|