aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPavel Labath <pavelo@centrum.sk>2011-08-02 11:56:31 (GMT)
committerPaul Tagliamonte <paultag@fluxbox.org>2012-04-07 02:13:12 (GMT)
commit224ac66f4f932ea05bec55a7e4a1f1e6c47fea50 (patch)
tree5585e75f941cff362da71d1d1224a4827b9a6ebc
parentfb697e49027f050c00e098f24898b17cfe639c5f (diff)
downloadfluxbox_paul-224ac66f4f932ea05bec55a7e4a1f1e6c47fea50.zip
fluxbox_paul-224ac66f4f932ea05bec55a7e4a1f1e6c47fea50.tar.bz2
Automatically save init file when a resource is modified
previously, init was autosaved only when a resource was modified using the menu. I have also included modifications through lua code. To avoid wasting resources, the file is not saved immediately, but with a 5 second delay (to enable saving a bunch of changes in one go)
-rw-r--r--src/FbTk/LResource.cc21
-rw-r--r--src/FbTk/LResource.hh11
-rw-r--r--src/FbTk/Resource.hh11
-rw-r--r--src/Screen.cc49
-rw-r--r--src/fluxbox.cc2
5 files changed, 59 insertions, 35 deletions
diff --git a/src/FbTk/LResource.cc b/src/FbTk/LResource.cc
index 31362bd..de90b95 100644
--- a/src/FbTk/LResource.cc
+++ b/src/FbTk/LResource.cc
@@ -26,8 +26,11 @@
26 26
27#include "I18n.hh" 27#include "I18n.hh"
28#include "LuaUtil.hh" 28#include "LuaUtil.hh"
29#include "MemFun.hh"
29#include "Resource.hh" 30#include "Resource.hh"
30 31
32// defined in LResourceHelper-lua.cc
33// contains the compiled code of LResourceHelper.lua
31extern const char LResourceHelper[]; 34extern const char LResourceHelper[];
32extern const unsigned int LResourceHelper_size; 35extern const unsigned int LResourceHelper_size;
33 36
@@ -101,8 +104,15 @@ void LResourceManager::convert(ResourceManager &old, const std::string &new_file
101 new_rm.save(new_file.c_str(), NULL); 104 new_rm.save(new_file.c_str(), NULL);
102} 105}
103 106
104LResourceManager::LResourceManager(const std::string &root, Lua &l) 107LResourceManager::LResourceManager(const std::string &root, Lua &l, unsigned int autosave)
105 : ResourceManager_base(root), m_l(&l) { 108 : ResourceManager_base(root), m_l(&l) {
109
110 m_savetimer.setInterval(autosave);
111 m_savetimer.fireOnce(true);
112 m_savetimer.setFunctor( MemFunBind(*this, &LResourceManager::save,
113 static_cast<const char *>(NULL), static_cast<const char *>(NULL)
114 ) );
115
106 setLua(l); 116 setLua(l);
107} 117}
108 118
@@ -143,6 +153,8 @@ bool LResourceManager::save(const char *filename, const char *) {
143 if(filename == NULL) 153 if(filename == NULL)
144 filename = m_filename.c_str(); 154 filename = m_filename.c_str();
145 155
156 std::cerr << "XXX SAVING " << filename << std::endl;
157
146 m_l->getfield(lua::REGISTRYINDEX, dump_resources); 158 m_l->getfield(lua::REGISTRYINDEX, dump_resources);
147 m_l->getfield(lua::GLOBALSINDEX, m_root.c_str()); 159 m_l->getfield(lua::GLOBALSINDEX, m_root.c_str());
148 m_l->pushstring(filename); 160 m_l->pushstring(filename);
@@ -194,6 +206,11 @@ void LResourceManager::doRemoveResource(Resource_base &r) {
194 m_l->pop(); 206 m_l->pop();
195} 207}
196 208
209void LResourceManager::resourceChanged(Resource_base &r) {
210 if(! m_savetimer.isTiming())
211 m_savetimer.start();
212}
213
197void LResourceManager::setLua(Lua &l) { 214void LResourceManager::setLua(Lua &l) {
198 l.checkstack(2); 215 l.checkstack(2);
199 lua::stack_sentry s(l); 216 lua::stack_sentry s(l);
diff --git a/src/FbTk/LResource.hh b/src/FbTk/LResource.hh
index f0f8050..30f0e02 100644
--- a/src/FbTk/LResource.hh
+++ b/src/FbTk/LResource.hh
@@ -28,6 +28,7 @@
28#include <string> 28#include <string>
29 29
30#include "Resource.hh" 30#include "Resource.hh"
31#include "Timer.hh"
31 32
32namespace FbTk { 33namespace FbTk {
33 34
@@ -37,11 +38,18 @@ class LResourceManager: public ResourceManager_base {
37public: 38public:
38 static void convert(ResourceManager &old, const std::string &new_file); 39 static void convert(ResourceManager &old, const std::string &new_file);
39 40
40 LResourceManager(const std::string &root, Lua &l); 41 /**
42 * @param root the name of the table where settings will reside
43 * @param l lua context
44 * @param autosave delay (in seconds) for automatic saving of resources. Modifying a resource
45 * starts a timer. If another resource is modified, the timer is restarted. 0 = disabled
46 */
47 LResourceManager(const std::string &root, Lua &l, unsigned int autosave = 0);
41 void load(const std::string &filename, const std::string &fallback); 48 void load(const std::string &filename, const std::string &fallback);
42 virtual bool save(const char *filename, const char *); 49 virtual bool save(const char *filename, const char *);
43 virtual void addResource(Resource_base &r); 50 virtual void addResource(Resource_base &r);
44 virtual void removeResource(Resource_base &r); 51 virtual void removeResource(Resource_base &r);
52 virtual void resourceChanged(Resource_base &r);
45 void setLua(Lua &l); 53 void setLua(Lua &l);
46 54
47private: 55private:
@@ -50,6 +58,7 @@ private:
50 58
51 Lua *m_l; 59 Lua *m_l;
52 std::string m_filename; 60 std::string m_filename;
61 Timer m_savetimer;
53}; 62};
54 63
55} // end namespace FbTk 64} // end namespace FbTk
diff --git a/src/FbTk/Resource.hh b/src/FbTk/Resource.hh
index 7a797a1..c4a7951 100644
--- a/src/FbTk/Resource.hh
+++ b/src/FbTk/Resource.hh
@@ -96,8 +96,6 @@ public:
96 /// @return true on success 96 /// @return true on success
97 virtual bool save(const char *filename, const char *mergefilename=0) = 0; 97 virtual bool save(const char *filename, const char *mergefilename=0) = 0;
98 98
99
100
101 /// Add resource to list, only used in Resource<T> 99 /// Add resource to list, only used in Resource<T>
102 virtual void addResource(Resource_base &r); 100 virtual void addResource(Resource_base &r);
103 101
@@ -106,6 +104,9 @@ public:
106 m_resourcelist.remove(&r); 104 m_resourcelist.remove(&r);
107 } 105 }
108 106
107 /// Called by Resources when their value changes
108 virtual void resourceChanged(Resource_base &r) = 0;
109
109 /// searches for the resource with the resourcename 110 /// searches for the resource with the resourcename
110 /// @return pointer to resource base on success, else 0. 111 /// @return pointer to resource base on success, else 0.
111 Resource_base *findResource(const std::string &resourcename); 112 Resource_base *findResource(const std::string &resourcename);
@@ -154,6 +155,8 @@ public:
154 /// Add resource to list, only used in Resource<T> 155 /// Add resource to list, only used in Resource<T>
155 virtual void addResource(Resource_base &r); 156 virtual void addResource(Resource_base &r);
156 157
158 virtual void resourceChanged(Resource_base &r) {};
159
157 // this marks the database as "in use" and will avoid reloading 160 // this marks the database as "in use" and will avoid reloading
158 // resources unless it is zero. 161 // resources unless it is zero.
159 // It returns this resource manager. Useful for passing to 162 // It returns this resource manager. Useful for passing to
@@ -211,12 +214,14 @@ public:
211 214
212 void setDefaultValue() { 215 void setDefaultValue() {
213 m_value = m_defaultval; 216 m_value = m_defaultval;
217 m_rm.resourceChanged(*this);
214 m_modified_sig.emit(m_value); 218 m_modified_sig.emit(m_value);
215 } 219 }
216 /// sets resource from string, specialized, must be implemented 220 /// sets resource from string, specialized, must be implemented
217 void setFromString(const char *strval) { 221 void setFromString(const char *strval) {
218 try { 222 try {
219 m_value = Traits::fromString(strval); 223 m_value = Traits::fromString(strval);
224 m_rm.resourceChanged(*this);
220 m_modified_sig.emit(m_value); 225 m_modified_sig.emit(m_value);
221 } 226 }
222 catch(ConversionError &e) { 227 catch(ConversionError &e) {
@@ -226,6 +231,7 @@ public:
226 } 231 }
227 Accessor<T> &operator =(const T& newvalue) { 232 Accessor<T> &operator =(const T& newvalue) {
228 m_value = newvalue; 233 m_value = newvalue;
234 m_rm.resourceChanged(*this);
229 m_modified_sig.emit(m_value); 235 m_modified_sig.emit(m_value);
230 return *this; 236 return *this;
231 } 237 }
@@ -236,6 +242,7 @@ public:
236 virtual void setFromLua(lua::state &l) { 242 virtual void setFromLua(lua::state &l) {
237 try { 243 try {
238 m_value = Traits::fromLua(l); 244 m_value = Traits::fromLua(l);
245 m_rm.resourceChanged(*this);
239 m_modified_sig.emit(m_value); 246 m_modified_sig.emit(m_value);
240 } 247 }
241 catch(ConversionError &e) { 248 catch(ConversionError &e) {
diff --git a/src/Screen.cc b/src/Screen.cc
index e504a9b..250857b 100644
--- a/src/Screen.cc
+++ b/src/Screen.cc
@@ -184,9 +184,8 @@ int calcSquareDistance(int x1, int y1, int x2, int y2) {
184class TabPlacementMenuItem: public FbTk::RadioMenuItem { 184class TabPlacementMenuItem: public FbTk::RadioMenuItem {
185public: 185public:
186 TabPlacementMenuItem(const FbTk::FbString & label, BScreen &screen, 186 TabPlacementMenuItem(const FbTk::FbString & label, BScreen &screen,
187 FbWinFrame::TabPlacement place, 187 FbWinFrame::TabPlacement place):
188 FbTk::RefCount<FbTk::Command<void> > &cmd): 188 FbTk::RadioMenuItem(label),
189 FbTk::RadioMenuItem(label, cmd),
190 m_screen(screen), 189 m_screen(screen),
191 m_place(place) { 190 m_place(place) {
192 setCloseOnClick(false); 191 setCloseOnClick(false);
@@ -1491,15 +1490,8 @@ void BScreen::setupConfigmenu(FbTk::Menu &menu) {
1491 1490
1492 menu.removeAll(); 1491 menu.removeAll();
1493 1492
1494 FbTk::MacroCommand *s_a_reconf_macro = new FbTk::MacroCommand();
1495 FbTk::RefCount<FbTk::Command<void> > saverc_cmd(new FbTk::SimpleCommand<Fluxbox>(
1496 *Fluxbox::instance(),
1497 &Fluxbox::save_rc));
1498 FbTk::RefCount<FbTk::Command<void> > reconf_cmd(FbTk::CommandParser<void>::instance().parse("reconfigure")); 1493 FbTk::RefCount<FbTk::Command<void> > reconf_cmd(FbTk::CommandParser<void>::instance().parse("reconfigure"));
1499 1494
1500 s_a_reconf_macro->add(saverc_cmd);
1501 s_a_reconf_macro->add(reconf_cmd);
1502 FbTk::RefCount<FbTk::Command<void> > save_and_reconfigure(s_a_reconf_macro);
1503 // create focus menu 1495 // create focus menu
1504 // we don't set this to internal menu so will 1496 // we don't set this to internal menu so will
1505 // be deleted toghether with the parent 1497 // be deleted toghether with the parent
@@ -1508,12 +1500,12 @@ void BScreen::setupConfigmenu(FbTk::Menu &menu) {
1508 "Method used to give focus to windows"); 1500 "Method used to give focus to windows");
1509 FbTk::RefCount<FbTk::Menu> focus_menu( createMenu(focusmenu_label) ); 1501 FbTk::RefCount<FbTk::Menu> focus_menu( createMenu(focusmenu_label) );
1510 1502
1511#define _BOOLITEM(m,a, b, c, d, e, f) (m).insert(new FbTk::BoolMenuItem(_FB_XTEXT(a, b, c, d), e, f)) 1503#define _BOOLITEM(m,a, b, c, d, e) (m).insert(new FbTk::BoolMenuItem(_FB_XTEXT(a, b, c, d), e))
1512 1504
1513 1505
1514#define _FOCUSITEM(a, b, c, d, e) \ 1506#define _FOCUSITEM(a, b, c, d, e) \
1515 focus_menu->insert(new FocusModelMenuItem(_FB_XTEXT(a, b, c, d), focusControl(), \ 1507 focus_menu->insert(new FocusModelMenuItem(_FB_XTEXT(a, b, c, d), focusControl(), \
1516 e, save_and_reconfigure)) 1508 e, reconf_cmd))
1517 1509
1518 _FOCUSITEM(Configmenu, ClickFocus, 1510 _FOCUSITEM(Configmenu, ClickFocus,
1519 "Click To Focus", "Click to focus", 1511 "Click To Focus", "Click to focus",
@@ -1531,10 +1523,10 @@ void BScreen::setupConfigmenu(FbTk::Menu &menu) {
1531 focus_menu->insert(new FbTk::MenuSeparator()); 1523 focus_menu->insert(new FbTk::MenuSeparator());
1532 focus_menu->insert(new TabFocusModelMenuItem(_FB_XTEXT(Configmenu, 1524 focus_menu->insert(new TabFocusModelMenuItem(_FB_XTEXT(Configmenu,
1533 ClickTabFocus, "ClickTabFocus", "Click tab to focus windows"), 1525 ClickTabFocus, "ClickTabFocus", "Click tab to focus windows"),
1534 focusControl(), FocusControl::CLICKTABFOCUS, save_and_reconfigure)); 1526 focusControl(), FocusControl::CLICKTABFOCUS, reconf_cmd));
1535 focus_menu->insert(new TabFocusModelMenuItem(_FB_XTEXT(Configmenu, 1527 focus_menu->insert(new TabFocusModelMenuItem(_FB_XTEXT(Configmenu,
1536 MouseTabFocus, "MouseTabFocus", "Hover over tab to focus windows"), 1528 MouseTabFocus, "MouseTabFocus", "Hover over tab to focus windows"),
1537 focusControl(), FocusControl::MOUSETABFOCUS, save_and_reconfigure)); 1529 focusControl(), FocusControl::MOUSETABFOCUS, reconf_cmd));
1538 focus_menu->insert(new FbTk::MenuSeparator()); 1530 focus_menu->insert(new FbTk::MenuSeparator());
1539 1531
1540 try { 1532 try {
@@ -1548,10 +1540,10 @@ void BScreen::setupConfigmenu(FbTk::Menu &menu) {
1548 1540
1549 _BOOLITEM(*focus_menu, Configmenu, AutoRaise, 1541 _BOOLITEM(*focus_menu, Configmenu, AutoRaise,
1550 "Auto Raise", "Auto Raise windows on sloppy", 1542 "Auto Raise", "Auto Raise windows on sloppy",
1551 resource.auto_raise, saverc_cmd); 1543 resource.auto_raise);
1552 _BOOLITEM(*focus_menu, Configmenu, ClickRaises, 1544 _BOOLITEM(*focus_menu, Configmenu, ClickRaises,
1553 "Click Raises", "Click Raises", 1545 "Click Raises", "Click Raises",
1554 resource.click_raises, saverc_cmd); 1546 resource.click_raises);
1555 1547
1556 focus_menu->updateMenu(); 1548 focus_menu->updateMenu();
1557 1549
@@ -1567,17 +1559,17 @@ void BScreen::setupConfigmenu(FbTk::Menu &menu) {
1567 1559
1568 _BOOLITEM(*maxmenu, Configmenu, FullMax, 1560 _BOOLITEM(*maxmenu, Configmenu, FullMax,
1569 "Full Maximization", "Maximise over slit, toolbar, etc", 1561 "Full Maximization", "Maximise over slit, toolbar, etc",
1570 resource.full_max, saverc_cmd); 1562 resource.full_max);
1571 _BOOLITEM(*maxmenu, Configmenu, MaxIgnoreInc, 1563 _BOOLITEM(*maxmenu, Configmenu, MaxIgnoreInc,
1572 "Ignore Resize Increment", 1564 "Ignore Resize Increment",
1573 "Maximizing Ignores Resize Increment (e.g. xterm)", 1565 "Maximizing Ignores Resize Increment (e.g. xterm)",
1574 resource.max_ignore_inc, saverc_cmd); 1566 resource.max_ignore_inc);
1575 _BOOLITEM(*maxmenu, Configmenu, MaxDisableMove, 1567 _BOOLITEM(*maxmenu, Configmenu, MaxDisableMove,
1576 "Disable Moving", "Don't Allow Moving While Maximized", 1568 "Disable Moving", "Don't Allow Moving While Maximized",
1577 resource.max_disable_move, saverc_cmd); 1569 resource.max_disable_move);
1578 _BOOLITEM(*maxmenu, Configmenu, MaxDisableResize, 1570 _BOOLITEM(*maxmenu, Configmenu, MaxDisableResize,
1579 "Disable Resizing", "Don't Allow Resizing While Maximized", 1571 "Disable Resizing", "Don't Allow Resizing While Maximized",
1580 resource.max_disable_resize, saverc_cmd); 1572 resource.max_disable_resize);
1581 1573
1582 maxmenu->updateMenu(); 1574 maxmenu->updateMenu();
1583 menu.insert(maxmenu_label, maxmenu); 1575 menu.insert(maxmenu_label, maxmenu);
@@ -1597,13 +1589,13 @@ void BScreen::setupConfigmenu(FbTk::Menu &menu) {
1597 1589
1598 _BOOLITEM(*tab_menu,Configmenu, TabsInTitlebar, 1590 _BOOLITEM(*tab_menu,Configmenu, TabsInTitlebar,
1599 "Tabs in Titlebar", "Tabs in Titlebar", 1591 "Tabs in Titlebar", "Tabs in Titlebar",
1600 resource.default_internal_tabs, saverc_cmd); 1592 resource.default_internal_tabs);
1601 tab_menu->insert(new FbTk::BoolMenuItem(_FB_XTEXT(Common, MaximizeOver, 1593 tab_menu->insert(new FbTk::BoolMenuItem(_FB_XTEXT(Common, MaximizeOver,
1602 "Maximize Over", "Maximize over this thing when maximizing"), 1594 "Maximize Over", "Maximize over this thing when maximizing"),
1603 resource.max_over_tabs, save_and_reconfigure)); 1595 resource.max_over_tabs, reconf_cmd));
1604 tab_menu->insert(new FbTk::BoolMenuItem(_FB_XTEXT(Toolbar, ShowIcons, 1596 tab_menu->insert(new FbTk::BoolMenuItem(_FB_XTEXT(Toolbar, ShowIcons,
1605 "Show Pictures", "chooses if little icons are shown next to title in the iconbar"), 1597 "Show Pictures", "chooses if little icons are shown next to title in the iconbar"),
1606 resource.tabs_use_pixmap, save_and_reconfigure)); 1598 resource.tabs_use_pixmap, reconf_cmd));
1607 1599
1608 FbTk::MenuItem *tab_width_item = 1600 FbTk::MenuItem *tab_width_item =
1609 new FbTk::IntMenuItem(_FB_XTEXT(Configmenu, ExternalTabWidth, 1601 new FbTk::IntMenuItem(_FB_XTEXT(Configmenu, ExternalTabWidth,
@@ -1611,7 +1603,6 @@ void BScreen::setupConfigmenu(FbTk::Menu &menu) {
1611 "Width of external-style tabs"), 1603 "Width of external-style tabs"),
1612 resource.tab_width, 10, 3000, /* silly number */ 1604 resource.tab_width, 10, 3000, /* silly number */
1613 *tab_menu); 1605 *tab_menu);
1614 tab_width_item->setCommand(saverc_cmd);
1615 tab_menu->insert(tab_width_item); 1606 tab_menu->insert(tab_width_item);
1616 1607
1617 // menu is 3 wide, 5 down 1608 // menu is 3 wide, 5 down
@@ -1646,7 +1637,7 @@ void BScreen::setupConfigmenu(FbTk::Menu &menu) {
1646 tabplacement_menu->insert(p.label); 1637 tabplacement_menu->insert(p.label);
1647 tabplacement_menu->setItemEnabled(i, false); 1638 tabplacement_menu->setItemEnabled(i, false);
1648 } else 1639 } else
1649 tabplacement_menu->insert(new TabPlacementMenuItem(p.label, *this, p.placement, saverc_cmd)); 1640 tabplacement_menu->insert(new TabPlacementMenuItem(p.label, *this, p.placement));
1650 } 1641 }
1651 tabplacement_menu->updateMenu(); 1642 tabplacement_menu->updateMenu();
1652 1643
@@ -1665,13 +1656,13 @@ void BScreen::setupConfigmenu(FbTk::Menu &menu) {
1665 alpha_menu->insert(new FbTk::BoolMenuItem(_FB_XTEXT(Configmenu, ForcePseudoTrans, 1656 alpha_menu->insert(new FbTk::BoolMenuItem(_FB_XTEXT(Configmenu, ForcePseudoTrans,
1666 "Force Pseudo-Transparency", 1657 "Force Pseudo-Transparency",
1667 "When composite is available, still use old pseudo-transparency"), 1658 "When composite is available, still use old pseudo-transparency"),
1668 Fluxbox::instance()->getPseudoTransResource(), save_and_reconfigure)); 1659 Fluxbox::instance()->getPseudoTransResource(), reconf_cmd));
1669 } 1660 }
1670 1661
1671 // in order to save system resources, don't save or reconfigure alpha 1662 // in order to save system resources, don't save or reconfigure alpha
1672 // settings until after the user is done changing them 1663 // settings until after the user is done changing them
1673 FbTk::RefCount<FbTk::Command<void> > delayed_save_and_reconf( 1664 FbTk::RefCount<FbTk::Command<void> > delayed_save_and_reconf(
1674 new FbTk::DelayedCmd(save_and_reconfigure)); 1665 new FbTk::DelayedCmd(reconf_cmd));
1675 1666
1676 FbTk::MenuItem *focused_alpha_item = 1667 FbTk::MenuItem *focused_alpha_item =
1677 new FbTk::IntMenuItem(_FB_XTEXT(Configmenu, FocusedAlpha, 1668 new FbTk::IntMenuItem(_FB_XTEXT(Configmenu, FocusedAlpha,
@@ -1711,11 +1702,11 @@ void BScreen::setupConfigmenu(FbTk::Menu &menu) {
1711 _BOOLITEM(menu, Configmenu, OpaqueMove, 1702 _BOOLITEM(menu, Configmenu, OpaqueMove,
1712 "Opaque Window Moving", 1703 "Opaque Window Moving",
1713 "Window Moving with whole window visible (as opposed to outline moving)", 1704 "Window Moving with whole window visible (as opposed to outline moving)",
1714 resource.opaque_move, saverc_cmd); 1705 resource.opaque_move);
1715 _BOOLITEM(menu, Configmenu, WorkspaceWarping, 1706 _BOOLITEM(menu, Configmenu, WorkspaceWarping,
1716 "Workspace Warping", 1707 "Workspace Warping",
1717 "Workspace Warping - dragging windows to the edge and onto the next workspace", 1708 "Workspace Warping - dragging windows to the edge and onto the next workspace",
1718 resource.workspace_warping, saverc_cmd); 1709 resource.workspace_warping);
1719 1710
1720#undef _BOOLITEM 1711#undef _BOOLITEM
1721 1712
diff --git a/src/fluxbox.cc b/src/fluxbox.cc
index a26fd66..75f38d7 100644
--- a/src/fluxbox.cc
+++ b/src/fluxbox.cc
@@ -230,7 +230,7 @@ Fluxbox::Fluxbox(int argc, char **argv,
230 : FbTk::App(dpy_name.c_str()), 230 : FbTk::App(dpy_name.c_str()),
231 m_l(new Lua), 231 m_l(new Lua),
232 m_fbatoms(FbAtoms::instance()), 232 m_fbatoms(FbAtoms::instance()),
233 m_resourcemanager("session", *m_l), 233 m_resourcemanager("session", *m_l, 5),
234 234
235 m_RC_PATH(rc_path), 235 m_RC_PATH(rc_path),
236 m_RC_INIT_FILE("init"), 236 m_RC_INIT_FILE("init"),