File: io_xaw.c

package info (click to toggle)
playmidi 2.4debian-13
  • links: PTS
  • area: main
  • in suites: bookworm
  • size: 804 kB
  • sloc: ansic: 3,691; makefile: 166; sh: 53; sed: 2
file content (450 lines) | stat: -rw-r--r-- 13,724 bytes parent folder | download | duplicates (6)
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
/************************************************************************
   io_xaw.c  -- shows midi events in X11 using Xaw widgets

   Copyright (C) 1995-1996 Nathan I. Laredo

   This program is modifiable/redistributable under the terms
   of the GNU General Public Licence.

   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., 675 Mass Ave, Cambridge, MA 02139, USA.
   Send your comments and all your spare pocket change to
   laredo@gnu.ai.mit.edu (Nathan Laredo) or to PSC 1, BOX 709, 2401
   Kelly Drive, Lackland AFB, TX 78236-5128, USA.
 *************************************************************************/
#include "playmidi.h"
#include "bitmaps.h"
#include <X11/Intrinsic.h>
#include <X11/Shell.h>
#include <X11/Xaw/Form.h>
#include <X11/Xaw/Label.h>
#include <X11/Xaw/Toggle.h>
#include <X11/Xaw/Command.h>
#include <X11/Xaw/AsciiText.h>
#include <X11/Xaw/Scrollbar.h>
#include <X11/StringDefs.h>
#include <sys/time.h>
#include <unistd.h>
#include <errno.h>

extern int chanmask, play_gus, play_fm, play_ext, gus_dev, sb_dev, ext_dev;
extern int graphics, verbose, perc, ntrks;
extern int note_vel[16][128];
extern unsigned long int ticks;
extern char *filename, *gmvoice[256];
extern float skew;
extern void seq_reset();
extern struct chanstate channel[16];
extern void seq_stop_note(int, int, int, int);
extern struct timeval start_time;
struct timeval want_time, now_time;

int cdeltat(t1, t2)
struct timeval *t1;
struct timeval *t2;
{
    int d1, d2;

    d1 = t1->tv_sec - t2->tv_sec;
    if((d2 = t1->tv_usec - t2->tv_usec) < 0)
	(d2 += 1000000, d1 -= 1);
    d2 /= 10000;
    return (d2 + d1 * 100);
}

XtAppContext context;
Widget toplevel, manager, titlebar, status, text, exit_button;
Widget scrollbar, skew_label, nextsong, repeatsong, prevsong;
Pixmap Exit, Next, Play, Prev;	/* button icons */
Widget cbutton[16];		/* program for each channel (also toggles) */
Widget cmeter[16];		/* vu meter for each channel */
#define TEXTSIZE	8192
char textbuf[TEXTSIZE];

int i, return_do_what;
int vu_level[16], vu_delta[16];

char *drumset[11] =
{
 "Standard Kit", "Room Kit", "Power Kit", "Electronic Kit", "TR-808 Kit",
    "Jazz Kit", "Brush Kit", "Orchestra Kit", "Sound FX Kit",
    "Program Kit", "MT-32 Kit"
};

#define SET(x) (x == 8 ? 1 : x >= 16 && x <= 23 ? 2 : x == 24 ? 3 : \
		x == 25  ? 4 : x == 32 ? 5 : x >= 40 && x <= 47 ? 6 : \
		x == 48 ? 7 : x == 56 ? 8 : x >= 96 && x <= 111 ? 9 : \
		x == 127 ? 10 : 0)

void close_show(error, file)
int error;
const char *file;
{
    if (errno)
    {
      if (file)
	fprintf (stderr, "%s: %s\n", file, strerror (errno));
      else
	fprintf (stderr, "%s\n", strerror (errno));
    }
    if (error)
      exit(error);
}

void ExitCallback(w, client_data, call_data)
Widget w;
XtPointer client_data;
XtPointer call_data;
{
    close_show(0, 0);
    exit (0);
}

void MaskCallback(w, client_data, call_data)
Widget w;
XtPointer client_data;
XtPointer call_data;
{
    int i, j, k;
    for (i = 0; i < 16 && cbutton[i] != w; i++);
    if (i < 0 || i >= 16)
	return;
    if (call_data)		/* channel should be enabled */
	chanmask |= (1 << i);
    else {			/* need to stop all notes in channel */
	chanmask &= (~(1 << i));
	if (ISGUS(i))
	    k = gus_dev;
	else if (ISFM(i))
	    k = sb_dev;
	else
	    k = ext_dev;
	for (j = 0; j < 128; j++)
	    if (note_vel[i][j])
		seq_stop_note(k, i, j, note_vel[i][j] = 0);
    }
}

void SkewCallback(w, client_data, call_data)
Widget w;
XtPointer client_data;
XtPointer call_data;
{
    float position;

    position = *(float *) call_data;
    skew = (position * 3.75) + 0.25;
    sprintf(textbuf, "%1.2f", skew);
    XtVaSetValues(skew_label, XtNlabel, textbuf, NULL);
};

void SkewIncCallback(w, client_data, call_data)
Widget w;
XtPointer client_data;
XtPointer call_data;
{
    double s_position;
    intptr_t movewhere = (intptr_t) call_data;
    if (movewhere < 0) {
	skew += 0.01;
	if (skew > 4.0)
	    skew = 4.0;
    } else if (movewhere > 0) {
	skew -= 0.01;
	if (skew < 0.25)
	    skew = 0.25;
    } else
	return;
    s_position = (skew - 0.25) / 3.75;	/* scale is 0.0 - 1.0 */
    XawScrollbarSetThumb(scrollbar, s_position, 0.01);
    sprintf(textbuf, "%1.2f", skew);
    XtVaSetValues(skew_label, XtNlabel, textbuf, NULL);
};

void PrevSongCallback(w, client_data, call_data)
Widget w;
XtPointer client_data;
XtPointer call_data;
{
    if ((skew += 0.01) > 4)
	seq_reset();
    return_do_what = -1;
}

void RepeatCallback(w, client_data, call_data)
Widget w;
XtPointer client_data;
XtPointer call_data;
{
    seq_reset();
    return_do_what = 0;
}

void NextSongCallback(w, client_data, call_data)
Widget w;
XtPointer client_data;
XtPointer call_data;
{
    seq_reset();
    return_do_what = 1;
}

void updateMeter(chn)
int chn;
{
    XtVaSetValues(cmeter[chn], XtNwidth, 5 + vu_level[chn], NULL);
}

XtIntervalId mytimerid;
Boolean return_now;
int oldsec = 0, oldusec = 0;

void TimerCallback(w, client_data, timerid)
Widget w;
XtPointer client_data;
XtIntervalId timerid;
{
    int i, sec, usec;

    /* update vu meter values */
    for (i = 0; i < 16; i++)
	if (vu_delta[i]) {	/* decay meter */
	    vu_level[i] -= vu_delta[i];
	    if (vu_level[i] < 0)
		vu_level[i] = vu_delta[i] = 0;
	    updateMeter(i);
	}
    gettimeofday(&now_time, NULL);
    sec = now_time.tv_sec - start_time.tv_sec;
    usec = now_time.tv_usec - start_time.tv_usec;
    if (usec < 0)
	(usec += 1000000, sec -= 1);
    usec /= 100000;	/* change to 10ths of a second */
    sprintf(textbuf, "%02d:%02d.%d", sec / 60, sec % 60, usec);
    if (sec > oldsec || usec > oldusec) {
	XtVaSetValues(status, XtNstring, textbuf, NULL);
	oldsec = sec; oldusec = usec;
    }
    if (return_do_what != NO_EXIT || cdeltat(&want_time, &now_time) < 15)
	return_now = True;
    else
	mytimerid = XtAppAddTimeOut(context, 100, TimerCallback, NULL);
}

XEvent event;

int updatestatus()
{
    XtInputMask mask;

    return_now = False;
    want_time.tv_sec = start_time.tv_sec + (ticks / 100);
    want_time.tv_usec = start_time.tv_usec + (ticks % 100) * 10000;
    if (want_time.tv_usec > 1000000)
	(want_time.tv_usec -= 1000000, want_time.tv_sec++);
    return_do_what = NO_EXIT;
    TimerCallback(context, NULL, 0);		/* start timer */
    do {	/* toolkit main loop */
	if ((mask = XtAppPending(context)))	/* non-blocking */
	    XtAppProcessEvent(context, mask);
#ifndef HOG_CPU
	if (!mask && !return_now) {	/* sleep 1/10th of a second */
	    usleep(750);
	}
#endif
    } while (!return_now || mask);
    return return_do_what;
}


void AppendText(s)
char *s;
{
    XawTextBlock textblk;
    static XawTextPosition whereami;


    if (s == NULL) {	/* reset text to empty */
	XtVaSetValues(text, XtNstring, "", NULL);
	whereami = 0;
	return;
    }
    textblk.firstPos = 0;
    textblk.length = strlen(s);
    textblk.ptr = s;
    textblk.format = XawFmt8Bit;
    XawTextReplace(text, whereami, whereami, &textblk);
    whereami += textblk.length;
}

#define TEXTBUF		(&textbuf[strlen(textbuf)])

void showevent(cmd, data, length)
int cmd;
unsigned char *data;
int length;
{
    int chn, note, vel, i;

    chn = (cmd & 0x0f);
    note = (data[0] & 0x7f);
    vel = (data[1] & 0x7f);
    if (cmd < 8 && cmd > 0) {
	strncpy(textbuf, (const char *)data, length > TEXTSIZE - 4 ? TEXTSIZE - 4 : length);
	textbuf[(length > TEXTSIZE - 4) ? TEXTSIZE - 4 : length] = '\0';
	sprintf(TEXTBUF, "\n");
	AppendText(textbuf);
	if (verbose > 2)
	    printf("%s", textbuf);
    } else if (cmd & 0x80)
	switch (cmd & 0xf0) {
	case MIDI_NOTEON:
	    if (!vel && vu_level[chn])
		vu_delta[chn] = 32;
	    else
		(vu_level[chn] = vel, vu_delta[chn] = 0);
	    updateMeter(chn);
	    break;
	case MIDI_NOTEOFF:
	    if (vu_level[chn])
		vu_delta[chn] = vel / 2;
	    updateMeter(chn);
	    break;
	case MIDI_PGM_CHANGE:
	    if (!ISPERC(chn))
		XtVaSetValues(cbutton[chn], XtNlabel, gmvoice[note], NULL);
	    else
		XtVaSetValues(cbutton[chn], XtNlabel, drumset[SET(note)],
		NULL);
	    break;
	case 0xf0:
	case 0xf7:
	    sprintf(textbuf, "Sysex(%2x): ", cmd);
	    for (i = 0; i < length && i < (TEXTSIZE / 2) - 16; i++)
		sprintf(TEXTBUF, "%02x", data[i]);
	    sprintf(TEXTBUF, "\n");
	    AppendText(textbuf);
	    if (verbose > 2)
		printf("%s", textbuf);
	    break;
	default:
	    break;
	}
}

void init_show()
{
    int i;
    double s_position;

    AppendText(NULL);	/* reset text window */
    s_position = (skew - 0.25) / 3.75;	/* scale is 0.0 - 1.0 */
    sprintf(textbuf, "%1.2f", skew);
    XawScrollbarSetThumb(scrollbar, s_position, 0.01);
    XtVaSetValues(skew_label, XtNlabel, textbuf, NULL);
    XStoreName(XtDisplay(toplevel), XtWindow(toplevel), filename);
    oldsec = oldusec = 0;
    for (i = 0; i < 16; i++) {
	vu_level[i] = vu_delta[i] = 0;
	updateMeter(i);
	sprintf(textbuf, "channel%02d", i + 1);
	XtVaSetValues(cbutton[i], XtNlabel, textbuf, NULL);
    }
    XtVaSetValues(status, XtNlabel, "00:00.0" , NULL);
}

void setup_show(argc, argv)
int argc;
char **argv;
{
    int i;
    Pixel bg, fg;
    Display *mydisp;
    Screen *myscreen;
    Window mywin;

    graphics++;			/* force -r option if not selected */
    toplevel = XtVaOpenApplication(&context, "XPlaymidi", NULL, 0,
	&argc, argv, NULL, sessionShellWidgetClass, XtNminWidth, 425,
	XtNwidth, 425, XtNminHeight, 479, XtNheight, 500, NULL);
    manager = XtVaCreateManagedWidget("controls", formWidgetClass,
	toplevel, XtNdefaultDistance, 2, NULL);
    titlebar = XtVaCreateManagedWidget("titlebar", labelWidgetClass,
	manager, XtNtop, XawChainTop, XtNright, XawChainRight, XtNleft,
	XawChainLeft, XtNborderWidth, 0, XtNfromVert, cbutton[15],
	XtNlabel, "X" RELEASE " by Nathan Laredo" , NULL);
    skew_label = XtVaCreateManagedWidget("skew", labelWidgetClass, manager,
	XtNborderWidth, 0, XtNleft, XawChainLeft, XtNfromVert, titlebar,
	XtNlabel, "1.00", NULL);
    scrollbar = XtVaCreateManagedWidget("scrollbar", scrollbarWidgetClass,
	manager, XtNorientation, XtorientHorizontal, XtNlength, 365,
	XtNfromVert, titlebar, XtNfromHoriz, skew_label, NULL);
    text = XtVaCreateManagedWidget("text", asciiTextWidgetClass, manager,
	XtNbottom, XawChainBottom, XtNleft, XawChainLeft, XtNfromVert,
	skew_label, XtNeditType, XawtextAppend, XtNheight, 400,
	XtNwidth, 132, XtNscrollHorizontal, XawtextScrollAlways,
	XtNscrollVertical, XawtextScrollAlways, XtNtype, XawAsciiString,
	XtNdisplayCaret, False, NULL);
    for (i = 0; i < 16; i++) {
	sprintf(textbuf, "channel%02d", i + 1);
	cbutton[i] = XtVaCreateManagedWidget(textbuf, toggleWidgetClass,
	    manager, XtNstate, (ISPLAYING(i) ? True : False),
	    XtNwidth, 100, XtNjustify, XtJustifyLeft, XtNfromHoriz, text,
	    XtNresizable, False, XtNresize, False, NULL);
	if (!i)
	    XtVaSetValues(cbutton[i], XtNfromVert, scrollbar, NULL);
	else
	    XtVaSetValues(cbutton[i], XtNfromVert, cbutton[i - 1], NULL);
	XtAddCallback(cbutton[i], XtNcallback, MaskCallback, NULL);
	sprintf(textbuf, "meter%02d", i + 1);
	cmeter[i] = XtVaCreateManagedWidget(textbuf, toggleWidgetClass,
	    manager, XtNsensitive, False, XtNfromHoriz, cbutton[i],
	    XtNlabel, "", XtNresize, False, XtNjustify, XtJustifyLeft,
	    XtNwidth, 5, XtNresizable, True, NULL);
	if (!i)
	    XtVaSetValues(cmeter[i], XtNfromVert, scrollbar, NULL);
	else
	    XtVaSetValues(cmeter[i], XtNfromVert, cmeter[i - 1], NULL);
    }
    exit_button = XtVaCreateManagedWidget("exit", commandWidgetClass,
	manager, XtNfromHoriz, text, XtNfromVert, cbutton[15],
	XtNinternalWidth, 1, XtNinternalHeight, 0, XtNborderWidth, 0, NULL);
    prevsong = XtVaCreateManagedWidget("prevfile", commandWidgetClass,
	manager, XtNfromHoriz, exit_button, XtNfromVert, cbutton[15],
	XtNinternalWidth, 1, XtNinternalHeight, 0, XtNborderWidth, 0, NULL);
    repeatsong = XtVaCreateManagedWidget("repeat", commandWidgetClass,
	manager, XtNfromHoriz, prevsong, XtNfromVert, cbutton[15],
	XtNinternalWidth, 1, XtNinternalHeight, 0, XtNborderWidth, 0, NULL);
    nextsong = XtVaCreateManagedWidget("nextfile", commandWidgetClass,
	manager, XtNfromHoriz, repeatsong, XtNfromVert, cbutton[15],
	XtNinternalWidth, 1, XtNinternalHeight, 0, XtNborderWidth, 0, NULL);
    status = XtVaCreateManagedWidget("status", asciiTextWidgetClass,
	manager, XtNfromHoriz, nextsong, XtNfromVert, cbutton[15],
	XtNborderWidth, 0, XtNstring, "--:--.-", XtNeditType, XawtextRead,
	XtNdisplayCaret, False, XtNuseStringInPlace, True, NULL);
    XtAddCallback(scrollbar, XtNjumpProc, SkewCallback, NULL);
    XtAddCallback(scrollbar, XtNscrollProc, SkewIncCallback, NULL);
    XtAddCallback(exit_button, XtNcallback, ExitCallback, NULL);
    XtAddCallback(nextsong, XtNcallback, NextSongCallback, NULL);
    XtAddCallback(repeatsong, XtNcallback, RepeatCallback, NULL);
    XtAddCallback(prevsong, XtNcallback, PrevSongCallback, NULL);
    myscreen = XtScreen(toplevel);
    mydisp = DisplayOfScreen(myscreen); 
    mywin = RootWindowOfScreen(myscreen);
    fg = WhitePixelOfScreen(myscreen);
    bg = BlackPixelOfScreen(myscreen);
    Exit = XCreatePixmapFromBitmapData(mydisp, mywin, Eject_bits,
	Eject_width, Eject_height, fg, bg, 1);
    XtVaSetValues(exit_button, XtNbitmap, Exit, NULL);
    Prev = XCreatePixmapFromBitmapData(mydisp, mywin, Prev_bits,
	Prev_width, Prev_height, fg, bg, 1);
    XtVaSetValues(prevsong, XtNbitmap, Prev, NULL);
    Play = XCreatePixmapFromBitmapData(mydisp, mywin, Play_bits,
	Play_width, Play_height, fg, bg, 1);
    XtVaSetValues(repeatsong, XtNbitmap, Play, NULL);
    Next = XCreatePixmapFromBitmapData(mydisp, mywin, Next_bits,
	Next_width, Next_height, fg, bg, 1);
    XtVaSetValues(nextsong, XtNbitmap, Next, NULL);
    XtRealizeWidget(toplevel);
}