File: timer.c

package info (click to toggle)
pcb-rnd 2.1.1-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 29,260 kB
  • sloc: ansic: 198,059; sh: 5,767; yacc: 5,568; makefile: 2,519; awk: 1,737; lex: 1,073; python: 519; lisp: 169; tcl: 67; xml: 40; perl: 34; ruby: 5
file content (152 lines) | stat: -rw-r--r-- 4,051 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/*
 *                            COPYRIGHT
 *
 *  pcb-rnd, interactive printed circuit board design
 *  Copyright (C) 2018 Tibor 'Igor2' Palinkas
 *
 *  This module, debug, was written and is Copyright (C) 2016 by Tibor Palinkas
 *  this module is also subject to the GNU GPL as described below
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 *  Contact:
 *    Project page: http://repo.hu/projects/pcb-rnd
 *    lead developer: http://repo.hu/projects/pcb-rnd/contact.html
 *    mailing list: pcb-rnd (at) list.repo.hu (send "subscribe")
 */

#include "config.h"

#include <genvector/vtp0.h>

typedef struct {
	double next, period;
	long count;
	char *user_data;
	char aname[1];
} script_timer_t;

vtp0_t timers;
int timer_running = 0;

static void start_timer(void);
static void timer_cb(pcb_hidval_t hv)
{
	long n, len;
	double now;

	len = vtp0_len(&timers);
	if (len == 0) {
		timer_running = 0;
		return; /* don't even restart the timer if we are not waiting for anything */
	}

	now = pcb_dtime();

	/* we could do something clever here, e.g. sort timers by ->next but
	   in reality there will be so few timers at any given time that it's
	   probably not worth the effort */
	for(n = len-1; n >= 0; n--) {
		script_timer_t *t = timers.array[n];
		fgw_func_t *f;
		fgw_arg_t res, argv[4];

		if (t->next <= now) {
			t->next += t->period;

			f = fgw_func_lookup(&pcb_fgw, t->aname);
			if (f == NULL)
				goto remove;
			argv[0].type = FGW_FUNC;
			argv[0].val.func = f;
			argv[1].type = FGW_DOUBLE;
			argv[1].val.nat_double = now;
			argv[2].type = FGW_LONG;
			argv[2].val.nat_long = t->count;
			if (t->user_data != NULL) {
				argv[3].type = FGW_STR;
				argv[3].val.str = t->user_data;
			}
			else {
				argv[3].type = FGW_STR;
				argv[3].val.str = "";
			}

			res.type = FGW_INVALID;
			if (pcb_actionv_(f, &res, 4, argv) != 0)
				goto remove;
			fgw_arg_conv(&pcb_fgw, &res, FGW_INT);
			if ((res.type != FGW_INT) || (res.val.nat_int != 0)) /* action requested timer removal */
				goto remove;

			if (t->count > 0) {
				t->count--;
				if (t->count == 0) {
					remove:;
					vtp0_remove(&timers, n, 1);
					free(t->user_data);
					free(t);
				}
			}
		}
	}

	start_timer();
}

static void start_timer(void)
{
	static pcb_hidval_t hv;
	timer_running = 1;
	pcb_gui->add_timer(timer_cb, 100, hv);
}


static const char pcb_acth_AddTimer[] = "Add a new timer";
static const char pcb_acts_AddTimer[] = "AddTimer(action, period, [repeat], [userdata])";
/* DOC: addtimer.html */
static fgw_error_t pcb_act_AddTimer(fgw_arg_t *res, int argc, fgw_arg_t *argv)
{
	const char *act, *user_data = NULL;
	double period;
	int count = 1, len;
	char fn[PCB_ACTION_NAME_MAX];
	script_timer_t *t;

	PCB_ACT_CONVARG(1, FGW_STR, AddTimer, act = argv[1].val.str);
	PCB_ACT_CONVARG(2, FGW_DOUBLE, AddTimer, period = argv[2].val.nat_double);
	PCB_ACT_MAY_CONVARG(3, FGW_INT, AddTimer, count = argv[3].val.nat_int);
	PCB_ACT_MAY_CONVARG(4, FGW_STR, AddTimer, user_data = argv[4].val.str);

	pcb_aname(fn, act);
	len = strlen(fn);
	t = malloc(sizeof(script_timer_t) + len);
	t->next = pcb_dtime() + period;
	t->period = period;
	t->count = count;
	strcpy(t->aname, fn);
	if (user_data != NULL)
		t->user_data = pcb_strdup(user_data);
	else
		t->user_data = NULL;

	vtp0_append(&timers, t);

	if (!timer_running)
		start_timer();

	PCB_ACT_IRES(0);
	return 0;
}