Debian Codegraph @

utils/lstopo/lstopo-windows.c

raw
1 /*
2 * SPDX-License-Identifier: BSD-3-Clause
3 * Copyright © 2009 CNRS
4 * Copyright © 2009-2021 Inria. All rights reserved.
5 * Copyright © 2009-2010, 2012 Université Bordeaux
6 * Copyright © 2011 Cisco Systems, Inc. All rights reserved.
7 * See COPYING in top-level directory.
8 */
9
10 #include "private/autogen/config.h"
11 #include "hwloc.h"
12
13 #include <stdlib.h>
14 #include <stdio.h>
15 #ifdef HAVE_UNISTD_H
16 #include <unistd.h>
17 #endif
18 #include <string.h>
19
20 #include "private/windows.h" /* must be before windows.h */
21
22 #include <windows.h>
23 #include <windowsx.h>
24
25 #include "lstopo.h"
26
27 /* windows back-end. */
28
29 struct lstopo_windows_output {
30 struct lstopo_output *loutput;
31 PAINTSTRUCT ps;
32 };
33
34 static HFONT font_default;
35 static HFONT font_bold;
36 static HPEN pen_default;
37 static HPEN pen_style[4];
38
39 static struct lstopo_windows_output the_output;
40 static int state, control;
41 static int the_x, the_y, x_delta, y_delta;
42 static int finish;
43 static int the_width, the_height;
44 static int win_width, win_height;
45 static unsigned int the_fontsize, the_gridsize;
46 static float the_scale;
47 static int auto_resize;
48 static int needs_resize;
49 static int ignore_wm_size;
50
51 static void
52 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);
53
54 static LRESULT CALLBACK
55 WndProc(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam)
56 {
57 struct lstopo_output *loutput = the_output.loutput;
58 int redraw = 0;
59
60 switch (message) {
61 case WM_CHAR: {
62 switch (wparam) {
63 case '+':
64 the_scale *= 1.2f;
65 redraw = 1;
66 if (auto_resize)
67 needs_resize = 1;
68 break;
69 case '-':
70 the_scale /= 1.2f;
71 redraw = 1;
72 if (auto_resize)
73 needs_resize = 1;
74 break;
75 case 'F': {
76 /* fit drawing to window, don't resize the window */
77 float wscale, hscale;
78 wscale = win_width / (float)the_width;
79 hscale = win_height / (float)the_height;
80 the_scale *= wscale > hscale ? hscale : wscale;
81 redraw = 1;
82 break;
83 }
84 case '1':
85 the_scale = 1.0;
86 redraw = 1;
87 if (auto_resize)
88 needs_resize = 1;
89 break;
90 case 'r':
91 needs_resize = 1; /* forced */
92 break;
93 case 'R':
94 auto_resize ^= 1;
95 if (auto_resize)
96 needs_resize = 1;
97 printf("%s window autoresizing\n", auto_resize ? "enabled" : "disabled");
98 break;
99 case 'a':
100 loutput->show_attrs_enabled ^= 1;
101 printf("%s object attributes\n", loutput->show_attrs_enabled ? "enabled" : "disabled");
102 redraw = 1;
103 if (auto_resize)
104 needs_resize = 1;
105 break;
106 case 't':
107 loutput->show_text_enabled ^= 1;
108 printf("%s object text\n", loutput->show_text_enabled ? "enabled" : "disabled");
109 redraw = 1;
110 if (auto_resize)
111 needs_resize = 1;
112 break;
113 case 'i':
114 if (loutput->index_type == LSTOPO_INDEX_TYPE_DEFAULT) {
115 loutput->index_type = LSTOPO_INDEX_TYPE_PHYSICAL;
116 printf("switched to physical indexes\n");
117 } else if (loutput->index_type == LSTOPO_INDEX_TYPE_PHYSICAL) {
118 loutput->index_type = LSTOPO_INDEX_TYPE_LOGICAL;
119 printf("switched to logical indexes\n");
120 } else if (loutput->index_type == LSTOPO_INDEX_TYPE_LOGICAL) {
121 loutput->index_type = LSTOPO_INDEX_TYPE_NONE;
122 printf("switched to no indexes\n");
123 } else if (loutput->index_type == LSTOPO_INDEX_TYPE_NONE) {
124 loutput->index_type = LSTOPO_INDEX_TYPE_DEFAULT;
125 printf("switched to default indexes\n");
126 } else {
127 abort();
128 }
129 redraw = 1;
130 if (auto_resize)
131 needs_resize = 1;
132 break;
133 case 'k':
134 loutput->show_cpukinds ^= 1;
135 printf("%s displaying of CPU kinds\n", loutput->show_cpukinds ? "enabled" : "disabled");
136 redraw = 1;
137 break;
138 case 'd':
139 loutput->show_disallowed ^= 1;
140 printf("%s coloring of disallowed resources\n", loutput->show_disallowed ? "enabled" : "disabled");
141 redraw = 1;
142 break;
143 case 'b':
144 loutput->show_binding ^= 1;
145 printf("%s coloring of binding resources\n", loutput->show_binding ? "enabled" : "disabled");
146 redraw = 1;
147 break;
148 case 'l':
149 if (loutput->show_legend == LSTOPO_SHOW_LEGEND_ALL) {
150 loutput->show_legend = LSTOPO_SHOW_LEGEND_NONE;
151 printf("switched to no legend lines\n");
152 } else if (loutput->show_legend == LSTOPO_SHOW_LEGEND_NONE) {
153 loutput->show_legend = LSTOPO_SHOW_LEGEND_NO_DEFAULT;
154 printf("switched to no default legend lines\n");
155 } else if (loutput->show_legend == LSTOPO_SHOW_LEGEND_NO_DEFAULT) {
156 loutput->show_legend = LSTOPO_SHOW_LEGEND_ALL;
157 printf("switched to all legend lines\n");
158 } else {
159 abort();
160 }
161 redraw = 1;
162 if (auto_resize)
163 needs_resize = 1;
164 break;
165 case 'f':
166 /* alternate between factorize+collapse, collapse only, and none */
167 if (loutput->factorize_enabled && loutput->pci_collapse_enabled) {
168 loutput->factorize_enabled = 0;
169 printf("factorizing disabled, PCI collapsing still enabled\n");
170 } else if (!loutput->factorize_enabled && loutput->pci_collapse_enabled) {
171 loutput->pci_collapse_enabled = 0;
172 printf("factorizing and PCI collapsing disabled\n");
173 } else {
174 loutput->factorize_enabled = 1;
175 loutput->pci_collapse_enabled = 1;
176 printf("factorizing and PCI collapsing enabled\n");
177 }
178 redraw = 1;
179 if (auto_resize)
180 needs_resize = 1;
181 break;
182 case 'E':
183 lstopo_show_interactive_cli_options(loutput);
184 fflush(stdout);
185 break;
186 case 'h':
187 case 'H':
188 case '?':
189 lstopo_show_interactive_help();
190 break;
191 case 'q':
192 case 'Q':
193 finish = 1;
194 break;
195 }
196 break;
197 }
198
199 case WM_PAINT: {
200 #ifdef HWLOC_HAVE_GCC_W_MISSING_FIELD_INITIALIZERS
201 #pragma GCC diagnostic ignored "-Wmissing-field-initializers"
202 #endif
203 struct lstopo_color white = {0xff, 0xff, 0xff};
204 #ifdef HWLOC_HAVE_GCC_W_MISSING_FIELD_INITIALIZERS
205 #pragma GCC diagnostic warning "-Wmissing-field-initializers"
206 #endif
207 BeginPaint(hwnd, &the_output.ps);
208 font_default = CreateFont(loutput->fontsize, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH, NULL);
209 SelectObject(the_output.ps.hdc, (HGDIOBJ) font_default);
210 pen_default = CreatePen(PS_SOLID, loutput->thickness, RGB(0,0,0));
211 SelectObject(the_output.ps.hdc, pen_default);
212 if (loutput->nr_cpukind_styles > 1) {
213 font_bold = CreateFont(loutput->fontsize, 0, 0, 0, FW_BOLD, FALSE, FALSE, FALSE, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH, NULL);
214 /* only 1-pixel width is supported for dash and dots */
215 pen_style[0] = CreatePen(PS_DASH, 1, RGB(0,0,0));
216 pen_style[1] = CreatePen(PS_DOT, 1, RGB(0,0,0));
217 pen_style[2] = CreatePen(PS_DASHDOT, 1, RGB(0,0,0));
218 pen_style[3] = CreatePen(PS_DASHDOTDOT, 1, RGB(0,0,0));
219 }
220 SetBkMode(the_output.ps.hdc, TRANSPARENT);
221 loutput->drawing = LSTOPO_DRAWING_PREPARE;
222 output_draw(loutput);
223 the_width = loutput->width;
224 the_height = loutput->height;
225
226 /* now that we computed the new drawing size, resize the window if needed */
227 if (needs_resize) {
228 RECT rect;
229 x_delta = 0;
230 y_delta = 0;
231 rect.top = 0;
232 rect.left = 0;
233 rect.right = the_width;
234 rect.bottom = the_height;
235 AdjustWindowRect(&rect, WS_OVERLAPPEDWINDOW, FALSE);
236
237 /* SetWindowPos() generates a WM_SIZE event, we don't want to scale the drawing there */
238 ignore_wm_size = 1;
239 SetWindowPos(hwnd, HWND_TOP, 0, 0, rect.right - rect.left, rect.bottom - rect.top, SWP_NOCOPYBITS|SWP_NOMOVE|SWP_NOOWNERZORDER|SWP_NOZORDER);
240 ignore_wm_size = 0;
241
242 needs_resize = 0;
243 }
244
245 loutput->drawing = LSTOPO_DRAWING_DRAW;
246 windows_box(loutput, &white, 0, 0, win_width, 0, win_height, NULL, 0);
247 output_draw(loutput);
248 DeleteObject(pen_default);
249 DeleteObject(font_default);
250 if (loutput->nr_cpukind_styles > 1) {
251 DeleteObject(font_bold);
252 DeleteObject(pen_style[0]);
253 DeleteObject(pen_style[1]);
254 DeleteObject(pen_style[2]);
255 DeleteObject(pen_style[3]);
256 }
257 EndPaint(hwnd, &the_output.ps);
258 break;
259 }
260 case WM_LBUTTONDOWN:
261 state = 1;
262 the_x = GET_X_LPARAM(lparam);
263 the_y = GET_Y_LPARAM(lparam);
264 break;
265 case WM_LBUTTONUP:
266 state = 0;
267 break;
268 case WM_MOUSEMOVE:
269 if (!(wparam & MK_LBUTTON))
270 state = 0;
271 if (state) {
272 int new_x = GET_X_LPARAM(lparam);
273 int new_y = GET_Y_LPARAM(lparam);
274 x_delta -= new_x - the_x;
275 y_delta -= new_y - the_y;
276 the_x = new_x;
277 the_y = new_y;
278 redraw = 1;
279 }
280 break;
281 case WM_KEYDOWN:
282 switch (wparam) {
283 case VK_ESCAPE:
284 finish = 1;
285 break;
286 case VK_LEFT:
287 x_delta -= win_width/10;
288 redraw = 1;
289 break;
290 case VK_RIGHT:
291 x_delta += win_width/10;
292 redraw = 1;
293 break;
294 case VK_UP:
295 y_delta -= win_height/10;
296 redraw = 1;
297 break;
298 case VK_DOWN:
299 y_delta += win_height/10;
300 redraw = 1;
301 break;
302 case VK_PRIOR:
303 if (control) {
304 x_delta -= win_width;
305 redraw = 1;
306 } else {
307 y_delta -= win_height;
308 redraw = 1;
309 }
310 break;
311 case VK_NEXT:
312 if (control) {
313 x_delta += win_width;
314 redraw = 1;
315 } else {
316 y_delta += win_height;
317 redraw = 1;
318 }
319 break;
320 case VK_HOME:
321 x_delta = 0;
322 y_delta = 0;
323 redraw = 1;
324 break;
325 case VK_END:
326 x_delta = INT_MAX;
327 y_delta = INT_MAX;
328 redraw = 1;
329 break;
330 case VK_CONTROL:
331 control = 1;
332 break;
333 case VK_F5:
334 loutput->needs_topology_refresh = 1;
335 break;
336 }
337 break;
338 case WM_KEYUP:
339 switch (wparam) {
340 case VK_CONTROL:
341 control = 0;
342 break;
343 }
344 break;
345 case WM_DESTROY:
346 PostQuitMessage(0);
347 return 0;
348 case WM_SIZE: {
349 win_width = LOWORD(lparam);
350 win_height = HIWORD(lparam);
351 if (!ignore_wm_size) {
352 float wscale, hscale;
353 wscale = win_width / (float)the_width;
354 hscale = win_height / (float)the_height;
355 the_scale *= wscale > hscale ? hscale : wscale;
356 if (the_scale < 1.0f)
357 the_scale = 1.0f;
358 redraw = 1;
359 }
360 break;
361 }
362 }
363 if (needs_resize) {
364 /* force a redraw since resizing occurs during redraw */
365 redraw = 1;
366 }
367 if (redraw) {
368 if (x_delta > the_width - win_width)
369 x_delta = the_width - win_width;
370 if (y_delta > the_height - win_height)
371 y_delta = the_height - win_height;
372 if (x_delta < 0)
373 x_delta = 0;
374 if (y_delta < 0)
375 y_delta = 0;
376 loutput->fontsize = (unsigned)(the_fontsize * the_scale);
377 loutput->gridsize = (unsigned)(the_gridsize * the_scale);
378 RedrawWindow(hwnd, NULL, NULL, RDW_INVALIDATE);
379 }
380 return DefWindowProc(hwnd, message, wparam, lparam);
381 }
382
383 static int
384 windows_declare_color(struct lstopo_output *loutput __hwloc_attribute_unused, struct lstopo_color *lcolor)
385 {
386 int r = lcolor->r, g = lcolor->g, b = lcolor->b;
387 HBRUSH brush;
388 COLORREF color;
389
390 color = RGB(r, g, b);
391 brush = CreateSolidBrush(color);
392 if (!brush) {
393 fprintf(stderr,"Could not allocate color %02x%02x%02x\n", r, g, b);
394 exit(EXIT_FAILURE);
395 }
396
397 lcolor->private.windows.brush = (HGDIOBJ) brush;
398 lcolor->private.windows.color = color;
399 return 0;
400 }
401
402 static void
403 windows_destroy_color(struct lstopo_output *loutput __hwloc_attribute_unused, struct lstopo_color *lcolor)
404 {
405 DeleteObject(lcolor->private.windows.brush);
406 }
407
408 static void
409 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)
410 {
411 struct lstopo_windows_output *woutput = loutput->backend_data;
412 unsigned cpukind_style = lstopo_obj_cpukind_style(loutput, obj);
413 PAINTSTRUCT *ps = &woutput->ps;
414
415 SelectObject(ps->hdc, lcolor->private.windows.brush);
416 SetBkColor(ps->hdc, lcolor->private.windows.color);
417 if (cpukind_style)
418 SelectObject(ps->hdc, pen_style[(cpukind_style-1)%4]);
419 Rectangle(ps->hdc, x - x_delta, y - y_delta, x + width - x_delta, y + height - y_delta);
420 if (cpukind_style)
421 SelectObject(ps->hdc, pen_default);
422 }
423
424 static void
425 windows_line(struct lstopo_output *loutput, 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)
426 {
427 struct lstopo_windows_output *woutput = loutput->backend_data;
428 PAINTSTRUCT *ps = &woutput->ps;
429
430 MoveToEx(ps->hdc, x1 - x_delta, y1 - y_delta, NULL);
431 LineTo(ps->hdc, x2 - x_delta, y2 - y_delta);
432 }
433
434 static void
435 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)
436 {
437 struct lstopo_windows_output *woutput = loutput->backend_data;
438 unsigned cpukind_style = lstopo_obj_cpukind_style(loutput, obj);
439 PAINTSTRUCT *ps = &woutput->ps;
440
441 SetTextColor(ps->hdc, lcolor->private.windows.color);
442 if (cpukind_style % 2)
443 SelectObject(ps->hdc, font_bold);
444 TextOut(ps->hdc, x - x_delta, y - y_delta, text, (int)strlen(text));
445 if (cpukind_style % 2)
446 SelectObject(ps->hdc, font_default);
447 }
448
449 static void
450 windows_textsize(struct lstopo_output *loutput, const char *text, unsigned textlength, unsigned fontsize __hwloc_attribute_unused, unsigned *width)
451 {
452 struct lstopo_windows_output *woutput = loutput->backend_data;
453 PAINTSTRUCT *ps = &woutput->ps;
454 SIZE size;
455
456 GetTextExtentPoint32(ps->hdc, text, textlength, &size);
457 *width = size.cx;
458 }
459
460 struct draw_methods windows_draw_methods = {
461 windows_declare_color,
462 windows_destroy_color,
463 windows_box,
464 windows_line,
465 windows_text,
466 windows_textsize,
467 };
468
469 static HWND toplevel = NULL;
470
471 int
472 output_windows (struct lstopo_output *loutput, const char *dummy __hwloc_attribute_unused)
473 {
474 unsigned width, height;
475 MSG msg;
476 RECT rect;
477
478 memset(&the_output, 0, sizeof(the_output));
479 the_output.loutput = loutput;
480 loutput->methods = &windows_draw_methods;
481 loutput->backend_data = &the_output;
482
483 if (!toplevel) {
484 /* create the toplevel window, with random size for now */
485 WNDCLASS wndclass;
486 memset(&wndclass, 0, sizeof(wndclass));
487 wndclass.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
488 wndclass.hCursor = LoadCursor(NULL, IDC_SIZEALL);
489 wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
490 wndclass.lpfnWndProc = WndProc;
491 wndclass.lpszClassName = "lstopo";
492
493 RegisterClass(&wndclass);
494
495 toplevel = CreateWindow("lstopo", loutput->title, WS_OVERLAPPEDWINDOW,
496 CW_USEDEFAULT, CW_USEDEFAULT,
497 10, 10, NULL, NULL, NULL, NULL);
498 assert(!loutput->refreshing);
499 } else {
500 assert(loutput->refreshing);
501 }
502
503 /* recurse once for preparing sizes and positions using a fake top level window */
504 loutput->drawing = LSTOPO_DRAWING_PREPARE;
505 BeginPaint(toplevel, &the_output.ps);
506
507 font_default = CreateFont(loutput->fontsize, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH, NULL);
508 if (loutput->nr_cpukind_styles > 1)
509 font_bold = CreateFont(loutput->fontsize, 0, 0, 0, FW_BOLD, FALSE, FALSE, FALSE, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH, NULL);
510 SelectObject(the_output.ps.hdc, (HGDIOBJ) font_default);
511
512 output_draw(loutput);
513
514 DeleteObject(font_default);
515 if (loutput->nr_cpukind_styles > 1)
516 DeleteObject(font_bold);
517
518 EndPaint(toplevel, &the_output.ps);
519 loutput->drawing = LSTOPO_DRAWING_DRAW;
520
521 /* now create the actual toplevel with the sizes */
522 width = loutput->width;
523 height = loutput->height;
524
525 /* compute the window dimensions with borders/title/... */
526 rect.top = 0;
527 rect.left = 0;
528 rect.right = width;
529 rect.bottom = height;
530 AdjustWindowRect(&rect, WS_OVERLAPPEDWINDOW, FALSE);
531 win_width = rect.right - rect.left;
532 win_height = rect.bottom - rect.top;
533
534 /* don't make it bigger than the screen */
535 if (win_width > GetSystemMetrics(SM_CXFULLSCREEN))
536 win_width = GetSystemMetrics(SM_CXFULLSCREEN);
537 if (win_height > GetSystemMetrics(SM_CYFULLSCREEN))
538 win_height = GetSystemMetrics(SM_CYFULLSCREEN);
539
540 ignore_wm_size = 1;
541 SetWindowPos(toplevel, HWND_TOP, 0, 0, win_width, win_height, SWP_NOCOPYBITS|SWP_NOMOVE|SWP_NOOWNERZORDER|SWP_NOZORDER);
542 ignore_wm_size = 0;
543
544 the_width = width;
545 the_height = height;
546
547 the_scale = 1.0f;
548
549 the_fontsize = loutput->fontsize;
550 the_gridsize = loutput->gridsize;
551
552 auto_resize = 1;
553 needs_resize = 0;
554 ignore_wm_size = 0;
555
556 /* ready */
557 declare_colors(loutput);
558 lstopo_prepare_custom_styles(loutput);
559
560 if (!loutput->refreshing)
561 lstopo_show_interactive_help();
562
563 ShowWindow(toplevel, SW_SHOWDEFAULT);
564 InvalidateRect(toplevel, NULL, 1); /* make sure UpdateWindow() will update something when refreshing the topology */
565 UpdateWindow(toplevel);
566
567 while (!finish && !loutput->needs_topology_refresh && GetMessage(&msg, NULL, 0, 0)) {
568 TranslateMessage(&msg);
569 DispatchMessage(&msg);
570 }
571
572 if (!loutput->needs_topology_refresh)
573 DestroyWindow(toplevel);
574 destroy_colors(loutput);
575 return 0;
576 }