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 | /* Add recurrent function. It will be called at each main loop iteration, unless it is removed */ |
---|
33 | int add_recurrent(struct recurrent_list *list, |
---|
34 | int (*func)(struct screen_list*, struct recurrent* rec, void *user, long long unsigned int t), |
---|
35 | void *user) |
---|
36 | { |
---|
37 | if(list == NULL || func == NULL) return -1; |
---|
38 | |
---|
39 | list->recurrent = (struct recurrent**) realloc(list->recurrent, |
---|
40 | sizeof(struct recurrent*) |
---|
41 | * (list->count+1)); |
---|
42 | |
---|
43 | if(!list->recurrent) |
---|
44 | fprintf(stderr, "Can't allocate memory at %s:%d\n", __FUNCTION__, __LINE__); |
---|
45 | |
---|
46 | list->recurrent[list->count] = malloc(sizeof(struct recurrent)); |
---|
47 | |
---|
48 | list->recurrent[list->count]->kill_me = 0; |
---|
49 | list->recurrent[list->count]->function = func; |
---|
50 | list->recurrent[list->count]->user = user; |
---|
51 | list->recurrent[list->count]->start_time = get_us(); |
---|
52 | list->count++; |
---|
53 | |
---|
54 | return list->count-1; |
---|
55 | } |
---|
56 | |
---|
57 | |
---|
58 | /* Remove recurrent. Do *NOT* call this from a recurrent function. */ |
---|
59 | int remove_recurrent(struct recurrent_list *list, int n) |
---|
60 | { |
---|
61 | |
---|
62 | if(n>=list->count) return -1; |
---|
63 | |
---|
64 | memmove(&list->recurrent[n], |
---|
65 | &list->recurrent[n+1], |
---|
66 | sizeof(struct recurrent*)*(list->count-(n+1))); |
---|
67 | |
---|
68 | free(list->recurrent[n]); |
---|
69 | list->recurrent = (struct recurrent**) realloc(list->recurrent, |
---|
70 | sizeof(sizeof(struct recurrent*)) |
---|
71 | * (list->count)); |
---|
72 | if(!list->recurrent) |
---|
73 | fprintf(stderr, "Can't allocate memory at %s:%d\n", __FUNCTION__, __LINE__); |
---|
74 | |
---|
75 | |
---|
76 | |
---|
77 | list->count--; |
---|
78 | return 0; |
---|
79 | } |
---|