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

Last change on this file since 2495 was 2495, checked in by Jean-Yves Lamoureux, 12 years ago
  • Removed general local variables from main() and put it in struct screen_list
  • Moved most of the input handling to input.c
  • Moved lock feature to lock.c
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 2401 2008-06-15 12:50:12Z 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 <cucul.h>
28#include <caca.h>
29
30#include "neercs.h"
31
32
33int handle_recurrents(struct screen_list* screen_list)
34{
35    int refresh = 0, i;
36    /* Recurrent functions */
37    for(i=0; i<screen_list->recurrent_list->count; i++)
38    {
39        if(screen_list->recurrent_list->recurrent[i]->function)
40        {
41            refresh |= screen_list->recurrent_list->recurrent[i]->function(screen_list,
42                                                                           screen_list->recurrent_list->recurrent[i],
43                                                                           screen_list->recurrent_list->recurrent[i]->user,
44                                                                           get_us());
45        }
46    }
47    /* Delete recurrent functions */
48    for(i=0; i<screen_list->recurrent_list->count; i++)
49    {
50        if(screen_list->recurrent_list->recurrent[i]->kill_me)
51        {
52            remove_recurrent(screen_list->recurrent_list, i);
53            i = 0;
54        }
55    }
56    return refresh;
57}
58
59
60/* Add recurrent function. It will be called at each main loop iteration, unless it is removed */
61int add_recurrent(struct recurrent_list *list,
62                  int (*func)(struct screen_list*, struct recurrent* rec, void *user, long long unsigned int t),
63                  void *user)
64{
65    if(list == NULL || func == NULL) return -1;
66
67    list->recurrent = (struct recurrent**) realloc(list->recurrent,
68                                                   sizeof(struct recurrent*)
69                                                   * (list->count+1));
70
71    if(!list->recurrent)
72        fprintf(stderr, "Can't allocate memory at %s:%d\n", __FUNCTION__, __LINE__);
73
74    list->recurrent[list->count] = malloc(sizeof(struct recurrent));
75
76    list->recurrent[list->count]->kill_me    = 0;
77    list->recurrent[list->count]->function   = func;
78    list->recurrent[list->count]->user       = user;
79    list->recurrent[list->count]->start_time = get_us();
80    list->count++;
81
82    return list->count-1;
83}
84
85
86/* Remove recurrent. Do *NOT* call this from a recurrent function. */
87int remove_recurrent(struct recurrent_list *list, int n)
88{
89
90    if(n>=list->count) return -1;
91
92    memmove(&list->recurrent[n],
93            &list->recurrent[n+1],
94            sizeof(struct recurrent*)*(list->count-(n+1)));
95
96    free(list->recurrent[n]);
97    list->recurrent = (struct recurrent**) realloc(list->recurrent,
98                                             sizeof(sizeof(struct recurrent*))
99                                             * (list->count));
100    if(!list->recurrent)
101        fprintf(stderr, "Can't allocate memory at %s:%d\n", __FUNCTION__, __LINE__);
102
103
104
105    list->count--;
106    return 0;
107}
Note: See TracBrowser for help on using the repository browser.