summaryrefslogtreecommitdiff
path: root/src/lib/ecore/efl_loop_fd.c
blob: 50bd4c2b7895711c9648ab1a8785a16ef4325e94 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#include <Ecore.h>

#include "ecore_private.h"

#define MY_CLASS EFL_LOOP_FD_CLASS

typedef struct _Efl_Loop_Fd_Data Efl_Loop_Fd_Data;
struct _Efl_Loop_Fd_Data
{
   Ecore_Fd_Handler *handler;

   struct {
      unsigned int read;
      unsigned int write;
      unsigned int error;
   } references;

   int fd;

   Eina_Bool file : 1;
};

static Eina_Bool
_efl_loop_fd_read_cb(void *data, Ecore_Fd_Handler *fd_handler)
{
   Eo *obj = data;

   if (ecore_main_fd_handler_active_get(fd_handler, ECORE_FD_READ))
     {
        eo_event_callback_call(obj, EFL_LOOP_FD_EVENT_READ, NULL);
     }
   if (ecore_main_fd_handler_active_get(fd_handler, ECORE_FD_WRITE))
     {
        eo_event_callback_call(obj, EFL_LOOP_FD_EVENT_WRITE, NULL);
     }
   if (ecore_main_fd_handler_active_get(fd_handler, ECORE_FD_ERROR))
     {
        eo_event_callback_call(obj, EFL_LOOP_FD_EVENT_ERROR, NULL);
     }

   return ECORE_CALLBACK_RENEW;
}

static void
_efl_loop_fd_reset(Eo *obj, Efl_Loop_Fd_Data *pd)
{
   int flags = 0;

   if (pd->handler) ecore_main_fd_handler_del(pd->handler);
   pd->handler = NULL;
   if (pd->fd < 0) return ;
   flags |= pd->references.read > 0 ? ECORE_FD_READ : 0;
   flags |= pd->references.write > 0 ? ECORE_FD_WRITE : 0;
   flags |= pd->references.error > 0 ? ECORE_FD_ERROR : 0;
   if (flags == 0) return ;

   if (pd->file)
     pd->handler = ecore_main_fd_handler_file_add(pd->fd, flags, _efl_loop_fd_read_cb, obj, NULL, NULL);
   else
     pd->handler = ecore_main_fd_handler_add(pd->fd, flags, _efl_loop_fd_read_cb, obj, NULL, NULL);
}

static void
_efl_loop_fd_fd_set(Eo *obj, Efl_Loop_Fd_Data *pd, int fd)
{
   pd->fd = fd;
   pd->file = EINA_FALSE;
   _efl_loop_fd_reset(obj, pd);
}

static int
_efl_loop_fd_fd_get(Eo *obj EINA_UNUSED, Efl_Loop_Fd_Data *pd)
{
   return pd->file ? -1 : pd->fd;
}

static void
_efl_loop_fd_fd_file_set(Eo *obj, Efl_Loop_Fd_Data *pd, int fd)
{
   pd->fd = fd;
   pd->file = EINA_TRUE;
   _efl_loop_fd_reset(obj, pd);
}

static int
_efl_loop_fd_fd_file_get(Eo *obj EINA_UNUSED, Efl_Loop_Fd_Data *pd)
{
   return pd->file ? pd->fd : -1;
}

static Eina_Bool
_check_fd_event_catcher_add(void *data, const Eo_Event *event)
{
   const Eo_Callback_Array_Item *array = event->info;
   Efl_Loop_Fd_Data *fd = data;
   int i;

   for (i = 0; array[i].desc != NULL; i++)
     {
        if (array[i].desc == EFL_LOOP_FD_EVENT_READ)
          {
             if (fd->references.read++ > 0) continue;
             _efl_loop_fd_reset(event->object, fd);
          }
        else if (array[i].desc == EFL_LOOP_FD_EVENT_WRITE)
          {
             if (fd->references.write++ > 0) continue;
             _efl_loop_fd_reset(event->object, fd);
          }
        if (array[i].desc == EFL_LOOP_FD_EVENT_ERROR)
          {
             if (fd->references.error++ > 0) continue;
             _efl_loop_fd_reset(event->object, fd);
          }
     }

   return EO_CALLBACK_CONTINUE;
}

static Eina_Bool
_check_fd_event_catcher_del(void *data, const Eo_Event *event)
{
   const Eo_Callback_Array_Item *array = event->info;
   Efl_Loop_Fd_Data *fd = data;
   int i;

   for (i = 0; array[i].desc != NULL; i++)
     {
        if (array[i].desc == EFL_LOOP_FD_EVENT_READ)
          {
             if (fd->references.read++ > 0) continue;
             _efl_loop_fd_reset(event->object, fd);
          }
        else if (array[i].desc == EFL_LOOP_FD_EVENT_WRITE)
          {
             if (fd->references.write++ > 0) continue;
             _efl_loop_fd_reset(event->object, fd);
          }
        if (array[i].desc == EFL_LOOP_FD_EVENT_ERROR)
          {
             if (fd->references.error++ > 0) continue;
             _efl_loop_fd_reset(event->object, fd);
          }
     }

   return EO_CALLBACK_CONTINUE;
}

EO_CALLBACKS_ARRAY_DEFINE(fd_watch,
                          { EO_EVENT_CALLBACK_ADD, _check_fd_event_catcher_add },
                          { EO_EVENT_CALLBACK_DEL, _check_fd_event_catcher_del });

static Eo_Base *
_efl_loop_fd_eo_base_constructor(Eo *obj, Efl_Loop_Fd_Data *pd)
{
   eo_constructor(eo_super(obj, MY_CLASS));

   eo_event_callback_array_add(obj, fd_watch(), pd);

   pd->fd = -1;

   return obj;
}

static void
_efl_loop_fd_eo_base_destructor(Eo *obj, Efl_Loop_Fd_Data *pd)
{
   eo_destructor(eo_super(obj, MY_CLASS));

   ecore_main_fd_handler_del(pd->handler);
}

#include "efl_loop_fd.eo.c"