1 
2 /*
3  *  Diverse Bristol audio routines.
4  *  Copyright (c) by Nick Copeland <nickycopeland@hotmail.com> 1996,2012
5  *
6  *
7  *   This program is free software; you can redistribute it and/or modify
8  *   it under the terms of the GNU General Public License as published by
9  *   the Free Software Foundation; either version 3 of the License, or
10  *   (at your option) any later version.
11  *
12  *   This program is distributed in the hope that it will be useful,
13  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *   GNU General Public License for more details.
16  *
17  *   You should have received a copy of the GNU General Public License
18  *   along with this program; if not, see <http://www.gnu.org/licenses/>.
19  *
20  */
21 
22 /*
23  * Slow timer events.
24  *
25  * Devices can register for slow timers, cancel requests, and a call has to be
26  * made to clock the slow timer. Per default this is from brighton.c on the
27  * active sense timeout, default every 1000ms.
28  */
29 #include "brightoninternals.h"
30 
31 static struct {
32 	brightonWindow *win;
33 	int panel;
34 	int index;
35 } brightonSTL[1024];
36 
37 static void
brightonScanTimerList()38 brightonScanTimerList()
39 {
40 	int i;
41 	brightonEvent event;
42 
43 //printf("Brighton ST Clock scan\n");
44 
45 	for (i = BRIGHTON_ST_FIRST; i < 1024; i++)
46 	{
47 		if (brightonSTL[i].win != 0)
48 		{
49 //printf("matched on %i/%i\n", brightonSTL[i].panel, brightonSTL[i].index);
50 
51 			event.command = BRIGHTON_SLOW_TIMER;
52 			event.value = 0;
53 			brightonParamChange(brightonSTL[i].win, brightonSTL[i].panel,
54 				brightonSTL[i].index, &event);
55 		}
56 	}
57 }
58 
59 static int
brightonSTRegister(brightonWindow * bwin,brightonDevice * dev)60 brightonSTRegister(brightonWindow *bwin, brightonDevice *dev)
61 {
62 	int i, free = 0;
63 
64 	/*
65 	 * We should scan our list to see if this is a duplicate then insert it
66 	 * at the first free location
67 	 */
68 	for (i = BRIGHTON_ST_FIRST; i < 1024; i++)
69 	{
70 		if ((brightonSTL[i].win == NULL) && (free == 0))
71 			free = i;
72 
73 		if ((brightonSTL[i].win == bwin)
74 			&& (brightonSTL[i].panel == dev->panel)
75 			&& (brightonSTL[i].index == dev->index))
76 			return(i);
77 	}
78 
79 	if (free == 0)
80 		return(-1);
81 
82 	brightonSTL[free].win = bwin;
83 	brightonSTL[free].panel = dev->panel;
84 	brightonSTL[free].index = dev->index;
85 
86 //printf("Register slow timer %i: %x/%i/%i\n", free, (size_t) bwin, brightonSTL[free].panel, brightonSTL[free].index);
87 
88 	return(-1);
89 }
90 
91 static int
brightonSTDegister(brightonWindow * bwin,brightonDevice * dev,int id)92 brightonSTDegister(brightonWindow *bwin, brightonDevice *dev, int id)
93 {
94 	int i;
95 
96 	if ((brightonSTL[id].win == bwin)
97 		&& (brightonSTL[id].panel == dev->panel)
98 		&& (brightonSTL[id].index == dev->index))
99 	{
100 //printf("Deregister slow timer %i: %i/%i\n", id, brightonSTL[id].panel, brightonSTL[id].index);
101 		brightonSTL[id].win = NULL;
102 		return(id);
103 	}
104 
105 	/*
106 	 * If we get here then the deregister failed on the index. Scan the list
107 	 * to see if it is still a known timer
108 	 */
109 	for (i = BRIGHTON_ST_FIRST; i < 1024; i++)
110 	{
111 		if ((brightonSTL[i].win == bwin)
112 			&& (brightonSTL[i].panel == dev->panel)
113 			&& (brightonSTL[i].index == dev->index))
114 		{
115 //printf("Deregister slow timer %i: %i/%i\n", i, brightonSTL[i].panel, brightonSTL[i].index);
116 			brightonSTL[i].win = NULL;
117 			return(i);
118 		}
119 	}
120 
121 //printf("Failed to deregister slow timer %i/%i\n", brightonSTL[id].panel, brightonSTL[id].index);
122 
123 	return(-1);
124 }
125 
126 int
brightonSlowTimer(brightonWindow * bwin,brightonDevice * dev,int command)127 brightonSlowTimer(brightonWindow *bwin, brightonDevice *dev, int command)
128 {
129 //printf("brightonSlowTimer(%i)\n", command);
130 
131 	if (command < 0)
132 		return(command);
133 
134 	switch (command) {
135 		case BRIGHTON_ST_CLOCK:
136 			brightonScanTimerList();
137 			break;
138 		case BRIGHTON_ST_REQ:
139 			return(brightonSTRegister(bwin, dev));
140 		case BRIGHTON_ST_CANCEL:
141 		default:
142 			return(brightonSTDegister(bwin, dev, command));
143 	}
144 
145 	return(0);
146 }
147 
148