summaryrefslogtreecommitdiff
path: root/synth/watch_files.cpp
blob: 738e45795f27320dd2b7cd6a9bd72f2d5d9fa35c (plain)
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/*
    Copyright (C) 2010-2012 Florian Jung
     
    This file is part of flo's FM synth.

    flo's FM synth 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 3 of the License, or
    (at your option) any later version.

    flo's FM synth 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 flo's FM synth.  If not, see <http://www.gnu.org/licenses/>.
*/


#include "defines.h"
#ifdef WATCHFILES

#include <iostream>
#include <pthread.h>
#include <unistd.h>
#include <sys/inotify.h>

#include "watch_files.h"
#include "util.h"
#include "globals.h"
#include "in_synth_cli.h"

using namespace std;

int fd=-1;
map<int, set<int> > inotify_map;
pthread_mutex_t inotify_map_mutex;

void watch_files_cleanup(void* unused)
{
	if (fd==-1)
	{
		output_verbose("NOTE: no cleaning necessary for watch-files-thread");
	}
	else
	{
		output_verbose("NOTE: cleaning up for watch-files-thread...");
		
	}
}

void* watch_files(void* unused)
{
	pthread_cleanup_push(watch_files_cleanup, NULL);
	
	pthread_mutex_init(&inotify_map_mutex, NULL);
	
	fd=inotify_init();
	if (fd==-1)
	{
		output_warning("WARNING: could not initalize inotify. you must inform me about\n"
									 "         updated files manually.");
		while (true) sleep(10);
	}
	else
	{
		for (int i=0;i<128;i++) // add watches for all loaded programs
			if (programfile[i]!="")
				add_watch(i);
		
		inotify_event ev;
		size_t s;
		while (true)
		{
			s=read (fd, &ev, sizeof(inotify_event));
			while (s<sizeof(inotify_event))
				s+=read (fd,(char*)&ev + s, sizeof(inotify_event)-s);
			
			pthread_mutex_lock(&inotify_map_mutex);
			
			if (ev.mask & IN_MODIFY)
			{
				if (verbose)
				{
					string str="";
					set<int>& tmp=inotify_map[ev.wd];
					for (set<int>::iterator it=tmp.begin(); it!=tmp.end(); it++)
						str+="#"+IntToStr(*it)+" ";
					
					output_verbose("NOTE: reloading programs "+str+"...");
				}

				set<int>& tmp=inotify_map[ev.wd];
				for (set<int>::iterator it=tmp.begin(); it!=tmp.end(); it++)
					lock_and_load_program_no_watch_updates(*it, programfile[*it]);
			}
			else if (ev.mask & (IN_MOVE_SELF | IN_DELETE_SELF))
			{
				if (verbose)
				{
					string str="";
					set<int>& tmp=inotify_map[ev.wd];
					for (set<int>::iterator it=tmp.begin(); it!=tmp.end(); it++)
						str+="#"+IntToStr(*it)+" ";
					
					output_verbose("NOTE: removed watch for programs "+str);
				}
				
				inotify_map.erase(ev.wd);
				inotify_rm_watch(fd,ev.wd);
			}
			else if (ev.mask != IN_IGNORED)
			{
				output_note("NOTE: in watch_files-thread: unknown event received ("+IntToStrHex(ev.mask)+")");
			}

			pthread_mutex_unlock(&inotify_map_mutex);
		}
	}
		
	pthread_cleanup_pop(0);
}

void remove_watch(int prog)
{
	if (watchfiles)
	{
		map<int, set<int> >::iterator mit;
		set<int>* tmp;
		set<int>::iterator sit;
		
		pthread_mutex_lock(&inotify_map_mutex);
		
		//search in all known watch descriptors
		for (mit=inotify_map.begin(); mit!=inotify_map.end(); mit++)
		{
			tmp=&(mit->second);
			sit=tmp->find(prog);
			
			//search for some wd which affects $prog
			if (sit!=tmp->end()) //found?
			{
				//erase $prog from the affect-set
				tmp->erase(sit);
				if (tmp->empty())
				{
					//if the affect-set is now empty, we can garbage-collect
					//the wd (i.e., remove it)
					cout << "garbage collecting wd #"<<mit->first<<endl;
					inotify_rm_watch(fd, mit->first);
					inotify_map.erase(mit);
				}
				
				//we're done now
				break;
			}
		}
		
		pthread_mutex_unlock(&inotify_map_mutex);
	}
}

void add_watch(int prog)
{
	if (watchfiles)
	{
		int wd=inotify_add_watch(fd, programfile[prog].c_str(), IN_MODIFY | IN_MOVE_SELF | IN_DELETE_SELF);
		
		pthread_mutex_lock(&inotify_map_mutex);
		
		if (wd!=-1)
		{
			inotify_map[wd].insert(prog);
		}
		else
		{
			//TODO: warning
		}
		
		pthread_mutex_unlock(&inotify_map_mutex);
	}
}

#endif