~vanvugt/compiz-animation-plugin/fix-930192

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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
/*
 * Animation plugin for compiz/beryl
 *
 * animation.c
 *
 * Copyright : (C) 2006 Erkin Bahceci
 * E-mail    : erkinbah@gmail.com
 *
 * Based on Wobbly and Minimize plugins by
 *           : David Reveman
 * E-mail    : davidr@novell.com>
 *
 * Particle system added by : (C) 2006 Dennis Kasprzyk
 * E-mail                   : onestone@beryl-project.org
 *
 * Beam-Up added by : Florencio Guimaraes
 * E-mail           : florencio@nexcorp.com.br
 *
 * Hexagon tessellator added by : Mike Slegeir
 * E-mail                       : mikeslegeir@mail.utexas.edu>
 *
 * 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.
 */

#include "private.h"

// =====================  Restack  =========================

RestackAnim::RestackAnim (CompWindow *w,
			  WindowEvent curWindowEvent,
			  float duration,
			  const AnimEffect info,
			  const CompRect &icon) :
    Animation::Animation (w, curWindowEvent, duration, info, icon)
{
    mRestackData = static_cast<RestackPersistentData *>
    	(AnimWindow::get (w)->persistentData["restack"]);
}

void
RestackAnim::cleanUp (bool closing, bool destructing)
{
    if (mRestackData->restackInfo ())
	mRestackData->resetRestackInfo ();

    bool thereIsUnfinishedChainElem = false;

    // Look for still playing windows in parent-child chain
    CompWindow *wCur = mRestackData->mMoreToBePaintedNext;
    while (wCur)
    {
	AnimWindow *awCur = AnimWindow::get (wCur);

	if (awCur->curAnimation () &&
	    awCur->curAnimation ()->remainingTime () > 0)
	{
	    thereIsUnfinishedChainElem = true;
	    break;
	}
	RestackPersistentData *dataCur = static_cast<RestackPersistentData *>
	    (awCur->persistentData["restack"]);
	wCur = dataCur->mMoreToBePaintedNext;
    }
    if (!thereIsUnfinishedChainElem)
    {
	wCur = mRestackData->mMoreToBePaintedPrev;
	while (wCur)
	{
	    AnimWindow *awCur = AnimWindow::get (wCur);

	    if (awCur->curAnimation () &&
		awCur->curAnimation ()->remainingTime () > 0)
	    {
		thereIsUnfinishedChainElem = true;
		break;
	    }
	    RestackPersistentData *dataCur =
		static_cast<RestackPersistentData *>
		(awCur->persistentData["restack"]);
	    wCur = dataCur->mMoreToBePaintedPrev;
	}
    }

    if (closing || destructing || !thereIsUnfinishedChainElem)
    {
	// Finish off all windows in parent-child chain
	CompWindow *wCur = mRestackData->mMoreToBePaintedNext;
	while (wCur)
	{
	    AnimWindow *awCur = AnimWindow::get (wCur);
	    RestackPersistentData *dataCur =
		static_cast<RestackPersistentData *>
		(awCur->persistentData["restack"]);
	    wCur = dataCur->mMoreToBePaintedNext;
	    static_cast<ExtensionPluginAnimation *>
		(getExtensionPluginInfo ())->cleanUpParentChildChainItem (awCur);
	}
	wCur = mWindow;
	while (wCur)
	{
	    AnimWindow *awCur = AnimWindow::get (wCur);
	    RestackPersistentData *dataCur =
		static_cast<RestackPersistentData *>
		(awCur->persistentData["restack"]);
	    wCur = dataCur->mMoreToBePaintedPrev;
	    static_cast<ExtensionPluginAnimation *>
		(getExtensionPluginInfo ())->cleanUpParentChildChainItem (awCur);
	}
    }

    ExtensionPluginAnimation *extPlugin =
	static_cast<ExtensionPluginAnimation *> (getExtensionPluginInfo ());
    extPlugin->decrementCurRestackAnimCount ();
}

bool
RestackAnim::initiateRestackAnim (int duration)
{
    CompWindow *wStart = 0;
    CompWindow *wEnd = 0;
    CompWindow *wOldAbove = 0;

    if (!mRestackData)
    	return false;

    ExtensionPluginAnimation *extPlugin =
	static_cast<ExtensionPluginAnimation *> (getExtensionPluginInfo ());
    extPlugin->incrementCurRestackAnimCount ();

    // If a focus chain (application with open dialog, etc.) is the subject,
    // in compiz++, their order changes during restack (which wasn't the case
    // in compiz 0.8. e.g: (subject chain: a b, dodger: x) (a: secondary)
    // a b x
    // b x a
    // x a b
    if (mRestackData->mIsSecondary)
    {
    	if (!mRestackData->mMoreToBePaintedNext)
    	    return false;

    	AnimWindow *awAbove =
    	    AnimWindow::get (mRestackData->mMoreToBePaintedNext);
    	RestackPersistentData *dataAbove = static_cast<RestackPersistentData *>
    	    (awAbove->persistentData["restack"]);

	mTotalTime = awAbove->curAnimation ()->totalTime ();
	mRemainingTime = mTotalTime;

	if (dataAbove && dataAbove->mWinThisIsPaintedBefore)
	{
	    // Host this subject instead, on the above subject's host
	    mRestackData->getHostedOnWin (mWindow,
	    				  dataAbove->mWinThisIsPaintedBefore);
	}
	// do basic secondary subject initialization
	postInitiateRestackAnim (0, 0, 0, 0, false);

	return true; // We're done here
    }

    RestackInfo *restackInfo = mRestackData->restackInfo ();
    bool raised = true;

    if (restackInfo)
    {
	wStart = restackInfo->wStart;
	wEnd = restackInfo->wEnd;
	wOldAbove = restackInfo->wOldAbove;
	raised = restackInfo->raised;
    }

    // Find union region of all windows that will be
    // faded through by w. If the region is empty, don't
    // run focus fade effect.

    CompRegion fadeRegion;

    int numSelectedCandidates = 0;

    CompRegion subjectsRegion (unionRestackChain (mWindow));

    // Compute subject win. region

    // wCand: Dodge or Focus fade candidate window
    for (CompWindow *wCand = wStart; wCand && wCand != wEnd->next;
	 wCand = wCand->next)
    {
	RestackPersistentData *dataCand = static_cast<RestackPersistentData *>
	    (AnimWindow::get (wCand)->persistentData["restack"]);
	if (!extPlugin->relevantForRestackAnim (wCand))
	    continue;

	// Skip windows that have been restacked
	if (wCand != wEnd && dataCand->restackInfo ())
	    continue;

	if (wCand->minimized ())
	    continue;

	if (!CompositeWindow::get (wCand)->pixmap ())
	    continue;

	if (onSameRestackChain (mWindow, wCand))
	    continue;

	// Compute intersection of this (wCand) with subject
	CompRegion candidateWinRegion (wCand->inputRect ());
	CompRegion candidateAndSubjectIntersection
	    (candidateWinRegion.intersected (subjectsRegion));
	fadeRegion += candidateAndSubjectIntersection;

	if (!candidateAndSubjectIntersection.isEmpty ())
	    processCandidate (wCand, mWindow, candidateAndSubjectIntersection,
			      numSelectedCandidates);
    }

    if (fadeRegion.isEmpty ())
    {
	// empty intersection -> won't be drawn
	return false;
    }
    if (wOldAbove)
    {
	// Store this window in the next window
	// so that this is drawn before that, i.e. in its old place
	mRestackData->getHostedOnWin (mWindow, wOldAbove);
    }

    postInitiateRestackAnim (numSelectedCandidates, duration,
			     wStart, wEnd, raised);

    // Handle other subjects down the chain if there are any
    if (mRestackData->mMoreToBePaintedPrev)
    {
	RestackPersistentData *dataCur;
	for (CompWindow *wCur = mRestackData->mMoreToBePaintedPrev; wCur;
	     wCur = dataCur->mMoreToBePaintedPrev)
	{
	    dataCur = static_cast<RestackPersistentData *>
		(AnimWindow::get (wCur)->persistentData["restack"]);
	    if (!dataCur)
		break;
	    dataCur->mIsSecondary = true;
	}
    }
    return true;
}

bool
RestackAnim::onSameRestackChain (CompWindow *wSubject, CompWindow *wOther)
{
    RestackPersistentData *dataCur;
    for (CompWindow *wCur = wSubject; wCur;
	 wCur = dataCur->mMoreToBePaintedNext)
    {
	if (wOther == wCur)
	    return true;
	dataCur = static_cast<RestackPersistentData *>
	    (AnimWindow::get (wCur)->persistentData["restack"]);
	if (!dataCur)
	    break;
    }

    RestackPersistentData *dataSubj = static_cast<RestackPersistentData *>
	    (AnimWindow::get (wSubject)->
	     persistentData["restack"]);
    for (CompWindow *wCur = dataSubj->mMoreToBePaintedPrev; wCur;
	 wCur = dataCur->mMoreToBePaintedPrev)
    {
	if (wOther == wCur)
	    return true;
	dataCur = static_cast<RestackPersistentData *>
	    (AnimWindow::get (wCur)->persistentData["restack"]);
	if (!dataCur)
	    break;
    }
    return false;
}

bool
RestackAnim::overNewCopy ()
{
    bool lowering = (mRestackData->restackInfo () &&
		     !mRestackData->restackInfo ()->raised);

    // Reverse behavior if lowering (i.e. not raising)
    return ((!lowering && mRestackData->mVisitCount == 2) ||
	    (lowering && mRestackData->mVisitCount == 1));
}

CompRegion
RestackAnim::unionRestackChain (CompWindow *w)
{
    CompRegion unionRegion;

    RestackPersistentData *dataCur;
    for (CompWindow *wCur = w; wCur;
	 wCur = dataCur->mMoreToBePaintedNext)
    {
	unionRegion += wCur->inputRect ();
	dataCur = static_cast<RestackPersistentData *>
	    (AnimWindow::get (wCur)->persistentData["restack"]);
	if (!dataCur)
	    break;
    }

    RestackPersistentData *dataSubj = static_cast<RestackPersistentData *>
	    (AnimWindow::get (w)->
	     persistentData["restack"]);
    for (CompWindow *wCur = dataSubj->mMoreToBePaintedPrev; wCur;
	 wCur = dataCur->mMoreToBePaintedPrev)
    {
	unionRegion += wCur->inputRect ();
	dataCur = static_cast<RestackPersistentData *>
	    (AnimWindow::get (wCur)->persistentData["restack"]);
	if (!dataCur)
	    break;
    }

    return unionRegion;
}

RestackInfo::RestackInfo (CompWindow *wRestacked,
			  CompWindow *wStart,
			  CompWindow *wEnd,
			  CompWindow *wOldAbove,
			  bool raised) :
    wRestacked (wRestacked),
    wStart (wStart),
    wEnd (wEnd),
    wOldAbove (wOldAbove),
    raised (raised)
{
}

RestackPersistentData::RestackPersistentData () :
    PersistentData (),
    mRestackInfo (0),
    mWinToBePaintedBeforeThis (0),
    mWinThisIsPaintedBefore (0),
    mMoreToBePaintedPrev (0),
    mMoreToBePaintedNext (0),
    mWinPassingThrough (0),
    mWalkerOverNewCopy (false),
    mVisitCount (0),
    mIsSecondary (false)
{
}

RestackPersistentData::~RestackPersistentData ()
{
    if (mRestackInfo)
	delete mRestackInfo;
}

void
RestackPersistentData::resetRestackInfo (bool alsoResetChain)
{
    delete mRestackInfo;
    mRestackInfo = 0;

    if (alsoResetChain)
    {
    	// Reset chain connections as this is not on a chain
    	mMoreToBePaintedNext = 0;
    	mMoreToBePaintedPrev = 0;
    }
}

void
RestackPersistentData::setRestackInfo (CompWindow *wRestacked,
				       CompWindow *wStart,
				       CompWindow *wEnd,
				       CompWindow *wOldAbove,
				       bool raised)
{
    if (mRestackInfo)
	delete mRestackInfo;
    mRestackInfo =
	new RestackInfo (wRestacked, wStart, wEnd, wOldAbove, raised);
}

/// Make this window be hosted on (i.e. drawn before) the given window.
void
RestackPersistentData::getHostedOnWin (CompWindow *wGuest, CompWindow *wHost)
{
    RestackPersistentData *dataHost = static_cast<RestackPersistentData *>
	(AnimWindow::get (wHost)->persistentData["restack"]);
    dataHost->mWinToBePaintedBeforeThis = wGuest;
    mWinThisIsPaintedBefore = wHost;
}