]> icculus.org git repositories - mikachu/openbox.git/blob - src/Workspace.cc
added UnderMouse windwo placement.
[mikachu/openbox.git] / src / Workspace.cc
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)
5 //
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:
12 //
13 // The above copyright notice and this permission notice shall be included in
14 // all copies or substantial portions of the Software.
15 //
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.
23
24 // stupid macros needed to access some functions in version 2 of the GNU C
25 // library
26 #ifndef   _GNU_SOURCE
27 #define   _GNU_SOURCE
28 #endif // _GNU_SOURCE
29
30 #ifdef    HAVE_CONFIG_H
31 #  include "../config.h"
32 #endif // HAVE_CONFIG_H
33
34 #include <X11/Xlib.h>
35 #include <X11/Xatom.h>
36
37 #include "i18n.h"
38 #include "openbox.h"
39 #include "Clientmenu.h"
40 #include "Screen.h"
41 #include "Toolbar.h"
42 #include "Window.h"
43 #include "Workspace.h"
44
45 #include "Windowmenu.h"
46 #include "Geometry.h"
47 #include "Util.h"
48
49 #ifdef    HAVE_STDIO_H
50 #  include <stdio.h>
51 #endif // HAVE_STDIO_H
52
53 #ifdef    HAVE_STDLIB_H
54 #  include <stdlib.h>
55 #endif // HAVE_STDLIB_H
56
57 #ifdef    HAVE_STRING_H
58 #  include <string.h>
59 #endif // HAVE_STRING_H
60
61 #include <vector>
62 #include <algorithm>
63 typedef vector<Rect> rectList;
64
65 Workspace::Workspace(BScreen &scrn, int i) : screen(scrn) {
66
67   cascade_x = cascade_y = 0;
68
69   id = i;
70
71   stackingList = new LinkedList<OpenboxWindow>;
72   windowList = new LinkedList<OpenboxWindow>;
73   clientmenu = new Clientmenu(*this);
74
75   lastfocus = (OpenboxWindow *) 0;
76
77   name = (char *) 0;
78   char *tmp = screen.getNameOfWorkspace(id);
79   setName(tmp);
80 }
81
82
83 Workspace::~Workspace(void) {
84   delete stackingList;
85   delete windowList;
86   delete clientmenu;
87
88   if (name)
89     delete [] name;
90 }
91
92
93 const int Workspace::addWindow(OpenboxWindow *w, Bool place) {
94   if (! w) return -1;
95
96   if (place) placeWindow(*w);
97
98   w->setWorkspace(id);
99   w->setWindowNumber(windowList->count());
100
101   stackingList->insert(w, 0);
102   windowList->insert(w);
103
104   clientmenu->insert((const char **) w->getTitle());
105   clientmenu->update();
106
107   screen.updateNetizenWindowAdd(w->getClientWindow(), id);
108
109   raiseWindow(w);
110
111   return w->getWindowNumber();
112 }
113
114
115 const int Workspace::removeWindow(OpenboxWindow *w) {
116   if (! w) return -1;
117
118   stackingList->remove(w);
119
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);
126     } else {
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);
133       }
134     }
135   }
136   
137   if (lastfocus == w)
138     lastfocus = (OpenboxWindow *) 0;
139
140   windowList->remove(w->getWindowNumber());
141   clientmenu->remove(w->getWindowNumber());
142   clientmenu->update();
143
144   screen.updateNetizenWindowDel(w->getClientWindow());
145
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);
150
151   return windowList->count();
152 }
153
154
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);
159 }
160
161
162 void Workspace::hideAll(void) {
163   LinkedList<OpenboxWindow> lst;
164
165   LinkedListIterator<OpenboxWindow> it(stackingList);
166   for (OpenboxWindow *bw = it.current(); bw; it++, bw = it.current())
167     lst.insert(bw, 0);
168
169   LinkedListIterator<OpenboxWindow> it2(&lst);
170   for (OpenboxWindow *bw = it2.current(); bw; it2++, bw = it2.current())
171     if (! bw->isStuck())
172       bw->withdraw();
173 }
174
175
176 void Workspace::removeAll(void) {
177   LinkedListIterator<OpenboxWindow> it(windowList);
178   for (OpenboxWindow *bw = it.current(); bw; it++, bw = it.current())
179     bw->iconify();
180 }
181
182
183 void Workspace::raiseWindow(OpenboxWindow *w) {
184   OpenboxWindow *win = (OpenboxWindow *) 0, *bottom = w;
185
186   while (bottom->isTransient() && bottom->getTransientFor())
187     bottom = bottom->getTransientFor();
188
189   int i = 1;
190   win = bottom;
191   while (win->hasTransient() && win->getTransient()) {
192     win = win->getTransient();
193
194     i++;
195   }
196
197   Window *nstack = new Window[i], *curr = nstack;
198   Workspace *wkspc;
199
200   win = bottom;
201   while (True) {
202     *(curr++) = win->getFrameWindow();
203     screen.updateNetizenWindowRaise(win->getClientWindow());
204
205     if (! win->isIconic()) {
206       wkspc = screen.getWorkspace(win->getWorkspaceNumber());
207       wkspc->stackingList->remove(win);
208       wkspc->stackingList->insert(win, 0);
209     }
210
211     if (! win->hasTransient() || ! win->getTransient())
212       break;
213
214     win = win->getTransient();
215   }
216
217   screen.raiseWindows(nstack, i);
218
219   delete [] nstack;
220 }
221
222
223 void Workspace::lowerWindow(OpenboxWindow *w) {
224   OpenboxWindow *win = (OpenboxWindow *) 0, *bottom = w;
225
226   while (bottom->isTransient() && bottom->getTransientFor())
227     bottom = bottom->getTransientFor();
228
229   int i = 1;
230   win = bottom;
231   while (win->hasTransient() && win->getTransient()) {
232     win = win->getTransient();
233
234     i++;
235   }
236
237   Window *nstack = new Window[i], *curr = nstack;
238   Workspace *wkspc;
239
240   while (True) {
241     *(curr++) = win->getFrameWindow();
242     screen.updateNetizenWindowLower(win->getClientWindow());
243
244     if (! win->isIconic()) {
245       wkspc = screen.getWorkspace(win->getWorkspaceNumber());
246       wkspc->stackingList->remove(win);
247       wkspc->stackingList->insert(win);
248     }
249
250     if (! win->getTransientFor())
251       break;
252
253     win = win->getTransientFor();
254   }
255
256   screen.getOpenbox().grab();
257
258   XLowerWindow(screen.getBaseDisplay().getXDisplay(), *nstack);
259   XRestackWindows(screen.getBaseDisplay().getXDisplay(), nstack, i);
260
261   screen.getOpenbox().ungrab();
262
263   delete [] nstack;
264 }
265
266
267 void Workspace::reconfigure(void) {
268   clientmenu->reconfigure();
269
270   LinkedListIterator<OpenboxWindow> it(windowList);
271   for (OpenboxWindow *bw = it.current(); bw; it++, bw = it.current()) {
272     if (bw->validateClient())
273       bw->reconfigure();
274   }
275 }
276
277
278 OpenboxWindow *Workspace::getWindow(int index) {
279   if ((index >= 0) && (index < windowList->count()))
280     return windowList->find(index);
281   else
282     return 0;
283 }
284
285
286 const int Workspace::getCount(void) {
287   return windowList->count();
288 }
289
290
291 void Workspace::update(void) {
292   clientmenu->update();
293   screen.getToolbar()->redrawWindowLabel(True);
294 }
295
296
297 Bool Workspace::isCurrent(void) {
298   return (id == screen.getCurrentWorkspaceID());
299 }
300
301
302 Bool Workspace::isLastWindow(OpenboxWindow *w) {
303   return (w == windowList->last());
304 }
305
306 void Workspace::setCurrent(void) {
307   screen.changeWorkspaceID(id);
308 }
309
310
311 void Workspace::setName(char *new_name) {
312   if (name)
313     delete [] name;
314
315   if (new_name) {
316     name = bstrdup(new_name);
317   } else {
318     name = new char[128];
319     sprintf(name, i18n->getMessage(WorkspaceSet, WorkspaceDefaultNameFormat,
320                                    "Workspace %d"), id + 1);
321   }
322   
323   clientmenu->setLabel(name);
324   clientmenu->update();
325   screen.saveWorkspaceNames();
326 }
327
328
329 void Workspace::shutdown(void) {
330   while (windowList->count()) {
331     windowList->first()->restore();
332     delete windowList->first();
333   }
334 }
335
336 static rectList calcSpace(const Rect &win, const rectList &spaces) {
337   rectList result;
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,
345                               siter->h()));
346       //Check for space above the window
347       if(win.y() > siter->y())
348         result.push_back(Rect(siter->x(), siter->y(),
349                               siter->w(),
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,
355                               siter->y(),
356                               siter->x() + siter->w() -
357                               win.x() - win.w() - 1,
358                               siter->h()));
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,
364                               siter->w(),
365                               siter->y() + siter->h()-
366                               win.y() - win.h() - 1));
367
368     }
369     else
370       result.push_back(*siter);
371   }
372   return result;
373 }
374
375
376 bool incWidth(const Rect &first, const Rect &second) {
377   return first.x() < second.x();
378 }
379
380
381 bool decWidth(const Rect &first, const Rect &second) {
382   if (second.x() == first.x())
383      return second.w() < first.w();
384   return second.x() < first.x();
385 }
386
387
388 bool incHeight(const Rect &first, const Rect &second) {
389   return first.y() < second.y();
390 }
391
392
393 bool decHeight(const Rect &first, const Rect &second) {
394   if (second.y() == first.y())
395      return second.h() < first.h();
396   return second.y() < first.y();
397 }
398
399
400 //BestFitPlacement finds the smallest free space that fits the window
401 //to be placed. It currentl ignores whether placement is right to left or top
402 //to bottom.
403 Point *Workspace::bestFitPlacement(const Size &win_size, const Rect &space) {
404   const Rect *best;
405   rectList spaces;
406   LinkedListIterator<OpenboxWindow> it(windowList);
407   rectList::const_iterator siter;
408   spaces.push_back(space); //initially the entire screen is free
409   it.reset();
410   
411   //Find Free Spaces
412   for (OpenboxWindow *cur=it.current(); cur!=NULL; it++, cur=it.current())
413      spaces = calcSpace(cur->area().Inflate(screen.getBorderWidth() * 4),
414                         spaces);
415   
416   //Sort the spaces by placement choice
417   if (screen.rowPlacementDirection() == BScreen::TopBottom)
418      sort(spaces.begin(), spaces.end(), decHeight);
419   else
420      sort(spaces.begin(), spaces.end(), incHeight);
421   if (screen.colPlacementDirection() == BScreen::TopBottom)
422      stable_sort(spaces.begin(), spaces.end(), incHeight);
423   else
424      stable_sort(spaces.begin(), spaces.end(), decHeight);
425
426   //Find first space that fits the window
427   best = NULL;
428   for (siter=spaces.begin(); siter!=spaces.end(); ++siter) {
429     if ((siter->w() >= win_size.w()) && (siter->h() >= win_size.h())) {
430       if (best==NULL)
431         best = siter;
432       else if(siter->w()*siter->h()<best->h()*best->w())
433         best = siter;
434     }
435   }
436   if (best != NULL) {
437     Point *pt = new Point(best->origin());
438     if (screen.colPlacementDirection() != BScreen::TopBottom)
439       pt->setY(pt->y() + (best->h() - win_size.h()));
440     if (screen.rowPlacementDirection() != BScreen::LeftRight)
441       pt->setX(pt->x() + (best->w() - win_size.w()));
442     return pt;
443   } else
444     return NULL; //fall back to cascade
445 }
446
447 Point *Workspace::underMousePlacement(const Size &win_size, const Rect &space) {
448   Point *pt;
449
450   int x, y, rx, ry;
451   Window c, r;
452   unsigned int m;
453   XQueryPointer(screen.getOpenbox().getXDisplay(), screen.getRootWindow(),
454                 &r, &c, &rx, &ry, &x, &y, &m);
455   pt = new Point(rx - win_size.w() / 2, ry - win_size.h() / 2);
456
457   if (pt->x() < space.x())
458     pt->setX(space.x());
459   if (pt->y() < space.y())
460     pt->setY(space.y());
461   if (pt->x() + win_size.w() > space.x() + space.w())
462     pt->setX(space.x() + space.w() - win_size.w());
463   if (pt->y() + win_size.h() > space.y() + space.h())
464     pt->setY(space.y() + space.h() - win_size.h());
465   return pt;
466 }
467
468 Point *Workspace::rowSmartPlacement(const Size &win_size, const Rect &space) {
469   bool placed=false;
470   int test_x, test_y, place_x = 0, place_y = 0;
471   int start_pos = 0;
472   int change_y =
473      ((screen.colPlacementDirection() == BScreen::TopBottom) ? 1 : -1);
474   int change_x =
475      ((screen.rowPlacementDirection() == BScreen::LeftRight) ? 1 : -1);
476   int delta_x = 8, delta_y = 8;
477   LinkedListIterator<OpenboxWindow> it(windowList);
478
479   test_y = (screen.colPlacementDirection() == BScreen::TopBottom) ?
480     start_pos : screen.size().h() - win_size.h() - start_pos;
481
482   while(!placed &&
483         ((screen.colPlacementDirection() == BScreen::BottomTop) ?
484          test_y > 0 : test_y + win_size.h() < (signed) space.h())) {
485     test_x = (screen.rowPlacementDirection() == BScreen::LeftRight) ?
486       start_pos : space.w() - win_size.w() - start_pos;
487     while (!placed &&
488            ((screen.rowPlacementDirection() == BScreen::RightLeft) ?
489             test_x > 0 : test_x + win_size.w() < (signed) space.w())) {
490       placed = true;
491
492       it.reset();
493       for (OpenboxWindow *curr = it.current(); placed && curr;
494            it++, curr = it.current()) {
495         int curr_w = curr->area().w() + (screen.getBorderWidth() * 4);
496         int curr_h = curr->area().h() + (screen.getBorderWidth() * 4);
497
498         if (curr->area().x() < test_x + win_size.w() &&
499             curr->area().x() + curr_w > test_x &&
500             curr->area().y() < test_y + win_size.h() &&
501             curr->area().y() + curr_h > test_y) {
502           placed = false;
503         }
504       }
505
506       // Removed code for checking toolbar and slit
507       // The space passed in should not include either
508
509       if (placed) {
510         place_x = test_x;
511         place_y = test_y;
512
513         break;
514       }   
515
516       test_x += (change_x * delta_x);
517     }
518
519     test_y += (change_y * delta_y);
520   }
521   if (placed)
522     return new Point(place_x, place_y);
523   else
524     return NULL; // fall back to cascade
525 }
526
527 Point *Workspace::colSmartPlacement(const Size &win_size, const Rect &space) {
528   Point *pt;
529   bool placed=false;
530   int test_x, test_y;
531   int start_pos = 0;
532   int change_y =
533     ((screen.colPlacementDirection() == BScreen::TopBottom) ? 1 : -1);
534   int change_x =
535     ((screen.rowPlacementDirection() == BScreen::LeftRight) ? 1 : -1);
536   int delta_x = 8, delta_y = 8;
537   LinkedListIterator<OpenboxWindow> it(windowList);
538
539   test_x = (screen.rowPlacementDirection() == BScreen::LeftRight) ?
540     start_pos : screen.size().w() - win_size.w() - start_pos;
541
542   while(!placed &&
543         ((screen.rowPlacementDirection() == BScreen::RightLeft) ?
544          test_x > 0 : test_x + win_size.w() < (signed) space.w())) {
545     test_y = (screen.colPlacementDirection() == BScreen::TopBottom) ?
546       start_pos : screen.size().h() - win_size.h() - start_pos;
547
548     while(!placed &&
549           ((screen.colPlacementDirection() == BScreen::BottomTop) ?
550            test_y > 0 : test_y + win_size.h() < (signed) space.h())){
551
552       placed = true;
553
554       it.reset();
555       for (OpenboxWindow *curr = it.current(); placed && curr;
556            it++, curr = it.current()) {
557         int curr_w = curr->area().w() + (screen.getBorderWidth() * 4);
558         int curr_h = curr->area().h() + (screen.getBorderWidth() * 4);
559
560         if (curr->area().x() < test_x + win_size.w() &&
561             curr->area().x() + curr_w > test_x &&
562             curr->area().y() < test_y + win_size.h() &&
563             curr->area().y() + curr_h > test_y) {
564           placed = False;
565         }
566       }
567
568       // Removed code checking for intersection with Toolbar and Slit
569       // The space passed to this method should not include either
570
571       if (placed) {
572         pt= new Point(test_x,test_y);
573
574         break;
575       }
576
577       test_y += (change_y * delta_y);
578     }
579
580     test_x += (change_x * delta_x);
581   }
582   if (placed)
583     return pt;
584   else
585     return NULL;
586 }
587
588
589 Point *const Workspace::cascadePlacement(const OpenboxWindow &win,
590                                          const Rect &space) {
591   if ((cascade_x + win.area().w() + screen.getBorderWidth() * 2 >
592        (space.x() + space.w())) ||
593       (cascade_y + win.area().h() + screen.getBorderWidth() * 2 >
594        (space.y() + space.h())))
595     cascade_x = cascade_y = 0;
596   if (cascade_x < space.x() || cascade_y < space.y()) {
597     cascade_x = space.x();
598     cascade_y = space.y();
599   }
600
601   Point *p = new Point(cascade_x, cascade_y);
602   cascade_x += win.getTitleHeight();
603   cascade_y += win.getTitleHeight();
604   return p;
605 }
606
607
608 void Workspace::placeWindow(OpenboxWindow &win) {
609   Rect space = screen.availableArea();
610   const Size window_size(win.area().w()+screen.getBorderWidth() * 2,
611                          win.area().h()+screen.getBorderWidth() * 2);
612   Point *place = NULL;
613   LinkedListIterator<OpenboxWindow> it(windowList);
614
615   switch (screen.placementPolicy()) {
616   case BScreen::BestFitPlacement:
617     place = bestFitPlacement(window_size, space);
618     break;
619   case BScreen::RowSmartPlacement:
620     place = rowSmartPlacement(window_size, space);
621     break;
622   case BScreen::ColSmartPlacement:
623     place = colSmartPlacement(window_size, space);
624     break;
625   case BScreen::UnderMousePlacement:
626     place = underMousePlacement(window_size, space);
627     break;
628   } // switch
629
630   if (place == NULL)
631     place = cascadePlacement(win, space);
632  
633   ASSERT(place != NULL);  
634   if (place->x() + window_size.w() > (signed) space.x() + space.w())
635     place->setX(((signed) space.x() + space.w() - window_size.w()) / 2);
636   if (place->y() + window_size.h() > (signed) space.y() + space.h())
637     place->setY(((signed) space.y() + space.h() - window_size.h()) / 2);
638
639   win.configure(place->x(), place->y(), win.area().w(), win.area().h());
640   delete place;
641 }