// WinClient.cc for Fluxbox - an X11 Window manager // Copyright (c) 2003 - 2006 Henrik Kinnunen (fluxgen at fluxbox dot org) // // Permission is hereby granted, free of charge, to any person obtaining a // copy of this software and associated documentation files (the "Software"), // to deal in the Software without restriction, including without limitation // the rights to use, copy, modify, merge, publish, distribute, sublicense, // and/or sell copies of the Software, and to permit persons to whom the // Software is furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER // DEALINGS IN THE SOFTWARE. #include "WinClient.hh" #include "Window.hh" #include "fluxbox.hh" #include "FocusControl.hh" #include "Screen.hh" #include "FbAtoms.hh" #include "Xutil.hh" #include "FbTk/EventManager.hh" #include "FbTk/MultLayers.hh" #include #include #include #include #include #ifdef HAVE_CASSERT #include #else #include #endif #ifdef HAVE_CSTRING #include #else #include #endif using std::string; using std::list; using std::mem_fun; #ifdef DEBUG using std::cerr; using std::endl; using std::hex; using std::dec; #endif // DEBUG WinClient::TransientWaitMap WinClient::s_transient_wait; WinClient::WinClient(Window win, BScreen &screen, FluxboxWindow *fbwin): Focusable(screen, fbwin), FbTk::FbWindow(win), transient_for(0), window_group(0), x(0), y(0), old_bw(0), min_width(1), min_height(1), max_width(0), max_height(0), width_inc(1), height_inc(1), min_aspect_x(0), min_aspect_y(0), max_aspect_x(0), max_aspect_y(0), base_width(1), base_height(1), initial_state(0), normal_hint_flags(0), wm_hint_flags(0), m_modal_count(0), m_modal(false), accepts_input(false), send_focus_message(false), send_close_message(false), m_win_gravity(0), m_title_override(false), m_icon_override(false), m_window_type(Focusable::TYPE_NORMAL), m_mwm_hint(0), m_strut(0) { updateWMProtocols(); updateMWMHints(); updateWMHints(); updateWMNormalHints(); updateWMClassHint(); updateTitle(); Fluxbox::instance()->saveWindowSearch(win, this); if (window_group != None) Fluxbox::instance()->saveGroupSearch(window_group, this); // search for this in transient waiting list if (s_transient_wait.find(win) != s_transient_wait.end()) { // Found transients that are waiting for this. // For each transient that waits call updateTransientInfo for_each(s_transient_wait[win].begin(), s_transient_wait[win].end(), mem_fun(&WinClient::updateTransientInfo)); // clear transient waiting list for this window s_transient_wait.erase(win); } // also check if this window is a transient // this needs to be done before creating an fbwindow, so this doesn't get // tabbed using the apps file updateTransientInfo(); } WinClient::~WinClient() { #ifdef DEBUG cerr<<__FILE__<<"(~"<<__FUNCTION__<<")[this="<remove(window()); clearStrut(); // // clear transients and transient_for // if (transient_for != 0) { assert(transient_for != this); transient_for->transientList().remove(this); if (m_modal) transient_for->removeModal(); transient_for = 0; } while (!transients.empty()) { transients.back()->transient_for = 0; transients.pop_back(); } accepts_input = send_focus_message = false; if (fbwindow() != 0) fbwindow()->removeClient(*this); // this takes care of any focus issues m_diesig.notify(); Fluxbox *fluxbox = Fluxbox::instance(); // This fixes issue 1 (see WinClient.hh): // If transients die before the transient_for is created removeTransientFromWaitingList(); s_transient_wait.erase(window()); if (window_group != 0) { fluxbox->removeGroupSearch(window_group); window_group = 0; } if (m_mwm_hint != 0) XFree(m_mwm_hint); if (window()) fluxbox->removeWindowSearch(window()); } bool WinClient::acceptsFocus() const { return ((accepts_input || send_focus_message) && // focusing fbpanel messes up quite a few things m_window_type != Focusable::TYPE_DOCK && m_window_type != Focusable::TYPE_SPLASH); } bool WinClient::sendFocus() { if (accepts_input) { setInputFocus(RevertToPointerRoot, CurrentTime); FocusControl::setExpectingFocus(this); return true; } if (!send_focus_message) return false; #ifdef DEBUG cerr<<"WinClient::"<<__FUNCTION__<<": this = "<getWMProtocolsAtom(); ce.xclient.display = display(); ce.xclient.window = window(); ce.xclient.format = 32; ce.xclient.data.l[0] = FbAtoms::instance()->getWMTakeFocusAtom(); ce.xclient.data.l[1] = Fluxbox::instance()->getLastTime(); ce.xclient.data.l[2] = 0l; ce.xclient.data.l[3] = 0l; ce.xclient.data.l[4] = 0l; // send focus msg XSendEvent(display(), window(), false, NoEventMask, &ce); FocusControl::setExpectingFocus(this); return true; } void WinClient::sendClose(bool forceful) { if (forceful || !send_close_message) XKillClient(display(), window()); else { // send WM_DELETE message // fill in XClientMessage structure for delete message XEvent ce; ce.xclient.type = ClientMessage; ce.xclient.message_type = FbAtoms::instance()->getWMProtocolsAtom(); ce.xclient.display = display(); ce.xclient.window = window(); ce.xclient.format = 32; ce.xclient.data.l[0] = FbAtoms::instance()->getWMDeleteAtom(); ce.xclient.data.l[1] = CurrentTime; ce.xclient.data.l[2] = 0l; ce.xclient.data.l[3] = 0l; ce.xclient.data.l[4] = 0l; // send event delete message to client window XSendEvent(display(), window(), false, NoEventMask, &ce); } } bool WinClient::getAttrib(XWindowAttributes &attr) const { return XGetWindowAttributes(display(), window(), &attr); } bool WinClient::getWMName(XTextProperty &textprop) const { return XGetWMName(display(), window(), &textprop); } bool WinClient::getWMIconName(XTextProperty &textprop) const { return XGetWMIconName(display(), window(), &textprop); } string WinClient::getWMRole() const { Atom wm_role = XInternAtom(FbTk::App::instance()->display(), "WM_WINDOW_ROLE", False); return textProperty(wm_role); } void WinClient::updateWMClassHint() { XClassHint ch; if (XGetClassHint(display(), window(), &ch) == 0) { #ifdef DEBUG cerr<<"WinClient: Failed to read class hint!"<(ch.res_name); XFree(ch.res_name); ch.res_name = 0; } else m_instance_name = ""; if (ch.res_class != 0) { m_class_name = const_cast(ch.res_class); XFree(ch.res_class); ch.res_class = 0; } else m_class_name = ""; } } void WinClient::updateTransientInfo() { // remove this from parent if (transientFor() != 0) { transientFor()->transientList().remove(this); if (m_modal) transientFor()->removeModal(); } transient_for = 0; // determine if this is a transient window Window win = 0; if (!XGetTransientForHint(display(), window(), &win)) { #ifdef DEBUG cerr<<__FUNCTION__<<": window() = 0x"<searchWindow(win); // if we did not find a transient WinClient but still // have a transient X window, then we have to put the // X transient_for window in a waiting list and update this clients transient // list later when the transient_for has a Winclient if (!transient_for) { // We might also already waiting for an old transient_for; // // this call fixes issue 2: // If transients changes to new transient_for before the old transient_for is created. // (see comment in WinClient.hh) // removeTransientFromWaitingList(); s_transient_wait[win].push_back(this); } #ifdef DEBUG cerr<<__FUNCTION__<<": transient_for window = 0x"<addModal(); else transient_for->removeModal(); } // TODO: we're not implementing the following part of EWMH spec: // "if WM_TRANSIENT_FOR is not set or set to the root window the dialog is // modal for its window group." } bool WinClient::validateClient() const { FbTk::App::instance()->sync(false); XEvent e; if (( XCheckTypedWindowEvent(display(), window(), DestroyNotify, &e) || XCheckTypedWindowEvent(display(), window(), UnmapNotify, &e)) && XPutBackEvent(display(), &e)) { Fluxbox::instance()->ungrab(); return false; } return true; } void WinClient::setStrut(Strut *strut) { clearStrut(); m_strut = strut; } void WinClient::clearStrut() { if (m_strut != 0) { screen().clearStrut(m_strut); screen().updateAvailableWorkspaceArea(); m_strut = 0; } } bool WinClient::focus() { if (fbwindow() == 0) return false; else return fbwindow()->setCurrentClient(*this, true); } bool WinClient::isFocused() const { return (fbwindow() ? fbwindow()->isFocused() && &fbwindow()->winClient() == this : false); } void WinClient::setAttentionState(bool value) { Focusable::setAttentionState(value); if (fbwindow() && !fbwindow()->isFocused()) fbwindow()->setAttentionState(value); } void WinClient::updateWMProtocols() { Atom *proto = 0; int num_return = 0; FbAtoms *fbatoms = FbAtoms::instance(); if (XGetWMProtocols(display(), window(), &proto, &num_return)) { // defaults send_focus_message = false; send_close_message = false; for (int i = 0; i < num_return; ++i) { if (proto[i] == fbatoms->getWMDeleteAtom()) send_close_message = true; else if (proto[i] == fbatoms->getWMTakeFocusAtom()) send_focus_message = true; } XFree(proto); if (fbwindow()) fbwindow()->updateFunctions(); #ifdef DEBUG } else { cerr<<"Warning: Failed to read WM Protocols. "<(min_width)) width = min_width; if (height < 0 || height < static_cast(min_height)) height = min_height; // Check maximum size if (max_width > 0 && width > static_cast(max_width)) width = max_width; if (max_height > 0 && height > static_cast(max_height)) height = max_height; // we apply aspect ratios before incrementals // Too difficult to exactly satisfy both incremental+aspect // in most situations // (they really shouldn't happen at the same time anyway). /* aspect ratios are applied exclusive to the base_width * * min_aspect_x width max_aspect_x * ------------ < ------- < ------------ * min_aspect_y height max_aspect_y * * beware of integer maximum (so I'll use doubles instead and divide) * * The trick is how to get back to the aspect ratio with minimal * change - do we modify x, y or both? * A: we minimise the distance between the current point, and * the target aspect ratio (consider them as x,y coordinates) * Consider that the aspect ratio is a line, and the current * w/h is a point, so we're just using the formula for * shortest distance from a point to a line! * * When maximizing, we must not increase any of the sizes, because we * would end up with the window partly off a screen, so a simpler formula * is used in that case. */ if (min_aspect_y > 0 && max_aspect_y > 0 && (height - base_height) > 0) { double widthd = static_cast(width - base_width); double heightd = static_cast(height - base_height); double min = static_cast(min_aspect_x) / static_cast(min_aspect_y); double max = static_cast(max_aspect_x) / static_cast(max_aspect_y); double actual = widthd / heightd; if (max > 0 && min > 0 && actual > 0) { // don't even try otherwise bool changed = false; if (actual < min) { changed = true; if (maximizing) heightd = widthd / min; else closestPointToLine(widthd, heightd, widthd, heightd, min); } else if (actual > max) { changed = true; if (maximizing) widthd = heightd * max; else closestPointToLine(widthd, heightd, widthd, heightd, max); } if (changed) { width = static_cast(widthd) + base_width; height = static_cast(heightd) + base_height; } } } // enforce incremental size limits, wrt base size // only calculate this if we really need to i = (width - static_cast(base_width)) / static_cast(width_inc); width = i*static_cast(width_inc) + static_cast(base_width); j = (height - static_cast(base_height)) / static_cast(height_inc); height = j*static_cast(height_inc) + static_cast(base_height); if (display_width) *display_width = i; if (display_height) *display_height = j; } // check if the given width and height satisfy the size hints bool WinClient::checkSizeHints(unsigned int width, unsigned int height) { if (width < min_width || height < min_height) return false; if (width > max_width || height > max_height) return false; if ((width - base_width) % width_inc != 0) return false; if ((height - base_height) % height_inc != 0) return false; double ratio = (double)width / (double)height; if (min_aspect_y > 0 && (double)min_aspect_x / (double)min_aspect_y > ratio) return false; if (max_aspect_y > 0 && (double)max_aspect_x / (double)max_aspect_y < ratio) return false; return true; } void WinClient::removeTransientFromWaitingList() { // holds the windows that dont have empty // transient waiting list list remove_list; // The worst case complexity is huge, but since we usually do not (virtualy never) // have a large transient waiting list the time spent here is neglectable TransientWaitMap::iterator t_it = s_transient_wait.begin(); TransientWaitMap::iterator t_it_end = s_transient_wait.end(); for (; t_it != t_it_end; ++t_it) { (*t_it).second.remove(this); // if the list is empty, add it to remove list // so we can erase it later if ((*t_it).second.empty()) remove_list.push_back((*t_it).first); } // erase empty waiting lists list::iterator it = remove_list.begin(); list::iterator it_end = remove_list.end(); for (; it != it_end; ++it) s_transient_wait.erase(*it); }