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 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675
|
/*
* ion/ioncore/focus.c
*
* Copyright (c) Tuomo Valkonen 1999-2007.
*
* See the included file LICENSE for details.
*/
#include <libmainloop/hooks.h>
#include <libmainloop/signal.h>
#include "common.h"
#include "focus.h"
#include "global.h"
#include "window.h"
#include "region.h"
#include "frame.h"
#include "colormap.h"
#include "activity.h"
#include "xwindow.h"
#include "regbind.h"
#include "log.h"
#include "screen-notify.h"
static void region_focuslist_awaiting_insertion_trigger(void);
static WRegion *find_warp_to_reg(WRegion *reg);
/*{{{ Hooks. */
WHook *region_do_warp_alt=NULL;
/*}}}*/
/*{{{ Focus list */
void region_focuslist_remove_with_mgrs(WRegion *reg)
{
WRegion *mgrp=region_manager_or_parent(reg);
UNLINK_ITEM(ioncore_g.focuslist, reg, active_next, active_prev);
if(mgrp!=NULL)
region_focuslist_remove_with_mgrs(mgrp);
}
void region_focuslist_push(WRegion *reg)
{
region_focuslist_remove_with_mgrs(reg);
LINK_ITEM_FIRST(ioncore_g.focuslist, reg, active_next, active_prev);
}
void region_focuslist_move_after(WRegion *reg, WRegion *after)
{
region_focuslist_remove_with_mgrs(reg);
LINK_ITEM_AFTER(ioncore_g.focuslist, after, reg,
active_next, active_prev);
}
static void region_focuslist_deinit(WRegion *reg)
{
WRegion *replace=region_manager_or_parent(reg);
if(replace!=NULL)
region_focuslist_move_after(replace, reg);
UNLINK_ITEM(ioncore_g.focuslist, reg, active_next, active_prev);
}
/*EXTL_DOC
* Go to and return to a previously active region (if any).
*
* Note that this function is asynchronous; the region will not
* actually have received the focus when this function returns.
*/
EXTL_EXPORT
WRegion *ioncore_goto_previous()
{
WRegion *next;
if(ioncore_g.focuslist==NULL)
return NULL;
/* We're trying to access the focus list from lua (likely from the UI). I
* thus force any pending focuslist updates to complete now */
region_focuslist_awaiting_insertion_trigger();
/* Find the first region on focus history list that isn't currently
* active.
*/
for(next=ioncore_g.focuslist->active_next;
next!=NULL;
next=next->active_next){
if(!REGION_IS_ACTIVE(next))
break;
}
if(next!=NULL)
region_goto(next);
return next;
}
/*EXTL_DOC
* Iterate over focus history until \var{iterfn} returns \code{false}.
* The function itself returns \code{true} if it reaches the end of list
* without this happening.
*/
EXTL_EXPORT
bool ioncore_focushistory_i(ExtlFn iterfn)
{
WRegion *next;
if(ioncore_g.focuslist==NULL)
return FALSE;
/* We're trying to access the focus list from lua (likely from the UI). I
* thus force any pending focuslist updates to complete now */
region_focuslist_awaiting_insertion_trigger();
/* Find the first region on focus history list that isn't currently
* active.
*/
for(next=ioncore_g.focuslist->active_next;
next!=NULL;
next=next->active_next){
if(!extl_iter_obj(iterfn, (Obj*)next))
return FALSE;
}
return TRUE;
}
/*}}}*/
/*{{{ Await focus */
static Watch await_watch=WATCH_INIT;
static void await_watch_handler(Watch *UNUSED(watch), WRegion *prev)
{
WRegion *r;
while(1){
r=REGION_PARENT_REG(prev);
if(r==NULL)
break;
if(watch_setup(&await_watch, (Obj*)r,
(WatchHandler*)await_watch_handler))
break;
prev=r;
}
}
void region_set_await_focus(WRegion *reg)
{
if(reg==NULL){
watch_reset(&await_watch);
}else{
watch_setup(&await_watch, (Obj*)reg,
(WatchHandler*)await_watch_handler);
}
}
static bool region_is_parent(WRegion *reg, WRegion *aw)
{
while(aw!=NULL){
if(aw==reg)
return TRUE;
aw=REGION_PARENT_REG(aw);
}
return FALSE;
}
static bool region_is_await(WRegion *reg)
{
return region_is_parent(reg, (WRegion*)await_watch.obj);
}
static bool region_is_focusnext(WRegion *reg)
{
return region_is_parent(reg, ioncore_g.focus_next);
}
/* Only keep await status if focus event is to an ancestor of the await
* region.
*/
static void check_clear_await(WRegion *reg)
{
if(region_is_await(reg) && reg!=(WRegion*)await_watch.obj)
return;
watch_reset(&await_watch);
}
WRegion *ioncore_await_focus()
{
return (WRegion*)(await_watch.obj);
}
/*}}}*/
/*{{{ focuslist delayed insertion logic */
static WTimer* focuslist_insert_timer=NULL;
static WRegion* region_awaiting_insertion;
static void timer_expired__focuslist_insert(WTimer* UNUSED(dummy1), Obj* UNUSED(dummy2))
{
region_focuslist_push(region_awaiting_insertion);
region_awaiting_insertion = NULL;
}
static void schedule_focuslist_insert_timer(WRegion *reg)
{
/* if the delay is disabled, add to the list NOW */
if(ioncore_g.focuslist_insert_delay<=0){
region_focuslist_push(reg);
return;
}
if(focuslist_insert_timer==NULL){
focuslist_insert_timer=create_timer();
if(focuslist_insert_timer==NULL){
return;
}
}
region_awaiting_insertion=reg;
timer_set(focuslist_insert_timer, ioncore_g.focuslist_insert_delay,
(WTimerHandler*)timer_expired__focuslist_insert, NULL);
}
static void region_focuslist_awaiting_insertion_cancel_if_is( WRegion* reg )
{
if(region_awaiting_insertion==reg &&
focuslist_insert_timer !=NULL)
{
timer_reset(focuslist_insert_timer);
}
}
static void region_focuslist_awaiting_insertion_trigger(void)
{
if(focuslist_insert_timer !=NULL &&
region_awaiting_insertion!=NULL)
{
timer_expired__focuslist_insert(NULL, NULL);
timer_reset(focuslist_insert_timer);
}
}
/*}}}*/
/*{{{ Events */
void region_got_focus(WRegion *reg)
{
WRegion *par;
check_clear_await(reg);
region_set_activity(reg, SETPARAM_UNSET);
if(reg->active_sub==NULL)
{
/* I update the current focus indicator right now. The focuslist is
* updated on a timer to keep the list ordered by importance (to keep
* windows that the user quickly cycles through at the bottom of the list) */
ioncore_g.focus_current = reg;
schedule_focuslist_insert_timer(reg);
}
if(!REGION_IS_ACTIVE(reg)){
D(fprintf(stderr, "got focus (inact) %s [%p]\n", OBJ_TYPESTR(reg), reg);)
reg->flags|=REGION_ACTIVE;
region_set_manager_pseudoactivity(reg);
par=REGION_PARENT_REG(reg);
if(par!=NULL){
par->active_sub=reg;
region_update_owned_grabs(par);
}
region_activated(reg);
region_notify_change(reg, ioncore_g.notifies.activated);
}else{
D(fprintf(stderr, "got focus (act) %s [%p]\n", OBJ_TYPESTR(reg), reg);)
}
/* Install default colour map only if there is no active subregion;
* their maps should come first. WClientWins will install their maps
* in region_activated. Other regions are supposed to use the same
* default map.
*/
if(reg->active_sub==NULL && !OBJ_IS(reg, WClientWin))
rootwin_install_colormap(region_rootwin_of(reg), None);
screen_update_workspace_indicatorwin( reg );
}
void region_lost_focus(WRegion *reg)
{
WRegion *par;
if(!REGION_IS_ACTIVE(reg)){
D(fprintf(stderr, "lost focus (inact) %s [%p:]\n", OBJ_TYPESTR(reg), reg);)
return;
}
par=REGION_PARENT_REG(reg);
if(par!=NULL && par->active_sub==reg){
par->active_sub=NULL;
region_update_owned_grabs(par);
}
D(fprintf(stderr, "lost focus (act) %s [%p:]\n", OBJ_TYPESTR(reg), reg);)
reg->flags&=~REGION_ACTIVE;
region_unset_manager_pseudoactivity(reg);
region_inactivated(reg);
region_notify_change(reg, ioncore_g.notifies.inactivated);
}
void region_focus_deinit(WRegion *reg)
{
/* if the region that's waiting to be added to the focuslist is being
* deleted, cancel the insertion */
if(region_awaiting_insertion)
region_focuslist_awaiting_insertion_cancel_if_is(reg);
region_focuslist_deinit(reg);
if(ioncore_g.focus_current==reg)
ioncore_g.focus_current=ioncore_g.focuslist;
}
/*}}}*/
/*{{{ Focus status requests */
/*EXTL_DOC
* Is \var{reg} active/does it or one of it's children of focus?
*/
EXTL_SAFE
EXTL_EXPORT_MEMBER
bool region_is_active(WRegion *reg)
{
return REGION_IS_ACTIVE(reg);
}
static bool region_manager_is_focusnext(WRegion *reg)
{
if(reg==NULL || ioncore_g.focus_next==NULL)
return FALSE;
if(reg==ioncore_g.focus_next)
return TRUE;
return region_manager_is_focusnext(REGION_MANAGER(reg));
}
bool region_may_control_focus(WRegion *reg)
{
if(OBJ_IS_BEING_DESTROYED(reg))
return FALSE;
if(REGION_IS_ACTIVE(reg) || REGION_IS_PSEUDOACTIVE(reg))
return TRUE;
if(region_is_await(reg) || region_is_focusnext(reg))
return TRUE;
if(region_manager_is_focusnext(reg))
return TRUE;
return FALSE;
}
/*}}}*/
/*{{{ set_focus, warp */
/*Time ioncore_focus_time=CurrentTime;*/
bool ioncore_should_focus_parent_when_refusing_focus(WRegion* reg)
{
WWindow* parent = reg->parent;
LOG(DEBUG, FOCUS, "Region %s refusing focus", reg->ni.name);
if(parent==NULL)
return FALSE;
/**
* If the region is refusing focus, this might be because the mouse
* entered it but it doesn't accept any focus (like in the case of
* oclock). In that case we want to focus its parent WTiling, if any.
*
* However, when for example IntelliJ IDEA's main window is refusing
* the focus because some transient completion popup is open, we want
* to leave the focus alone.
*/
LOG(DEBUG, FOCUS, "Parent is %s", parent->region.ni.name);
if(obj_is((Obj*)parent, &CLASSDESCR(WFrame))
&& ((WFrame*)parent)->mode == FRAME_MODE_TILED)
{
LOG(DEBUG, FOCUS, "Parent %s is a tiled WFrame", parent->region.ni.name);
return TRUE;
}
return FALSE;
}
void region_finalise_focusing(WRegion* reg, Window win, bool warp, int set_input)
{
if(warp) {
WRegion* reg_warp=find_warp_to_reg(reg);
if(reg_warp!=NULL && !region_is_cursor_inside(reg_warp)){
region_do_warp(reg_warp);
}
}
if(REGION_IS_ACTIVE(reg) && ioncore_await_focus()==NULL)
return;
region_set_await_focus(reg);
if(set_input)
XSetInputFocus(ioncore_g.dpy, win, RevertToParent, CurrentTime);
else if(ioncore_should_focus_parent_when_refusing_focus(reg)) {
XSetInputFocus(ioncore_g.dpy, reg->parent->win, RevertToParent, CurrentTime);
}
}
static WRegion *find_warp_to_reg(WRegion *reg)
{
if(reg==NULL)
return NULL;
if(reg->flags®ION_PLEASE_WARP)
return reg;
return find_warp_to_reg(region_manager_or_parent(reg));
}
bool region_is_cursor_inside(WRegion *reg)
{
int x, y, w, h, px=0, py=0;
WRootWin *root;
D(fprintf(stderr, "region_is_cursor_inside %p %s\n", reg, OBJ_TYPESTR(reg)));
root=region_rootwin_of(reg);
region_rootpos(reg, &x, &y);
w=REGION_GEOM(reg).w;
h=REGION_GEOM(reg).h;
if(xwindow_pointer_pos(WROOTWIN_ROOT(root), &px, &py)){
if(px>=x && py>=y && px<x+w && py<y+h)
return TRUE;
}
return FALSE;
}
bool region_do_warp_default(WRegion *reg)
{
int x, y;
WRootWin *root;
D(fprintf(stderr, "region_do_warp %p %s\n", reg, OBJ_TYPESTR(reg)));
root=region_rootwin_of(reg);
region_rootpos(reg, &x, &y);
x+=REGION_GEOM(reg).w*ioncore_g.warp_factor[0];
y+=REGION_GEOM(reg).h*ioncore_g.warp_factor[1];
if(ioncore_g.warp_factor[0] < 0.5) {
x+=ioncore_g.warp_margin;
}else if(ioncore_g.warp_factor[0] > 0.5) {
x-=ioncore_g.warp_margin;
}
if(ioncore_g.warp_factor[1] < 0.5) {
y+=ioncore_g.warp_margin;
}else if(ioncore_g.warp_factor[1] > 0.5) {
y-=ioncore_g.warp_margin;
}
rootwin_warp_pointer(root, x, y);
return TRUE;
}
void region_do_warp(WRegion *reg)
{
extl_protect(NULL);
hook_call_alt_o(region_do_warp_alt, (Obj*)reg);
extl_unprotect(NULL);
}
void region_maybewarp(WRegion *reg, bool warp)
{
ioncore_g.focus_next=reg;
ioncore_g.focus_next_source=IONCORE_FOCUSNEXT_OTHER;
ioncore_g.warp_next=(warp && ioncore_g.warp_enabled);
}
void region_maybewarp_now(WRegion *reg, bool warp)
{
ioncore_g.focus_next=NULL;
/* TODO: what if focus isn't set? Should focus_next be reset then? */
region_do_set_focus(reg, warp && ioncore_g.warp_enabled);
}
void region_set_focus(WRegion *reg)
{
region_maybewarp(reg, FALSE);
}
void region_warp(WRegion *reg)
{
region_maybewarp(reg, TRUE);
}
/*}}}*/
/*{{{ Misc. */
bool region_skip_focus(WRegion *reg)
{
while(reg!=NULL){
if(reg->flags®ION_SKIP_FOCUS)
return TRUE;
reg=REGION_PARENT_REG(reg);
}
return FALSE;
}
/*EXTL_DOC
* Returns the currently focused region, if any.
*/
EXTL_EXPORT
WRegion *ioncore_current()
{
return ioncore_g.focus_current;
}
/*}}}*/
/*{{{ Pointer focus hack */
/* This ugly hack tries to prevent focus change, when the pointer is
* in a window to be unmapped (or destroyed), and that does not have
* the focus, or should not soon have it.
*/
void region_pointer_focus_hack(WRegion *reg)
{
WRegion *act;
if(ioncore_g.opmode!=IONCORE_OPMODE_NORMAL)
return;
if(ioncore_g.focus_next!=NULL &&
ioncore_g.focus_next_source<=IONCORE_FOCUSNEXT_POINTERHACK){
return;
}
act=ioncore_await_focus();
if((REGION_IS_ACTIVE(reg) && act==NULL) || !region_is_fully_mapped(reg))
return;
if(act==NULL)
act=ioncore_g.focus_current;
if(act==NULL ||
OBJ_IS_BEING_DESTROYED(act) ||
!region_is_fully_mapped(act) ||
region_skip_focus(act)){
return;
}
region_set_focus(act);
ioncore_g.focus_next_source=IONCORE_FOCUSNEXT_POINTERHACK;
}
/*}}}*/
/*{{{ Debug printers */
void region_focuslist_debugprint(void)
{
WRegion* reg;
LOG(DEBUG, GENERAL, "focus list start =========================");
LOG(DEBUG, GENERAL, "currently-focused region ");
region_debugprint_parents( ioncore_g.focus_current );
LOG(DEBUG, GENERAL, "");
LOG(DEBUG, GENERAL, "top-of-focuslist ");
region_debugprint_parents( ioncore_g.focuslist );
LOG(DEBUG, GENERAL, "");
reg = ioncore_g.focuslist;
while(1){
LOG(DEBUG, GENERAL, "%p (%s)", (void*)reg, reg != NULL ? reg->ni.name : "NULL");
if(reg == NULL ||
reg->active_next == NULL ||
reg == reg->active_next )
{
break;
}
reg = reg->active_next;
}
LOG(DEBUG, GENERAL, "focus list end =========================");
}
/*}}}*/
|