From 251a4c9ac62050e912fd861c4443ebe83948e22d Mon Sep 17 00:00:00 2001 From: Pavel Labath Date: Mon, 20 Jun 2011 22:16:30 +0200 Subject: Convert slitlist from a separate file to a regular lua resource --- src/Screen.cc | 3 +- src/Slit.cc | 133 ++++++++++++++++++++++++++++++--------------------------- src/Slit.hh | 27 ++++++++++-- src/fluxbox.cc | 8 ---- src/fluxbox.hh | 3 +- 5 files changed, 96 insertions(+), 78 deletions(-) diff --git a/src/Screen.cc b/src/Screen.cc index 77b7ca3..8ce0528 100644 --- a/src/Screen.cc +++ b/src/Screen.cc @@ -455,8 +455,7 @@ BScreen::BScreen(FbTk::ResourceManager_base &rm, changeWorkspaceID(first_desktop); #ifdef SLIT - m_slit.reset(new Slit(*this, *layerManager().getLayer(ResourceLayer::DESKTOP), - fluxbox->getSlitlistFilename().c_str())); + m_slit.reset(new Slit(*this, *layerManager().getLayer(ResourceLayer::DESKTOP))); #endif // SLIT XFlush(disp); diff --git a/src/Slit.cc b/src/Slit.cc index d5d2dde..2e32f27 100644 --- a/src/Slit.cc +++ b/src/Slit.cc @@ -164,7 +164,67 @@ private: unsigned int Slit::s_eventmask = SubstructureRedirectMask | ButtonPressMask | EnterWindowMask | LeaveWindowMask | ExposureMask; -Slit::Slit(BScreen &scr, FbTk::Layer &layer, const char *filename) +void Slit::SlitClientsRes::setFromLua(lua::state &l) { + lua::stack_sentry s(l, -1); + l.checkstack(1); + + // to avoid modifying the master copy until we are sure the new list is ok + SlitClients copy(*this); + // this will be the new list once we are done + SlitClients t; + + if(l.type(-1) != lua::TTABLE) { + std::cerr << "Cannot convert to a client list from lua type " + << l.type_name(l.type(-1)) << std::endl; + return; + } + for(size_t i = 1; l.rawgeti(-1, i), !l.isnil(-1); l.pop(), ++i) { + if(l.type(-1) != lua::TSTRING && l.type(-1) != lua::TNUMBER) { + std::cerr << "Cannot convert to a client name from lua type " + << l.type_name(l.type(-1)) << std::endl; + continue; + } + const std::string &name = l.tostring(-1); + // look for a matching window + bool found = false; + for(SlitClients::iterator j = copy.begin(); j != copy.end(); ++j) { + if((*j)->matchName().logical() == name) { + t.push_back(*j); + copy.erase(j); + found = true; + break; + } + } + if(!found) { + // no matching window, create a placeholder + t.push_back( new SlitClient(name.c_str()) ); + } + } l.pop(); + + // move remaining non-placeholder clients to the new list + while(!copy.empty()) { + if(copy.front()->window() != None) + t.push_back(copy.front()); + copy.pop_front(); + } + SlitClients::operator=(t); + + l.pop(); +} + +void Slit::SlitClientsRes::pushToLua(lua::state &l) const { + l.checkstack(2); + l.createtable(size()); + lua::stack_sentry s(l); + + int j = 1; + for(const_iterator i = begin(); i != end(); ++i) { + l.pushstring((*i)->matchName().logical()); + l.rawseti(-2, j++); + } +} + +Slit::Slit(BScreen &scr, FbTk::Layer &layer) : m_hidden(false), m_visible(false), m_screen(scr), m_clientlist_menu(scr.menuTheme(), @@ -188,7 +248,7 @@ Slit::Slit(BScreen &scr, FbTk::Layer &layer, const char *filename) m_slit_theme(new SlitTheme(scr.rootWindow().screenNumber())), m_strut(0), // resources - // lock in first resource + m_client_list(scr.resourceManager(), scr.name() + ".slit.clientList"), m_rc_kde_dockapp(scr.resourceManager(), true, scr.name() + ".slit.acceptKdeDockapps"), m_rc_auto_hide(scr.resourceManager(), false, scr.name() + ".slit.autoHide"), // TODO: this resource name must change @@ -243,11 +303,6 @@ Slit::Slit(BScreen &scr, FbTk::Layer &layer, const char *filename) moveToLayer(static_cast(*m_rc_layernum)); - - - // Get client list for sorting purposes - loadClientList(filename); - setupMenu(); } @@ -800,7 +855,6 @@ void Slit::reposition() { void Slit::shutdown() { - saveClientList(); while (!m_client_list.empty()) removeClient(m_client_list.front(), true, true); } @@ -824,6 +878,8 @@ void Slit::clientUp(SlitClient* client) { break; } } + + saveClientList(); } void Slit::clientDown(SlitClient* client) { @@ -845,6 +901,8 @@ void Slit::clientDown(SlitClient* client) { break; } } + + saveClientList(); } void Slit::cycleClientsUp() { @@ -857,6 +915,8 @@ void Slit::cycleClientsUp() { m_client_list.erase(it); m_client_list.push_back(client); reconfigure(); + + saveClientList(); } void Slit::cycleClientsDown() { @@ -868,6 +928,8 @@ void Slit::cycleClientsDown() { m_client_list.remove(client); m_client_list.push_front(client); reconfigure(); + + saveClientList(); } void Slit::handleEvent(XEvent &event) { @@ -998,40 +1060,6 @@ void Slit::toggleHidden() { frame.window.move(frame.x, frame.y); } -void Slit::loadClientList(const char *filename) { - if (filename == 0 || filename[0] == '\0') - return; - - // save filename so we can save client list later - m_filename = filename; - string real_filename= FbTk::StringUtil::expandFilename(filename); - - struct stat buf; - if (stat(real_filename.c_str(), &buf) == 0) { - ifstream file(real_filename.c_str()); - string name; - while (! file.eof()) { - name = ""; - getline(file, name); // get the entire line - if (name.empty()) - continue; - - // remove whitespaces from start and end - FbTk::StringUtil::removeFirstWhitespace(name); - - // the cleaned string could still be a comment, or blank - if ( name.empty() || name[0] == '#' || name[0] == '!' ) - continue; - - // trailing whitespace won't affect the above test - FbTk::StringUtil::removeTrailingWhitespace(name); - - SlitClient *client = new SlitClient(name.c_str()); - m_client_list.push_back(client); - } - } -} - void Slit::updateClientmenu() { if (screen().isShuttingdown()) return; @@ -1055,30 +1083,11 @@ void Slit::updateClientmenu() { m_clientlist_menu.insert(new SlitClientMenuItem(*this, *(*it), reconfig)); } - m_clientlist_menu.insert(new FbTk::MenuSeparator()); - FbTk::RefCount > savecmd(new FbTk::SimpleCommand(*this, &Slit::saveClientList)); - m_clientlist_menu.insert(_FB_XTEXT(Slit, - SaveSlitList, - "Save SlitList", "Saves the current order in the slit"), - savecmd); - m_clientlist_menu.updateMenu(); } void Slit::saveClientList() { - - ofstream file(FbTk::StringUtil::expandFilename(m_filename).c_str()); - SlitClients::iterator it = m_client_list.begin(); - SlitClients::iterator it_end = m_client_list.end(); - string prevName; - string name; - for (; it != it_end; ++it) { - name = (*it)->matchName().logical(); - if (name != prevName) - file << name.c_str() << endl; - - prevName = name; - } + Fluxbox::instance()->save_rc(); } void Slit::setupMenu() { diff --git a/src/Slit.hh b/src/Slit.hh index 5c69a8c..9384ad6 100644 --- a/src/Slit.hh +++ b/src/Slit.hh @@ -68,7 +68,7 @@ public: RIGHTBOTTOM, RIGHTCENTER, RIGHTTOP }; - Slit(BScreen &screen, FbTk::Layer &layer, const char *filename = 0); + Slit(BScreen &screen, FbTk::Layer &layer); virtual ~Slit(); void show() { frame.window.show(); m_visible = true; } @@ -137,7 +137,6 @@ private: void setupMenu(); void removeClient(SlitClient *client, bool remap, bool destroy); - void loadClientList(const char *filename); void updateClientmenu(); void clearStrut(); void updateStrut(); @@ -148,13 +147,11 @@ private: BScreen &m_screen; FbTk::Timer m_timer; - SlitClients m_client_list; std::auto_ptr m_layermenu; FbMenu m_clientlist_menu, m_slitmenu; #ifdef XINERAMA XineramaHeadMenu *m_xineramaheadmenu; #endif // XINERAMA - std::string m_filename; struct frame { frame(const FbTk::FbWindow &parent): @@ -179,6 +176,28 @@ private: static unsigned int s_eventmask; Strut *m_strut; + class SlitClientsRes: public FbTk::Resource_base, public SlitClients { + public: + SlitClientsRes(FbTk::ResourceManager_base &rm, const std::string &name) + : FbTk::Resource_base(name, name), m_rm(rm) { + m_rm.addResource(*this); + } + + ~SlitClientsRes() { + m_rm.removeResource(*this); + } + + virtual void setDefaultValue() {} + virtual void setFromString(const char *) { assert(0); } + virtual std::string getString() const { assert(0); } + virtual void setFromLua(lua::state &l); + virtual void pushToLua(lua::state &l) const; + + private: + FbTk::ResourceManager_base &m_rm; + }; + + SlitClientsRes m_client_list; FbTk::BoolResource m_rc_kde_dockapp, m_rc_auto_hide, m_rc_maximize_over; FbTk::Resource > m_rc_placement; FbTk::IntResource m_rc_alpha, m_rc_on_head; diff --git a/src/fluxbox.cc b/src/fluxbox.cc index 156febf..07d5436 100644 --- a/src/fluxbox.cc +++ b/src/fluxbox.cc @@ -243,7 +243,6 @@ Fluxbox::Fluxbox(int argc, char **argv, m_rc_styleoverlayfile(m_resourcemanager, m_RC_PATH + "/overlay", "styleOverlay", "StyleOverlay"), m_rc_menufile(m_resourcemanager, m_RC_PATH + "/menu", "menuFile", "MenuFile"), m_rc_keyfile(m_resourcemanager, m_RC_PATH + "/keys", "keyFile", "KeyFile"), - m_rc_slitlistfile(m_resourcemanager, m_RC_PATH + "/slitlist", "slitlistFile", "SlitlistFile"), m_rc_appsfile(m_resourcemanager, m_RC_PATH + "/apps", "appsFile", "AppsFile"), m_rc_tabs_attach_area(m_resourcemanager, ATTACH_AREA_WINDOW, "tabsAttachArea", "TabsAttachArea"), m_rc_cache_life(m_resourcemanager, 5, "cacheLife", "CacheLife"), @@ -1174,13 +1173,6 @@ void Fluxbox::load_rc() { FbTk::Transparent::usePseudoTransparent(*m_rc_pseudotrans); - if (!m_rc_slitlistfile->empty()) { - *m_rc_slitlistfile = StringUtil::expandFilename(*m_rc_slitlistfile); - } else { - string filename = getDefaultDataFilename("slitlist"); - m_rc_slitlistfile.setFromString(filename.c_str()); - } - *m_rc_colors_per_channel = FbTk::Util::clamp(*m_rc_colors_per_channel, 2, 6); if (m_rc_stylefile->empty()) diff --git a/src/fluxbox.hh b/src/fluxbox.hh index 47a8187..e7d2d68 100644 --- a/src/fluxbox.hh +++ b/src/fluxbox.hh @@ -121,7 +121,6 @@ public: const std::string &getStyleOverlayFilename() const { return *m_rc_styleoverlayfile; } const std::string &getMenuFilename() const { return *m_rc_menufile; } - const std::string &getSlitlistFilename() const { return *m_rc_slitlistfile; } const std::string &getAppsFilename() const { return *m_rc_appsfile; } const std::string &getKeysFilename() const { return *m_rc_keyfile; } int colorsPerChannel() const { return *m_rc_colors_per_channel; } @@ -242,7 +241,7 @@ private: m_rc_tabs_padding; FbTk::StringResource m_rc_stylefile, m_rc_styleoverlayfile, - m_rc_menufile, m_rc_keyfile, m_rc_slitlistfile, + m_rc_menufile, m_rc_keyfile, m_rc_appsfile; -- cgit v0.11.2