"Fossies" - the Fresh Open Source Software Archive

Member "feh-3.4.1/src/timers.c" (29 May 2020, 3582 Bytes) of package /linux/privat/feh-3.4.1.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 "timers.c" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 3.4_vs_3.4.1.

    1 /* timers.c
    2 
    3 Copyright (C) 1999-2003 Tom Gilbert.
    4 Copyright (C) 2010-2020 Daniel Friesel.
    5 
    6 Permission is hereby granted, free of charge, to any person obtaining a copy
    7 of this software and associated documentation files (the "Software"), to
    8 deal in the Software without restriction, including without limitation the
    9 rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
   10 sell copies of the Software, and to permit persons to whom the Software is
   11 furnished to do so, subject to the following conditions:
   12 
   13 The above copyright notice and this permission notice shall be included in
   14 all copies of the Software and its documentation and acknowledgment shall be
   15 given in the documentation and software packages that this Software was
   16 used.
   17 
   18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
   19 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
   20 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
   21 THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
   22 IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
   23 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
   24 
   25 */
   26 
   27 #include "feh.h"
   28 #include "options.h"
   29 #include "timers.h"
   30 
   31 fehtimer first_timer = NULL;
   32 
   33 void feh_handle_timer(void)
   34 {
   35     fehtimer ft;
   36 
   37     if (!first_timer) {
   38         D(("No timer to handle, returning\n"));
   39         return;
   40     }
   41     ft = first_timer;
   42     first_timer = first_timer->next;
   43     D(("Executing timer function now\n"));
   44     (*(ft->func)) (ft->data);
   45     D(("Freeing the timer\n"));
   46     if (ft && ft->name)
   47         free(ft->name);
   48     if (ft)
   49         free(ft);
   50     return;
   51 }
   52 
   53 double feh_get_time(void)
   54 {
   55     struct timeval timev;
   56 
   57     gettimeofday(&timev, NULL);
   58     return((double) timev.tv_sec + (((double) timev.tv_usec) / 1000000));
   59 }
   60 
   61 void feh_remove_timer(char *name)
   62 {
   63     fehtimer ft, ptr, pptr;
   64 
   65     D(("removing %s\n", name));
   66     pptr = NULL;
   67     ptr = first_timer;
   68     while (ptr) {
   69         D(("Stepping through event list\n"));
   70         ft = ptr;
   71         if (!strcmp(ft->name, name)) {
   72             D(("Found it. Removing\n"));
   73             if (pptr)
   74                 pptr->next = ft->next;
   75             else
   76                 first_timer = ft->next;
   77             if (ft->next)
   78                 ft->next->in += ft->in;
   79             if (ft->name)
   80                 free(ft->name);
   81             if (ft)
   82                 free(ft);
   83             return;
   84         }
   85         pptr = ptr;
   86         ptr = ptr->next;
   87     }
   88     return;
   89 }
   90 
   91 void feh_add_timer(void (*func) (void *data), void *data, double in, char *name)
   92 {
   93     fehtimer ft, ptr, pptr;
   94     double tally;
   95 
   96     D(("adding timer %s for %f seconds time\n", name, in));
   97     feh_remove_timer(name);
   98     ft = emalloc(sizeof(_fehtimer));
   99     ft->next = NULL;
  100     ft->func = func;
  101     ft->data = data;
  102     ft->name = estrdup(name);
  103     ft->just_added = 1;
  104     ft->in = in;
  105     D(("ft->in = %f\n", ft->in));
  106     if (!first_timer) {
  107         D(("No first timer\n"));
  108         first_timer = ft;
  109     } else {
  110         D(("There is a first timer\n"));
  111         pptr = NULL;
  112         ptr = first_timer;
  113         tally = 0.0;
  114         while (ptr) {
  115             tally += ptr->in;
  116             if (tally > in) {
  117                 tally -= ptr->in;
  118                 ft->next = ptr;
  119                 if (pptr)
  120                     pptr->next = ft;
  121                 else
  122                     first_timer = ft;
  123                 ft->in -= tally;
  124                 if (ft->next)
  125                     ft->next->in -= ft->in;
  126                 return;
  127             }
  128             pptr = ptr;
  129             ptr = ptr->next;
  130         }
  131         if (pptr)
  132             pptr->next = ft;
  133         else
  134             first_timer = ft;
  135         ft->in -= tally;
  136     }
  137     D(("ft->in = %f\n", ft->in));
  138     return;
  139 }
  140 
  141 void feh_add_unique_timer(void (*func) (void *data), void *data, double in)
  142 {
  143     static long i = 0;
  144     char evname[20];
  145 
  146     snprintf(evname, sizeof(evname), "T_%ld", i);
  147     D(("adding timer with unique name %s\n", evname));
  148     feh_add_timer(func, data, in, evname);
  149     i++;
  150     /* Mega paranoia ;) */
  151     if (i > 1000000)
  152         i = 0;
  153     return;
  154 }