summaryrefslogtreecommitdiff
path: root/src/startup.cpp
blob: ca69a890b29e9ee1e079786816b1ad296e414c3b (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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
#include "startup.h"


COMPIZ_PLUGIN_20090315 (startup, StartupPluginVTable);

// mkdir_p: Creates a directory making parent directories as needed.
// (same as mkdir -p)

void
mkdir_p(CompString &pathname) {
	if (mkdir(pathname.c_str(), 0777) < 0) {
		// If we couldn't make the whole directory because of ENOENT (a
		// parent directory doesn't exist), then try recursively to make
		// the immediate parent directory.
		if (errno == ENOENT) {

			size_t slash = pathname.rfind('/');
			if (slash != CompString::npos) {
				CompString prefix = pathname.substr(0, slash);
				mkdir_p(prefix);
				mkdir(pathname.c_str(), 0777);
			}
		}
	}
}

void
StartupScreen::RunCommands()
{
	updateOptions();

	if (numCommands < 1);
		ranCommands = true;

	for (int i = 0; i < numCommands; i++)
	{
		// FIXME: There may be a neater way to set this bool
		if ( i == 0)
		ranCommands = true;

		if (error)
		{
			compLogMessage ("startup", CompLogLevelError, "An error occurred, doing nothing.");
			return;
		}
		// else
		// Run the commands.
		// If first time in this session, run all commands except with option 2
		if ( firstSession && commands[i].interval != 2 )
		{
			screen->runCommand ( commands[i].command );
			continue;
		}
		// If we've already run compiz at least once in this session, run all commands with option 1 (Every)
		if ( firstRun && commands[i].interval == 1 )
		{
			screen->runCommand ( commands[i].command );
			continue;
		}
		// Hopefully, these will be all of the commands with option 2 :-P
		if ( !firstSession && !alreadyRunning && commands[i].interval == 2 )
		screen->runCommand ( commands[i].command );
	}

	return;
}

void
StartupScreen::UpdateStatus()
{

		// We're only interested in the dbus ID
		CompString s(dbus_env_var);
		size_t idx = s.find("guid=");
		CompString dbus_id = s.substr(idx+5) + "\n";


		std::fstream lock;
		int nCompLock = 0;
		int nCompPid = getpid();
		CompString compLockFile = lockdir + "complock" + dsp;
		CompString xLockFile = lockdir + "xlock" + dsp;

		lock.open(xLockFile.c_str (), std::ios::in);
		if (!lock)
		{
			compLogMessage ("startup", CompLogLevelWarn, "Could not open %s for reading: %s", xLockFile.c_str (), strerror(errno));
			compLogMessage ("startup", CompLogLevelWarn, "Perhaps this is the first time using this plugin?");
		}
		else
		{
			std::stringstream xstrm;
			xstrm << lock.rdbuf ();
			lock.close ();
			lock.open(compLockFile.c_str (), std::ios::in);
			if (!lock)
			{
				compLogMessage ("startup", CompLogLevelWarn, "Could not open %s for reading: %s", compLockFile.c_str (), strerror(errno));
				compLogMessage ("startup", CompLogLevelWarn, "Perhaps this is the first time using this plugin?");
			}
			else
			{
				lock >> nCompLock;
				lock.close ();
				if (xstrm.str () == dbus_id)
				{
					// Compiz has already been run in this X session
					if (nCompLock == nCompPid)
					{
						// Control is reaching this path but compiz is already running
						// Plugins are most likely being reloaded
						if (!firstRun)
						alreadyRunning = true;
						return;
					}
					else
					{
						lock.open(compLockFile.c_str (), std::ios::out);
						if (!lock)
							compLogMessage ("startup", CompLogLevelWarn, "Couldn't open %s for writing: %s", compLockFile.c_str (), strerror(errno));
						else
						{
							// Compiz has just been started. Save it's PID
							if (!firstSession)
							firstRun = true;
							lock << nCompPid;
							lock.close ();
							return;
						}
					}
				}
			}
		}
		mkdir_p(lockdir);
		lock.open (xLockFile.c_str (), std::ios::out);
		if (!lock)
			compLogMessage ("startup", CompLogLevelWarn, "Could not open %s for writing: %s", xLockFile.c_str (), strerror(errno));
		else
		{
			// First time compiz has been run in this X session
			firstSession = true;
			lock << dbus_id;
			lock.close();
		}
		lock.open(compLockFile.c_str (), std::ios::out);
		if (!lock)
		{
			compLogMessage ("startup", CompLogLevelWarn, "Could not open %s for writing: %s", compLockFile.c_str (), strerror(errno));
		}
		else
		{
			// Save the PID
			if(firstSession)
			lock << nCompPid;
			lock.close ();
			return;
		}

	return;
}

bool
StartupWindow::damageRect (bool initial,
			   const CompRect &rect)
{
	STARTUP_SCREEN (screen);

    if (!ss->ranCommands)
		ss->RunCommands();
    else
		cWindow->damageRectSetEnabled (this, false);

    return cWindow->damageRect (initial, rect);
}

void
StartupScreen::updateOptions ()
{

#define GET_OPTION(opt) CompOption::Value::Vector c##opt = optionGet##opt ();
    GET_OPTION (PostStartupCommand);
    GET_OPTION (PostStartupInterval);
#undef GET_OPTION

	if (cPostStartupCommand.size () != cPostStartupInterval.size ())
	{
		compLogMessage ("startup", CompLogLevelWarn, "Malformed option");
		return;
	}

	commands.clear ();

	for (int i = 0; i < cPostStartupCommand.size (); i++, numCommands++)
    {
     	commands.push_back (StartupCommand ());
		commands[i].command = cPostStartupCommand[i].s ();
		commands[i].interval = cPostStartupInterval[i].i ();
	}

	UpdateStatus ();

	return;
}

void
StartupScreen::optionChanged (CompOption *opt, Options num)
{
	updateOptions ();
}

StartupScreen::StartupScreen (CompScreen *screen) :
    PluginClassHandler <StartupScreen, CompScreen> (screen),
    cScreen (CompositeScreen::get (screen)),
    commands ()
{
	optionSetPostStartupCommandNotify    (boost::bind (&StartupScreen::
				optionChanged, this, _1, _2));

    ranCommands = firstRun = firstSession = alreadyRunning = error = false;

	numCommands = 0;

#define GET_ENV_VAR(ourvar, envvar, error)																	\
		ourvar = getenv(#envvar);																			\
		if ( ourvar == NULL )																				\
		{																									\
			compLogMessage ("startup", CompLogLevelError, "Environment variable " #envvar " is not set");	\
			error = true;																					\
			return;																							\
		}

		GET_ENV_VAR (dbus_env_var, DBUS_SESSION_BUS_ADDRESS, error)
		GET_ENV_VAR (dsp, DISPLAY, error)
		GET_ENV_VAR (home, HOME, error)
#undef GET_ENV_VAR

	// We may be at risk of modifying the actual DISPLAY env var here
	dsp[2] = 0;
	// though tests have shown it doesn't

	lockdir = "/.compiz/locks/startup/";
	lockdir.insert (0,home);
}

StartupWindow::StartupWindow (CompWindow *window) :
    PluginClassHandler <StartupWindow, CompWindow> (window),
    cWindow (CompositeWindow::get (window))
{
	CompositeWindowInterface::setHandler (cWindow);
}

StartupScreen::~StartupScreen ()
{
}

StartupWindow::~StartupWindow ()
{
}

bool
StartupPluginVTable::init ()
{
    if (!CompPlugin::checkPluginABI ("core", CORE_ABIVERSION) ||
	!CompPlugin::checkPluginABI ("composite", COMPIZ_COMPOSITE_ABI))
    {
		compLogMessage ("startup", CompLogLevelError, "Unable to verify core and compositing are present\n");
		return false;
    }

    return true;
}