File: mixerwatch.c

package info (click to toggle)
osdsh 0.7.0-10.2
  • links: PTS
  • area: main
  • in suites: stretch
  • size: 452 kB
  • ctags: 346
  • sloc: ansic: 1,063; tcl: 335; makefile: 73
file content (162 lines) | stat: -rw-r--r-- 4,092 bytes parent folder | download | duplicates (3)
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
#include "osdsh.h"
#include "../config.h"
#define IAM 0
#define MYNAME "Mixer"

// globally defined by Joachim
// char mixerdevice[PATH_MAX+1] = MIXER;
int dev;
settings_t mixerset;
int vol[SOUND_MIXER_NRDEVICES];
int old_vol[SOUND_MIXER_NRDEVICES];
char *devicelabels[SOUND_MIXER_NRDEVICES]=SOUND_DEVICE_LABELS;

void *mixer_watch(void *arg);

/* ==================== REQUIERED FOR PLUGINS ============================== 

***** ID of the plugin, contact me before you asign this to your plugin!! *****
*/
int whoami(void) {
	return IAM;
}

/* Return a pointer to the name of the plugin */
char *mynameis() {
	char *a = MYNAME;
	return a;
}

/* This function sets the default (initial) values for our osd 
	Here i just load the default values, but you can load
	your own in your plugin.
*/
void initialize_myosd() {
	set_defaults(&mixerset);
}

/* This function takes a command and arguments, if the command is ours, execute
   it and return 1, if not, return 0*/

int isitmine( char command[BUFSIZ], char arg_first[BUFSIZ],
			char arg_secound[BUFSIZ])
{
	int i=1;

	if(strcmp(command, "stop")==0) {
		mixerset.displaying = 0;
	}
	else if(strcmp(command, "start")==0) {
		if(!mixerset.displaying) {
			mixerset.displaying = 1;
			pthread_create(&mixerset.mythread, NULL, mixer_watch, NULL);
		}
	}
	else if (strcmp(command, "smix")==0) {
		control_options(&mixerset, arg_first, arg_secound);
	}
	else if (strcmp(command, "mixr")==0) {
		if (atoi(arg_first)) {
			isitmine("start", NULL, NULL);
		}
		else {
			mixerset.displaying = 0;
		}
	}
	else {
		i = 0;
	}
	return i;
}




/*The next functions were taken from osdd-0.0.2,
written by W. Michael Petullo <osdd@flyn.org>, with some ideas, and slightly
(one or two chars) modified by me*/

/* ============================ initialize_vols () ========================= */
void initialize_vols( int devmask)
{
    int i;
    int volume;
    for (i=0;i<SOUND_MIXER_NRDEVICES;i++) {
	if (devmask & (1 << i)) {
	    if (ioctl(dev, MIXER_READ(i), &volume) == -1) {
            	perror(mixerdevice);
		mixerset.displaying=0;
	    }
	    old_vol[i] = vol[i] = volume;
	}
    }
}

/* ============================ display_channel () ========================= */
void display_channel(int channel)
{
    char channel_name[BUFSIZ];
    if (!channel) 
	sprintf(channel_name, "Master Volume");
    else 
	sprintf(channel_name, "%s Volume", devicelabels[channel]);

    if (ioctl(dev, MIXER_READ(channel), &vol[channel]) == -1) {
        perror(mixerdevice);
        mixerset.displaying = 0;
    }
    /*if muted, we say it!!*/
    if (!(vol[0] & 0xff || (vol[0] >> 8) & 0xff)) {
        xosd_display(mixerset.myosd, mixerset.position, XOSD_bottom, "Muted");
        xosd_display(mixerset.myosd, !mixerset.position, XOSD_bottom, "");
    }
    /* not muted, so lets check if the vol changed, if so, display
    them, if not, just ignore it*/
    else if (vol[channel] != old_vol[channel]) {
	xosd_display(mixerset.myosd, 0, XOSD_string, channel_name);
	xosd_display(mixerset.myosd, 1, XOSD_percentage,
		(((vol[channel]>>8)&0xff)+vol[channel]&0xff)/2);
    }
}

/*================================= mixer_watch() ============================*/
void *mixer_watch(void *arg)
{
    int devmask;
    int i;
    /* we open the osd for the mixer*/
    /* Mixer usually goes on the bottom of the screen... so*/

    mixerset.position = XOSD_bottom;

    mixerset.myosd = xosd_create(2);
    initialize_osd(&mixerset);

    /*open the mixer device for reading*/

    if ((dev = open(mixerdevice, O_RDWR)) < 0) {
	perror("Error opening");
        perror(mixerdevice);
        pthread_exit(0);
    }
    if (ioctl(dev, SOUND_MIXER_READ_DEVMASK, &devmask) == -1) {
	perror("Error reading");
        perror(mixerdevice);
        pthread_exit(0);
    }
    /*initialize the volume.....*/
    initialize_vols(devmask);

    while(mixerset.displaying) {
	    for (i=0;i<SOUND_MIXER_NRDEVICES;i++) {
		if (devmask & (1 << i)) {
		    display_channel(i);
		    old_vol[i] = vol[i];
		}
	    }
	    usleep(1);
    }
    xosd_destroy(mixerset.myosd);
    pthread_exit(0);
}