"Fossies" - the Fresh Open Source Software Archive

Member "gambas-3.16.3/gb.gtk/src/watcher.cpp" (7 Sep 2021, 4285 Bytes) of package /linux/misc/gambas-3.16.3.tar.bz2:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "watcher.cpp" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 3.16.2_vs_3.16.3.

    1 /***************************************************************************
    2 
    3   watcher.cpp
    4 
    5   (c) 2004-2006 - Daniel Campos Fernández <dcamposf@gmail.com>
    6 
    7   This program is free software; you can redistribute it and/or modify
    8   it under the terms of the GNU General Public License as published by
    9   the Free Software Foundation; either version 2, or (at your option)
   10   any later version.
   11 
   12   This program is distributed in the hope that it will be useful,
   13   but WITHOUT ANY WARRANTY; without even the implied warranty of
   14   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   15   GNU General Public License for more details.
   16 
   17   You should have received a copy of the GNU General Public License
   18   along with this program; if not, write to the Free Software
   19   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
   20   MA 02110-1301, USA.
   21 
   22 ***************************************************************************/
   23 
   24 #include "main.h"
   25 #include "gambas.h"
   26 #include "watcher.h"
   27 
   28 //#define DEBUG_ME 1
   29 
   30 static WATCH **watch = NULL;
   31 
   32 static gboolean watch_adaptor(GIOChannel *source, GIOCondition condition, gpointer param)
   33 {
   34     WATCH *data = (WATCH *)param;
   35 
   36 #if DEBUG_ME
   37     fprintf(stderr, "watch_adaptor: %p: condition = %d, data = %p\n", source, (int)condition, (void *)param);
   38 #endif
   39 
   40     if (!data) return true;
   41     
   42     if (condition & G_IO_IN)
   43         (*data->callback_read)(data->fd, GB_WATCH_READ, data->param_read);
   44     else if (condition & G_IO_OUT)
   45         (*data->callback_write)(data->fd, GB_WATCH_WRITE, data->param_write);
   46 
   47     return true;
   48 }
   49 
   50 void CWatcher::init()
   51 {
   52     GB.NewArray(POINTER(&watch), sizeof(WATCH *), 0);
   53 }
   54 
   55 void CWatcher::exit()
   56 {
   57     Clear();
   58     GB.FreeArray(POINTER(&watch));
   59 }
   60 
   61 void CWatcher::Clear()
   62 {
   63     while (count()) 
   64     {
   65         CWatcher::Add(watch[0]->fd, GB_WATCH_NONE, NULL, 0);
   66     }
   67 }
   68 
   69 void CWatcher::Remove(int fd)
   70 {
   71     CWatcher::Add(fd, GB_WATCH_NONE, NULL, 0);
   72 }
   73 
   74 void CWatcher::Add(int fd, int type, void *callback, intptr_t param)
   75 {
   76     WATCH *data = NULL;
   77     WATCH **pwatch;
   78     int i;
   79 
   80     for (i = 0; i < count(); i++)
   81     {
   82         if (watch[i]->fd == fd)
   83         {
   84             data = watch[i];
   85             break;
   86         }
   87     }
   88     
   89     if (!data)
   90     {
   91         if (type == GB_WATCH_NONE || !callback)
   92             return;
   93         
   94         pwatch = (WATCH **)GB.Add(&watch);
   95 
   96         GB.Alloc(POINTER(pwatch), sizeof(WATCH));
   97         data = *pwatch;
   98         data->fd = fd;
   99         data->channel_read = data->channel_write = 0;
  100         data->callback_read = data->callback_write = 0;
  101     }
  102 
  103     if (data->callback_read && (type == GB_WATCH_NONE || type == GB_WATCH_READ))
  104     {
  105 #if DEBUG_ME
  106         fprintf(stderr, "remove watch on fd %d for read (%p)\n", data->fd, data->channel_read);
  107 #endif
  108         g_source_remove(data->id_read);
  109         g_io_channel_unref(data->channel_read);
  110         data->callback_read = 0;
  111         data->channel_read = 0;
  112     }
  113     
  114     if (data->callback_write && (type == GB_WATCH_NONE || type == GB_WATCH_WRITE))
  115     {
  116 #if DEBUG_ME
  117         fprintf(stderr, "remove watch on fd %d for read (%p)\n", data->fd, data->channel_write);
  118 #endif
  119         g_source_remove(data->id_write);
  120         g_io_channel_unref(data->channel_write);
  121         data->callback_write = 0;
  122         data->channel_write = 0;
  123     }
  124     
  125     if (callback)
  126     {
  127         if (type == GB_WATCH_READ)
  128         {
  129             data->callback_read = (WATCH_CALLBACK)callback;
  130             data->param_read = param;
  131             data->channel_read = g_io_channel_unix_new(fd);
  132             g_io_channel_set_encoding(data->channel_read, NULL, NULL);
  133             g_io_channel_set_buffered(data->channel_read, FALSE);
  134             data->id_read = g_io_add_watch_full(data->channel_read, G_PRIORITY_DEFAULT, G_IO_IN, watch_adaptor, (void*)data, NULL);
  135 #if DEBUG_ME
  136             fprintf(stderr, "add watch on fd %d for read (%p)\n", fd, data->channel_read);
  137 #endif
  138         }
  139         else if (type == GB_WATCH_WRITE)
  140         {
  141             data->callback_write = (WATCH_CALLBACK)callback;
  142             data->param_write = param;
  143             data->channel_write = g_io_channel_unix_new(fd);
  144             g_io_channel_set_encoding(data->channel_write, NULL, NULL);
  145             g_io_channel_set_buffered(data->channel_write, FALSE);
  146             data->id_write = g_io_add_watch_full(data->channel_write, G_PRIORITY_DEFAULT, G_IO_OUT, watch_adaptor, (void*)data, NULL);
  147 #if DEBUG_ME
  148             fprintf(stderr, "add watch on fd %d for write (%p)\n", fd, data->channel_write);
  149 #endif
  150         }
  151     }
  152 
  153     if (!data->callback_read && !data->callback_write)
  154     {
  155         GB.Free(POINTER(&data));
  156         GB.Remove(&watch, i, 1);
  157         MAIN_check_quit();
  158     }
  159 }
  160 
  161 int CWatcher::count()
  162 {
  163     return GB.Count(watch);
  164 }