wmix: rewrote parser of config file to report problems to user
[dockapps.git] / wmix / misc.c
blob9120bd97584e22bf89f8b439f8a49cd3aac94082
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>
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.
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.
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif
25 #include <sys/types.h>
26 #include <stdio.h>
27 #include <assert.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <unistd.h>
31 #include <sys/time.h>
33 #include "include/common.h"
34 #include "include/misc.h"
36 typedef struct {
37 int enable;
38 int x;
39 int y;
40 int width;
41 int height;
42 } MRegion;
43 MRegion mr[16];
46 /* Converts separate left and right channel volumes (each in [0, 1]) to
47 * volume and balance values. (Volume is in [0, 1], balance is in [-1, 1])
49 void lr_to_vb(float left, float right, float *volume, float *balance)
51 assert((left >= 0.0) && (right >= 0.0));
53 *volume = MAX(left, right);
55 if (left > right)
56 *balance = -1.0 + right / left;
57 else if (right > left)
58 *balance = 1.0 - left / right;
59 else
60 *balance = 0.0;
63 /* Performs the reverse calculation of lr_to_vb()
65 void vb_to_lr(float volume, float balance, float *left, float *right)
67 /* *left = volume; *right = volume; return; // XXX */
69 *left = volume * (1.0 - MAX(0.0, balance));
70 *right = volume * (1.0 + MIN(0.0, balance));
73 double get_current_time(void)
75 struct timeval tv;
76 double t;
78 gettimeofday(&tv, NULL);
80 t = (double)tv.tv_sec;
81 t += (double)tv.tv_usec / 1.0e6;
83 return t;
86 void add_region(int index, int x, int y, int width, int height)
88 mr[index].enable = 1;
89 mr[index].x = x;
90 mr[index].y = y;
91 mr[index].width = width;
92 mr[index].height = height;
95 int check_region(int x, int y)
97 register int i;
98 bool found = false;
100 for (i = 0; i < 16 && !found; i++) {
101 if (mr[i].enable && x >= mr[i].x &&
102 x <= mr[i].x + mr[i].width &&
103 y >= mr[i].y && y <= mr[i].y + mr[i].height)
104 found = true;
106 if (!found)
107 return -1;
108 return (i - 1);
111 /* handle writing PID file, silently ignore if we can't do it */
112 void create_pid_file(void)
114 char *home;
115 char *pid;
116 FILE *fp;
118 home = getenv("HOME");
119 if (home == NULL)
120 return;
122 pid = calloc(1, strlen(home) + 10);
123 sprintf(pid, "%s/.wmix.pid", home);
124 fp = fopen(pid, "w");
125 if (fp) {
126 fprintf(fp, "%d\n", getpid());
127 fclose(fp);
129 free(pid);