-
Notifications
You must be signed in to change notification settings - Fork 3
/
layout.c
325 lines (259 loc) · 7.23 KB
/
layout.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
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
/*
* Copyright (c) 2008 Apple Computer, Inc. All rights reserved.
*
* @APPLE_LICENSE_HEADER_START@
*
* The contents of this file constitute Original Code as defined in and
* are subject to the Apple Public Source License Version 1.1 (the
* "License"). You may not use this file except in compliance with the
* License. Please obtain a copy of the License at
* http://www.apple.com/publicsource and read it before using this file.
*
* This Original Code and all software distributed under the License are
* distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
* EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
* INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
* License for the specific language governing rights and limitations
* under the License.
*
* @APPLE_LICENSE_HEADER_END@
*/
#include "layout.h"
#include "log.h"
#include "top.h"
#include <assert.h>
#include <math.h>
#include <stdlib.h>
enum { COLUMN_PADDING = 1 };
struct enlarge_data {
int add;
int maxy;
int count;
};
static bool
enlarge_iterator(struct statistic *s, void *ptr)
{
struct enlarge_data *data = ptr;
int size;
if (s->request_size.width > s->minimum_size.width) {
size = s->request_size.width;
} else {
size = s->minimum_size.width;
}
if (data->count > 0)
size += COLUMN_PADDING;
s->actual_size.width = size;
s->actual_size.width += data->add;
s->actual_size.height = data->maxy;
data->count += 1;
/* Return true for more iterations (if possible). */
return true;
}
/* This assumes that toadd is >= 1.0, so the caller must verify that. */
static void
enlarge(struct statistics_controller *c, int maxy, int toadd)
{
struct enlarge_data data;
data.add = toadd;
data.maxy = maxy;
data.count = 0;
c->iterate(c, enlarge_iterator, &data);
}
struct do_layout_data {
int x, y;
bool error;
};
static bool
do_layout_iterator(struct statistic *s, void *ptr)
{
struct do_layout_data *data = ptr;
struct statistic_size firstsize = { .width = 1, .height = 1 };
if (s->callbacks.resize_cells(s, &firstsize)) {
data->error = true;
return false;
}
if (s->callbacks.move_cells(s, data->x, data->y)) {
top_log("error: moving cells for %s!\n", s->header);
top_log("error info: data->x %d data->y %d\n", data->x, data->y);
data->error = true;
/*stop iterating*/
return false;
}
if (s->callbacks.resize_cells(s, &s->actual_size)) {
top_log("error: resizing cells for %s!\n", s->header);
top_log("error info: data->x %d s->actual_size.width %d "
"s->actual_size.height %d\n",
data->x, s->actual_size.width, s->actual_size.height);
data->error = true;
/*stop iterating*/
return false;
}
data->x += s->actual_size.width;
/*continue iterating*/
return true;
}
static bool
do_layout(struct statistics_controller *c, int y)
{
struct do_layout_data data;
data.x = 0;
data.y = y;
data.error = false;
c->iterate(c, do_layout_iterator, &data);
return data.error;
}
struct get_size_data {
struct statistic_size *reqsize;
struct statistic_size *minsize;
int count;
};
/* Return true if this iterator wants more data. */
static bool
get_size_iterator(struct statistic *s, void *ptr)
{
struct get_size_data *data = ptr;
int minwidth, reqwidth;
s->callbacks.get_request_size(s);
s->callbacks.get_minimum_size(s);
minwidth = s->minimum_size.width;
reqwidth = s->request_size.width;
/* Skip padding the first column's left side. */
if (data->count > 0) {
minwidth += COLUMN_PADDING;
reqwidth += COLUMN_PADDING;
}
data->minsize->width += minwidth;
if (s->minimum_size.height > data->minsize->height)
data->minsize->height = s->minimum_size.height;
if (reqwidth > minwidth) {
data->reqsize->width += reqwidth;
} else {
data->reqsize->width += minwidth;
}
if (s->request_size.height > data->reqsize->height)
data->reqsize->height = s->request_size.height;
data->count += 1;
return true;
}
static void
get_size(struct statistics_controller *c, struct statistic_size *reqsize,
struct statistic_size *minsize)
{
struct get_size_data data;
reqsize->width = 0;
reqsize->height = 0;
minsize->width = 0;
minsize->height = 0;
data.reqsize = reqsize;
data.minsize = minsize;
data.count = 0;
c->iterate(c, get_size_iterator, &data);
}
struct minsize_fit_data {
int maxy;
int count;
};
static bool
minsize_fit_iterator(struct statistic *s, void *ptr)
{
struct minsize_fit_data *data = ptr;
s->actual_size.width = s->minimum_size.width;
/* Don't pad the first column's left side. */
if (data->count > 0)
s->actual_size.width += COLUMN_PADDING;
s->actual_size.height = data->maxy;
data->count += 1;
/*more iterations*/
return true;
}
static void
minsize_fit(struct statistics_controller *c, int maxy)
{
struct minsize_fit_data data;
data.maxy = maxy;
data.count = 0;
c->iterate(c, minsize_fit_iterator, &data);
}
/*
* The layout algorithm:
* We first attempt to fit as many statistics as possible using the
* minimum size of each. If we are at a point where the width of the
* terminal is such that the total statistics is equal to the total
* possible statistics, then we attempt to use the request size.
* If the request size exceeds the terminal size, then we fall back
* to the minimum size. If the request size fits, then we try to
* enlarge proportionately every column, as long as the enlargement
* is > 1 for each column.
*/
/* Return true if an error occurred. */
/* The caller will probably want to schedule another layout after a failure. */
bool
layout_statistics(struct statistics_controller *c, int maxx, int maxy, int y)
{
struct statistic_size reqsize, minsize;
int total_stats = 0;
int inserts = 0, removes = 0;
if (maxy <= 0)
return false;
while (1) {
get_size(c, &reqsize, &minsize);
total_stats = c->get_total_active(c);
if (minsize.width == maxx) {
minsize_fit(c, maxy);
break;
} else if (minsize.width > maxx) {
/* The minsize is greater than the total window width. */
if (total_stats > 1) {
/* Mark that we removed to prevent an endless loop. */
++removes;
// werase(c->parent);
c->remove_tail(c);
// werase(c->parent);
continue;
}
minsize_fit(c, maxy);
break;
} else {
if (0 == removes && total_stats < c->get_total_possible(c)) {
++inserts;
c->insert_tail(c);
continue;
}
minsize_fit(c, maxy);
break;
}
}
// werase(c->parent);
total_stats = c->get_total_active(c);
if (total_stats >= c->get_total_possible(c)) {
/*
* We have all of the possible statistics.
* Now we attempt to enlarge to the request size or beyond.
*/
get_size(c, &reqsize, &minsize);
if (reqsize.width == maxx) {
/* The request size fit exactly. */
enlarge(c, maxy, /*add*/ 0);
} else if (reqsize.width > maxx) {
/* Fallback to the minsize. */
minsize_fit(c, maxy);
} else {
/* reqsize.width < maxx */
/*
* We can probably enlarge some if not all of the stats.
*/
int xdelta = maxx - reqsize.width;
int enlargement = xdelta / c->get_total_possible(c);
if (enlargement > 0) {
enlarge(c, maxy, enlargement);
} else {
enlarge(c, maxy, 0);
}
}
}
top_log("c->get_total_active(c) is %d\n", c->get_total_active(c));
if (do_layout(c, y))
return true;
return false;
}