1 // -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*-
2 // Workspace.cc for Blackbox - an X11 Window manager
3 // Copyright (c) 2001 - 2002 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.
25 # include "../config.h"
26 #endif // HAVE_CONFIG_H
30 #include <X11/Xatom.h>
34 #endif // HAVE_STDIO_H
38 #endif // HAVE_STRING_H
49 #include "blackbox.hh"
50 #include "Clientmenu.hh"
56 #include "Workspace.hh"
57 #include "Windowmenu.hh"
61 Workspace::Workspace(BScreen *scrn, unsigned int i) {
63 xatom = screen->getBlackbox()->getXAtom();
65 cascade_x = cascade_y = 32;
69 clientmenu = new Clientmenu(this);
71 lastfocus = (BlackboxWindow *) 0;
77 void Workspace::addWindow(BlackboxWindow *w, bool place) {
80 if (place) placeWindow(w);
83 w->setWindowNumber(windowList.size());
85 stackingList.push_front(w);
86 windowList.push_back(w);
88 clientmenu->insert(w->getTitle());
91 screen->updateNetizenWindowAdd(w->getClientWindow(), id);
97 unsigned int Workspace::removeWindow(BlackboxWindow *w) {
100 stackingList.remove(w);
102 // pass focus to the next appropriate window
103 if ((w->isFocused() || w == lastfocus) &&
104 ! screen->getBlackbox()->doShutdown()) {
105 if (id == screen->getCurrentWorkspaceID()) {
106 // The window is on the visible workspace
109 // The window is not on the visible workspace.
110 if (lastfocus == w) {
111 // The window was the last-focus target, so we need to replace it.
112 setLastFocusedWindow(stackingList.front());
114 // if the window focused on the current workspace, then reapply that
115 // workspace's focus too
117 screen->getCurrentWorkspace()->focusFallback(w);
121 windowList.remove(w);
122 clientmenu->remove(w->getWindowNumber());
123 clientmenu->update();
125 screen->updateNetizenWindowDel(w->getClientWindow());
127 BlackboxWindowList::iterator it = windowList.begin();
128 const BlackboxWindowList::iterator end = windowList.end();
130 for (; it != end; ++it, ++i)
131 (*it)->setWindowNumber(i);
134 cascade_x = cascade_y = 32;
140 void Workspace::focusFallback(const BlackboxWindow *old_window) {
141 BlackboxWindow *newfocus = 0;
143 // if it's a transient, then try to focus its parent
144 if (old_window && old_window->isTransient()) {
145 newfocus = old_window->getTransientFor();
148 newfocus->isIconic() || // do not focus icons
149 newfocus->getWorkspaceNumber() != id || // or other workspaces
150 ! newfocus->setInputFocus())
155 BlackboxWindowList::iterator it = stackingList.begin(),
156 end = stackingList.end();
157 for (; it != end; ++it) {
158 BlackboxWindow *tmp = *it;
159 if (tmp && tmp->setInputFocus()) {
160 // we found our new focus target
167 screen->getBlackbox()->setFocusedWindow(newfocus);
171 void Workspace::showAll(void) {
172 std::for_each(stackingList.begin(), stackingList.end(),
173 std::mem_fun(&BlackboxWindow::show));
177 void Workspace::hideAll(void) {
178 // withdraw in reverse order to minimize the number of Expose events
180 BlackboxWindowList lst(stackingList.rbegin(), stackingList.rend());
182 BlackboxWindowList::iterator it = lst.begin();
183 const BlackboxWindowList::iterator end = lst.end();
184 for (; it != end; ++it) {
185 BlackboxWindow *bw = *it;
192 void Workspace::removeAll(void) {
193 while (! windowList.empty())
194 windowList.front()->iconify();
199 * returns the number of transients for win, plus the number of transients
200 * associated with each transient of win
202 static int countTransients(const BlackboxWindow * const win) {
203 int ret = win->getTransients().size();
205 BlackboxWindowList::const_iterator it, end = win->getTransients().end();
206 for (it = win->getTransients().begin(); it != end; ++it) {
207 ret += countTransients(*it);
215 * puts the transients of win into the stack. windows are stacked above
216 * the window before it in the stackvector being iterated, meaning
217 * stack[0] is on bottom, stack[1] is above stack[0], stack[2] is above
220 void Workspace::raiseTransients(const BlackboxWindow * const win,
221 StackVector::iterator &stack) {
222 if (win->getTransients().size() == 0) return; // nothing to do
224 // put win's transients in the stack
225 BlackboxWindowList::const_iterator it, end = win->getTransients().end();
226 for (it = win->getTransients().begin(); it != end; ++it) {
227 *stack++ = (*it)->getFrameWindow();
228 screen->updateNetizenWindowRaise((*it)->getClientWindow());
230 if (! (*it)->isIconic()) {
231 Workspace *wkspc = screen->getWorkspace((*it)->getWorkspaceNumber());
232 wkspc->stackingList.remove((*it));
233 wkspc->stackingList.push_front((*it));
237 // put transients of win's transients in the stack
238 for (it = win->getTransients().begin(); it != end; ++it) {
239 raiseTransients(*it, stack);
244 void Workspace::lowerTransients(const BlackboxWindow * const win,
245 StackVector::iterator &stack) {
246 if (win->getTransients().size() == 0) return; // nothing to do
248 // put transients of win's transients in the stack
249 BlackboxWindowList::const_reverse_iterator it,
250 end = win->getTransients().rend();
251 for (it = win->getTransients().rbegin(); it != end; ++it) {
252 lowerTransients(*it, stack);
255 // put win's transients in the stack
256 for (it = win->getTransients().rbegin(); it != end; ++it) {
257 *stack++ = (*it)->getFrameWindow();
258 screen->updateNetizenWindowLower((*it)->getClientWindow());
260 if (! (*it)->isIconic()) {
261 Workspace *wkspc = screen->getWorkspace((*it)->getWorkspaceNumber());
262 wkspc->stackingList.remove((*it));
263 wkspc->stackingList.push_back((*it));
269 void Workspace::raiseWindow(BlackboxWindow *w) {
270 BlackboxWindow *win = w;
272 // walk up the transient_for's to the window that is not a transient
273 while (win->isTransient()) {
274 if (! win->getTransientFor()) break;
275 win = win->getTransientFor();
278 // get the total window count (win and all transients)
279 unsigned int i = 1 + countTransients(win);
281 // stack the window with all transients above
282 StackVector stack_vector(i);
283 StackVector::iterator stack = stack_vector.begin();
285 *(stack++) = win->getFrameWindow();
286 screen->updateNetizenWindowRaise(win->getClientWindow());
287 if (! win->isIconic()) {
288 Workspace *wkspc = screen->getWorkspace(win->getWorkspaceNumber());
289 wkspc->stackingList.remove(win);
290 wkspc->stackingList.push_front(win);
293 raiseTransients(win, stack);
295 screen->raiseWindows(&stack_vector[0], stack_vector.size());
299 void Workspace::lowerWindow(BlackboxWindow *w) {
300 BlackboxWindow *win = w;
302 // walk up the transient_for's to the window that is not a transient
303 while (win->isTransient()) {
304 if (! win->getTransientFor()) break;
305 win = win->getTransientFor();
308 // get the total window count (win and all transients)
309 unsigned int i = 1 + countTransients(win);
311 // stack the window with all transients above
312 StackVector stack_vector(i);
313 StackVector::iterator stack = stack_vector.begin();
315 lowerTransients(win, stack);
317 *(stack++) = win->getFrameWindow();
318 screen->updateNetizenWindowLower(win->getClientWindow());
319 if (! win->isIconic()) {
320 Workspace *wkspc = screen->getWorkspace(win->getWorkspaceNumber());
321 wkspc->stackingList.remove(win);
322 wkspc->stackingList.push_back(win);
325 screen->lowerWindows(&stack_vector[0], stack_vector.size());
329 void Workspace::reconfigure(void) {
330 clientmenu->reconfigure();
331 std::for_each(windowList.begin(), windowList.end(),
332 std::mem_fun(&BlackboxWindow::reconfigure));
336 BlackboxWindow *Workspace::getWindow(unsigned int index) {
337 if (index < windowList.size()) {
338 BlackboxWindowList::iterator it = windowList.begin();
339 for(; index > 0; --index, ++it); /* increment to index */
347 Workspace::getNextWindowInList(BlackboxWindow *w) {
348 BlackboxWindowList::iterator it = std::find(windowList.begin(),
351 assert(it != windowList.end()); // window must be in list
353 if (it == windowList.end())
354 return windowList.front(); // if we walked off the end, wrap around
360 BlackboxWindow* Workspace::getPrevWindowInList(BlackboxWindow *w) {
361 BlackboxWindowList::iterator it = std::find(windowList.begin(),
364 assert(it != windowList.end()); // window must be in list
365 if (it == windowList.begin())
366 return windowList.back(); // if we walked of the front, wrap around
372 BlackboxWindow* Workspace::getTopWindowOnStack(void) const {
373 return stackingList.front();
377 void Workspace::sendWindowList(Netizen &n) {
378 BlackboxWindowList::iterator it = windowList.begin(),
379 end = windowList.end();
380 for(; it != end; ++it)
381 n.sendWindowAdd((*it)->getClientWindow(), getID());
385 unsigned int Workspace::getCount(void) const {
386 return windowList.size();
390 void Workspace::appendStackOrder(BlackboxWindowList &stack_order) const {
391 BlackboxWindowList::const_reverse_iterator it = stackingList.rbegin();
392 const BlackboxWindowList::const_reverse_iterator end = stackingList.rend();
393 for (; it != end; ++it)
394 stack_order.push_back(*it);
398 bool Workspace::isCurrent(void) const {
399 return (id == screen->getCurrentWorkspaceID());
403 bool Workspace::isLastWindow(const BlackboxWindow* const w) const {
404 return (w == windowList.back());
408 void Workspace::setCurrent(void) {
409 screen->changeWorkspaceID(id);
413 void Workspace::setName(const string& new_name) {
414 if (! new_name.empty()) {
417 // attempt to get from the _NET_WM_DESKTOP_NAMES property
418 XAtom::StringVect namesList;
419 unsigned long numnames = id + 1;
420 if (xatom->getValue(screen->getRootWindow(), XAtom::net_desktop_names,
421 XAtom::utf8, numnames, namesList) &&
422 namesList.size() > id) {
423 name = namesList[id];
425 string tmp =i18n(WorkspaceSet, WorkspaceDefaultNameFormat,
427 assert(tmp.length() < 32);
428 char default_name[32];
429 sprintf(default_name, tmp.c_str(), id + 1);
434 // reset the property with the new name
435 XAtom::StringVect namesList;
436 unsigned long numnames = (unsigned) -1;
437 if (xatom->getValue(screen->getRootWindow(), XAtom::net_desktop_names,
438 XAtom::utf8, numnames, namesList)) {
439 if (namesList.size() > id)
440 namesList[id] = name;
442 namesList.push_back(name);
444 xatom->setValue(screen->getRootWindow(), XAtom::net_desktop_names,
445 XAtom::utf8, namesList);
447 clientmenu->setLabel(name);
448 clientmenu->update();
449 screen->saveWorkspaceNames();
454 * Calculate free space available for window placement.
456 typedef std::vector<Rect> rectList;
458 static rectList calcSpace(const Rect &win, const rectList &spaces) {
461 rectList::const_iterator siter, end = spaces.end();
462 for (siter = spaces.begin(); siter != end; ++siter) {
463 const Rect &curr = *siter;
465 if(! win.intersects(curr)) {
466 result.push_back(curr);
470 /* Use an intersection of win and curr to determine the space around
471 * curr that we can use.
473 * NOTE: the spaces calculated can overlap.
478 extra.setCoords(curr.left(), curr.top(),
479 isect.left() - 1, curr.bottom());
480 if (extra.valid()) result.push_back(extra);
483 extra.setCoords(curr.left(), curr.top(),
484 curr.right(), isect.top() - 1);
485 if (extra.valid()) result.push_back(extra);
488 extra.setCoords(isect.right() + 1, curr.top(),
489 curr.right(), curr.bottom());
490 if (extra.valid()) result.push_back(extra);
493 extra.setCoords(curr.left(), isect.bottom() + 1,
494 curr.right(), curr.bottom());
495 if (extra.valid()) result.push_back(extra);
501 static bool rowRLBT(const Rect &first, const Rect &second) {
502 if (first.bottom() == second.bottom())
503 return first.right() > second.right();
504 return first.bottom() > second.bottom();
507 static bool rowRLTB(const Rect &first, const Rect &second) {
508 if (first.y() == second.y())
509 return first.right() > second.right();
510 return first.y() < second.y();
513 static bool rowLRBT(const Rect &first, const Rect &second) {
514 if (first.bottom() == second.bottom())
515 return first.x() < second.x();
516 return first.bottom() > second.bottom();
519 static bool rowLRTB(const Rect &first, const Rect &second) {
520 if (first.y() == second.y())
521 return first.x() < second.x();
522 return first.y() < second.y();
525 static bool colLRTB(const Rect &first, const Rect &second) {
526 if (first.x() == second.x())
527 return first.y() < second.y();
528 return first.x() < second.x();
531 static bool colLRBT(const Rect &first, const Rect &second) {
532 if (first.x() == second.x())
533 return first.bottom() > second.bottom();
534 return first.x() < second.x();
537 static bool colRLTB(const Rect &first, const Rect &second) {
538 if (first.right() == second.right())
539 return first.y() < second.y();
540 return first.right() > second.right();
543 static bool colRLBT(const Rect &first, const Rect &second) {
544 if (first.right() == second.right())
545 return first.bottom() > second.bottom();
546 return first.right() > second.right();
550 bool Workspace::smartPlacement(Rect& win, const Rect& availableArea) {
552 spaces.push_back(availableArea); //initially the entire screen is free
555 BlackboxWindowList::const_iterator wit = windowList.begin(),
556 end = windowList.end();
558 for (; wit != end; ++wit) {
559 const BlackboxWindow* const curr = *wit;
561 if (curr->isShaded()) continue;
563 tmp.setRect(curr->frameRect().x(), curr->frameRect().y(),
564 curr->frameRect().width() + screen->getBorderWidth(),
565 curr->frameRect().height() + screen->getBorderWidth());
567 spaces = calcSpace(tmp, spaces);
570 if (screen->getPlacementPolicy() == BScreen::RowSmartPlacement) {
571 if(screen->getRowPlacementDirection() == BScreen::LeftRight) {
572 if(screen->getColPlacementDirection() == BScreen::TopBottom)
573 std::sort(spaces.begin(), spaces.end(), rowLRTB);
575 std::sort(spaces.begin(), spaces.end(), rowLRBT);
577 if(screen->getColPlacementDirection() == BScreen::TopBottom)
578 std::sort(spaces.begin(), spaces.end(), rowRLTB);
580 std::sort(spaces.begin(), spaces.end(), rowRLBT);
583 if(screen->getColPlacementDirection() == BScreen::TopBottom) {
584 if(screen->getRowPlacementDirection() == BScreen::LeftRight)
585 std::sort(spaces.begin(), spaces.end(), colLRTB);
587 std::sort(spaces.begin(), spaces.end(), colRLTB);
589 if(screen->getRowPlacementDirection() == BScreen::LeftRight)
590 std::sort(spaces.begin(), spaces.end(), colLRBT);
592 std::sort(spaces.begin(), spaces.end(), colRLBT);
596 rectList::const_iterator sit = spaces.begin(), spaces_end = spaces.end();
597 for(; sit != spaces_end; ++sit) {
598 if (sit->width() >= win.width() && sit->height() >= win.height())
602 if (sit == spaces_end)
605 //set new position based on the empty space found
606 const Rect& where = *sit;
610 // adjust the location() based on left/right and top/bottom placement
611 if (screen->getPlacementPolicy() == BScreen::RowSmartPlacement) {
612 if (screen->getRowPlacementDirection() == BScreen::RightLeft)
613 win.setX(where.right() - win.width());
614 if (screen->getColPlacementDirection() == BScreen::BottomTop)
615 win.setY(where.bottom() - win.height());
617 if (screen->getColPlacementDirection() == BScreen::BottomTop)
618 win.setY(win.y() + where.height() - win.height());
619 if (screen->getRowPlacementDirection() == BScreen::RightLeft)
620 win.setX(win.x() + where.width() - win.width());
626 bool Workspace::underMousePlacement(Rect &win, const Rect &availableArea) {
630 XQueryPointer(screen->getBlackbox()->getXDisplay(), screen->getRootWindow(),
631 &r, &c, &rx, &ry, &x, &y, &m);
632 x = rx - win.width() / 2;
633 y = ry - win.height() / 2;
635 if (x < availableArea.x())
636 x = availableArea.x();
637 if (y < availableArea.y())
638 y = availableArea.y();
639 if (x + win.width() > availableArea.x() + availableArea.width())
640 x = availableArea.x() + availableArea.width() - win.width();
641 if (y + win.height() > availableArea.y() + availableArea.height())
642 y = availableArea.y() + availableArea.height() - win.height();
651 bool Workspace::cascadePlacement(Rect &win, const Rect &availableArea) {
652 if ((cascade_x > static_cast<signed>(availableArea.width() / 2)) ||
653 (cascade_y > static_cast<signed>(availableArea.height() / 2)))
654 cascade_x = cascade_y = 32;
656 if (cascade_x == 32) {
657 cascade_x += availableArea.x();
658 cascade_y += availableArea.y();
661 win.setPos(cascade_x, cascade_y);
667 void Workspace::placeWindow(BlackboxWindow *win) {
668 Rect availableArea(screen->availableArea()),
669 new_win(availableArea.x(), availableArea.y(),
670 win->frameRect().width(), win->frameRect().height());
673 switch (screen->getPlacementPolicy()) {
674 case BScreen::RowSmartPlacement:
675 case BScreen::ColSmartPlacement:
676 placed = smartPlacement(new_win, availableArea);
678 case BScreen::UnderMousePlacement:
679 placed = underMousePlacement(new_win, availableArea);
681 break; // handled below
684 if (placed == False) {
685 cascadePlacement(new_win, availableArea);
686 cascade_x += win->getTitleHeight() + (screen->getBorderWidth() * 2);
687 cascade_y += win->getTitleHeight() + (screen->getBorderWidth() * 2);
690 if (new_win.right() > availableArea.right())
691 new_win.setX(availableArea.left());
692 if (new_win.bottom() > availableArea.bottom())
693 new_win.setY(availableArea.top());
694 win->configure(new_win.x(), new_win.y(), new_win.width(), new_win.height());