-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmisc.c
105 lines (90 loc) · 2.4 KB
/
misc.c
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
/* wmbright -- a brightness control using randr.
* Copyright (C) 2000, 2001
* Daniel Richard G. <[email protected]>,
* timecop <[email protected]>
* Copyright (C) 2019
* Johannes Holmberg <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <sys/types.h>
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/time.h>
#include "include/common.h"
#include "include/misc.h"
typedef struct {
int enable;
int x;
int y;
int width;
int height;
} MRegion;
MRegion mr[16];
double get_current_time(void)
{
struct timeval tv;
double t;
gettimeofday(&tv, NULL);
t = (double)tv.tv_sec;
t += (double)tv.tv_usec / 1.0e6;
return t;
}
void add_region(int index, int x, int y, int width, int height)
{
mr[index].enable = 1;
mr[index].x = x;
mr[index].y = y;
mr[index].width = width;
mr[index].height = height;
}
int check_region(int x, int y)
{
register int i;
bool found = false;
for (i = 0; i < 16 && !found; i++) {
if (mr[i].enable && x >= mr[i].x &&
x <= mr[i].x + mr[i].width &&
y >= mr[i].y && y <= mr[i].y + mr[i].height)
found = true;
}
if (!found)
return -1;
return (i - 1);
}
/* handle writing PID file, silently ignore if we can't do it */
void create_pid_file(void)
{
char *home;
char *pid;
FILE *fp;
home = getenv("HOME");
if (home == NULL)
return;
pid = malloc(strlen(home) + 11);
sprintf(pid, "%s/.wmbright.pid", home);
fp = fopen(pid, "w");
if (fp) {
fprintf(fp, "%d\n", getpid());
fclose(fp);
}
free(pid);
}