/
lstopo-windows.c
468 lines (424 loc) · 13.5 KB
/
lstopo-windows.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
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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
/*
* Copyright © 2009 CNRS
* Copyright © 2009-2019 Inria. All rights reserved.
* Copyright © 2009-2010, 2012 Université Bordeaux
* Copyright © 2011 Cisco Systems, Inc. All rights reserved.
* See COPYING in top-level directory.
*/
#include "private/autogen/config.h"
#include "hwloc.h"
#include <stdlib.h>
#include <stdio.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#include <string.h>
#include <windows.h>
#include <windowsx.h>
#include "lstopo.h"
/* windows back-end. */
struct lstopo_windows_output {
struct lstopo_output *loutput;
PAINTSTRUCT ps;
HWND toplevel;
};
static struct lstopo_windows_output the_output;
static int state, control;
static int the_x, the_y, x_delta, y_delta;
static int finish;
static int the_width, the_height;
static int win_width, win_height;
static unsigned int the_fontsize, the_gridsize;
static float the_scale;
static int needs_resize;
static void
windows_box(struct lstopo_output *loutput, const struct lstopo_color *lcolor, unsigned depth __hwloc_attribute_unused, unsigned x, unsigned width, unsigned y, unsigned height, hwloc_obj_t obj __hwloc_attribute_unused, unsigned box_id __hwloc_attribute_unused);
static LRESULT CALLBACK
WndProc(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam)
{
struct lstopo_output *loutput = the_output.loutput;
int redraw = 0;
switch (message) {
case WM_CHAR: {
switch (wparam) {
case '+':
the_scale *= 1.2f;
redraw = 1;
break;
case '-':
the_scale /= 1.2f;
redraw = 1;
break;
case 'F': {
float wscale, hscale;
wscale = win_width / (float)the_width;
hscale = win_height / (float)the_height;
the_scale *= wscale > hscale ? hscale : wscale;
redraw = 1;
break;
}
case '1':
the_scale = 1.0;
redraw = 1;
break;
case 'r':
needs_resize = 1;
break;
case 'a':
loutput->show_attrs_enabled ^= 1;
printf("%s object attributes\n", loutput->show_attrs_enabled ? "enabled" : "disabled");
redraw = 1;
break;
case 't':
loutput->show_text_enabled ^= 1;
printf("%s object text\n", loutput->show_text_enabled ? "enabled" : "disabled");
redraw = 1;
break;
case 'i':
if (loutput->index_type == LSTOPO_INDEX_TYPE_DEFAULT) {
loutput->index_type = LSTOPO_INDEX_TYPE_PHYSICAL;
printf("switched to physical indexes\n");
} else if (loutput->index_type == LSTOPO_INDEX_TYPE_PHYSICAL) {
loutput->index_type = LSTOPO_INDEX_TYPE_LOGICAL;
printf("switched to logical indexes\n");
} else if (loutput->index_type == LSTOPO_INDEX_TYPE_LOGICAL) {
loutput->index_type = LSTOPO_INDEX_TYPE_NONE;
printf("switched to no indexes\n");
} else if (loutput->index_type == LSTOPO_INDEX_TYPE_NONE) {
loutput->index_type = LSTOPO_INDEX_TYPE_DEFAULT;
printf("switched to default indexes\n");
} else {
abort();
}
redraw = 1;
break;
case 'd':
loutput->show_disallowed ^= 1;
printf("%s coloring of disallowed resources\n", loutput->show_disallowed ? "enabled" : "disabled");
redraw = 1;
break;
case 'b':
loutput->show_binding ^= 1;
printf("%s coloring of binding resources\n", loutput->show_binding ? "enabled" : "disabled");
redraw = 1;
break;
case 'l':
loutput->legend ^= 1;
printf("%s legend\n", loutput->legend ? "enabled" : "disabled");
redraw = 1;
break;
case 'f':
/* alternate between factorize+collapse, collapse only, and none */
if (loutput->factorize_enabled && loutput->pci_collapse_enabled) {
loutput->factorize_enabled = 0;
printf("factorizing disabled, PCI collapsing still enabled\n");
} else if (!loutput->factorize_enabled && loutput->pci_collapse_enabled) {
loutput->pci_collapse_enabled = 0;
printf("factorizing and PCI collapsing disabled\n");
} else {
loutput->factorize_enabled = 1;
loutput->pci_collapse_enabled = 1;
printf("factorizing and PCI collapsing enabled\n");
}
redraw = 1;
break;
case 'E':
lstopo_show_interactive_cli_options(loutput);
fflush(stdout);
break;
case 'h':
case 'H':
case '?':
lstopo_show_interactive_help();
break;
case 'q':
case 'Q':
finish = 1;
break;
}
break;
}
case WM_PAINT: {
HFONT font;
struct lstopo_color white = {0xff, 0xff, 0xff};
BeginPaint(hwnd, &the_output.ps);
font = CreateFont(loutput->fontsize, 0, 0, 0, 0, FALSE, FALSE, FALSE, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH, NULL);
SelectObject(the_output.ps.hdc, (HGDIOBJ) font);
SetBkMode(the_output.ps.hdc, TRANSPARENT);
loutput->drawing = LSTOPO_DRAWING_PREPARE;
output_draw(loutput);
the_width = loutput->width;
the_height = loutput->height;
/* now that we computed the new drawing size, resize the window if needed */
if (needs_resize) {
RECT rect;
x_delta = 0;
y_delta = 0;
rect.top = 0;
rect.left = 0;
rect.right = the_width;
rect.bottom = the_height;
AdjustWindowRect(&rect, WS_OVERLAPPEDWINDOW, FALSE);
SetWindowPos(the_output.toplevel, HWND_TOP, 0, 0, rect.right - rect.left, rect.bottom - rect.top, SWP_DEFERERASE|SWP_NOCOPYBITS|SWP_NOMOVE|SWP_NOOWNERZORDER|SWP_NOZORDER);
needs_resize = 0;
}
loutput->drawing = LSTOPO_DRAWING_DRAW;
windows_box(loutput, &white, 0, 0, win_width, 0, win_height, NULL, 0);
output_draw(loutput);
DeleteObject(font);
EndPaint(hwnd, &the_output.ps);
break;
}
case WM_LBUTTONDOWN:
state = 1;
the_x = GET_X_LPARAM(lparam);
the_y = GET_Y_LPARAM(lparam);
break;
case WM_LBUTTONUP:
state = 0;
break;
case WM_MOUSEMOVE:
if (!(wparam & MK_LBUTTON))
state = 0;
if (state) {
int new_x = GET_X_LPARAM(lparam);
int new_y = GET_Y_LPARAM(lparam);
x_delta -= new_x - the_x;
y_delta -= new_y - the_y;
the_x = new_x;
the_y = new_y;
redraw = 1;
}
break;
case WM_KEYDOWN:
switch (wparam) {
case VK_ESCAPE:
finish = 1;
break;
case VK_LEFT:
x_delta -= win_width/10;
redraw = 1;
break;
case VK_RIGHT:
x_delta += win_width/10;
redraw = 1;
break;
case VK_UP:
y_delta -= win_height/10;
redraw = 1;
break;
case VK_DOWN:
y_delta += win_height/10;
redraw = 1;
break;
case VK_PRIOR:
if (control) {
x_delta -= win_width;
redraw = 1;
} else {
y_delta -= win_height;
redraw = 1;
}
break;
case VK_NEXT:
if (control) {
x_delta += win_width;
redraw = 1;
} else {
y_delta += win_height;
redraw = 1;
}
break;
case VK_HOME:
x_delta = 0;
y_delta = 0;
redraw = 1;
break;
case VK_END:
x_delta = INT_MAX;
y_delta = INT_MAX;
redraw = 1;
break;
case VK_CONTROL:
control = 1;
break;
}
break;
case WM_KEYUP:
switch (wparam) {
case VK_CONTROL:
control = 0;
break;
}
break;
case WM_DESTROY:
/* only kill the program if closing the actual toplevel, not the fake one */
if (hwnd == the_output.toplevel)
PostQuitMessage(0);
return 0;
case WM_SIZE: {
float wscale, hscale;
win_width = LOWORD(lparam);
win_height = HIWORD(lparam);
wscale = win_width / (float)the_width;
hscale = win_height / (float)the_height;
the_scale *= wscale > hscale ? hscale : wscale;
if (the_scale < 1.0f)
the_scale = 1.0f;
redraw = 1;
break;
}
}
if (needs_resize) {
/* force a redraw since resizing occurs during redraw */
redraw = 1;
}
if (redraw) {
if (x_delta > the_width - win_width)
x_delta = the_width - win_width;
if (y_delta > the_height - win_height)
y_delta = the_height - win_height;
if (x_delta < 0)
x_delta = 0;
if (y_delta < 0)
y_delta = 0;
loutput->fontsize = (unsigned)(the_fontsize * the_scale);
loutput->gridsize = (unsigned)(the_gridsize * the_scale);
RedrawWindow(hwnd, NULL, NULL, RDW_INVALIDATE);
}
return DefWindowProc(hwnd, message, wparam, lparam);
}
static int
windows_declare_color(struct lstopo_output *loutput __hwloc_attribute_unused, struct lstopo_color *lcolor)
{
int r = lcolor->r, g = lcolor->g, b = lcolor->b;
HBRUSH brush;
COLORREF color;
color = RGB(r, g, b);
brush = CreateSolidBrush(color);
if (!brush) {
fprintf(stderr,"Could not allocate color %02x%02x%02x\n", r, g, b);
exit(EXIT_FAILURE);
}
lcolor->private.windows.brush = (HGDIOBJ) brush;
lcolor->private.windows.color = color;
return 0;
}
static void
windows_box(struct lstopo_output *loutput, const struct lstopo_color *lcolor, unsigned depth __hwloc_attribute_unused, unsigned x, unsigned width, unsigned y, unsigned height, hwloc_obj_t obj __hwloc_attribute_unused, unsigned box_id __hwloc_attribute_unused)
{
struct lstopo_windows_output *woutput = loutput->backend_data;
PAINTSTRUCT *ps = &woutput->ps;
SelectObject(ps->hdc, lcolor->private.windows.brush);
SetBkColor(ps->hdc, lcolor->private.windows.color);
Rectangle(ps->hdc, x - x_delta, y - y_delta, x + width - x_delta, y + height - y_delta);
}
static void
windows_line(struct lstopo_output *loutput, const struct lstopo_color *lcolor, unsigned depth __hwloc_attribute_unused, unsigned x1, unsigned y1, unsigned x2, unsigned y2, hwloc_obj_t obj __hwloc_attribute_unused, unsigned line_id __hwloc_attribute_unused)
{
struct lstopo_windows_output *woutput = loutput->backend_data;
PAINTSTRUCT *ps = &woutput->ps;
SelectObject(ps->hdc, lcolor->private.windows.brush);
MoveToEx(ps->hdc, x1 - x_delta, y1 - y_delta, NULL);
LineTo(ps->hdc, x2 - x_delta, y2 - y_delta);
}
static void
windows_text(struct lstopo_output *loutput, const struct lstopo_color *lcolor, int size __hwloc_attribute_unused, unsigned depth __hwloc_attribute_unused, unsigned x, unsigned y, const char *text, hwloc_obj_t obj __hwloc_attribute_unused, unsigned text_id __hwloc_attribute_unused)
{
struct lstopo_windows_output *woutput = loutput->backend_data;
PAINTSTRUCT *ps = &woutput->ps;
SetTextColor(ps->hdc, lcolor->private.windows.color);
TextOut(ps->hdc, x - x_delta, y - y_delta, text, (int)strlen(text));
}
static void
windows_textsize(struct lstopo_output *loutput, const char *text, unsigned textlength, unsigned fontsize __hwloc_attribute_unused, unsigned *width)
{
struct lstopo_windows_output *woutput = loutput->backend_data;
PAINTSTRUCT *ps = &woutput->ps;
SIZE size;
GetTextExtentPoint32(ps->hdc, text, textlength, &size);
*width = size.cx;
}
struct draw_methods windows_draw_methods = {
windows_declare_color,
windows_box,
windows_line,
windows_text,
windows_textsize,
};
int
output_windows (struct lstopo_output *loutput, const char *dummy __hwloc_attribute_unused)
{
WNDCLASS wndclass;
HWND toplevel, faketoplevel;
unsigned width, height;
HFONT font;
MSG msg;
RECT rect;
memset(&the_output, 0, sizeof(the_output));
the_output.loutput = loutput;
loutput->methods = &windows_draw_methods;
loutput->backend_data = &the_output;
/* make sure WM_DESTROY on the faketoplevel won't kill the program */
the_output.toplevel = NULL;
/* create the toplevel window, with random size for now */
memset(&wndclass, 0, sizeof(wndclass));
wndclass.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
wndclass.hCursor = LoadCursor(NULL, IDC_SIZEALL);
wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wndclass.lpfnWndProc = WndProc;
wndclass.lpszClassName = "lstopo";
RegisterClass(&wndclass);
/* recurse once for preparing sizes and positions using a fake top level window */
loutput->drawing = LSTOPO_DRAWING_PREPARE;
faketoplevel = CreateWindow("lstopo", "lstopo", WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT,
10, 10, NULL, NULL, NULL, NULL);
BeginPaint(faketoplevel, &the_output.ps);
font = CreateFont(loutput->fontsize, 0, 0, 0, 0, FALSE, FALSE, FALSE, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH, NULL);
SelectObject(the_output.ps.hdc, (HGDIOBJ) font);
output_draw(loutput);
DeleteObject(font);
EndPaint(faketoplevel, &the_output.ps);
DestroyWindow(faketoplevel);
loutput->drawing = LSTOPO_DRAWING_DRAW;
/* now create the actual toplevel with the sizes */
width = loutput->width;
height = loutput->height;
/* compute the window dimensions with borders/title/... */
rect.top = 0;
rect.left = 0;
rect.right = width;
rect.bottom = height;
AdjustWindowRect(&rect, WS_OVERLAPPEDWINDOW, FALSE);
win_width = rect.right - rect.left;
win_height = rect.bottom - rect.top;
/* don't make it bigger than the screen */
if (win_width > GetSystemMetrics(SM_CXFULLSCREEN))
win_width = GetSystemMetrics(SM_CXFULLSCREEN);
if (win_height > GetSystemMetrics(SM_CYFULLSCREEN))
win_height = GetSystemMetrics(SM_CYFULLSCREEN);
toplevel = CreateWindow("lstopo", "lstopo", WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT,
win_width, win_height, NULL, NULL, NULL, NULL);
the_output.toplevel = toplevel;
the_width = width;
the_height = height;
the_scale = 1.0f;
the_fontsize = loutput->fontsize;
the_gridsize = loutput->gridsize;
needs_resize = 0;
/* and display the window */
ShowWindow(toplevel, SW_SHOWDEFAULT);
lstopo_show_interactive_help();
/* ready */
declare_colors(loutput);
lstopo_prepare_custom_styles(loutput);
UpdateWindow(the_output.toplevel);
while (!finish && GetMessage(&msg, NULL, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
destroy_colors();
return 0;
}