summaryrefslogtreecommitdiff
path: root/src/switcher-util.c
blob: 82afa32a45633f9868d0c669210b4ee825513d71 (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
/**
 *
 * Compiz switcher utility library
 *
 * switcher-utils.c
 *
 * Copyright © 2007 Christopher James Halse Rogers <chalserogers@gmail.com>
 *
 * Authors:
 * Christopher James Halse Rogers <chalserogers@gmail.com>
 *
 * Based on switcher.c:
 * Copyright : (C) 2007 David Reveman
 * E-mail    : davidr@novell.com
 *
 * 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.
 *
 **/

#include <stdlib.h>
#include <compiz.h>

#include "switcher-util.h"


/**
 * WindowList constructor
**/
WindowList *
newWindowList (void)
{
    WindowList *ret;
    ret = malloc (sizeof(WindowList));
    ret->nWindows = 0;
    ret->windowsSize = 32;
    ret->w = malloc (sizeof(CompWindow *) * 32);
    return ret;
}

/**
 * WindowList destructor
**/
void
destroyWindowList (WindowList *l)
{
    free (l->w);
    free (l);
}

/**
 * Add a window to the window list, reallocing if necessary
 * @param l: @WindowList to add the window to.
 * @param w: @CompWindow to add to the list
 *
 * @return: True on success
**/
Bool
addWindowToList (WindowList *l,
		 CompWindow *w)
{
    if (l->windowsSize <= l->nWindows)
    {
	l->w = realloc (l->w,
			sizeof (CompWindow *) * (l->nWindows + 32));
	if (!l->w)
	    return FALSE;

	l->windowsSize = l->nWindows + 32;
    }

    l->w[l->nWindows++] = w;
    return TRUE;
}

/**
 * WindowTree constructor
**/
WindowTree *
newWindowTree (void)
{
    WindowTree *ret;
    ret = malloc (sizeof(WindowTree));
    ret->windows = newWindowList();
    ret->childrenSize = 4;
    ret->children = malloc (sizeof (WindowTree *) * 4);
    return ret;
}

/**
 * WindowTree destructor
**/
void
destroyWindowTree (WindowTree *node)
{
    destroyWindowList (node->windows);
    for (int i = 0; i < childrenSize; ++i)
    {
	destroyWindowTree (node->children[i]);
    }
    free (node->children);
    free (node);
}

/**
 * Add a window to the window tree
 * @param node:	The @WindowTree to add the window to
 * @param w: The window to add to the tree
 * @param isSameGroup: Comparison function to determine whether w should be
 *		       a child of an existing window.
 *
 * @return: True on success 
**/
Bool
addWindowToTree (WindowTree *node,
		 CompWindow 	*w,
		 WindowCmpProc	isSameGroup)
{
    for (int i = 0; i < node->windows->nWindows; ++i)
    {
	if (isSameGroup (node->windows->w[i], w))
	{
	    if (!node->children[i])
		node->children[i] = newWindowTree();
	    return addWindowToTree (node->children[i], w, isSameGroup);
	}
    }
    if (node->childrenSize <= node->windows->nWindows)
	node->children = realloc (node->children,
				  sizeof(WindowTree *) * \
				  (node->childrenSize + 32));
    if (!node->children)
	return FALSE;
    node->childrenSize += 32;
    
    return addWindowToList (node->windows, w);
}

/**
 * Create a WindowTree from a list of windows.
 *
 * @param windows: The @WindowList to built the tree from.
 * @param isSameGroup: Comparison function, true when the two window should be
 * 		       placed in the same group
 *
 * @return: The root node of the tree.  The caller is responsible for freeing
 *	    the tree once it is no longer needed.
**/
WindowTree *
createWindowTree (WindowList *windows,
		  WindowCmpProc isSameGroup)
{
    WindowTree *root;

    root = newWindowTree();
    	/* XXX: Most naieve possible algorithm! */
    for (int i = 0; i < windows->nWindows; ++i)
    {
	if (!addWindowToTree (root, windows->w[i], isSameGroup))
	{
	    destroyWindowTree(root);
	    return NULL;
	}
    }
    return root;
}