Added send monitor function and handled mouse cursor events like dwm does, to drag/resize events causing bugs on multi monitor setups.
Browse files
+243 −67 src/window_manager.rs
| @@ -930,6 +930,37 @@ impl WindowManager { | ||
| 930 | 930 | best_monitor |
| 931 | 931 | } |
| 932 | 932 | |
| 933 | + fn sendmon(&mut self, window: Window, target_monitor_index: usize) -> WmResult<()> { | |
| 934 | + let current_monitor_index = self.clients | |
| 935 | + .get(&window) | |
| 936 | + .map(|c| c.monitor_index); | |
| 937 | + | |
| 938 | + if let Some(current_idx) = current_monitor_index { | |
| 939 | + if current_idx == target_monitor_index { | |
| 940 | + return Ok(()); | |
| 941 | + } | |
| 942 | + } | |
| 943 | + | |
| 944 | + self.unfocus(window)?; | |
| 945 | + self.detach(window); | |
| 946 | + self.detach_stack(window); | |
| 947 | + | |
| 948 | + if let Some(client) = self.clients.get_mut(&window) { | |
| 949 | + client.monitor_index = target_monitor_index; | |
| 950 | + if let Some(target_monitor) = self.monitors.get(target_monitor_index) { | |
| 951 | + client.tags = target_monitor.tagset[target_monitor.selected_tags_index]; | |
| 952 | + } | |
| 953 | + } | |
| 954 | + | |
| 955 | + self.attach_aside(window, target_monitor_index); | |
| 956 | + self.attach_stack(window, target_monitor_index); | |
| 957 | + | |
| 958 | + self.focus(None)?; | |
| 959 | + self.apply_layout()?; | |
| 960 | + | |
| 961 | + Ok(()) | |
| 962 | + } | |
| 963 | + | |
| 933 | 964 | fn dir_to_monitor(&self, direction: i32) -> Option<usize> { |
| 934 | 965 | if self.monitors.len() <= 1 { |
| 935 | 966 | return None; |
| @@ -2323,62 +2354,146 @@ impl WindowManager { | ||
| 2323 | 2354 | } |
| 2324 | 2355 | |
| 2325 | 2356 | fn move_mouse(&mut self, window: Window) -> WmResult<()> { |
| 2326 | - self.floating_windows.insert(window); | |
| 2357 | + let is_fullscreen = self.clients | |
| 2358 | + .get(&window) | |
| 2359 | + .map(|c| c.is_fullscreen) | |
| 2360 | + .unwrap_or(false); | |
| 2327 | 2361 | |
| 2328 | - let geometry = self.connection.get_geometry(window)?.reply()?; | |
| 2362 | + if is_fullscreen { | |
| 2363 | + return Ok(()); | |
| 2364 | + } | |
| 2329 | 2365 | |
| 2330 | - self.connection | |
| 2331 | - .grab_pointer( | |
| 2332 | - false, | |
| 2333 | - self.root, | |
| 2334 | - (EventMask::POINTER_MOTION | EventMask::BUTTON_RELEASE).into(), | |
| 2335 | - GrabMode::ASYNC, | |
| 2336 | - GrabMode::ASYNC, | |
| 2337 | - x11rb::NONE, | |
| 2338 | - x11rb::NONE, | |
| 2339 | - x11rb::CURRENT_TIME, | |
| 2340 | - )? | |
| 2341 | - .reply()?; | |
| 2366 | + let client_info = self.clients.get(&window).map(|c| { | |
| 2367 | + (c.x_position, c.y_position, c.width, c.height, c.is_floating, c.monitor_index) | |
| 2368 | + }); | |
| 2369 | + | |
| 2370 | + let Some((orig_x, orig_y, width, height, was_floating, monitor_idx)) = client_info else { | |
| 2371 | + return Ok(()); | |
| 2372 | + }; | |
| 2373 | + | |
| 2374 | + let monitor = self.monitors.get(monitor_idx).cloned(); | |
| 2375 | + let Some(monitor) = monitor else { | |
| 2376 | + return Ok(()); | |
| 2377 | + }; | |
| 2378 | + | |
| 2379 | + let snap = 32; | |
| 2380 | + let is_normie = self.layout.name() == "normie"; | |
| 2381 | + | |
| 2382 | + self.connection.grab_pointer( | |
| 2383 | + false, | |
| 2384 | + self.root, | |
| 2385 | + (EventMask::POINTER_MOTION | EventMask::BUTTON_RELEASE | | |
| 2386 | + EventMask::BUTTON_PRESS).into(), | |
| 2387 | + GrabMode::ASYNC, | |
| 2388 | + GrabMode::ASYNC, | |
| 2389 | + x11rb::NONE, | |
| 2390 | + x11rb::NONE, | |
| 2391 | + x11rb::CURRENT_TIME, | |
| 2392 | + )?.reply()?; | |
| 2342 | 2393 | |
| 2343 | 2394 | let pointer = self.connection.query_pointer(self.root)?.reply()?; |
| 2344 | - let (start_x, start_y) = (pointer.root_x, pointer.root_y); | |
| 2395 | + let (start_x, start_y) = (pointer.root_x as i32, pointer.root_y as i32); | |
| 2345 | 2396 | |
| 2346 | - self.connection.configure_window( | |
| 2347 | - window, | |
| 2348 | - &ConfigureWindowAux::new().stack_mode(StackMode::ABOVE), | |
| 2349 | - )?; | |
| 2397 | + let mut last_time = 0u32; | |
| 2350 | 2398 | |
| 2351 | 2399 | loop { |
| 2352 | 2400 | let event = self.connection.wait_for_event()?; |
| 2353 | 2401 | match event { |
| 2402 | + Event::ConfigureRequest(_) | Event::MapRequest(_) | Event::Expose(_) => {} | |
| 2354 | 2403 | Event::MotionNotify(e) => { |
| 2355 | - let new_x = geometry.x + (e.root_x - start_x); | |
| 2356 | - let new_y = geometry.y + (e.root_y - start_y); | |
| 2357 | - self.connection.configure_window( | |
| 2358 | - window, | |
| 2359 | - &ConfigureWindowAux::new().x(new_x as i32).y(new_y as i32), | |
| 2360 | - )?; | |
| 2361 | - self.connection.flush()?; | |
| 2404 | + if e.time.wrapping_sub(last_time) <= 16 { | |
| 2405 | + continue; | |
| 2406 | + } | |
| 2407 | + last_time = e.time; | |
| 2408 | + | |
| 2409 | + let mut new_x = orig_x as i32 + (e.root_x as i32 - start_x); | |
| 2410 | + let mut new_y = orig_y as i32 + (e.root_y as i32 - start_y); | |
| 2411 | + | |
| 2412 | + if (monitor.window_area_x - new_x).abs() < snap { | |
| 2413 | + new_x = monitor.window_area_x; | |
| 2414 | + } else if ((monitor.window_area_x + monitor.window_area_width) - (new_x + width as i32)).abs() < snap { | |
| 2415 | + new_x = monitor.window_area_x + monitor.window_area_width - width as i32; | |
| 2416 | + } | |
| 2417 | + | |
| 2418 | + if (monitor.window_area_y - new_y).abs() < snap { | |
| 2419 | + new_y = monitor.window_area_y; | |
| 2420 | + } else if ((monitor.window_area_y + monitor.window_area_height) - (new_y + height as i32)).abs() < snap { | |
| 2421 | + new_y = monitor.window_area_y + monitor.window_area_height - height as i32; | |
| 2422 | + } | |
| 2423 | + | |
| 2424 | + if !was_floating && !is_normie && | |
| 2425 | + ((new_x - orig_x as i32).abs() > snap || (new_y - orig_y as i32).abs() > snap) { | |
| 2426 | + self.toggle_floating()?; | |
| 2427 | + } | |
| 2428 | + | |
| 2429 | + let should_resize = is_normie || self.clients | |
| 2430 | + .get(&window) | |
| 2431 | + .map(|c| c.is_floating) | |
| 2432 | + .unwrap_or(false); | |
| 2433 | + | |
| 2434 | + if should_resize { | |
| 2435 | + if let Some(client) = self.clients.get_mut(&window) { | |
| 2436 | + client.x_position = new_x as i16; | |
| 2437 | + client.y_position = new_y as i16; | |
| 2438 | + } | |
| 2439 | + | |
| 2440 | + self.connection.configure_window( | |
| 2441 | + window, | |
| 2442 | + &ConfigureWindowAux::new() | |
| 2443 | + .x(new_x) | |
| 2444 | + .y(new_y), | |
| 2445 | + )?; | |
| 2446 | + self.connection.flush()?; | |
| 2447 | + } | |
| 2362 | 2448 | } |
| 2363 | 2449 | Event::ButtonRelease(_) => break, |
| 2364 | 2450 | _ => {} |
| 2365 | 2451 | } |
| 2366 | 2452 | } |
| 2367 | 2453 | |
| 2368 | - self.connection | |
| 2369 | - .ungrab_pointer(x11rb::CURRENT_TIME)? | |
| 2370 | - .check()?; | |
| 2371 | - self.connection | |
| 2372 | - .allow_events(Allow::REPLAY_POINTER, x11rb::CURRENT_TIME)? | |
| 2373 | - .check()?; | |
| 2454 | + self.connection.ungrab_pointer(x11rb::CURRENT_TIME)?.check()?; | |
| 2455 | + | |
| 2456 | + let final_client = self.clients.get(&window).map(|c| { | |
| 2457 | + (c.x_position, c.y_position, c.width, c.height) | |
| 2458 | + }); | |
| 2459 | + | |
| 2460 | + if let Some((x, y, w, h)) = final_client { | |
| 2461 | + let new_monitor = self.rect_to_monitor(x as i32, y as i32, w as i32, h as i32); | |
| 2462 | + if new_monitor != monitor_idx { | |
| 2463 | + self.sendmon(window, new_monitor)?; | |
| 2464 | + self.selected_monitor = new_monitor; | |
| 2465 | + self.focus(None)?; | |
| 2466 | + } | |
| 2467 | + } | |
| 2374 | 2468 | |
| 2375 | 2469 | Ok(()) |
| 2376 | 2470 | } |
| 2377 | 2471 | |
| 2378 | 2472 | fn resize_mouse(&mut self, window: Window) -> WmResult<()> { |
| 2379 | - self.floating_windows.insert(window); | |
| 2473 | + let is_fullscreen = self.clients | |
| 2474 | + .get(&window) | |
| 2475 | + .map(|c| c.is_fullscreen) | |
| 2476 | + .unwrap_or(false); | |
| 2380 | 2477 | |
| 2381 | - let geometry = self.connection.get_geometry(window)?.reply()?; | |
| 2478 | + if is_fullscreen { | |
| 2479 | + return Ok(()); | |
| 2480 | + } | |
| 2481 | + | |
| 2482 | + let client_info = self.clients.get(&window).map(|c| { | |
| 2483 | + (c.x_position, c.y_position, c.width, c.height, c.border_width, c.is_floating, c.monitor_index) | |
| 2484 | + }); | |
| 2485 | + | |
| 2486 | + let Some((orig_x, orig_y, orig_width, orig_height, border_width, was_floating, monitor_idx)) = client_info else { | |
| 2487 | + return Ok(()); | |
| 2488 | + }; | |
| 2489 | + | |
| 2490 | + let monitor = self.monitors.get(monitor_idx).cloned(); | |
| 2491 | + let Some(monitor) = monitor else { | |
| 2492 | + return Ok(()); | |
| 2493 | + }; | |
| 2494 | + | |
| 2495 | + let snap = 32; | |
| 2496 | + let is_normie = self.layout.name() == "normie"; | |
| 2382 | 2497 | |
| 2383 | 2498 | self.connection.warp_pointer( |
| 2384 | 2499 | x11rb::NONE, |
| @@ -2387,54 +2502,115 @@ impl WindowManager { | ||
| 2387 | 2502 | 0, |
| 2388 | 2503 | 0, |
| 2389 | 2504 | 0, |
| 2390 | - geometry.width as i16, | |
| 2391 | - geometry.height as i16, | |
| 2505 | + (orig_width + border_width - 1) as i16, | |
| 2506 | + (orig_height + border_width - 1) as i16, | |
| 2392 | 2507 | )?; |
| 2393 | 2508 | |
| 2394 | - self.connection | |
| 2395 | - .grab_pointer( | |
| 2396 | - false, | |
| 2397 | - self.root, | |
| 2398 | - (EventMask::POINTER_MOTION | EventMask::BUTTON_RELEASE).into(), | |
| 2399 | - GrabMode::ASYNC, | |
| 2400 | - GrabMode::ASYNC, | |
| 2401 | - x11rb::NONE, | |
| 2402 | - x11rb::NONE, | |
| 2403 | - x11rb::CURRENT_TIME, | |
| 2404 | - )? | |
| 2405 | - .reply()?; | |
| 2509 | + self.connection.grab_pointer( | |
| 2510 | + false, | |
| 2511 | + self.root, | |
| 2512 | + (EventMask::POINTER_MOTION | EventMask::BUTTON_RELEASE | | |
| 2513 | + EventMask::BUTTON_PRESS).into(), | |
| 2514 | + GrabMode::ASYNC, | |
| 2515 | + GrabMode::ASYNC, | |
| 2516 | + x11rb::NONE, | |
| 2517 | + x11rb::NONE, | |
| 2518 | + x11rb::CURRENT_TIME, | |
| 2519 | + )?.reply()?; | |
| 2406 | 2520 | |
| 2407 | - self.connection.configure_window( | |
| 2408 | - window, | |
| 2409 | - &ConfigureWindowAux::new().stack_mode(StackMode::ABOVE), | |
| 2410 | - )?; | |
| 2521 | + let mut last_time = 0u32; | |
| 2411 | 2522 | |
| 2412 | 2523 | loop { |
| 2413 | 2524 | let event = self.connection.wait_for_event()?; |
| 2414 | 2525 | match event { |
| 2526 | + Event::ConfigureRequest(_) | Event::MapRequest(_) | Event::Expose(_) => {} | |
| 2415 | 2527 | Event::MotionNotify(e) => { |
| 2416 | - let new_width = (e.root_x - geometry.x).max(1) as u32; | |
| 2417 | - let new_height = (e.root_y - geometry.y).max(1) as u32; | |
| 2528 | + if e.time.wrapping_sub(last_time) <= 16 { | |
| 2529 | + continue; | |
| 2530 | + } | |
| 2531 | + last_time = e.time; | |
| 2532 | + | |
| 2533 | + let new_width = ((e.root_x as i32 - orig_x as i32 - 2 * border_width as i32 + 1).max(1)) as u32; | |
| 2534 | + let new_height = ((e.root_y as i32 - orig_y as i32 - 2 * border_width as i32 + 1).max(1)) as u32; | |
| 2535 | + | |
| 2536 | + let in_monitor_bounds = | |
| 2537 | + monitor.window_area_x + new_width as i32 >= monitor.window_area_x && | |
| 2538 | + monitor.window_area_x + new_width as i32 <= monitor.window_area_x + monitor.window_area_width && | |
| 2539 | + monitor.window_area_y + new_height as i32 >= monitor.window_area_y && | |
| 2540 | + monitor.window_area_y + new_height as i32 <= monitor.window_area_y + monitor.window_area_height; | |
| 2541 | + | |
| 2542 | + if in_monitor_bounds { | |
| 2543 | + if !was_floating && !is_normie && | |
| 2544 | + ((new_width as i32 - orig_width as i32).abs() > snap || | |
| 2545 | + (new_height as i32 - orig_height as i32).abs() > snap) { | |
| 2546 | + self.toggle_floating()?; | |
| 2547 | + } | |
| 2548 | + } | |
| 2418 | 2549 | |
| 2419 | - self.connection.configure_window( | |
| 2420 | - window, | |
| 2421 | - &ConfigureWindowAux::new() | |
| 2422 | - .width(new_width) | |
| 2423 | - .height(new_height), | |
| 2424 | - )?; | |
| 2425 | - self.connection.flush()?; | |
| 2550 | + let should_resize = is_normie || self.clients | |
| 2551 | + .get(&window) | |
| 2552 | + .map(|c| c.is_floating) | |
| 2553 | + .unwrap_or(false); | |
| 2554 | + | |
| 2555 | + if should_resize { | |
| 2556 | + if let Some(client) = self.clients.get(&window) { | |
| 2557 | + let (hint_width, hint_height) = self.apply_size_hints( | |
| 2558 | + client, | |
| 2559 | + new_width as i32, | |
| 2560 | + new_height as i32, | |
| 2561 | + ); | |
| 2562 | + | |
| 2563 | + if let Some(client_mut) = self.clients.get_mut(&window) { | |
| 2564 | + client_mut.width = hint_width as u16; | |
| 2565 | + client_mut.height = hint_height as u16; | |
| 2566 | + } | |
| 2567 | + | |
| 2568 | + self.connection.configure_window( | |
| 2569 | + window, | |
| 2570 | + &ConfigureWindowAux::new() | |
| 2571 | + .width(hint_width as u32) | |
| 2572 | + .height(hint_height as u32), | |
| 2573 | + )?; | |
| 2574 | + self.connection.flush()?; | |
| 2575 | + } | |
| 2576 | + } | |
| 2426 | 2577 | } |
| 2427 | 2578 | Event::ButtonRelease(_) => break, |
| 2428 | 2579 | _ => {} |
| 2429 | 2580 | } |
| 2430 | 2581 | } |
| 2431 | 2582 | |
| 2432 | - self.connection | |
| 2433 | - .ungrab_pointer(x11rb::CURRENT_TIME)? | |
| 2434 | - .check()?; | |
| 2435 | - self.connection | |
| 2436 | - .allow_events(Allow::REPLAY_POINTER, x11rb::CURRENT_TIME)? | |
| 2437 | - .check()?; | |
| 2583 | + let final_client = self.clients.get(&window).map(|c| { | |
| 2584 | + (c.width, c.border_width) | |
| 2585 | + }); | |
| 2586 | + | |
| 2587 | + if let Some((w, bw)) = final_client { | |
| 2588 | + self.connection.warp_pointer( | |
| 2589 | + x11rb::NONE, | |
| 2590 | + window, | |
| 2591 | + 0, | |
| 2592 | + 0, | |
| 2593 | + 0, | |
| 2594 | + 0, | |
| 2595 | + (w + bw - 1) as i16, | |
| 2596 | + (w + bw - 1) as i16, | |
| 2597 | + )?; | |
| 2598 | + } | |
| 2599 | + | |
| 2600 | + self.connection.ungrab_pointer(x11rb::CURRENT_TIME)?.check()?; | |
| 2601 | + | |
| 2602 | + let final_client_pos = self.clients.get(&window).map(|c| { | |
| 2603 | + (c.x_position, c.y_position, c.width, c.height) | |
| 2604 | + }); | |
| 2605 | + | |
| 2606 | + if let Some((x, y, w, h)) = final_client_pos { | |
| 2607 | + let new_monitor = self.rect_to_monitor(x as i32, y as i32, w as i32, h as i32); | |
| 2608 | + if new_monitor != monitor_idx { | |
| 2609 | + self.sendmon(window, new_monitor)?; | |
| 2610 | + self.selected_monitor = new_monitor; | |
| 2611 | + self.focus(None)?; | |
| 2612 | + } | |
| 2613 | + } | |
| 2438 | 2614 | |
| 2439 | 2615 | Ok(()) |
| 2440 | 2616 | } |