int get_window_monitor(Window win) { int x, y, w, h; get_window_coordinates(win, &x, &y, &w, &h); int best_match = -1; int match_right = 0; int match_bottom = 0; // There is an ambiguity when a window is right on the edge between screens. // In that case, prefer the monitor which is on the right and bottom of the window's top-left corner. for (int i = 0; i < server.num_monitors; i++) { if (x >= server.monitors[i].x && x <= (server.monitors[i].x + server.monitors[i].width) && y >= server.monitors[i].y && y <= (server.monitors[i].y + server.monitors[i].height)) { int current_right = x < (server.monitors[i].x + server.monitors[i].width); int current_bottom = y < (server.monitors[i].y + server.monitors[i].height); if (best_match < 0 || (!match_right && current_right) || (!match_bottom && current_bottom)) { best_match = i; } } } if (best_match < 0) best_match = 0; //fprintf(stderr, "desktop %d, window %lx %s : monitor %d, (%d, %d)\n", 1 + get_current_desktop(), win, get_task(win) ? get_task(win)->title : "??", best_match+1, x, y); return best_match; }
int get_window_desktop(Window win) { int desktop = get_property32(win, server.atom._NET_WM_DESKTOP, XA_CARDINAL); if (desktop == ALL_DESKTOPS) return desktop; if (!server.viewports) return CLAMP(desktop, 0, server.num_desktops - 1); int x, y, w, h; get_window_coordinates(win, &x, &y, &w, &h); desktop = get_current_desktop(); // Window coordinates are relative to the current viewport, make them absolute x += server.viewports[desktop].x; y += server.viewports[desktop].y; if (x < 0 || y < 0) { int num_results; long *x_screen_size = server_get_property(server.root_win, server.atom._NET_DESKTOP_GEOMETRY, XA_CARDINAL, &num_results); if (!x_screen_size) return 0; int x_screen_width = x_screen_size[0]; int x_screen_height = x_screen_size[1]; XFree(x_screen_size); // Wrap if (x < 0) x += x_screen_width; if (y < 0) y += x_screen_height; } int best_match = -1; int match_right = 0; int match_bottom = 0; // There is an ambiguity when a window is right on the edge between viewports. // In that case, prefer the viewports which is on the right and bottom of the window's top-left corner. for (int i = 0; i < server.num_desktops; i++) { if (x >= server.viewports[i].x && x <= (server.viewports[i].x + server.viewports[i].width) && y >= server.viewports[i].y && y <= (server.viewports[i].y + server.viewports[i].height)) { int current_right = x < (server.viewports[i].x + server.viewports[i].width); int current_bottom = y < (server.viewports[i].y + server.viewports[i].height); if (best_match < 0 || (!match_right && current_right) || (!match_bottom && current_bottom)) { best_match = i; } } } if (best_match < 0) best_match = 0; // fprintf(stderr, "window %lx %s : viewport %d, (%d, %d)\n", win, get_task(win) ? get_task(win)->title : "??", // best_match+1, x, y); return best_match; }