1 // Workspace.cc for Openbox
2 // Copyright (c) 2002 - 2002 Ben Jansens (ben@orodu.net)
3 // Copyright (c) 2001 Sean 'Shaleh' Perry <shaleh@debian.org>
4 // Copyright (c) 1997 - 2000 Brad Hughes (bhughes@tcac.net)
6 // Permission is hereby granted, free of charge, to any person obtaining a
7 // copy of this software and associated documentation files (the "Software"),
8 // to deal in the Software without restriction, including without limitation
9 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 // and/or sell copies of the Software, and to permit persons to whom the
11 // Software is furnished to do so, subject to the following conditions:
13 // The above copyright notice and this permission notice shall be included in
14 // all copies or substantial portions of the Software.
16 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 // DEALINGS IN THE SOFTWARE.
24 // stupid macros needed to access some functions in version 2 of the GNU C
31 # include "../config.h"
32 #endif // HAVE_CONFIG_H
35 #include <X11/Xatom.h>
39 #include "Clientmenu.h"
43 #include "Workspace.h"
45 #include "Windowmenu.h"
51 #endif // HAVE_STDIO_H
55 #endif // HAVE_STDLIB_H
59 #endif // HAVE_STRING_H
63 typedef vector
<Rect
> rectList
;
65 Workspace::Workspace(BScreen
&scrn
, int i
) : screen(scrn
) {
67 cascade_x
= cascade_y
= 0;
71 stackingList
= new LinkedList
<OpenboxWindow
>;
72 windowList
= new LinkedList
<OpenboxWindow
>;
73 clientmenu
= new Clientmenu(*this);
75 lastfocus
= (OpenboxWindow
*) 0;
78 char *tmp
= screen
.getNameOfWorkspace(id
);
83 Workspace::~Workspace(void) {
93 const int Workspace::addWindow(OpenboxWindow
*w
, Bool place
) {
96 if (place
) placeWindow(*w
);
99 w
->setWindowNumber(windowList
->count());
101 stackingList
->insert(w
, 0);
102 windowList
->insert(w
);
104 clientmenu
->insert((const char **) w
->getTitle());
105 clientmenu
->update();
107 screen
.updateNetizenWindowAdd(w
->getClientWindow(), id
);
111 return w
->getWindowNumber();
115 const int Workspace::removeWindow(OpenboxWindow
*w
) {
118 stackingList
->remove(w
);
120 if (w
->isFocused()) {
121 if (w
->isTransient() && w
->getTransientFor() &&
122 w
->getTransientFor()->isVisible()) {
123 w
->getTransientFor()->setInputFocus();
124 } else if (screen
.sloppyFocus()) {
125 screen
.getOpenbox().setFocusedWindow((OpenboxWindow
*) 0);
127 OpenboxWindow
*top
= stackingList
->first();
128 if (! top
|| ! top
->setInputFocus()) {
129 screen
.getOpenbox().setFocusedWindow((OpenboxWindow
*) 0);
130 XSetInputFocus(screen
.getOpenbox().getXDisplay(),
131 screen
.getToolbar()->getWindowID(),
132 RevertToParent
, CurrentTime
);
138 lastfocus
= (OpenboxWindow
*) 0;
140 windowList
->remove(w
->getWindowNumber());
141 clientmenu
->remove(w
->getWindowNumber());
142 clientmenu
->update();
144 screen
.updateNetizenWindowDel(w
->getClientWindow());
146 LinkedListIterator
<OpenboxWindow
> it(windowList
);
147 OpenboxWindow
*bw
= it
.current();
148 for (int i
= 0; bw
; it
++, i
++, bw
= it
.current())
149 bw
->setWindowNumber(i
);
151 return windowList
->count();
155 void Workspace::showAll(void) {
156 LinkedListIterator
<OpenboxWindow
> it(stackingList
);
157 for (OpenboxWindow
*bw
= it
.current(); bw
; it
++, bw
= it
.current())
158 bw
->deiconify(False
, False
);
162 void Workspace::hideAll(void) {
163 LinkedList
<OpenboxWindow
> lst
;
165 LinkedListIterator
<OpenboxWindow
> it(stackingList
);
166 for (OpenboxWindow
*bw
= it
.current(); bw
; it
++, bw
= it
.current())
169 LinkedListIterator
<OpenboxWindow
> it2(&lst
);
170 for (OpenboxWindow
*bw
= it2
.current(); bw
; it2
++, bw
= it2
.current())
176 void Workspace::removeAll(void) {
177 LinkedListIterator
<OpenboxWindow
> it(windowList
);
178 for (OpenboxWindow
*bw
= it
.current(); bw
; it
++, bw
= it
.current())
183 void Workspace::raiseWindow(OpenboxWindow
*w
) {
184 OpenboxWindow
*win
= (OpenboxWindow
*) 0, *bottom
= w
;
186 while (bottom
->isTransient() && bottom
->getTransientFor())
187 bottom
= bottom
->getTransientFor();
191 while (win
->hasTransient() && win
->getTransient()) {
192 win
= win
->getTransient();
197 Window
*nstack
= new Window
[i
], *curr
= nstack
;
202 *(curr
++) = win
->getFrameWindow();
203 screen
.updateNetizenWindowRaise(win
->getClientWindow());
205 if (! win
->isIconic()) {
206 wkspc
= screen
.getWorkspace(win
->getWorkspaceNumber());
207 wkspc
->stackingList
->remove(win
);
208 wkspc
->stackingList
->insert(win
, 0);
211 if (! win
->hasTransient() || ! win
->getTransient())
214 win
= win
->getTransient();
217 screen
.raiseWindows(nstack
, i
);
223 void Workspace::lowerWindow(OpenboxWindow
*w
) {
224 OpenboxWindow
*win
= (OpenboxWindow
*) 0, *bottom
= w
;
226 while (bottom
->isTransient() && bottom
->getTransientFor())
227 bottom
= bottom
->getTransientFor();
231 while (win
->hasTransient() && win
->getTransient()) {
232 win
= win
->getTransient();
237 Window
*nstack
= new Window
[i
], *curr
= nstack
;
241 *(curr
++) = win
->getFrameWindow();
242 screen
.updateNetizenWindowLower(win
->getClientWindow());
244 if (! win
->isIconic()) {
245 wkspc
= screen
.getWorkspace(win
->getWorkspaceNumber());
246 wkspc
->stackingList
->remove(win
);
247 wkspc
->stackingList
->insert(win
);
250 if (! win
->getTransientFor())
253 win
= win
->getTransientFor();
256 screen
.getOpenbox().grab();
258 XLowerWindow(screen
.getBaseDisplay().getXDisplay(), *nstack
);
259 XRestackWindows(screen
.getBaseDisplay().getXDisplay(), nstack
, i
);
261 screen
.getOpenbox().ungrab();
267 void Workspace::reconfigure(void) {
268 clientmenu
->reconfigure();
270 LinkedListIterator
<OpenboxWindow
> it(windowList
);
271 for (OpenboxWindow
*bw
= it
.current(); bw
; it
++, bw
= it
.current()) {
272 if (bw
->validateClient())
278 OpenboxWindow
*Workspace::getWindow(int index
) {
279 if ((index
>= 0) && (index
< windowList
->count()))
280 return windowList
->find(index
);
286 const int Workspace::getCount(void) {
287 return windowList
->count();
291 void Workspace::update(void) {
292 clientmenu
->update();
293 screen
.getToolbar()->redrawWindowLabel(True
);
297 Bool
Workspace::isCurrent(void) {
298 return (id
== screen
.getCurrentWorkspaceID());
302 Bool
Workspace::isLastWindow(OpenboxWindow
*w
) {
303 return (w
== windowList
->last());
306 void Workspace::setCurrent(void) {
307 screen
.changeWorkspaceID(id
);
311 void Workspace::setName(char *new_name
) {
316 name
= bstrdup(new_name
);
318 name
= new char[128];
319 sprintf(name
, i18n
->getMessage(WorkspaceSet
, WorkspaceDefaultNameFormat
,
320 "Workspace %d"), id
+ 1);
323 clientmenu
->setLabel(name
);
324 clientmenu
->update();
325 screen
.saveWorkspaceNames();
329 void Workspace::shutdown(void) {
330 while (windowList
->count()) {
331 windowList
->first()->restore();
332 delete windowList
->first();
336 static rectList
calcSpace(const Rect
&win
, const rectList
&spaces
) {
338 rectList::const_iterator siter
;
339 for(siter
=spaces
.begin(); siter
!=spaces
.end(); ++siter
) {
340 if(win
.Intersect(*siter
)) {
341 //Check for space to the left of the window
342 if(win
.x() > siter
->x())
343 result
.push_back(Rect(siter
->x(), siter
->y(),
344 win
.x() - siter
->x() - 1,
346 //Check for space above the window
347 if(win
.y() > siter
->y())
348 result
.push_back(Rect(siter
->x(), siter
->y(),
350 win
.y() - siter
->y() - 1));
351 //Check for space to the right of the window
352 if((win
.x()+win
.w()) <
353 (siter
->x()+siter
->w()))
354 result
.push_back(Rect(win
.x() + win
.w() + 1,
356 siter
->x() + siter
->w() -
357 win
.x() - win
.w() - 1,
359 //Check for space below the window
360 if((win
.y()+win
.h()) <
361 (siter
->y()+siter
->h()))
362 result
.push_back(Rect(siter
->x(),
363 win
.y() + win
.h() + 1,
365 siter
->y() + siter
->h()-
366 win
.y() - win
.h() - 1));
370 result
.push_back(*siter
);
375 bool rowRLBT(const Rect
&first
, const Rect
&second
){
376 if (first
.y()+first
.h()==second
.y()+second
.h())
377 return first
.x()+first
.w()>second
.x()+second
.w();
378 return first
.y()+first
.h()>second
.y()+second
.h();
381 bool rowRLTB(const Rect
&first
, const Rect
&second
){
382 if (first
.y()==second
.y())
383 return first
.x()+first
.w()>second
.x()+second
.w();
384 return first
.y()<second
.y();
387 bool rowLRBT(const Rect
&first
, const Rect
&second
){
388 if (first
.y()+first
.h()==second
.y()+second
.h())
389 return first
.x()<second
.x();
390 return first
.y()+first
.h()>second
.y()+second
.h();
393 bool rowLRTB(const Rect
&first
, const Rect
&second
){
394 if (first
.y()==second
.y())
395 return first
.x()<second
.x();
396 return first
.y()<second
.y();
399 bool colLRTB(const Rect
&first
, const Rect
&second
){
400 if (first
.x()==second
.x())
401 return first
.y()<second
.y();
402 return first
.x()<second
.y();
405 bool colLRBT(const Rect
&first
, const Rect
&second
){
406 if (first
.x()==second
.x())
407 return first
.y()+first
.h()>second
.y()+second
.h();
408 return first
.x()<second
.y();
411 bool colRLTB(const Rect
&first
, const Rect
&second
){
412 if (first
.x()+first
.w()==second
.x()+second
.w())
413 return first
.y()<second
.y();
414 return first
.x()+first
.w()>second
.x()+second
.w();
417 bool colRLBT(const Rect
&first
, const Rect
&second
){
418 if (first
.x()+first
.w()==second
.x()+second
.w())
419 return first
.y()+first
.h()>second
.y()+second
.h();
420 return first
.x()+first
.w()>second
.x()+second
.w();
424 //BestFitPlacement finds the smallest free space that fits the window
425 //to be placed. It currentl ignores whether placement is right to left or top
427 Point
*Workspace::bestFitPlacement(const Size
&win_size
, const Rect
&space
) {
430 LinkedListIterator
<OpenboxWindow
> it(windowList
);
431 rectList::const_iterator siter
;
432 spaces
.push_back(space
); //initially the entire screen is free
436 for (OpenboxWindow
*cur
=it
.current(); cur
!=NULL
; it
++, cur
=it
.current())
437 spaces
= calcSpace(cur
->area().Inflate(screen
.getBorderWidth() * 4),
440 //Find first space that fits the window
442 for (siter
=spaces
.begin(); siter
!=spaces
.end(); ++siter
) {
443 if ((siter
->w() >= win_size
.w()) && (siter
->h() >= win_size
.h())) {
446 else if(siter
->w()*siter
->h()<best
->h()*best
->w())
451 Point
*pt
= new Point(best
->origin());
452 if (screen
.colPlacementDirection() != BScreen::TopBottom
)
453 pt
->setY(pt
->y() + (best
->h() - win_size
.h()));
454 if (screen
.rowPlacementDirection() != BScreen::LeftRight
)
455 pt
->setX(pt
->x() + (best
->w() - win_size
.w()));
458 return NULL
; //fall back to cascade
461 Point
*Workspace::underMousePlacement(const Size
&win_size
, const Rect
&space
) {
467 XQueryPointer(screen
.getOpenbox().getXDisplay(), screen
.getRootWindow(),
468 &r
, &c
, &rx
, &ry
, &x
, &y
, &m
);
469 pt
= new Point(rx
- win_size
.w() / 2, ry
- win_size
.h() / 2);
471 if (pt
->x() < space
.x())
473 if (pt
->y() < space
.y())
475 if (pt
->x() + win_size
.w() > space
.x() + space
.w())
476 pt
->setX(space
.x() + space
.w() - win_size
.w());
477 if (pt
->y() + win_size
.h() > space
.y() + space
.h())
478 pt
->setY(space
.y() + space
.h() - win_size
.h());
482 Point
*Workspace::rowSmartPlacement(const Size
&win_size
, const Rect
&space
) {
485 LinkedListIterator
<OpenboxWindow
> it(windowList
);
487 rectList::const_iterator siter
;
488 spaces
.push_back(space
); //initially the entire screen is free
492 for (OpenboxWindow
*cur
=it
.current(); cur
!=NULL
; it
++, cur
=it
.current())
493 spaces
= calcSpace(cur
->area().Inflate(screen
.getBorderWidth() * 4),
495 //Sort spaces by preference
496 if(screen
.rowPlacementDirection() == BScreen::RightLeft
)
497 if(screen
.colPlacementDirection() == BScreen::TopBottom
)
498 sort(spaces
.begin(),spaces
.end(),rowRLTB
);
500 sort(spaces
.begin(),spaces
.end(),rowRLBT
);
502 if(screen
.colPlacementDirection() == BScreen::TopBottom
)
503 sort(spaces
.begin(),spaces
.end(),rowLRTB
);
505 sort(spaces
.begin(),spaces
.end(),rowLRBT
);
507 for (siter
=spaces
.begin(); siter
!=spaces
.end(); ++siter
)
508 if ((siter
->w() >= win_size
.w()) && (siter
->h() >= win_size
.h())) {
514 Point
*pt
= new Point(best
->origin());
515 if (screen
.colPlacementDirection() != BScreen::TopBottom
)
516 pt
->setY(best
->y() + best
->h() - win_size
.h());
517 if (screen
.rowPlacementDirection() != BScreen::LeftRight
)
518 pt
->setX(best
->x()+best
->w()-win_size
.w());
521 return NULL
; //fall back to cascade
524 Point
*Workspace::colSmartPlacement(const Size
&win_size
, const Rect
&space
) {
527 LinkedListIterator
<OpenboxWindow
> it(windowList
);
529 rectList::const_iterator siter
;
530 spaces
.push_back(space
); //initially the entire screen is free
534 for (OpenboxWindow
*cur
=it
.current(); cur
!=NULL
; it
++, cur
=it
.current())
535 spaces
= calcSpace(cur
->area().Inflate(screen
.getBorderWidth() * 4),
537 //Sort spaces by user preference
538 if(screen
.colPlacementDirection() == BScreen::TopBottom
)
539 if(screen
.rowPlacementDirection() == BScreen::LeftRight
)
540 sort(spaces
.begin(),spaces
.end(),colLRTB
);
542 sort(spaces
.begin(),spaces
.end(),colRLTB
);
544 if(screen
.rowPlacementDirection() == BScreen::LeftRight
)
545 sort(spaces
.begin(),spaces
.end(),colLRBT
);
547 sort(spaces
.begin(),spaces
.end(),colRLBT
);
548 fprintf(stderr
,"Spaces after sorting\n");
549 for (siter
=spaces
.begin(); siter
!=spaces
.end(); ++siter
)
550 fprintf(stderr
,"space(%d,%d)(%d,%d)\n",siter
->x(),siter
->y(),
551 siter
->x()+siter
->w(),siter
->y()+siter
->h());
552 //Find first space that fits the window
554 for (siter
=spaces
.begin(); siter
!=spaces
.end(); ++siter
)
555 if ((siter
->w() >= win_size
.w()) && (siter
->h() >= win_size
.h())) {
561 Point
*pt
= new Point(best
->origin());
562 if (screen
.colPlacementDirection() != BScreen::TopBottom
)
563 pt
->setY(pt
->y() + (best
->h() - win_size
.h()));
564 if (screen
.rowPlacementDirection() != BScreen::LeftRight
)
565 pt
->setX(pt
->x() + (best
->w() - win_size
.w()));
568 return NULL
; //fall back to cascade
572 Point
*const Workspace::cascadePlacement(const OpenboxWindow
&win
,
574 if ((cascade_x
+ win
.area().w() + screen
.getBorderWidth() * 2 >
575 (space
.x() + space
.w())) ||
576 (cascade_y
+ win
.area().h() + screen
.getBorderWidth() * 2 >
577 (space
.y() + space
.h())))
578 cascade_x
= cascade_y
= 0;
579 if (cascade_x
< space
.x() || cascade_y
< space
.y()) {
580 cascade_x
= space
.x();
581 cascade_y
= space
.y();
584 Point
*p
= new Point(cascade_x
, cascade_y
);
585 cascade_x
+= win
.getTitleHeight();
586 cascade_y
+= win
.getTitleHeight();
591 void Workspace::placeWindow(OpenboxWindow
&win
) {
592 Rect space
= screen
.availableArea();
593 const Size
window_size(win
.area().w()+screen
.getBorderWidth() * 2,
594 win
.area().h()+screen
.getBorderWidth() * 2);
596 LinkedListIterator
<OpenboxWindow
> it(windowList
);
598 switch (screen
.placementPolicy()) {
599 case BScreen::BestFitPlacement
:
600 place
= bestFitPlacement(window_size
, space
);
602 case BScreen::RowSmartPlacement
:
603 place
= rowSmartPlacement(window_size
, space
);
605 case BScreen::ColSmartPlacement
:
606 place
= colSmartPlacement(window_size
, space
);
608 case BScreen::UnderMousePlacement
:
609 case BScreen::ClickMousePlacement
:
610 place
= underMousePlacement(window_size
, space
);
615 place
= cascadePlacement(win
, space
);
617 ASSERT(place
!= NULL
);
618 if (place
->x() + window_size
.w() > (signed) space
.x() + space
.w())
619 place
->setX(((signed) space
.x() + space
.w() - window_size
.w()) / 2);
620 if (place
->y() + window_size
.h() > (signed) space
.y() + space
.h())
621 place
->setY(((signed) space
.y() + space
.h() - window_size
.h()) / 2);
623 win
.configure(place
->x(), place
->y(), win
.area().w(), win
.area().h());