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 #include "math.h"
23 #include "brightoninternals.h"
24
25 static float winwidth, winheight;
26
27 int
destroyRibbon(brightonDevice * dev)28 destroyRibbon(brightonDevice *dev)
29 {
30 printf("destroyRibbon()\n");
31
32 if (dev->image)
33 brightonFreeBitmap(dev->bwin, dev->image);
34 dev->image = NULL;
35
36 return(0);
37 }
38
39 static int
displayRibben(brightonDevice * dev)40 displayRibben(brightonDevice *dev)
41 {
42 brightonIResource *panel;
43
44 if (dev->bwin->app->resources[dev->panel].flags & BRIGHTON_WITHDRAWN)
45 return(0);
46
47 panel = &dev->bwin->app->resources[dev->panel];
48
49 dev->lastvalue = dev->value;
50 dev->lastposition = dev->position;
51
52 return(0);
53 }
54
considercallback(brightonDevice * dev)55 static void considercallback(brightonDevice *dev)
56 {
57 brightonIResource *panel = &dev->bwin->app->resources[dev->panel];
58
59 if (dev->lastvalue != dev->value)
60 {
61 if (panel->devlocn[dev->index].callback)
62 {
63 panel->devlocn[dev->index].callback(dev->bwin, dev->panel,
64 dev->index, dev->value);
65 } else {
66 if (panel->callback)
67 panel->callback(dev->bwin, dev->panel, dev->index, dev->value);
68 }
69 }
70
71 dev->lastvalue = dev->value;
72 dev->lastposition = dev->position;
73 }
74
75 static float rbckeymap[40] = {
76 0.0f,
77 0.0f,
78 2.0f,
79 2.0f,
80 4.0f,
81 4.0f,
82 5.0f,
83 5.0f,
84 7.0f,
85 7.0f,
86 9.0f,
87 9.0f,
88 11.0f,
89 11.0f,
90 12.0f,
91 12.0f,
92 14.0f,
93 14.0f,
94 16.0f,
95 16.0f,
96 0.0f,
97 1.0f,
98 1.0f,
99 3.0f,
100 3.0f,
101 4.0f,
102 5.0f,
103 6.0f,
104 6.0f,
105 8.0f,
106 8.0f,
107 10.0f,
108 10.0f,
109 11.0f,
110 12.0f,
111 13.0f,
112 13.0f,
113 15.0f,
114 15.0f,
115 16.0f,
116 };
117
118 static float
getRibbonKey(int x,int y,int w,int h)119 getRibbonKey(int x, int y, int w, int h)
120 {
121 int v;
122
123 if ((x <= 0) || (x >= w) || (y <= 0) || (y >= h))
124 return(-1.0f);
125
126 if (((v = x * 20 / w) < 0) || (v >= 20))
127 return(-1.0f);
128
129 if ((y << 1) < h) {
130 /* Black key */
131 return(rbckeymap[v + 20]);
132 } else {
133 /* White key */
134 return(rbckeymap[v]);
135 }
136
137 return(-1.0f);
138 }
139
140 static int
configure(brightonDevice * dev,brightonEvent * event)141 configure(brightonDevice *dev, brightonEvent *event)
142 {
143 //printf("configureRibbon(%i)\n", event->command);
144
145 if (event->command == -1)
146 return(-1);
147
148 if (event->command == BRIGHTON_RESIZE)
149 {
150 dev->originx = event->x;
151 dev->originy = event->y;
152
153 dev->x = event->x;
154 dev->y = event->y;
155 dev->width = event->w;
156 dev->height = event->h;
157
158 //printf("Ribben resize %i %i, %i %i, %i %i: %f\n",
159 //event->x, event->y, dev->x, dev->y, dev->width, dev->height, dev->value);
160
161 dev->lastvalue = -1;
162 displayRibben(dev);
163
164 return(0);
165 }
166
167 if (event->command == BRIGHTON_BUTTONPRESS)
168 {
169 if (dev->width == 0)
170 return(0);
171
172 dev->value = getRibbonKey(event->x, event->y, dev->width, dev->height);
173
174 //printf("Ribben press %i %i, %i %i, %i %i: %f\n",
175 //event->x, event->y, dev->x, dev->y, dev->width, dev->height, dev->value);
176
177 considercallback(dev);
178
179 return(0);
180 }
181
182 if (event->command == BRIGHTON_BUTTONRELEASE)
183 {
184 //printf("Ribben release %i %i, %i %i, %i %i: %f\n",
185 //event->x, event->y, dev->x, dev->y, dev->width, dev->height, dev->value);
186
187 dev->value = -1;
188
189 considercallback(dev);
190
191 return(0);
192 }
193
194 if (event->command == BRIGHTON_MOTION)
195 {
196 //printf("Ribben motion %i %i, %i %i, %i %i: %f\n",
197 //event->x, event->y, dev->x, dev->y, dev->width, dev->height, dev->value);
198
199 dev->value = getRibbonKey(event->x, event->y, dev->width, dev->height);
200
201 considercallback(dev);
202
203 return(0);
204 }
205 return(0);
206 }
207
208 int *
createRibbon(brightonWindow * bwin,brightonDevice * dev,int index,char * bitmap)209 createRibbon(brightonWindow *bwin, brightonDevice *dev, int index, char *bitmap)
210 {
211 //printf("createRibbon(%s): %i\n", bitmap, index);
212
213 winwidth = bwin->display->width / 2;
214 winheight = bwin->display->height / 2;
215
216 dev->destroy = destroyRibbon;
217 dev->configure = configure;
218 dev->index = index;
219
220 dev->bwin = bwin;
221
222 if (bitmap == 0)
223 {
224 if (dev->image)
225 brightonFreeBitmap(bwin, dev->image);
226
227 /*
228 * Open the default bitmap
229 */
230 if (bwin->app->resources[dev->panel].devlocn[dev->index].image != 0)
231 dev->image =
232 bwin->app->resources[dev->panel].devlocn[dev->index].image;
233 if (dev->image == 0)
234 dev->image = brightonReadImage(bwin, "bitmaps/images/pointer.xpm");
235 } else {
236 if (dev->image)
237 brightonFreeBitmap(bwin, dev->image);
238 dev->image = brightonReadImage(bwin, bitmap);
239 if (dev->image == 0)
240 dev->image = brightonReadImage(bwin, "bitmaps/images/pointer.xpm");
241 }
242
243 dev->lastvalue = -1;
244 dev->lastposition = 0;
245
246 return(0);
247 }
248
249