wmix: create the PID file only after successful initialisation
[dockapps.git] / wmix / misc.c
blob544a6e91639905f591cab42f055930c4a2a7c5a3
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];
45 extern Config config;
47 /* Converts separate left and right channel volumes (each in [0, 1]) to
48 * volume and balance values. (Volume is in [0, 1], balance is in [-1, 1])
50 void lr_to_vb(float left, float right, float *volume, float *balance)
52 assert((left >= 0.0) && (right >= 0.0));
54 *volume = MAX(left, right);
56 if (left > right)
57 *balance = -1.0 + right / left;
58 else if (right > left)
59 *balance = 1.0 - left / right;
60 else
61 *balance = 0.0;
64 /* Performs the reverse calculation of lr_to_vb()
66 void vb_to_lr(float volume, float balance, float *left, float *right)
68 /* *left = volume; *right = volume; return; // XXX */
70 *left = volume * (1.0 - MAX(0.0, balance));
71 *right = volume * (1.0 + MIN(0.0, balance));
74 double get_current_time(void)
76 struct timeval tv;
77 double t;
79 gettimeofday(&tv, NULL);
81 t = (double)tv.tv_sec;
82 t += (double)tv.tv_usec / 1.0e6;
84 return t;
87 void add_region(int index, int x, int y, int width, int height)
89 mr[index].enable = 1;
90 mr[index].x = x;
91 mr[index].y = y;
92 mr[index].width = width;
93 mr[index].height = height;
96 int check_region(int x, int y)
98 register int i;
99 bool found = false;
101 for (i = 0; i < 16 && !found; i++) {
102 if (mr[i].enable && x >= mr[i].x &&
103 x <= mr[i].x + mr[i].width &&
104 y >= mr[i].y && y <= mr[i].y + mr[i].height)
105 found = true;
107 if (!found)
108 return -1;
109 return (i - 1);
112 void config_read(void)
114 FILE *fp;
115 char buf[512];
116 char *ptr;
118 if (config.file == NULL)
119 return;
121 fp = fopen(config.file, "r");
122 if (!fp)
123 return;
125 while (fgets(buf, 512, fp)) {
126 if ((ptr = strstr(buf, "mousewheel="))) {
127 ptr += 11;
128 config.mousewheel = atoi(ptr);
130 if ((ptr = strstr(buf, "scrolltext="))) {
131 ptr += 11;
132 config.scrolltext = atoi(ptr);
134 if ((ptr = strstr(buf, "osd="))) {
135 ptr += 4;
136 config.osd = atoi(ptr);
138 if ((ptr = strstr(buf, "osdcolor="))) {
139 char *end;
140 ptr += 9;
141 end = strchr(ptr, '\n');
142 ptr[end - ptr] = '\0';
143 if (config.osd_color)
144 free(config.osd_color);
145 config.osd_color = strdup(ptr);
147 if ((ptr = strstr(buf, "wheelstep="))) {
148 ptr += 10;
149 /* detect old style config */
150 if (atoi(ptr) > 1)
151 config.scrollstep = (float)atoi(ptr) / 100.0;
152 else
153 config.scrollstep = atof(ptr);
155 if ((ptr = strstr(buf, "wheelbtn1="))) {
156 ptr += 10;
157 config.wheel_button_up = atoi(ptr);
159 if ((ptr = strstr(buf, "wheelbtn2="))) {
160 ptr += 10;
161 config.wheel_button_down = atoi(ptr);
164 fclose(fp);
167 /* handle writing PID file, silently ignore if we can't do it */
168 void create_pid_file(void)
170 char *home;
171 char *pid;
172 FILE *fp;
174 home = getenv("HOME");
175 if (home == NULL)
176 return;
178 pid = calloc(1, strlen(home) + 10);
179 sprintf(pid, "%s/.wmix.pid", home);
180 fp = fopen(pid, "w");
181 if (fp) {
182 fprintf(fp, "%d\n", getpid());
183 fclose(fp);
185 free(pid);