| 1 |
/* |
| 2 |
* SPDX-License-Identifier: BSD-3-Clause |
| 3 |
* Copyright © 2009 CNRS |
| 4 |
* Copyright © 2009-2021 Inria. All rights reserved. |
| 5 |
* Copyright © 2009-2010, 2014, 2017, 2020 Université Bordeaux |
| 6 |
* Copyright © 2009-2011 Cisco Systems, Inc. All rights reserved. |
| 7 |
* See COPYING in top-level directory. |
| 8 |
*/ |
| 9 |
|
| 10 |
#include "private/autogen/config.h" |
| 11 |
|
| 12 |
#include <cairo.h> |
| 13 |
|
| 14 |
#ifdef CAIRO_HAS_PDF_SURFACE |
| 15 |
#include <cairo-pdf.h> |
| 16 |
#endif /* CAIRO_HAS_PDF_SURFACE */ |
| 17 |
|
| 18 |
#ifdef CAIRO_HAS_PS_SURFACE |
| 19 |
#include <cairo-ps.h> |
| 20 |
#endif /* CAIRO_HAS_PS_SURFACE */ |
| 21 |
|
| 22 |
#ifdef CAIRO_HAS_SVG_SURFACE |
| 23 |
#include <cairo-svg.h> |
| 24 |
#endif /* CAIRO_HAS_SVG_SURFACE */ |
| 25 |
|
| 26 |
#ifdef LSTOPO_HAVE_X11 |
| 27 |
/* configure should enable X11 only if Cairo has XLIB SURFACE and there are X11 headers */ |
| 28 |
# ifndef HWLOC_HAVE_X11_KEYSYM |
| 29 |
# error LSTOPO_HAVE_X11 enabled without HWLOC_HAVE_X11_KEYSYM?! |
| 30 |
# endif |
| 31 |
# ifndef CAIRO_HAS_XLIB_SURFACE |
| 32 |
# error LSTOPO_HAVE_X11 enabled without CAIRO_HAS_XLIB_SURFACE?! |
| 33 |
# endif |
| 34 |
# include <cairo-xlib.h> |
| 35 |
# include <X11/Xlib.h> |
| 36 |
# include <X11/Xutil.h> |
| 37 |
# include <X11/Xresource.h> |
| 38 |
# include <X11/keysym.h> |
| 39 |
# include <X11/cursorfont.h> |
| 40 |
/* Avoid Xwindow's definition conflict with Windows' use for fields names. */ |
| 41 |
# undef Status |
| 42 |
#else /* LSTOPO_HAVE_X11 */ |
| 43 |
# if (defined HWLOC_HAVE_X11_KEYSYM) && (defined CAIRO_HAS_XLIB_SURFACE) |
| 44 |
# error HWLOC_HAVE_X11_KEYSYM and CAIRO_HAS_XLIB_SURFACE defined but LSTOPO_HAVE_X11 not enabled?! |
| 45 |
# endif |
| 46 |
#endif /* LSTOPO_HAVE_X11 */ |
| 47 |
|
| 48 |
#include <stdio.h> |
| 49 |
#include <stdlib.h> |
| 50 |
#include <string.h> |
| 51 |
#include <errno.h> |
| 52 |
#include <limits.h> |
| 53 |
|
| 54 |
#include "lstopo.h" |
| 55 |
|
| 56 |
#if (defined LSTOPO_HAVE_X11) + (defined CAIRO_HAS_PNG_FUNCTIONS) + (defined CAIRO_HAS_PDF_SURFACE) + (defined CAIRO_HAS_PS_SURFACE) + (defined CAIRO_HAS_SVG_SURFACE) |
| 57 |
struct lstopo_cairo_output { |
| 58 |
struct lstopo_output *loutput; |
| 59 |
cairo_surface_t *surface; |
| 60 |
cairo_t *context; |
| 61 |
}; |
| 62 |
|
| 63 |
/* Cairo methods */ |
| 64 |
static void |
| 65 |
topo_cairo_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, unsigned box_id __hwloc_attribute_unused) |
| 66 |
{ |
| 67 |
struct lstopo_cairo_output *coutput = loutput->backend_data; |
| 68 |
unsigned cpukind_style = lstopo_obj_cpukind_style(loutput, obj); |
| 69 |
cairo_t *c = coutput->context; |
| 70 |
int r = lcolor->r, g = lcolor->g, b = lcolor->b; |
| 71 |
|
| 72 |
cairo_rectangle(c, x, y, width, height); |
| 73 |
cairo_set_source_rgb(c, (float)r / 255, (float) g / 255, (float) b / 255); |
| 74 |
cairo_fill(c); |
| 75 |
|
| 76 |
cairo_rectangle(c, x, y, width, height); |
| 77 |
cairo_set_source_rgb(c, 0, 0, 0); |
| 78 |
|
| 79 |
if (cpukind_style) { |
| 80 |
double dash = (double)(1U << cpukind_style); |
| 81 |
cairo_set_dash(c, &dash, 1, 0); |
| 82 |
cairo_set_line_width(c, loutput->thickness * (1 + cpukind_style)); |
| 83 |
} |
| 84 |
|
| 85 |
cairo_stroke(c); |
| 86 |
|
| 87 |
if (cpukind_style) { |
| 88 |
cairo_set_dash(c, NULL, 0, 0); |
| 89 |
cairo_set_line_width(c, loutput->thickness); |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
static void |
| 94 |
topo_cairo_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) |
| 95 |
{ |
| 96 |
struct lstopo_cairo_output *coutput = loutput->backend_data; |
| 97 |
cairo_t *c = coutput->context; |
| 98 |
|
| 99 |
cairo_move_to(c, x1, y1); |
| 100 |
cairo_set_source_rgb(c, 0, 0, 0); |
| 101 |
cairo_line_to(c, x2, y2); |
| 102 |
cairo_stroke(c); |
| 103 |
} |
| 104 |
|
| 105 |
static void |
| 106 |
topo_cairo_text(struct lstopo_output *loutput, const struct lstopo_color *lcolor, int fontsize, 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) |
| 107 |
{ |
| 108 |
struct lstopo_cairo_output *coutput = loutput->backend_data; |
| 109 |
unsigned cpukind_style = lstopo_obj_cpukind_style(loutput, obj); |
| 110 |
cairo_t *c = coutput->context; |
| 111 |
int r = lcolor->r, g = lcolor->g, b = lcolor->b; |
| 112 |
|
| 113 |
cairo_move_to(c, x, y + fontsize); |
| 114 |
cairo_set_source_rgb(c, (float)r / 255, (float) g / 255, (float) b / 255); |
| 115 |
|
| 116 |
if (cpukind_style % 2) |
| 117 |
cairo_select_font_face(c, "default", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_BOLD); |
| 118 |
|
| 119 |
cairo_show_text(c, text); |
| 120 |
|
| 121 |
if (cpukind_style % 2) |
| 122 |
cairo_select_font_face(c, "default", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_NORMAL); |
| 123 |
} |
| 124 |
|
| 125 |
static void |
| 126 |
topo_cairo_textsize(struct lstopo_output *loutput, const char *text, unsigned textlength __hwloc_attribute_unused, unsigned fontsize __hwloc_attribute_unused, unsigned *width) |
| 127 |
{ |
| 128 |
struct lstopo_cairo_output *coutput = loutput->backend_data; |
| 129 |
cairo_t *c = coutput->context; |
| 130 |
cairo_text_extents_t extents; |
| 131 |
cairo_text_extents(c, text, &extents); |
| 132 |
*width = extents.width; |
| 133 |
} |
| 134 |
|
| 135 |
#if (defined CAIRO_HAS_PNG_FUNCTIONS) + (defined CAIRO_HAS_PDF_SURFACE) + (defined CAIRO_HAS_PS_SURFACE) + (defined CAIRO_HAS_SVG_SURFACE) |
| 136 |
static cairo_status_t |
| 137 |
topo_cairo_write(void *closure, const unsigned char *data, unsigned int length) |
| 138 |
{ |
| 139 |
if (fwrite(data, length, 1, closure) < 1) |
| 140 |
return CAIRO_STATUS_WRITE_ERROR; |
| 141 |
return CAIRO_STATUS_SUCCESS; |
| 142 |
} |
| 143 |
#endif /* (CAIRO_HAS_PNG_FUNCTIONS + CAIRO_HAS_PDF_SURFACE + CAIRO_HAS_PS_SURFACE + CAIRO_HAS_SVG_SURFACE) */ |
| 144 |
|
| 145 |
static void |
| 146 |
topo_cairo_paint(struct lstopo_cairo_output *coutput) |
| 147 |
{ |
| 148 |
unsigned fontsize = coutput->loutput->fontsize; |
| 149 |
cairo_surface_t *cs = coutput->surface; |
| 150 |
cairo_t *c = cairo_create(cs); |
| 151 |
coutput->context = c; |
| 152 |
cairo_set_font_size(c, fontsize); |
| 153 |
cairo_set_line_width(c, coutput->loutput->thickness); |
| 154 |
output_draw(coutput->loutput); |
| 155 |
cairo_show_page(c); |
| 156 |
cairo_destroy(c); |
| 157 |
coutput->context = NULL; |
| 158 |
} |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
#endif /* (LSTOPO_HAVE_X11 + CAIRO_HAS_PNG_FUNCTIONS + CAIRO_HAS_PDF_SURFACE + CAIRO_HAS_PS_SURFACE + CAIRO_HAS_SVG_SURFACE) */ |
| 163 |
|
| 164 |
|
| 165 |
#ifdef LSTOPO_HAVE_X11 |
| 166 |
/* X11 back-end */ |
| 167 |
|
| 168 |
struct lstopo_x11_output { |
| 169 |
struct lstopo_cairo_output coutput; /* must be at the beginning */ |
| 170 |
Display *dpy; |
| 171 |
int scr; |
| 172 |
Window top, win; |
| 173 |
Cursor hand; |
| 174 |
unsigned int orig_fontsize, orig_gridsize; |
| 175 |
int screen_width, screen_height; /** visible part size */ |
| 176 |
int last_screen_width, last_screen_height; /** last visible part size */ |
| 177 |
int width, height; /** total normal display size */ |
| 178 |
int x, y; /** top left corner of the visible part */ |
| 179 |
float scale, last_scale; |
| 180 |
int needs_redraw; |
| 181 |
int needs_resize; |
| 182 |
int auto_resize; |
| 183 |
int maximized; |
| 184 |
}; |
| 185 |
|
| 186 |
static void |
| 187 |
x11_create(struct lstopo_x11_output *disp, int width, int height) |
| 188 |
{ |
| 189 |
disp->win = XCreateSimpleWindow(disp->dpy, disp->top, 0, 0, width, height, 0, WhitePixel(disp->dpy, disp->scr), WhitePixel(disp->dpy, disp->scr)); |
| 190 |
disp->hand = XCreateFontCursor(disp->dpy, XC_fleur); |
| 191 |
XDefineCursor(disp->dpy, disp->win, disp->hand); |
| 192 |
XSelectInput(disp->dpy, disp->win, |
| 193 |
KeyPressMask | |
| 194 |
ButtonPressMask | ButtonReleaseMask | |
| 195 |
PointerMotionMask | |
| 196 |
ExposureMask); |
| 197 |
XMapWindow(disp->dpy, disp->win); |
| 198 |
disp->coutput.surface = cairo_xlib_surface_create(disp->dpy, disp->win, DefaultVisual(disp->dpy, disp->scr), width, height); |
| 199 |
} |
| 200 |
|
| 201 |
static void |
| 202 |
x11_destroy(struct lstopo_x11_output *disp) |
| 203 |
{ |
| 204 |
cairo_surface_destroy(disp->coutput.surface); |
| 205 |
XDestroyWindow(disp->dpy, disp->win); |
| 206 |
} |
| 207 |
|
| 208 |
static int |
| 209 |
x11_is_maximized(struct lstopo_x11_output *disp) |
| 210 |
{ |
| 211 |
Atom type, state, maxed_h, maxed_v; |
| 212 |
int format; |
| 213 |
unsigned char *props; |
| 214 |
unsigned long i, nr, bytesAfter; |
| 215 |
int got_maxed_h = 0, got_maxed_v = 0; |
| 216 |
|
| 217 |
state = XInternAtom(disp->dpy, "_NET_WM_STATE", True); |
| 218 |
props = NULL; |
| 219 |
if (Success == XGetWindowProperty(disp->dpy, disp->top, state, 0, (~0L), False, AnyPropertyType, &type, &format, &nr, &bytesAfter, &props)) { |
| 220 |
maxed_h = XInternAtom(disp->dpy, "_NET_WM_STATE_MAXIMIZED_VERT", True); |
| 221 |
maxed_v = XInternAtom(disp->dpy, "_NET_WM_STATE_MAXIMIZED_HORZ", True); |
| 222 |
for(i=0; i<nr; i++) { |
| 223 |
Atom prop = ((Atom*)(props))[i]; |
| 224 |
if (prop == maxed_h) |
| 225 |
got_maxed_h = 1; |
| 226 |
else if (prop == maxed_v) |
| 227 |
got_maxed_v = 1; |
| 228 |
} |
| 229 |
XFree(props); |
| 230 |
} |
| 231 |
/* if the window is maximized in a single dimension, zoom in/out will unmaximize it */ |
| 232 |
return got_maxed_h && got_maxed_v; |
| 233 |
} |
| 234 |
|
| 235 |
/** Clip coordinates of the visible part. */ |
| 236 |
static void |
| 237 |
move_x11(struct lstopo_x11_output *disp) |
| 238 |
{ |
| 239 |
struct lstopo_cairo_output *coutput = &disp->coutput; |
| 240 |
struct lstopo_output *loutput = coutput->loutput; |
| 241 |
|
| 242 |
if (disp->scale != disp->last_scale) { |
| 243 |
disp->x = disp->scale / disp->last_scale * (float)disp->x; |
| 244 |
disp->y = disp->scale / disp->last_scale * (float)disp->y; |
| 245 |
} |
| 246 |
|
| 247 |
if (disp->needs_redraw |
| 248 |
|| disp->screen_width != disp->last_screen_width |
| 249 |
|| disp->screen_height != disp->last_screen_height |
| 250 |
|| disp->scale != disp->last_scale) { |
| 251 |
disp->last_screen_width = disp->screen_width; |
| 252 |
disp->last_screen_height = disp->screen_height; |
| 253 |
disp->last_scale = disp->scale; |
| 254 |
loutput->fontsize = disp->orig_fontsize * disp->scale; |
| 255 |
loutput->gridsize = disp->orig_gridsize * disp->scale; |
| 256 |
|
| 257 |
x11_destroy(disp); |
| 258 |
|
| 259 |
x11_create(disp, disp->width, disp->height); |
| 260 |
/* recompute new sizes and positions */ |
| 261 |
loutput->drawing = LSTOPO_DRAWING_PREPARE; |
| 262 |
topo_cairo_paint(coutput); |
| 263 |
if (loutput->width > (unsigned) disp->width || loutput->height > (unsigned) disp->height) { |
| 264 |
/* need to extend the window before actual redraw */ |
| 265 |
x11_destroy(disp); |
| 266 |
x11_create(disp, loutput->width, loutput->height); |
| 267 |
} |
| 268 |
loutput->drawing = LSTOPO_DRAWING_DRAW; |
| 269 |
topo_cairo_paint(coutput); |
| 270 |
disp->width = loutput->width; |
| 271 |
disp->height = loutput->height; |
| 272 |
disp->needs_redraw = 0; |
| 273 |
} |
| 274 |
|
| 275 |
if (disp->width <= disp->screen_width) { |
| 276 |
disp->x = 0; |
| 277 |
} else { |
| 278 |
if (disp->x < 0) |
| 279 |
disp->x = 0; |
| 280 |
if (disp->x >= disp->width - disp->screen_width) |
| 281 |
disp->x = disp->width - disp->screen_width; |
| 282 |
} |
| 283 |
|
| 284 |
if (disp->height <= disp->screen_height) { |
| 285 |
disp->y = 0; |
| 286 |
} else { |
| 287 |
if (disp->y < 0) |
| 288 |
disp->y = 0; |
| 289 |
if (disp->y >= disp->height - disp->screen_height) |
| 290 |
disp->y = disp->height - disp->screen_height; |
| 291 |
} |
| 292 |
|
| 293 |
if (disp->needs_resize >= 1 && !disp->maximized) { |
| 294 |
if (disp->auto_resize || disp->needs_resize >= 2) { |
| 295 |
disp->last_screen_width = disp->screen_width = disp->width; |
| 296 |
disp->last_screen_height = disp->screen_height = disp->height; |
| 297 |
disp->x = 0; |
| 298 |
disp->y = 0; |
| 299 |
XResizeWindow(disp->dpy, disp->top, disp->screen_width, disp->screen_height); |
| 300 |
} |
| 301 |
disp->needs_resize = 0; |
| 302 |
} |
| 303 |
} |
| 304 |
|
| 305 |
static struct draw_methods x11_draw_methods = { |
| 306 |
NULL, |
| 307 |
NULL, |
| 308 |
topo_cairo_box, |
| 309 |
topo_cairo_line, |
| 310 |
topo_cairo_text, |
| 311 |
topo_cairo_textsize, |
| 312 |
}; |
| 313 |
|
| 314 |
int |
| 315 |
output_x11(struct lstopo_output *loutput, const char *dummy __hwloc_attribute_unused) |
| 316 |
{ |
| 317 |
struct lstopo_x11_output _disp, *disp = &_disp; |
| 318 |
struct lstopo_cairo_output *coutput; |
| 319 |
Display *dpy; |
| 320 |
Window root, top; |
| 321 |
XClassHint *class_hint; |
| 322 |
int scr; |
| 323 |
Screen *screen; |
| 324 |
int screen_width, screen_height; |
| 325 |
unsigned int dpi_x, dpi_y, dpi = 0; |
| 326 |
int finish = 0; |
| 327 |
int state = 0; |
| 328 |
int x = 0, y = 0; /* shut warning down */ |
| 329 |
int lastx, lasty; |
| 330 |
char *resources; |
| 331 |
|
| 332 |
coutput = &disp->coutput; |
| 333 |
memset(coutput, 0, sizeof(*coutput)); |
| 334 |
coutput->loutput = loutput; |
| 335 |
loutput->backend_data = coutput; |
| 336 |
loutput->methods = &x11_draw_methods; |
| 337 |
|
| 338 |
/* create the toplevel window */ |
| 339 |
if (!(dpy = XOpenDisplay(NULL))) { |
| 340 |
fprintf(stderr, "couldn't connect to X\n"); |
| 341 |
return -1; |
| 342 |
} |
| 343 |
|
| 344 |
XrmInitialize(); |
| 345 |
|
| 346 |
disp->dpy = dpy; |
| 347 |
disp->scr = scr = DefaultScreen(dpy); |
| 348 |
screen = ScreenOfDisplay(dpy, scr); |
| 349 |
|
| 350 |
/* Get DPI from xft, most often configured properly by users */ |
| 351 |
resources = XResourceManagerString(dpy); |
| 352 |
if (resources) { |
| 353 |
XrmDatabase database = XrmGetStringDatabase(resources); |
| 354 |
char *type; |
| 355 |
XrmValue value; |
| 356 |
|
| 357 |
if (XrmGetResource(database, "Xft.dpi", "Xft.dpi", &type, &value)) |
| 358 |
if (type && strcmp(type, "String") == 0) |
| 359 |
dpi = atoi(value.addr); |
| 360 |
|
| 361 |
XrmDestroyDatabase(database); |
| 362 |
} |
| 363 |
|
| 364 |
if (dpi == 0) { |
| 365 |
/* Fallback to value set by X server, but very often hardwired to 96dpi :/ */ |
| 366 |
/* 25.4mm per inch */ |
| 367 |
dpi_x = ((double) DisplayWidth(dpy, scr) * 25.4) / DisplayWidthMM(dpy, scr); |
| 368 |
dpi_y = ((double) DisplayHeight(dpy, scr) * 25.4) / DisplayHeightMM(dpy, scr); |
| 369 |
dpi = (dpi_x + dpi_y) / 2; |
| 370 |
} |
| 371 |
|
| 372 |
/* Original values for fontsize/gridsize were tuned for 96dpi */ |
| 373 |
coutput->loutput->fontsize = (coutput->loutput->fontsize * dpi) / 96; |
| 374 |
coutput->loutput->gridsize = (coutput->loutput->gridsize * dpi) / 96; |
| 375 |
|
| 376 |
/* recurse once for preparing sizes and positions using the root window surface */ |
| 377 |
root = RootWindow(dpy, scr); |
| 378 |
disp->top = root; |
| 379 |
loutput->drawing = LSTOPO_DRAWING_PREPARE; |
| 380 |
x11_create(disp, 1, 1); |
| 381 |
topo_cairo_paint(coutput); |
| 382 |
x11_destroy(disp); |
| 383 |
loutput->drawing = LSTOPO_DRAWING_DRAW; |
| 384 |
|
| 385 |
/* now create the actual window with the computed max size */ |
| 386 |
screen_width = loutput->width; |
| 387 |
screen_height = loutput->height; |
| 388 |
|
| 389 |
disp->top = top = XCreateSimpleWindow(dpy, root, 0, 0, screen_width, screen_height, 0, WhitePixel(dpy, scr), WhitePixel(dpy, scr)); |
| 390 |
XStoreName(dpy, top, loutput->title); |
| 391 |
XSetIconName(dpy, top, "lstopo"); |
| 392 |
class_hint = XAllocClassHint(); |
| 393 |
if (class_hint) { |
| 394 |
class_hint->res_name = class_hint->res_class = (char *) "lstopo"; |
| 395 |
XSetClassHint(dpy, top, class_hint); |
| 396 |
XFree(class_hint); |
| 397 |
} |
| 398 |
XSelectInput(dpy,top, StructureNotifyMask | KeyPressMask); |
| 399 |
|
| 400 |
if (screen_width >= screen->width) |
| 401 |
screen_width = screen->width; |
| 402 |
if (screen_height >= screen->height) |
| 403 |
screen_height = screen->height; |
| 404 |
disp->last_screen_width = 0; |
| 405 |
disp->last_screen_height = 0; |
| 406 |
disp->screen_width = screen_width; |
| 407 |
disp->screen_height = screen_height; |
| 408 |
disp->width = loutput->width; |
| 409 |
disp->height = loutput->height; |
| 410 |
disp->orig_fontsize = loutput->fontsize; |
| 411 |
disp->orig_gridsize = loutput->gridsize; |
| 412 |
disp->x = 0; |
| 413 |
disp->y = 0; |
| 414 |
disp->scale = disp->last_scale = 1.0f; |
| 415 |
/* TODO: if window got truncated, scale down? */ |
| 416 |
disp->needs_redraw = 0; |
| 417 |
disp->needs_resize = 0; |
| 418 |
disp->auto_resize = 1; |
| 419 |
disp->maximized = 0; |
| 420 |
|
| 421 |
x11_create(disp, loutput->width, loutput->height); |
| 422 |
|
| 423 |
XMapWindow(dpy, top); |
| 424 |
|
| 425 |
if (!loutput->refreshing) |
| 426 |
lstopo_show_interactive_help(); |
| 427 |
|
| 428 |
/* ready */ |
| 429 |
declare_colors(loutput); |
| 430 |
lstopo_prepare_custom_styles(loutput); |
| 431 |
|
| 432 |
lastx = disp->x; |
| 433 |
lasty = disp->y; |
| 434 |
|
| 435 |
topo_cairo_paint(coutput); |
| 436 |
|
| 437 |
while (!finish && !loutput->needs_topology_refresh) { |
| 438 |
XEvent e; |
| 439 |
if (!XEventsQueued(disp->dpy, QueuedAfterFlush)) { |
| 440 |
/* No pending event, flush moving windows before waiting for next event */ |
| 441 |
if (disp->x != lastx || disp->y != lasty) { |
| 442 |
XMoveWindow(disp->dpy, disp->win, -disp->x, -disp->y); |
| 443 |
lastx = disp->x; |
| 444 |
lasty = disp->y; |
| 445 |
} |
| 446 |
} |
| 447 |
XNextEvent(disp->dpy, &e); |
| 448 |
switch (e.type) { |
| 449 |
case Expose: |
| 450 |
if (e.xexpose.count < 1) |
| 451 |
topo_cairo_paint(coutput); |
| 452 |
break; |
| 453 |
case MotionNotify: |
| 454 |
if (state) { |
| 455 |
disp->x -= e.xmotion.x_root - x; |
| 456 |
disp->y -= e.xmotion.y_root - y; |
| 457 |
x = e.xmotion.x_root; |
| 458 |
y = e.xmotion.y_root; |
| 459 |
move_x11(disp); |
| 460 |
} |
| 461 |
break; |
| 462 |
case ConfigureNotify: { |
| 463 |
float wscale, hscale; |
| 464 |
disp->screen_width = e.xconfigure.width; |
| 465 |
disp->screen_height = e.xconfigure.height; |
| 466 |
if (disp->screen_width != disp->last_screen_width |
| 467 |
|| disp->screen_height != disp->last_screen_height) { |
| 468 |
wscale = disp->screen_width / (float)disp->width; |
| 469 |
hscale = disp->screen_height / (float)disp->height; |
| 470 |
disp->scale *= wscale > hscale ? hscale : wscale; |
| 471 |
if (disp->scale < 1.0f) |
| 472 |
disp->scale = 1.0f; |
| 473 |
move_x11(disp); |
| 474 |
} |
| 475 |
if (disp->x != lastx || disp->y != lasty) |
| 476 |
XMoveWindow(disp->dpy, disp->win, -disp->x, -disp->y); |
| 477 |
disp->maximized = x11_is_maximized(disp); |
| 478 |
break; |
| 479 |
} |
| 480 |
case ButtonPress: |
| 481 |
if (e.xbutton.button == Button1) { |
| 482 |
state = 1; |
| 483 |
x = e.xbutton.x_root; |
| 484 |
y = e.xbutton.y_root; |
| 485 |
} |
| 486 |
break; |
| 487 |
case ButtonRelease: |
| 488 |
if (e.xbutton.button == Button1) |
| 489 |
state = 0; |
| 490 |
break; |
| 491 |
case MappingNotify: |
| 492 |
XRefreshKeyboardMapping(&e.xmapping); |
| 493 |
break; |
| 494 |
case KeyPress: { |
| 495 |
KeySym keysym; |
| 496 |
XLookupString(&e.xkey, NULL, 0, &keysym, NULL); |
| 497 |
switch (keysym) { |
| 498 |
case XK_q: |
| 499 |
case XK_Q: |
| 500 |
case XK_Escape: |
| 501 |
finish = 1; |
| 502 |
break; |
| 503 |
case XK_F5: |
| 504 |
loutput->needs_topology_refresh = 1; |
| 505 |
break; |
| 506 |
case XK_Left: |
| 507 |
disp->x -= disp->screen_width/10; |
| 508 |
move_x11(disp); |
| 509 |
break; |
| 510 |
case XK_Right: |
| 511 |
disp->x += disp->screen_width/10; |
| 512 |
move_x11(disp); |
| 513 |
break; |
| 514 |
case XK_Up: |
| 515 |
disp->y -= disp->screen_height/10; |
| 516 |
move_x11(disp); |
| 517 |
break; |
| 518 |
case XK_Down: |
| 519 |
disp->y += disp->screen_height/10; |
| 520 |
move_x11(disp); |
| 521 |
break; |
| 522 |
case XK_Page_Up: |
| 523 |
if (e.xkey.state & ControlMask) { |
| 524 |
disp->x -= disp->screen_width; |
| 525 |
move_x11(disp); |
| 526 |
} else { |
| 527 |
disp->y -= disp->screen_height; |
| 528 |
move_x11(disp); |
| 529 |
} |
| 530 |
break; |
| 531 |
case XK_Page_Down: |
| 532 |
if (e.xkey.state & ControlMask) { |
| 533 |
disp->x += disp->screen_width; |
| 534 |
move_x11(disp); |
| 535 |
} else { |
| 536 |
disp->y += disp->screen_height; |
| 537 |
move_x11(disp); |
| 538 |
} |
| 539 |
break; |
| 540 |
case XK_Home: |
| 541 |
disp->x = 0; |
| 542 |
disp->y = 0; |
| 543 |
move_x11(disp); |
| 544 |
break; |
| 545 |
case XK_End: |
| 546 |
disp->x = INT_MAX; |
| 547 |
disp->y = INT_MAX; |
| 548 |
move_x11(disp); |
| 549 |
break; |
| 550 |
case XK_F: { |
| 551 |
/* fit drawing to window, dont't resize the window */ |
| 552 |
float wscale = disp->screen_width / (float)disp->width; |
| 553 |
float hscale = disp->screen_height / (float)disp->height; |
| 554 |
disp->scale *= wscale > hscale ? hscale : wscale; |
| 555 |
move_x11(disp); |
| 556 |
break; |
| 557 |
} |
| 558 |
case XK_plus: |
| 559 |
case XK_KP_Add: |
| 560 |
disp->scale *= 1.2f; |
| 561 |
disp->needs_resize = 1; |
| 562 |
move_x11(disp); |
| 563 |
break; |
| 564 |
case XK_minus: |
| 565 |
case XK_KP_Subtract: |
| 566 |
disp->scale /= 1.2f; |
| 567 |
disp->needs_resize = 1; |
| 568 |
move_x11(disp); |
| 569 |
break; |
| 570 |
case XK_1: |
| 571 |
case XK_KP_1: |
| 572 |
disp->scale = 1.0f; |
| 573 |
disp->needs_resize = 1; |
| 574 |
move_x11(disp); |
| 575 |
break; |
| 576 |
case XK_r: |
| 577 |
disp->needs_resize = 2; |
| 578 |
move_x11(disp); |
| 579 |
break; |
| 580 |
case XK_R: |
| 581 |
disp->auto_resize ^= 1; |
| 582 |
printf("%s window autoresizing\n", disp->auto_resize ? "enabled" : "disabled"); |
| 583 |
disp->needs_resize = 1; |
| 584 |
move_x11(disp); |
| 585 |
break; |
| 586 |
case XK_h: |
| 587 |
case XK_H: |
| 588 |
case XK_question: |
| 589 |
lstopo_show_interactive_help(); |
| 590 |
break; |
| 591 |
case XK_a: |
| 592 |
loutput->show_attrs_enabled ^= 1; |
| 593 |
printf("%s object attributes\n", loutput->show_attrs_enabled ? "enabled" : "disabled"); |
| 594 |
disp->needs_redraw = 1; |
| 595 |
disp->needs_resize = 1; |
| 596 |
move_x11(disp); |
| 597 |
break; |
| 598 |
case XK_t: |
| 599 |
loutput->show_text_enabled ^= 1; |
| 600 |
printf("%s object text\n", loutput->show_text_enabled ? "enabled" : "disabled"); |
| 601 |
disp->needs_redraw = 1; |
| 602 |
disp->needs_resize = 1; |
| 603 |
move_x11(disp); |
| 604 |
break; |
| 605 |
case XK_i: |
| 606 |
if (loutput->index_type == LSTOPO_INDEX_TYPE_DEFAULT) { |
| 607 |
loutput->index_type = LSTOPO_INDEX_TYPE_PHYSICAL; |
| 608 |
printf("switched to physical indexes\n"); |
| 609 |
} else if (loutput->index_type == LSTOPO_INDEX_TYPE_PHYSICAL) { |
| 610 |
loutput->index_type = LSTOPO_INDEX_TYPE_LOGICAL; |
| 611 |
printf("switched to logical indexes\n"); |
| 612 |
} else if (loutput->index_type == LSTOPO_INDEX_TYPE_LOGICAL) { |
| 613 |
loutput->index_type = LSTOPO_INDEX_TYPE_NONE; |
| 614 |
printf("switched to no indexes\n"); |
| 615 |
} else if (loutput->index_type == LSTOPO_INDEX_TYPE_NONE) { |
| 616 |
loutput->index_type = LSTOPO_INDEX_TYPE_DEFAULT; |
| 617 |
printf("switched to default indexes\n"); |
| 618 |
} else { |
| 619 |
abort(); |
| 620 |
} |
| 621 |
disp->needs_redraw = 1; |
| 622 |
disp->needs_resize = 1; |
| 623 |
move_x11(disp); |
| 624 |
break; |
| 625 |
case XK_b: |
| 626 |
loutput->show_binding ^= 1; |
| 627 |
printf("%s coloring of binding resources\n", loutput->show_binding ? "enabled" : "disabled"); |
| 628 |
disp->needs_redraw = 1; |
| 629 |
move_x11(disp); |
| 630 |
break; |
| 631 |
case XK_d: |
| 632 |
loutput->show_disallowed ^= 1; |
| 633 |
printf("%s coloring of disallowed resources\n", loutput->show_disallowed ? "enabled" : "disabled"); |
| 634 |
disp->needs_redraw = 1; |
| 635 |
move_x11(disp); |
| 636 |
break; |
| 637 |
case XK_k: |
| 638 |
loutput->show_cpukinds ^= 1; |
| 639 |
printf("%s displaying of CPU kinds\n", loutput->show_cpukinds ? "enabled" : "disabled"); |
| 640 |
disp->needs_redraw = 1; |
| 641 |
move_x11(disp); |
| 642 |
break; |
| 643 |
case XK_f: |
| 644 |
/* alternate between factorize+collapse, collapse only, and none */ |
| 645 |
if (loutput->factorize_enabled && loutput->pci_collapse_enabled) { |
| 646 |
loutput->factorize_enabled = 0; |
| 647 |
printf("factorizing disabled, PCI collapsing still enabled\n"); |
| 648 |
} else if (!loutput->factorize_enabled && loutput->pci_collapse_enabled) { |
| 649 |
loutput->pci_collapse_enabled = 0; |
| 650 |
printf("factorizing and PCI collapsing disabled\n"); |
| 651 |
} else { |
| 652 |
loutput->factorize_enabled = 1; |
| 653 |
loutput->pci_collapse_enabled = 1; |
| 654 |
printf("factorizing and PCI collapsing enabled\n"); |
| 655 |
} |
| 656 |
disp->needs_redraw = 1; |
| 657 |
disp->needs_resize = 1; |
| 658 |
move_x11(disp); |
| 659 |
break; |
| 660 |
case XK_l: |
| 661 |
if (loutput->show_legend == LSTOPO_SHOW_LEGEND_ALL) { |
| 662 |
loutput->show_legend = LSTOPO_SHOW_LEGEND_NONE; |
| 663 |
printf("switched to no legend lines\n"); |
| 664 |
} else if (loutput->show_legend == LSTOPO_SHOW_LEGEND_NONE) { |
| 665 |
loutput->show_legend = LSTOPO_SHOW_LEGEND_NO_DEFAULT; |
| 666 |
printf("switched to no default legend lines\n"); |
| 667 |
} else if (loutput->show_legend == LSTOPO_SHOW_LEGEND_NO_DEFAULT) { |
| 668 |
loutput->show_legend = LSTOPO_SHOW_LEGEND_ALL; |
| 669 |
printf("switched to all legend lines\n"); |
| 670 |
} else { |
| 671 |
abort(); |
| 672 |
} |
| 673 |
disp->needs_redraw = 1; |
| 674 |
disp->needs_resize = 1; |
| 675 |
move_x11(disp); |
| 676 |
break; |
| 677 |
case XK_E: |
| 678 |
lstopo_show_interactive_cli_options(loutput); |
| 679 |
break; |
| 680 |
} |
| 681 |
} |
| 682 |
} |
| 683 |
} |
| 684 |
x11_destroy(disp); |
| 685 |
XDestroyWindow(disp->dpy, disp->top); |
| 686 |
XFreeCursor(disp->dpy, disp->hand); |
| 687 |
XCloseDisplay(disp->dpy); |
| 688 |
|
| 689 |
destroy_colors(loutput); |
| 690 |
return 0; |
| 691 |
} |
| 692 |
#endif /* LSTOPO_HAVE_X11 */ |
| 693 |
|
| 694 |
|
| 695 |
#ifdef CAIRO_HAS_PNG_FUNCTIONS |
| 696 |
/* PNG back-end */ |
| 697 |
|
| 698 |
static struct draw_methods png_draw_methods = { |
| 699 |
NULL, |
| 700 |
NULL, |
| 701 |
topo_cairo_box, |
| 702 |
topo_cairo_line, |
| 703 |
topo_cairo_text, |
| 704 |
topo_cairo_textsize, |
| 705 |
}; |
| 706 |
|
| 707 |
int |
| 708 |
output_png(struct lstopo_output *loutput, const char *filename) |
| 709 |
{ |
| 710 |
struct lstopo_cairo_output coutput; |
| 711 |
FILE *output; |
| 712 |
cairo_surface_t *fakecs, *cs; |
| 713 |
|
| 714 |
output = open_output(filename, loutput->overwrite); |
| 715 |
if (!output) { |
| 716 |
fprintf(stderr, "Failed to open %s for writing (%s)\n", filename, strerror(errno)); |
| 717 |
return -1; |
| 718 |
} |
| 719 |
|
| 720 |
memset(&coutput, 0, sizeof(coutput)); |
| 721 |
coutput.loutput = loutput; |
| 722 |
loutput->backend_data = &coutput; |
| 723 |
loutput->methods = &png_draw_methods; |
| 724 |
loutput->file = output; |
| 725 |
|
| 726 |
/* create a fake surface */ |
| 727 |
fakecs = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 1, 1); |
| 728 |
coutput.surface = fakecs; |
| 729 |
|
| 730 |
/* recurse once for preparing sizes and positions using the fake surface */ |
| 731 |
loutput->drawing = LSTOPO_DRAWING_PREPARE; |
| 732 |
topo_cairo_paint(&coutput); |
| 733 |
loutput->drawing = LSTOPO_DRAWING_DRAW; |
| 734 |
cairo_surface_destroy(fakecs); |
| 735 |
|
| 736 |
/* create the actual surface with the right size */ |
| 737 |
cs = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, loutput->width, loutput->height); |
| 738 |
coutput.surface = cs; |
| 739 |
|
| 740 |
/* ready */ |
| 741 |
declare_colors(loutput); |
| 742 |
lstopo_prepare_custom_styles(loutput); |
| 743 |
|
| 744 |
topo_cairo_paint(&coutput); |
| 745 |
cairo_surface_write_to_png_stream(coutput.surface, topo_cairo_write, output); |
| 746 |
cairo_surface_destroy(coutput.surface); |
| 747 |
|
| 748 |
if (output != stdout) |
| 749 |
fclose(output); |
| 750 |
|
| 751 |
destroy_colors(loutput); |
| 752 |
return 0; |
| 753 |
} |
| 754 |
#endif /* CAIRO_HAS_PNG_FUNCTIONS */ |
| 755 |
|
| 756 |
|
| 757 |
#ifdef CAIRO_HAS_PDF_SURFACE |
| 758 |
/* PDF back-end */ |
| 759 |
|
| 760 |
static struct draw_methods pdf_draw_methods = { |
| 761 |
NULL, |
| 762 |
NULL, |
| 763 |
topo_cairo_box, |
| 764 |
topo_cairo_line, |
| 765 |
topo_cairo_text, |
| 766 |
topo_cairo_textsize, |
| 767 |
}; |
| 768 |
|
| 769 |
int |
| 770 |
output_pdf(struct lstopo_output *loutput, const char *filename) |
| 771 |
{ |
| 772 |
struct lstopo_cairo_output coutput; |
| 773 |
FILE *output; |
| 774 |
cairo_surface_t *fakecs, *cs; |
| 775 |
|
| 776 |
output = open_output(filename, loutput->overwrite); |
| 777 |
if (!output) { |
| 778 |
fprintf(stderr, "Failed to open %s for writing (%s)\n", filename, strerror(errno)); |
| 779 |
return -1; |
| 780 |
} |
| 781 |
|
| 782 |
memset(&coutput, 0, sizeof(coutput)); |
| 783 |
coutput.loutput = loutput; |
| 784 |
loutput->backend_data = &coutput; |
| 785 |
loutput->methods = &pdf_draw_methods; |
| 786 |
loutput->file = output; |
| 787 |
|
| 788 |
/* create a fake surface */ |
| 789 |
fakecs = cairo_pdf_surface_create_for_stream(NULL, NULL, 1, 1); |
| 790 |
coutput.surface = fakecs; |
| 791 |
|
| 792 |
/* recurse once for preparing sizes and positions using the fake surface */ |
| 793 |
loutput->drawing = LSTOPO_DRAWING_PREPARE; |
| 794 |
topo_cairo_paint(&coutput); |
| 795 |
loutput->drawing = LSTOPO_DRAWING_DRAW; |
| 796 |
cairo_surface_destroy(fakecs); |
| 797 |
|
| 798 |
/* create the actual surface with the right size */ |
| 799 |
cs = cairo_pdf_surface_create_for_stream(topo_cairo_write, loutput->file, loutput->width, loutput->height); |
| 800 |
coutput.surface = cs; |
| 801 |
|
| 802 |
/* ready */ |
| 803 |
declare_colors(loutput); |
| 804 |
lstopo_prepare_custom_styles(loutput); |
| 805 |
|
| 806 |
topo_cairo_paint(&coutput); |
| 807 |
cairo_surface_flush(coutput.surface); |
| 808 |
cairo_surface_destroy(coutput.surface); |
| 809 |
|
| 810 |
if (output != stdout) |
| 811 |
fclose(output); |
| 812 |
|
| 813 |
destroy_colors(loutput); |
| 814 |
return 0; |
| 815 |
} |
| 816 |
#endif /* CAIRO_HAS_PDF_SURFACE */ |
| 817 |
|
| 818 |
|
| 819 |
#ifdef CAIRO_HAS_PS_SURFACE |
| 820 |
/* PS back-end */ |
| 821 |
|
| 822 |
static struct draw_methods ps_draw_methods = { |
| 823 |
NULL, |
| 824 |
NULL, |
| 825 |
topo_cairo_box, |
| 826 |
topo_cairo_line, |
| 827 |
topo_cairo_text, |
| 828 |
topo_cairo_textsize, |
| 829 |
}; |
| 830 |
|
| 831 |
int |
| 832 |
output_ps(struct lstopo_output *loutput, const char *filename) |
| 833 |
{ |
| 834 |
struct lstopo_cairo_output coutput; |
| 835 |
FILE *output; |
| 836 |
cairo_surface_t *fakecs, *cs; |
| 837 |
|
| 838 |
output = open_output(filename, loutput->overwrite); |
| 839 |
if (!output) { |
| 840 |
fprintf(stderr, "Failed to open %s for writing (%s)\n", filename, strerror(errno)); |
| 841 |
return -1; |
| 842 |
} |
| 843 |
|
| 844 |
memset(&coutput, 0, sizeof(coutput)); |
| 845 |
coutput.loutput = loutput; |
| 846 |
loutput->backend_data = &coutput; |
| 847 |
loutput->methods = &ps_draw_methods; |
| 848 |
loutput->file = output; |
| 849 |
|
| 850 |
/* create a fake surface */ |
| 851 |
fakecs = cairo_ps_surface_create_for_stream(NULL, NULL, 1, 1); |
| 852 |
coutput.surface = fakecs; |
| 853 |
|
| 854 |
/* recurse once for preparing sizes and positions using the fake surface */ |
| 855 |
loutput->drawing = LSTOPO_DRAWING_PREPARE; |
| 856 |
topo_cairo_paint(&coutput); |
| 857 |
loutput->drawing = LSTOPO_DRAWING_DRAW; |
| 858 |
cairo_surface_destroy(fakecs); |
| 859 |
|
| 860 |
/* create the actual surface with the right size */ |
| 861 |
cs = cairo_ps_surface_create_for_stream(topo_cairo_write, loutput->file, loutput->width, loutput->height); |
| 862 |
coutput.surface = cs; |
| 863 |
|
| 864 |
/* ready */ |
| 865 |
declare_colors(loutput); |
| 866 |
lstopo_prepare_custom_styles(loutput); |
| 867 |
|
| 868 |
topo_cairo_paint(&coutput); |
| 869 |
cairo_surface_flush(coutput.surface); |
| 870 |
cairo_surface_destroy(coutput.surface); |
| 871 |
|
| 872 |
if (output != stdout) |
| 873 |
fclose(output); |
| 874 |
|
| 875 |
destroy_colors(loutput); |
| 876 |
return 0; |
| 877 |
} |
| 878 |
#endif /* CAIRO_HAS_PS_SURFACE */ |
| 879 |
|
| 880 |
|
| 881 |
#ifdef CAIRO_HAS_SVG_SURFACE |
| 882 |
/* SVG back-end */ |
| 883 |
|
| 884 |
static struct draw_methods svg_draw_methods = { |
| 885 |
NULL, |
| 886 |
NULL, |
| 887 |
topo_cairo_box, |
| 888 |
topo_cairo_line, |
| 889 |
topo_cairo_text, |
| 890 |
topo_cairo_textsize, |
| 891 |
}; |
| 892 |
|
| 893 |
int |
| 894 |
output_cairosvg(struct lstopo_output *loutput, const char *filename) |
| 895 |
{ |
| 896 |
struct lstopo_cairo_output coutput; |
| 897 |
FILE *output; |
| 898 |
cairo_surface_t *fakecs, *cs; |
| 899 |
|
| 900 |
output = open_output(filename, loutput->overwrite); |
| 901 |
if (!output) { |
| 902 |
fprintf(stderr, "Failed to open %s for writing (%s)\n", filename, strerror(errno)); |
| 903 |
return -1; |
| 904 |
} |
| 905 |
|
| 906 |
memset(&coutput, 0, sizeof(coutput)); |
| 907 |
coutput.loutput = loutput; |
| 908 |
loutput->backend_data = &coutput; |
| 909 |
loutput->methods = &svg_draw_methods; |
| 910 |
loutput->file = output; |
| 911 |
|
| 912 |
/* create a fake surface */ |
| 913 |
fakecs = cairo_svg_surface_create_for_stream(NULL, NULL, 1, 1); |
| 914 |
coutput.surface = fakecs; |
| 915 |
|
| 916 |
/* recurse once for preparing sizes and positions using the fake surface */ |
| 917 |
loutput->drawing = LSTOPO_DRAWING_PREPARE; |
| 918 |
topo_cairo_paint(&coutput); |
| 919 |
loutput->drawing = LSTOPO_DRAWING_DRAW; |
| 920 |
cairo_surface_destroy(fakecs); |
| 921 |
|
| 922 |
/* create the actual surface with the right size */ |
| 923 |
cs = cairo_svg_surface_create_for_stream(topo_cairo_write, loutput->file, loutput->width, loutput->height); |
| 924 |
coutput.surface = cs; |
| 925 |
|
| 926 |
/* ready */ |
| 927 |
declare_colors(loutput); |
| 928 |
lstopo_prepare_custom_styles(loutput); |
| 929 |
|
| 930 |
topo_cairo_paint(&coutput); |
| 931 |
cairo_surface_flush(coutput.surface); |
| 932 |
cairo_surface_destroy(coutput.surface); |
| 933 |
|
| 934 |
if (output != stdout) |
| 935 |
fclose(output); |
| 936 |
|
| 937 |
destroy_colors(loutput); |
| 938 |
return 0; |
| 939 |
} |
| 940 |
#endif /* CAIRO_HAS_SVG_SURFACE */ |