source: trunk/tools/neercs/old/recurrent.c @ 1656

Last change on this file since 1656 was 1656, checked in by sam, 7 years ago

neercs: start porting the old code to Windows; this breaks the Visual Studio
build until further fixes are made.

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