1 /* WMix 3.0 -- a mixer using the OSS mixer API.
2  * Copyright (C) 2000, 2001
3  *	Daniel Richard G. <skunk@mit.edu>,
4  *	timecop <timecop@japan.co.jp>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19  */
20 
21 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif
24 
25 #include <stdio.h>
26 #include <assert.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <sys/time.h>
30 
31 #include "include/common.h"
32 #include "include/misc.h"
33 
34 typedef struct {
35     int enable;
36     int x;
37     int y;
38     int width;
39     int height;
40 } MRegion;
41 MRegion mr[16];
42 
43 extern Config config;
44 
45 /* Converts separate left and right channel volumes (each in [0, 1]) to
46  * volume and balance values. (Volume is in [0, 1], balance is in [-1, 1])
47  */
lr_to_vb(float left,float right,float * volume,float * balance)48 void lr_to_vb(float left, float right, float *volume, float *balance)
49 {
50     assert((left >= 0.0) && (right >= 0.0));
51 
52     *volume = MAX(left, right);
53 
54     if (left > right)
55 	*balance = -1.0 + right / left;
56     else if (right > left)
57 	*balance = 1.0 - left / right;
58     else
59 	*balance = 0.0;
60 }
61 
62 /* Performs the reverse calculation of lr_to_vb()
63  */
vb_to_lr(float volume,float balance,float * left,float * right)64 void vb_to_lr(float volume, float balance, float *left, float *right)
65 {
66 /*    *left = volume; *right = volume; return; // XXX */
67 
68     *left = volume * (1.0 - MAX(0.0, balance));
69     *right = volume * (1.0 + MIN(0.0, balance));
70 }
71 
get_current_time(void)72 double get_current_time(void)
73 {
74     struct timeval tv;
75     double t;
76 
77     gettimeofday(&tv, NULL);
78 
79     t = (double)tv.tv_sec;
80     t += (double)tv.tv_usec / 1.0e6;
81 
82     return t;
83 }
84 
add_region(int index,int x,int y,int width,int height)85 void add_region(int index, int x, int y, int width, int height)
86 {
87     mr[index].enable = 1;
88     mr[index].x = x;
89     mr[index].y = y;
90     mr[index].width = width;
91     mr[index].height = height;
92 }
93 
check_region(int x,int y)94 int check_region(int x, int y)
95 {
96     register int i;
97     bool found = false;
98 
99     for (i = 0; i < 16 && !found; i++) {
100 	if (mr[i].enable && x >= mr[i].x &&
101 	    x <= mr[i].x + mr[i].width &&
102 	    y >= mr[i].y && y <= mr[i].y + mr[i].height)
103 	    found = true;
104     }
105     if (!found)
106 	return -1;
107     return (i - 1);
108 }
109 
config_read(void)110 void config_read(void)
111 {
112     FILE *fp;
113     char buf[512];
114     char *ptr;
115 
116     if (config.file == NULL)
117 	return;
118 
119     fp = fopen(config.file, "r");
120     if (!fp)
121 	return;
122 
123     while (fgets(buf, 512, fp)) {
124 	if ((ptr = strstr(buf, "mousewheel="))) {
125 	    ptr += 11;
126 	    config.mousewheel = atoi(ptr);
127 	}
128 	if ((ptr = strstr(buf, "scrolltext="))) {
129 	    ptr += 11;
130 	    config.scrolltext = atoi(ptr);
131 	}
132 	if ((ptr = strstr(buf, "osd="))) {
133 	    ptr += 4;
134 	    config.osd = atoi(ptr);
135 	}
136 	if ((ptr = strstr(buf, "osdcolor="))) {
137 	    char *end;
138 	    ptr += 9;
139 	    end = strchr(ptr, '\n');
140 	    ptr[end - ptr] = '\0';
141 	    if (config.osd_color)
142 		free(config.osd_color);
143 	    config.osd_color = strdup(ptr);
144 	}
145 	if ((ptr = strstr(buf, "wheelstep="))) {
146 	    ptr += 10;
147 	    /* detect old style config */
148 	    if (atoi(ptr) > 1)
149 		config.scrollstep = (float)atoi(ptr) / 100.0;
150 	    else
151 		config.scrollstep = atof(ptr);
152 	}
153 	if ((ptr = strstr(buf, "wheelbtn1="))) {
154 	    ptr += 10;
155 	    config.wheel_button_up = atoi(ptr);
156 	}
157 	if ((ptr = strstr(buf, "wheelbtn2="))) {
158 	    ptr += 10;
159 	    config.wheel_button_down = atoi(ptr);
160 	}
161     }
162     fclose(fp);
163 }
164