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
|
/*
* ion/ioncore/screen.c
*
* Copyright (c) Tuomo Valkonen 1999-2004.
*
* Ion is free software; you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*/
#include "common.h"
#include "global.h"
#include "screen.h"
#include "region.h"
#include "attach.h"
#include "manage.h"
#include "objp.h"
#include "focus.h"
#include "property.h"
#include "names.h"
#include "reginfo.h"
#include "saveload.h"
#include "resize.h"
#include "genws.h"
#include "event.h"
#include "bindmaps.h"
#include "regbind.h"
#include "minmax.h"
#include "genframe-pointer.h"
#include "extlconv.h"
#define SCR_MLIST(SCR) ((SCR)->mplex.managed_list)
#define SCR_MCOUNT(SCR) ((SCR)->mplex.managed_count)
#define SCR_CURRENT(SCR) ((SCR)->mplex.current_sub)
#define SCR_WIN(SCR) ((SCR)->mplex.win.win)
/*{{{ Init/deinit */
static bool screen_init(WScreen *scr, WRootWin *rootwin,
int id, const WRectangle *geom, bool useroot)
{
Window win;
XSetWindowAttributes attr;
ulong attrflags=0;
scr->id=id;
scr->atom_workspace=None;
scr->uses_root=useroot;
scr->configured=FALSE;
scr->managed_off.x=0;
scr->managed_off.y=0;
scr->managed_off.w=0;
scr->managed_off.h=0;
scr->next_scr=NULL;
scr->prev_scr=NULL;
if(useroot){
win=WROOTWIN_ROOT(rootwin);
}else{
attr.background_pixmap=ParentRelative;
attrflags=CWBackPixmap;
win=XCreateWindow(wglobal.dpy, WROOTWIN_ROOT(rootwin),
geom->x, geom->y, geom->w, geom->h, 0,
DefaultDepth(wglobal.dpy, rootwin->xscr),
InputOutput,
DefaultVisual(wglobal.dpy, rootwin->xscr),
attrflags, &attr);
if(win==None)
return FALSE;
}
if(!mplex_init((WMPlex*)scr, (WWindow*)rootwin, win, geom))
return FALSE;
/*scr->mplex.win.region.rootwin=rootwin;
region_set_parent((WRegion*)scr, (WRegion*)rootwin);*/
scr->mplex.flags|=WMPLEX_ADD_TO_END;
scr->mplex.win.region.flags|=REGION_BINDINGS_ARE_GRABBED;
if(useroot)
scr->mplex.win.region.flags|=REGION_MAPPED;
XSelectInput(wglobal.dpy, win,
FocusChangeMask|EnterWindowMask|
KeyPressMask|KeyReleaseMask|
ButtonPressMask|ButtonReleaseMask|
(useroot ? ROOT_MASK : 0));
if(id==0){
scr->atom_workspace=XInternAtom(wglobal.dpy, "_ION_WORKSPACE", False);
}else if(id>=0){
char *str;
libtu_asprintf(&str, "_ION_WORKSPACE%d", id);
if(str==NULL){
warn_err();
}else{
scr->atom_workspace=XInternAtom(wglobal.dpy, str, False);
free(str);
}
}
/* Add rootwin's bindings to screens (ungrabbed) so that bindings
* are called with the proper region.
*/
region_add_bindmap((WRegion*)scr, &ioncore_rootwin_bindmap);
LINK_ITEM(wglobal.screens, scr, next_scr, prev_scr);
if(wglobal.active_screen==NULL)
wglobal.active_screen=scr;
return TRUE;
}
WScreen *create_screen(WRootWin *rootwin, int id, const WRectangle *geom,
bool useroot)
{
CREATEOBJ_IMPL(WScreen, screen, (p, rootwin, id, geom, useroot));
}
void screen_deinit(WScreen *scr)
{
UNLINK_ITEM(wglobal.screens, scr, next_scr, prev_scr);
while(SCR_MLIST(scr)!=NULL)
region_unset_manager(SCR_MLIST(scr), (WRegion*)scr, &(SCR_MLIST(scr)));
if(scr->uses_root)
SCR_WIN(scr)=None;
mplex_deinit((WMPlex*)scr);
}
static bool create_initial_workspace_on_scr(WScreen *scr)
{
WRegionSimpleCreateFn *fn=NULL;
WRegion *reg=NULL;
/* Check default_ws_type */{
ExtlTab tab=extl_globals();
char *wsclass=NULL;
if(extl_table_gets_s(tab, "default_ws_type", &wsclass)){
fn=lookup_region_simple_create_fn_inh(wsclass);
free(wsclass);
}
extl_unref_table(tab);
}
if(fn==NULL)
fn=lookup_region_simple_create_fn_inh("WGenWS");
if(fn==NULL){
warn("Could not find a complete workspace class. "
"Please load some modules.");
return FALSE;
}
reg=mplex_attach_new_simple((WMPlex*)scr, fn, TRUE);
if(reg==NULL){
warn("Unable to create a workspace on screen %d\n", scr->id);
return FALSE;
}
region_set_name(reg, "main");
return TRUE;
}
bool screen_initialize_workspaces(WScreen* scr)
{
WRegion *ws=NULL;
if(scr->configured || SCR_MCOUNT(scr)>0)
return TRUE;
return create_initial_workspace_on_scr(scr);
}
/*}}}*/
/*{{{ Attach/detach */
void screen_managed_geom(WScreen *scr, WRectangle *geom)
{
geom->x=scr->managed_off.x;
geom->y=scr->managed_off.y;
geom->w=REGION_GEOM(scr).w+scr->managed_off.w;
geom->h=REGION_GEOM(scr).h+scr->managed_off.h;
geom->w=maxof(geom->w, 0);
geom->h=maxof(geom->h, 0);
}
static bool screen_handle_drop(WScreen *screen, int x, int y, WRegion *dropped)
{
WMPlex *mplex=(WMPlex*)screen;
if(mplex->current_sub!=NULL &&
HAS_DYN(mplex->current_sub, region_handle_drop)){
int rx, ry;
region_rootpos(mplex->current_sub, &rx, &ry);
if(coords_in_rect(®ION_GEOM(mplex->current_sub), x-rx, y-ry)){
if(region_handle_drop(mplex->current_sub, x, y, dropped))
return TRUE;
}
}
/* Do not attach to ourselves unlike generic WMPlex. */
return FALSE;
}
/*}}}*/
/*{{{ Region dynfun implementations */
static void screen_fit(WScreen *scr, const WRectangle *geom)
{
WRegion *sub;
if(scr->uses_root){
WRectangle mg;
screen_managed_geom(scr, &mg);
FOR_ALL_MANAGED_ON_LIST(SCR_MLIST(scr), sub){
region_fit(sub, &mg);
}
}else{
mplex_fit((WMPlex*)scr, geom);
}
}
static void screen_managed_changed(WScreen *scr, int mode, bool sw,
WRegion *reg_)
{
const char *n=NULL;
WRegion *reg;
if(!sw)
return;
reg=SCR_CURRENT(scr);
if(scr->atom_workspace!=None && wglobal.opmode!=OPMODE_DEINIT){
if(reg!=NULL)
n=region_name(reg);
set_string_property(ROOT_OF(scr), scr->atom_workspace,
n==NULL ? "" : n);
extl_call_named("call_hook", "soo", NULL,
"screen_workspace_switched", scr, reg);
}
}
static WRegion *screen_find_rescue_manager_for(WScreen *scr, WRegion *todst)
{
WRegion *other;
/* TODO: checking that todst is on the managed list should be done
* more cleanly.
*/
if(REGION_MANAGER(todst)!=(WRegion*)scr || todst==scr->mplex.current_input)
return FALSE;
other=todst;
while(1){
other=PREV_MANAGED(SCR_MLIST(scr), other);
if(other==NULL)
break;
if(region_has_manage_clientwin(other) &&
!WOBJ_IS_BEING_DESTROYED(other)){
return other;
}
}
other=todst;
while(1){
other=NEXT_MANAGED(SCR_MLIST(scr), other);
if(other==NULL)
break;
if(region_has_manage_clientwin(other) &&
!WOBJ_IS_BEING_DESTROYED(other)){
return other;
}
}
return NULL;
}
static void screen_map(WScreen *scr)
{
if(scr->uses_root)
return;
mplex_map((WMPlex*)scr);
}
static void screen_unmap(WScreen *scr)
{
if(scr->uses_root)
return;
mplex_unmap((WMPlex*)scr);
}
static void screen_activated(WScreen *scr)
{
wglobal.active_screen=scr;
}
/*}}}*/
/*{{{Misc. */
/*EXTL_DOC
* Returns the screen \var{reg} is on.
*/
EXTL_EXPORT_MEMBER
WScreen *region_screen_of(WRegion *reg)
{
while(reg!=NULL){
if(WOBJ_IS(reg, WScreen))
return (WScreen*)reg;
reg=region_parent(reg);
}
return NULL;
}
/*EXTL_DOC
* Find the screen with numerical id \var{id}. If Xinerama is
* not present, \var{id} corresponds to X screen numbers. Otherwise
* the ids are some arbitrary ordering of Xinerama rootwins.
*/
EXTL_EXPORT
WScreen *find_screen_id(int id)
{
WScreen *scr, *maxscr=NULL;
FOR_ALL_SCREENS(scr){
if(id==-1){
if(maxscr==NULL || scr->id>maxscr->id)
maxscr=scr;
}
if(scr->id==id)
return scr;
}
return maxscr;
}
/*EXTL_DOC
* Switch focus to the screen with id \var{id} and return it.
*
* Note that this function is asynchronous; the screen will not
* actually have received the focus when this function returns.
*/
EXTL_EXPORT
WScreen *goto_nth_screen(int id)
{
WScreen *scr=find_screen_id(id);
if(scr!=NULL)
region_goto((WRegion*)scr);
return scr;
}
/*EXTL_DOC
* Switch focus to the next screen and return it.
*
* Note that this function is asynchronous; the screen will not
* actually have received the focus when this function returns.
*/
EXTL_EXPORT
WScreen *goto_next_screen()
{
WScreen *scr=wglobal.active_screen;
if(scr!=NULL)
scr=scr->next_scr;
if(scr==NULL)
scr=wglobal.screens;
if(scr!=NULL)
region_goto((WRegion*)scr);
return scr;
}
/*EXTL_DOC
* Switch focus to the previous screen and return it.
*
* Note that this function is asynchronous; the screen will not
* actually have received the focus when this function returns.
*/
EXTL_EXPORT
WScreen *goto_prev_screen()
{
WScreen *scr=wglobal.active_screen;
if(scr!=NULL)
scr=scr->prev_scr;
else
scr=wglobal.screens;
if(scr!=NULL)
region_goto((WRegion*)scr);
return scr;
}
/*EXTL_DOC
* Return the numerical id for screen \var{scr}.
*/
EXTL_EXPORT_MEMBER
int screen_id(WScreen *scr)
{
return scr->id;
}
static bool screen_may_destroy_managed(WScreen *scr, WRegion *reg)
{
WRegion *r2;
FOR_ALL_MANAGED_ON_LIST(SCR_MLIST(scr), r2){
if(WOBJ_IS(r2, WGenWS) && r2!=reg)
return TRUE;
}
warn("Cannot destroy only workspace.");
return FALSE;
}
void screen_set_managed_offset(WScreen *scr, const WRectangle *off)
{
scr->managed_off=*off;
mplex_fit_managed((WMPlex*)scr);
}
/*EXTL_DOC
* Set offset of objects managed by the screen from actual screen geometry.
* The table \var{offset} should contain the entries \code{x}, \code{y},
* \code{w} and \code{h} indicating offsets of that component of screen
* geometry.
*/
EXTL_EXPORT_MEMBER_AS(WScreen, set_managed_offset)
bool screen_set_managed_offset_extl(WScreen *scr, ExtlTab offset)
{
WRectangle g;
if(!extltab_to_geom(offset, &g))
goto err;
if(-g.w>=REGION_GEOM(scr).w)
goto err;
if(-g.h>=REGION_GEOM(scr).h)
goto err;
screen_set_managed_offset(scr, &g);
return TRUE;
err:
warn("Invalid offset.");
return FALSE;
}
/*}}}*/
/*{{{ Save/load */
static bool screen_save_to_file(WScreen *scr, FILE *file, int lvl)
{
WRegion *sub;
begin_saved_region((WRegion*)scr, file, lvl);
save_indent_line(file, lvl);
fprintf(file, "subs = {\n");
FOR_ALL_MANAGED_ON_LIST(SCR_MLIST(scr), sub){
save_indent_line(file, lvl+1);
fprintf(file, "{\n");
region_save_to_file((WRegion*)sub, file, lvl+2);
if(sub==SCR_CURRENT(scr)){
save_indent_line(file, lvl+2);
fprintf(file, "switchto = true,\n");
}
save_indent_line(file, lvl+1);
fprintf(file, "},\n");
}
save_indent_line(file, lvl);
fprintf(file, "},\n");
return TRUE;
}
/*EXTL_DOC
* (Intended to be called from workspace savefiles.)
* Set screen name and initial workspaces etc.
*/
EXTL_EXPORT
bool initialise_screen_id(int id, ExtlTab tab)
{
char *name;
WScreen *scr=find_screen_id(id);
ExtlTab substab, subtab;
int n, i;
if(scr==NULL){
warn("No screen #%d\n", id);
return FALSE;
}
scr->configured=TRUE;
if(extl_table_gets_s(tab, "name", &name)){
region_set_name((WRegion*)scr, name);
free(name);
}
if(!extl_table_gets_t(tab, "subs", &substab))
return TRUE;
n=extl_table_get_n(substab);
for(i=1; i<=n; i++){
if(extl_table_geti_t(substab, i, &subtab)){
mplex_attach_new((WMPlex*)scr, subtab);
extl_unref_table(subtab);
}
}
extl_unref_table(substab);
return TRUE;
}
/*}}}*/
/*{{{ Dynamic function table and class implementation */
static DynFunTab screen_dynfuntab[]={
{region_fit, screen_fit},
{region_map, screen_map},
{region_unmap, screen_unmap},
{region_activated, screen_activated},
{(DynFun*)region_may_destroy_managed,
(DynFun*)screen_may_destroy_managed},
{(DynFun*)region_find_rescue_manager_for,
(DynFun*)screen_find_rescue_manager_for},
{mplex_managed_changed, screen_managed_changed},
{mplex_managed_geom, screen_managed_geom},
{(DynFun*)region_save_to_file,
(DynFun*)screen_save_to_file},
{(DynFun*)region_handle_drop,
(DynFun*)screen_handle_drop},
END_DYNFUNTAB
};
IMPLOBJ(WScreen, WMPlex, screen_deinit, screen_dynfuntab);
/*}}}*/
|