File: xsound.C

package info (click to toggle)
flying 6.20-6
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k, lenny
  • size: 752 kB
  • ctags: 1,873
  • sloc: cpp: 10,966; makefile: 189
file content (161 lines) | stat: -rw-r--r-- 3,290 bytes parent folder | download | duplicates (4)
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
#ifndef _global_h
#	include "global.h"
#endif

// to get: FIONREAD
#include <sys/ioctl.h>
// to get: unlink()
// #include <unistd.h>

#include <X11/Xlib.h>

#ifdef SOLARIS
#	include <sys/filio.h>
#endif

#define	QUIT	"quit"

#define	DBG	0

static XKeyboardState	old_keyboard_values;
static Display	*xsound_dpy;
static pid_t pid;
static int pipefd[2];
static int sound_on=0;
static void local_usleep( long time )
{
struct timeval  timeout;
int    nfound;

   timeout.tv_sec  = (long)0;
   timeout.tv_usec = (long)time;

   nfound=select(0,0,0,0,&timeout);
}

static void sound_loop( char *disp_name ) {
XKeyboardControl	values;
char		buffer[80];
int		valid;
int		pitch, percent,duration;
int		pri, old_pri;
int		len;

	xsound_dpy = XOpenDisplay(disp_name);
	if (!xsound_dpy) {
		fprintf( stderr, "can't open display\n" );
		exit(-1);
	}
	XGetKeyboardControl(xsound_dpy,&old_keyboard_values);

	valid=0;
	do {
		old_pri = -1;

		len=read(0,buffer+valid,sizeof(buffer)-valid);
		if (len<=0)		goto exit_sound;
#if (DBG)
		printf( "* at least %d bytes\n", len);
		fflush(stdout);
#endif

		do {
			valid += len;
			char	*lf = strchr(buffer,'\n');

			while (lf) {
				*lf = '\0';
#if (DBG)
				printf( "* %s\n", buffer );
				fflush(stdout);
#endif
				if ( !strcmp(buffer,QUIT) )		goto exit_sound;
				if ( sscanf(buffer,"%d %d %d %d",&pitch,&percent,&duration,&pri)
										==4 ) {
					if (pri>old_pri) {
						values.bell_pitch=pitch;
						values.bell_percent=percent;
						values.bell_duration=duration;
						old_pri=pri;
					}
				}
				valid-=(lf-buffer+1);
#if (0)
	//
	// If you got trouble with memmove, than you've got to change this switch
	//
				memmove(buffer,lf+1,valid);
#else
				for (int i=0;i<valid;i++)	buffer[i]=lf[i+1];
#endif

				lf = strchr(buffer,'\n');
			}

			ioctl(0,FIONREAD,&len);
			if (len)	len=read(0,buffer+valid,sizeof(buffer)-valid);
		} while(len>0);
	
#if (DBG)	
		printf( "* ------------------------\n" );
		fflush(stdout);
#endif
		XChangeKeyboardControl(xsound_dpy,KBBellPercent|KBBellPitch|KBBellDuration,&values);
		XBell(xsound_dpy,values.bell_percent);
		XFlush(xsound_dpy);
		local_usleep(200000);
	}
	while(1);

exit_sound:

#if (0)
	values.bell_pitch    = old_keyboard_values.bell_pitch;
	values.bell_percent  = old_keyboard_values.bell_percent;
	values.bell_duration = old_keyboard_values.bell_duration;
#else
	values.bell_pitch    = 400;
	values.bell_percent  =  50;
	values.bell_duration = 100;
#endif
	XChangeKeyboardControl(xsound_dpy,KBBellPercent|KBBellPitch|KBBellDuration,&values);

	XCloseDisplay( xsound_dpy );
	printf( "* subprocess xsound termiated\n" );
	exit(0);
}



void init_sound( char *disp_name ) {

	pipe (pipefd);

	pid=fork();
	if (pid == (pid_t)0) {
		printf( "* subprocess xsound started\n" );
		close(0);					// stdin schlie´┐Żen		(Sohn)
		dup (pipefd[0]);
		close (pipefd[1]);
		sound_loop(disp_name);
	}
	else {
		close (pipefd[0]);		//								(Vater)
		sound_on = 1;
	}
}

void end_sound() {
	if (!sound_on)		return;
	write( pipefd[1], QUIT "\n", strlen(QUIT)+1 );
	close( pipefd[1] );
}


void do_sound( int pitch, int percent, int duration, int pri ) {
char buffer[80];

	if (!sound_on)		return;
	sprintf( buffer, "%d %d %d %d\n", pitch, percent, duration, pri );
	write( pipefd[1],buffer,strlen(buffer));
}