From cb42b13e5dfcbebad6ac6d6cf757870b7b8a65ae 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 24e086c..6ddc74c 100644 --- a/src/Screen.cc +++ b/src/Screen.cc @@ -480,8 +480,7 @@ BScreen::BScreen(FbTk::ResourceManager_base &rm, changeWorkspaceID(first_desktop); #ifdef USE_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 // USE_SLIT XFlush(disp); diff --git a/src/Slit.cc b/src/Slit.cc index 6b4344e..210c2a5 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 @@ -245,11 +305,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(); } @@ -802,7 +857,6 @@ void Slit::reposition() { void Slit::shutdown() { - saveClientList(); while (!m_client_list.empty()) removeClient(m_client_list.front(), true, true); } @@ -826,6 +880,8 @@ void Slit::clientUp(SlitClient* client) { break; } } + + saveClientList(); } void Slit::clientDown(SlitClient* client) { @@ -847,6 +903,8 @@ void Slit::clientDown(SlitClient* client) { break; } } + + saveClientList(); } void Slit::cycleClientsUp() { @@ -859,6 +917,8 @@ void Slit::cycleClientsUp() { m_client_list.erase(it); m_client_list.push_back(client); reconfigure(); + + saveClientList(); } void Slit::cycleClientsDown() { @@ -870,6 +930,8 @@ void Slit::cycleClientsDown() { m_client_list.remove(client); m_client_list.push_front(client); reconfigure(); + + saveClientList(); } void Slit::handleEvent(XEvent &event) { @@ -1000,40 +1062,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; @@ -1057,30 +1085,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 8e9c543..177e985 100644 --- a/src/fluxbox.cc +++ b/src/fluxbox.cc @@ -251,7 +251,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"), @@ -1183,13 +1182,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 2e0aabb..97ea211 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; } @@ -241,7 +240,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