-
Notifications
You must be signed in to change notification settings - Fork 1
/
checkdir.c
78 lines (64 loc) · 2.69 KB
/
checkdir.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
#include <stdio.h>
#include <stdlib.h>
#include <sys/inotify.h> // for checking directory changes
#include <unistd.h> // provides access to the POSIX API(general api for unix-like systems)
#include <limits.h> // for PATH_MAX
#include <string.h>
#include "include/checkdir.h"
#include "include/sound.h"
static int inotify_fd; // file descriptor for inotify
static int watch_fd; // file descriptor for the directory to be watched
static char *watchDir = NULL; // directory to be watched
static char *soundFile = NULL; // filepath for the sound to be played
void set_dir(const char *dir) {
if(watchDir != NULL) {
free(watchDir); // if the directory has been set, reset it
}
watchDir = strdup(dir); // set the directory
printf("watchDir: %s\n", watchDir); // debug
}
void set_sound(const char *sound) {
if(soundFile != NULL) {
free(soundFile); // if the sound file has been set, reset it
}
soundFile = strdup(sound); // set the sound file
printf("soundFile: %s\n", soundFile); // debug
}
void handle_events(int fd) {
char buf[4096] // buffer for reading the events
__attribute__ ((aligned(__alignof__(struct inotify_event)))); // idk what this does but it's in the example haha
const struct inotify_event *event; // event structure
ssize_t len; // length of the event
char *ptr; // pointer for iterating through the buffer
len = read(fd, buf, sizeof(buf)); // read the events from the buffer, return the bytes read
printf("read the events: %ld\n", len); // debug
if(len == -1) {
return;
}
for(ptr = buf; ptr < buf + len; ptr += sizeof(struct inotify_event) + event->len) { // i guess this is for looping through the events
event = (const struct inotify_event *) ptr; // set the event to the current event
printf("inside the loop\n"); // debug
if(event->mask & (IN_MODIFY | IN_CREATE | IN_DELETE | IN_CLOSE_WRITE)) {
printf("inside the if\n"); // debug
make_sound(soundFile); // play the sound
}
}
}
void start_checkdir() {
if(watchDir == NULL) {
return; // if the directory has not been set, return
}
inotify_fd = inotify_init(); // initialize inotify
if(inotify_fd == -1) {
return; // if inotify initialization fails, return
}
watch_fd = inotify_add_watch(inotify_fd, watchDir, IN_MODIFY | IN_CREATE | IN_DELETE | IN_CLOSE_WRITE); // add the directory to be watched
if(watch_fd == -1) {
close(inotify_fd); // if adding the directory to be watched fails, close inotify
return;
}
while(1) {
handle_events(inotify_fd); // handle the events
usleep(1000); // sleep for 1000 microseconds for cpu usage
}
}