source: neercs/trunk/src/recurrent.c @ 4075

Last change on this file since 4075 was 4075, checked in by Jean-Yves Lamoureux, 11 years ago
  • Removed double includes of caca.h on most of the files
  • Property svn:keywords set to Id
File size: 3.4 KB
Line 
1/*
2 *  neercs        console-based window manager
3 *  Copyright (c) 2006 Sam Hocevar <sam@zoy.org>
4 *                2008 Jean-Yves Lamoureux <jylam@lnxscene.org>
5 *                All Rights Reserved
6 *
7 *  $Id: recurrent.c 4075 2009-11-30 10:34:19Z jylam $
8 *
9 *  This program is free software. It comes without any warranty, to
10 *  the extent permitted by applicable law. You can redistribute it
11 *  and/or modify it under the terms of the Do What The Fuck You Want
12 *  To Public License, Version 2, as published by Sam Hocevar. See
13 *  http://sam.zoy.org/wtfpl/COPYING for more details.
14 */
15
16#include "config.h"
17
18#include <stdio.h>
19#include <string.h>
20#include <stdlib.h>
21#include <sys/types.h>
22#include <signal.h>
23#include <sys/wait.h>
24#include <errno.h>
25#include <unistd.h>
26
27#include "neercs.h"
28
29
30int handle_recurrents(struct screen_list *screen_list)
31{
32    int refresh = 0, i;
33    /* Recurrent functions */
34    for (i = 0; i < screen_list->recurrent_list->count; i++)
35    {
36        if (screen_list->recurrent_list->recurrent[i]->function)
37        {
38            refresh |=
39                screen_list->recurrent_list->recurrent[i]->
40                function(screen_list,
41                         screen_list->recurrent_list->recurrent[i],
42                         screen_list->recurrent_list->recurrent[i]->user,
43                         get_us());
44        }
45    }
46    /* Delete recurrent functions */
47    for (i = 0; i < screen_list->recurrent_list->count; i++)
48    {
49        if (screen_list->recurrent_list->recurrent[i]->kill_me)
50        {
51            remove_recurrent(screen_list->recurrent_list, i);
52            i = 0;
53        }
54    }
55    return refresh;
56}
57
58
59/* Add recurrent function. It will be called at each main loop iteration,
60   unless it is removed */
61int add_recurrent(struct recurrent_list *list,
62                  int (*func) (struct screen_list *, struct recurrent * rec,
63                               void *user, long long unsigned int t),
64                  void *user)
65{
66    if (list == NULL || func == NULL)
67        return -1;
68
69    list->recurrent = (struct recurrent **)realloc(list->recurrent,
70                                                   sizeof(struct recurrent *)
71                                                   * (list->count + 1));
72
73    if (!list->recurrent)
74        fprintf(stderr, "Can't allocate memory at %s:%d\n", __FUNCTION__,
75                __LINE__);
76
77    list->recurrent[list->count] = malloc(sizeof(struct recurrent));
78
79    list->recurrent[list->count]->kill_me = 0;
80    list->recurrent[list->count]->function = func;
81    list->recurrent[list->count]->user = user;
82    list->recurrent[list->count]->start_time = get_us();
83    list->count++;
84
85    return list->count - 1;
86}
87
88
89/* Remove recurrent. Do *NOT* call this from a recurrent function. */
90int remove_recurrent(struct recurrent_list *list, int n)
91{
92
93    if (n >= list->count)
94        return -1;
95
96    memmove(&list->recurrent[n],
97            &list->recurrent[n + 1],
98            sizeof(struct recurrent *) * (list->count - (n + 1)));
99
100    free(list->recurrent[n]);
101    list->recurrent = (struct recurrent **)realloc(list->recurrent,
102                                                   sizeof(sizeof
103                                                          (struct recurrent *))
104                                                   * (list->count));
105    if (!list->recurrent)
106        fprintf(stderr, "Can't allocate memory at %s:%d\n", __FUNCTION__,
107                __LINE__);
108
109
110
111    list->count--;
112    return 0;
113}
Note: See TracBrowser for help on using the repository browser.