* add a _del function for win32 HANDLEs
authorcaro <caro@7cbeb6ba-43b4-40fd-8cce-4c39aea84d33>
Mon, 23 Nov 2009 23:09:48 +0000 (23:09 +0000)
committercaro <caro@7cbeb6ba-43b4-40fd-8cce-4c39aea84d33>
Mon, 23 Nov 2009 23:09:48 +0000 (23:09 +0000)
 * delete HANDLES when asked
 * minor fixes in the win32 code

git-svn-id: http://svn.enlightenment.org/svn/e/trunk/ecore@43939 7cbeb6ba-43b4-40fd-8cce-4c39aea84d33

src/lib/ecore/Ecore.h
src/lib/ecore/ecore.c
src/lib/ecore/ecore_main.c
src/lib/ecore/ecore_private.h

index 21ee10b..01bc5f0 100644 (file)
@@ -298,6 +298,7 @@ extern "C" {
    EAPI void              ecore_main_fd_handler_active_set(Ecore_Fd_Handler *fd_handler, Ecore_Fd_Handler_Flags flags);
 
    EAPI Ecore_Win32_Handler *ecore_main_win32_handler_add(void *h, int (*func) (void *data, Ecore_Win32_Handler *wh), const void *data);
+   EAPI void                *ecore_main_win32_handler_del(Ecore_Win32_Handler *win32_handler);
 
    EAPI Ecore_Pipe  *ecore_pipe_add(void (*handler) (void *data, void *buffer, unsigned int nbyte), const void *data);
    EAPI void        *ecore_pipe_del(Ecore_Pipe *p);
index 4c0da13..b40d0cf 100644 (file)
@@ -193,6 +193,9 @@ _ecore_magic_string_get(Ecore_Magic m)
       case ECORE_MAGIC_FD_HANDLER:
        return "Ecore_Fd_Handler (Fd Handler)";
        break;
+      case ECORE_MAGIC_WIN32_HANDLER:
+       return "Ecore_Win32_Handler (Win32 Handler)";
+       break;
       case ECORE_MAGIC_EVENT_HANDLER:
        return "Ecore_Event_Handler (Event Handler)";
        break;
index ee9c73c..62e5ecf 100644 (file)
@@ -38,7 +38,9 @@
 
 static int  _ecore_main_select(double timeout);
 static void _ecore_main_fd_handlers_cleanup(void);
+#ifndef _WIN32
 static void _ecore_main_fd_handlers_bads_rem(void);
+#endif
 static void _ecore_main_fd_handlers_call(void);
 static int  _ecore_main_fd_handlers_buf_call(void);
 static void _ecore_main_loop_iterate_internal(int once_only);
@@ -46,6 +48,7 @@ static void _ecore_main_loop_iterate_internal(int once_only);
 #ifdef _WIN32
 static int _ecore_main_win32_select(int nfds, fd_set *readfds, fd_set *writefds,
                                    fd_set *exceptfds, struct timeval *timeout);
+static void _ecore_main_win32_handlers_cleanup(void);
 #endif
 
 static int               in_main_loop = 0;
@@ -54,6 +57,7 @@ static Ecore_Fd_Handler *fd_handlers = NULL;
 static int               fd_handlers_delete_me = 0;
 #ifdef _WIN32
 static Ecore_Win32_Handler *win32_handlers = NULL;
+static int                  win32_handlers_delete_me = 0;
 #endif
 
 #ifdef _WIN32
@@ -234,6 +238,7 @@ ecore_main_win32_handler_add(void *h,
    if (!wh) return NULL;
    ECORE_MAGIC_SET(wh, ECORE_MAGIC_WIN32_HANDLER);
    wh->h = (HANDLE)h;
+   wh->delete_me = 0;
    wh->func = func;
    wh->data = (void *)data;
    win32_handlers = (Ecore_Win32_Handler *)eina_inlist_append(EINA_INLIST_GET(win32_handlers),
@@ -271,6 +276,28 @@ ecore_main_fd_handler_del(Ecore_Fd_Handler *fd_handler)
    return fd_handler->data;
 }
 
+#ifdef _WIN32
+EAPI void *
+ecore_main_win32_handler_del(Ecore_Win32_Handler *win32_handler)
+{
+   if (!ECORE_MAGIC_CHECK(win32_handler, ECORE_MAGIC_WIN32_HANDLER))
+     {
+       ECORE_MAGIC_FAIL(win32_handler, ECORE_MAGIC_WIN32_HANDLER,
+                        "ecore_main_win32_handler_del");
+       return NULL;
+     }
+   win32_handler->delete_me = 1;
+   win32_handlers_delete_me = 1;
+   return win32_handler->data;
+}
+#else
+EAPI void *
+ecore_main_win32_handler_del(Ecore_Win32_Handler *win32_handler)
+{
+   return NULL;
+}
+#endif
+
 EAPI void
 ecore_main_fd_handler_prepare_callback_set(Ecore_Fd_Handler *fd_handler, void (*func) (void *data, Ecore_Fd_Handler *fd_handler), const void *data)
 {
@@ -369,6 +396,20 @@ _ecore_main_shutdown(void)
        free(fdh);
      }
    fd_handlers_delete_me = 0;
+
+#ifdef _WIN32
+   while (win32_handlers)
+     {
+       Ecore_Win32_Handler *wh;
+
+       wh = win32_handlers;
+       win32_handlers = (Ecore_Win32_Handler *) eina_inlist_remove(EINA_INLIST_GET(win32_handlers),
+                                                                   EINA_INLIST_GET(wh));
+       ECORE_MAGIC_SET(wh, ECORE_MAGIC_NONE);
+       free(wh);
+     }
+   win32_handlers_delete_me = 0;
+#endif
 }
 
 static int
@@ -437,10 +478,7 @@ _ecore_main_select(double timeout)
    _ecore_loop_time = ecore_time_get();
    if (ret < 0)
      {
-#ifdef _WIN32
-       fprintf(stderr, "main_loop_select error %d\n", WSAGetLastError());
-       if (WSAEINTR == WSAGetLastError()) return -1;
-#else
+#ifndef _WIN32
        if (errno == EINTR) return -1;
        else if (errno == EBADF)
             _ecore_main_fd_handlers_bads_rem();
@@ -459,11 +497,15 @@ _ecore_main_select(double timeout)
                 fdh->error_active = 1;
            }
        _ecore_main_fd_handlers_cleanup();
+#ifdef _WIN32
+       _ecore_main_win32_handlers_cleanup();
+#endif
        return 1;
      }
    return 0;
 }
 
+#ifndef _WIN32
 static void
 _ecore_main_fd_handlers_bads_rem(void)
 {
@@ -501,6 +543,7 @@ _ecore_main_fd_handlers_bads_rem(void)
 
    _ecore_main_fd_handlers_cleanup();
 }
+#endif
 
 static void
 _ecore_main_fd_handlers_cleanup(void)
@@ -526,6 +569,31 @@ _ecore_main_fd_handlers_cleanup(void)
    fd_handlers_delete_me = 0;
 }
 
+#ifdef _WIN32
+static void
+_ecore_main_win32_handlers_cleanup(void)
+{
+   Ecore_Win32_Handler *wh;
+   Eina_Inlist *l;
+
+   if (!win32_handlers_delete_me) return;
+   for (l = EINA_INLIST_GET(win32_handlers); l; )
+     {
+        wh = (Ecore_Win32_Handler *)l;
+
+        l = l->next;
+        if (wh->delete_me)
+          {
+             win32_handlers = (Ecore_Win32_Handler *)eina_inlist_remove(EINA_INLIST_GET(win32_handlers),
+                                                                        EINA_INLIST_GET(wh));
+             ECORE_MAGIC_SET(wh, ECORE_MAGIC_NONE);
+             free(wh);
+          }
+     }
+   win32_handlers_delete_me = 0;
+}
+#endif
+
 static void
 _ecore_main_fd_handlers_call(void)
 {
@@ -742,9 +810,9 @@ _ecore_main_win32_select(int nfds, fd_set *readfds, fd_set *writefds,
    HANDLE objects[MAXIMUM_WAIT_OBJECTS];
    int    sockets[MAXIMUM_WAIT_OBJECTS];
    Ecore_Win32_Handler *wh;
-   int    objects_nbr = 0;
-   int    handles_nbr = 0;
-   int    events_nbr = 0;
+   unsigned int objects_nbr = 0;
+   unsigned int handles_nbr = 0;
+   unsigned int events_nbr = 0;
    DWORD  result;
    DWORD  timeout;
    MSG    msg;
@@ -798,7 +866,7 @@ _ecore_main_win32_select(int nfds, fd_set *readfds, fd_set *writefds,
    else
      timeout = (DWORD)(tv->tv_sec * 1000.0 + tv->tv_usec / 1000.0);
 
-   if (timeout == 0) return;
+   if (timeout == 0) return 0;
 
    result = MsgWaitForMultipleObjects(objects_nbr, (const HANDLE *)objects, FALSE,
                                      timeout, QS_ALLINPUT);
@@ -815,7 +883,7 @@ _ecore_main_win32_select(int nfds, fd_set *readfds, fd_set *writefds,
         msg = evil_last_error_get();
         printf (" * %s\n", msg);
         free(msg);
-        res = 0;
+        res = -1;
      }
    else if (result == WAIT_TIMEOUT)
      {
@@ -852,28 +920,25 @@ _ecore_main_win32_select(int nfds, fd_set *readfds, fd_set *writefds,
         EINA_INLIST_FOREACH(win32_handlers, wh)
           {
              if (objects[result - WAIT_OBJECT_0] == wh->h)
-               wh->func(wh->data, wh);
+               if (!wh->delete_me)
+                 if (!wh->func(wh->data, wh))
+                   {
+                      wh->delete_me = 1;
+                      win32_handlers_delete_me = 1;
+                   }
           }
         res = 1;
      }
    else
      {
         fprintf(stderr, "unknown result...\n");
+        res = -1;
      }
 
    /* Remove event objects again */
    for(i = 0; i < events_nbr; i++)
      WSACloseEvent(objects[i]);
 
-   /* remove HANDLEs */
-   while (win32_handlers)
-     {
-        wh = win32_handlers;
-        win32_handlers = (Ecore_Win32_Handler *)eina_inlist_remove(EINA_INLIST_GET(win32_handlers),
-                                                                   EINA_INLIST_GET(wh));
-        free(wh);
-     }
-
    return res;
 }
 #endif
index eb3249f..b1c7f1f 100644 (file)
@@ -338,6 +338,7 @@ struct _Ecore_Win32_Handler
    HANDLE         h;
    int          (*func) (void *data, Ecore_Win32_Handler *win32_handler);
    void          *data;
+   int            delete_me : 1;
 };
 #endif