From d446ba265a76d557c1fa7c73013506f9f468e63f Mon Sep 17 00:00:00 2001
From: markt <markt>
Date: Sat, 27 Jan 2007 18:06:28 +0000
Subject: bunch of cleanup, little bug fixes

---
 src/FbTk/KeyUtil.cc | 57 ++++++++++++-------------------
 src/FbTk/KeyUtil.hh |  2 ++
 src/FocusControl.cc |  1 +
 src/Screen.cc       | 12 ++++---
 src/Window.cc       | 96 +++++++++--------------------------------------------
 5 files changed, 47 insertions(+), 121 deletions(-)

diff --git a/src/FbTk/KeyUtil.cc b/src/FbTk/KeyUtil.cc
index b02a76b..3f413a9 100644
--- a/src/FbTk/KeyUtil.cc
+++ b/src/FbTk/KeyUtil.cc
@@ -118,44 +118,29 @@ void KeyUtil::grabKey(unsigned int key, unsigned int mod, Window win) {
     const unsigned int nummod = instance().numlock();
     const unsigned int scrollmod = instance().scrolllock();
 
-    XGrabKey(display, key, mod,
-             win, True,
-             GrabModeAsync, GrabModeAsync);
-
     // Grab with numlock, capslock and scrlock
+    for (int i = 0; i < 8; i++) {
+        XGrabKey(display, key, mod | (i & 1 ? capsmod : 0) |
+                 (i & 2 ? nummod : 0) | (i & 4 ? scrollmod : 0),
+                 win, True, GrabModeAsync, GrabModeAsync);
+    }
+
+}
 
-    //numlock
-    XGrabKey(display, key, mod|nummod,
-             win, True,
-             GrabModeAsync, GrabModeAsync);
-    //scrolllock
-    XGrabKey(display, key, mod|scrollmod,
-             win, True,
-             GrabModeAsync, GrabModeAsync);
-    //capslock
-    XGrabKey(display, key, mod|capsmod,
-             win, True,
-             GrabModeAsync, GrabModeAsync);
-
-    //capslock+numlock
-    XGrabKey(display, key, mod|capsmod|nummod,
-             win, True,
-             GrabModeAsync, GrabModeAsync);
-
-    //capslock+scrolllock
-    XGrabKey(display, key, mod|capsmod|scrollmod,
-             win, True,
-             GrabModeAsync, GrabModeAsync);
-
-    //capslock+numlock+scrolllock
-    XGrabKey(display, key, mod|capsmod|scrollmod|nummod,
-             win, True,
-             GrabModeAsync, GrabModeAsync);
-
-    //numlock+scrollLock
-    XGrabKey(display, key, mod|nummod|scrollmod,
-             win, True,
-             GrabModeAsync, GrabModeAsync);
+void KeyUtil::grabButton(unsigned int button, unsigned int mod, Window win,
+                         unsigned int event_mask, Cursor cursor) {
+    Display *display = App::instance()->display();
+    const unsigned int capsmod = instance().capslock();
+    const unsigned int nummod = instance().numlock();
+    const unsigned int scrollmod = instance().scrolllock();
+
+    // Grab with numlock, capslock and scrlock
+    for (int i = 0; i < 8; i++) {
+        XGrabButton(display, button, mod | (i & 1 ? capsmod : 0) |
+                    (i & 2 ? nummod : 0) | (i & 4 ? scrollmod : 0),
+                    win, True, event_mask, GrabModeAsync, GrabModeAsync,
+                    None, cursor);
+    }
 
 }
 
diff --git a/src/FbTk/KeyUtil.hh b/src/FbTk/KeyUtil.hh
index 902d27e..eb2cdd5 100644
--- a/src/FbTk/KeyUtil.hh
+++ b/src/FbTk/KeyUtil.hh
@@ -44,6 +44,8 @@ public:
        Grab the specified key
     */
     static void grabKey(unsigned int key, unsigned int mod, Window win);
+    static void grabButton(unsigned int button, unsigned int mod, Window win,
+                           unsigned int event_mask, Cursor cursor = None);
 
     /**
        convert the string to the keysym
diff --git a/src/FocusControl.cc b/src/FocusControl.cc
index 21c243d..29a1b8b 100644
--- a/src/FocusControl.cc
+++ b/src/FocusControl.cc
@@ -193,6 +193,7 @@ void FocusControl::stopCyclingFocus() {
         m_cycling_window = find(m_focused_list.begin(),m_focused_list.end(),*m_cycling_window);
     if (m_cycling_window != m_focused_list.end() &&
         m_cycling_window != m_creation_order_list.end() &&
+        (*m_cycling_window)->fbwindow() &&
         (*m_cycling_window)->fbwindow()->isVisible()) {
         WinClient *client = *m_cycling_window;
         m_focused_list.erase(m_cycling_window);
diff --git a/src/Screen.cc b/src/Screen.cc
index 7c0bed9..90d0a4a 100644
--- a/src/Screen.cc
+++ b/src/Screen.cc
@@ -1404,7 +1404,8 @@ FluxboxWindow *BScreen::createWindow(Window client) {
     }
 
     // add the window to the focus list
-    if (focusControl().focusNew())
+    // always add to front on startup to keep the focus order the same
+    if (focusControl().focusNew() || Fluxbox::instance()->isStartup())
         focusControl().addFocusFront(*winclient);
     else
         focusControl().addFocusBack(*winclient);
@@ -1435,7 +1436,7 @@ FluxboxWindow *BScreen::createWindow(WinClient &client) {
 
 #ifdef SLIT
     if (win->initialState() == WithdrawnState && slit() != 0) {
-        slit()->addClient(win->clientWindow());
+        slit()->addClient(client.window());
     }
 #endif // SLIT
 
@@ -1445,8 +1446,11 @@ FluxboxWindow *BScreen::createWindow(WinClient &client) {
         return 0;
     }
 
-    // can't setInputFocus yet and mapNotifyEvent doesn't happen for the client
-    if (focusControl().focusNew() || FocusControl::focusedWindow() == &client)
+    win->show();
+    // don't ask me why, but client doesn't seem to keep focus in new window
+    // and we don't seem to get a FocusIn event from setInputFocus
+    if ((focusControl().focusNew() || FocusControl::focusedWindow() == &client)
+            && win->setInputFocus())
         FocusControl::setFocusedWindow(&client);
 
     m_clientlist_sig.notify();
diff --git a/src/Window.cc b/src/Window.cc
index 5634f8f..bb271c3 100644
--- a/src/Window.cc
+++ b/src/Window.cc
@@ -104,57 +104,6 @@ using std::hex;
 
 namespace {
 
-void grabButton(unsigned int button,
-                Window window, Cursor cursor) {
-
-    static Display *display = App::instance()->display();
-
-    const int numlock = KeyUtil::instance().numlock();
-    const int capslock = KeyUtil::instance().capslock();
-    const int scrolllock = KeyUtil::instance().scrolllock();
-
-    // Grab with modkey and with all lock modifiers
-    // (num, scroll and caps)
-
-    unsigned int modkey = Fluxbox::instance()->getModKey();
-
-    //numlock
-    XGrabButton(display, button, modkey|numlock, window, True,
-                ButtonReleaseMask | ButtonMotionMask, GrabModeAsync,
-                GrabModeAsync, None, cursor);
-    //scrolllock
-    XGrabButton(display, button, modkey|scrolllock, window, True,
-                ButtonReleaseMask | ButtonMotionMask, GrabModeAsync,
-                GrabModeAsync, None, cursor);
-
-    //capslock
-    XGrabButton(display, button, modkey|capslock, window, True,
-                ButtonReleaseMask | ButtonMotionMask, GrabModeAsync,
-                GrabModeAsync, None, cursor);
-
-    //capslock+numlock
-    XGrabButton(display, Button1, modkey|capslock|numlock, window, True,
-                ButtonReleaseMask | ButtonMotionMask, GrabModeAsync,
-                GrabModeAsync, None, cursor);
-
-    //capslock+scrolllock
-    XGrabButton(display, button, modkey|capslock|scrolllock, window, True,
-                ButtonReleaseMask | ButtonMotionMask, GrabModeAsync,
-                GrabModeAsync, None, cursor);
-
-    //capslock+numlock+scrolllock
-    XGrabButton(display, button, modkey|capslock|numlock|scrolllock, window,
-                True,
-                ButtonReleaseMask | ButtonMotionMask, GrabModeAsync,
-                GrabModeAsync, None, cursor);
-
-    //numlock+scrollLock
-    XGrabButton(display, button, modkey|numlock|scrolllock, window, True,
-                ButtonReleaseMask | ButtonMotionMask, GrabModeAsync,
-                GrabModeAsync, None, cursor);
-
-}
-
 // X event scanner for enter/leave notifies - adapted from twm
 typedef struct scanargs {
     Window w;
@@ -402,8 +351,7 @@ void FluxboxWindow::init() {
 
     assert(m_client);
     m_client->setFluxboxWindow(this);
-    if (!m_client->hasGroupLeftWindow())
-        m_client->setGroupLeftWindow(None); // nothing to the left.
+    m_client->setGroupLeftWindow(None); // nothing to the left.
 
     // check for shape extension and whether the window is shaped
     m_shaped = false;
@@ -702,10 +650,10 @@ void FluxboxWindow::attachClient(WinClient &client, int x, int y) {
                          frame().clientArea().width(),
                          frame().clientArea().height());
 
-        if (&client == FocusControl::focusedWindow()) {
-            was_focused = true;
+        // right now, this block only happens with new windows or on restart
+        if (screen().focusControl().focusNew() ||
+                Fluxbox::instance()->isStartup())
             focused_win = &client;
-        }
 
         client.saveBlackboxAttribs(m_blackbox_attrib);
         m_clientlist.push_back(&client);
@@ -718,16 +666,15 @@ void FluxboxWindow::attachClient(WinClient &client, int x, int y) {
     m_workspacesig.notify();
     m_layersig.notify();
 
-    if (was_focused)
-        // already has focus, we're just assuming the state of the old window
+    if (was_focused) {
+        // don't ask me why, but client doesn't seem to keep focus in new window
+        // and we don't seem to get a FocusIn event from setInputFocus
+        setCurrentClient(client);
         FocusControl::setFocusedWindow(&client);
+    } else if (focused_win)
+        setCurrentClient(*focused_win, false);
 
     frame().reconfigure();
-
-    // keep the current window on top
-    if (focused_win)
-        m_client = focused_win;
-    m_client->raise();
 }
 
 
@@ -748,11 +695,7 @@ bool FluxboxWindow::detachClient(WinClient &client) {
         (*client_it_after)->setGroupLeftWindow(leftwin);
 
     removeClient(client);
-
-    // m_client must be valid as there should be at least one other window
-    // otherwise this wouldn't be here (refer numClients() <= 1 return)
-    client.setFluxboxWindow(screen().createWindow(client));
-    client.setGroupLeftWindow(None);
+    screen().createWindow(client);
     return true;
 }
 
@@ -1123,22 +1066,16 @@ void FluxboxWindow::grabButtons() {
     unsigned int modkey = Fluxbox::instance()->getModKey();
 
     if (modkey) {
-        XGrabButton(display, Button1, modkey, frame().window().window(), True,
-                    ButtonReleaseMask | ButtonMotionMask, GrabModeAsync,
-                    GrabModeAsync, None, frame().theme().moveCursor());
-
         //----grab with "all" modifiers
-        grabButton(Button1, frame().window().window(), frame().theme().moveCursor());
+        FbTk::KeyUtil::grabButton(Button1, modkey, frame().window().window(),
+            ButtonReleaseMask | ButtonMotionMask, frame().theme().moveCursor());
 
         XGrabButton(display, Button2, modkey, frame().window().window(), True,
                     ButtonReleaseMask, GrabModeAsync, GrabModeAsync, None, None);
 
-        XGrabButton(display, Button3, modkey, frame().window().window(), True,
-                    ButtonReleaseMask | ButtonMotionMask, GrabModeAsync,
-                    GrabModeAsync, None, None);
-
         //---grab with "all" modifiers
-        grabButton(Button3, frame().window().window(), None);
+        FbTk::KeyUtil::grabButton(Button3, modkey, frame().window().window(),
+            ButtonReleaseMask | ButtonMotionMask);
     }
 }
 
@@ -3641,9 +3578,6 @@ void FluxboxWindow::attachTo(int x, int y, bool interrupted) {
             // since just detached, move relative to old location
             if (client.fbwindow() != 0) {
                 client.fbwindow()->move(frame().x() - m_last_resize_x + x, frame().y() - m_last_resize_y + y);
-                client.fbwindow()->show();
-                FocusControl::setFocusedWindow(&client);
-                client.fbwindow()->setInputFocus();
             }
         } else if( attach_to_win == this && attach_to_win->isTabable()) {
             //reording of tabs within a frame
-- 
cgit v0.11.2