Skip to content

Commit 4446242

Browse files
committed
actions: add action for environment update
1 parent c53e98e commit 4446242

5 files changed

Lines changed: 71 additions & 31 deletions

File tree

NEWS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ ncmpcpp-0.7 (????-??-??)
2222
* NCurses terminal sequence escaping is no longer used as it's not accurate enough.
2323
* Selecting items no longer depends on space mode and is bound by default to Insert key.
2424
* Support for Alt, Ctrl and Shift modifiers as well as Escape key was added.
25+
* Action that updates the environment can now be used in bindings configuration file.
2526

2627
ncmpcpp-0.6.4 (2015-05-02)
2728

doc/bindings

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,13 @@
114114
## could be used. Then ncmpcpp will not wait for confirmation
115115
## (enter) and will execute the command the moment it sees it.
116116
##
117+
## Note: while command chains are executed, internal environment
118+
## update (which includes current window refresh and mpd status
119+
## update) is not performed for performance reasons. However, it
120+
## may be desirable to do so in some situration. Therefore it's
121+
## possible to invoke by hand by performing 'update enviroment'
122+
## action.
123+
##
117124
## Note: There is a difference between:
118125
##
119126
## def_key "key"

src/actions.cpp

Lines changed: 41 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -190,37 +190,37 @@ void resizeScreen(bool reload_main_window)
190190
using Global::MainHeight;
191191
using Global::wHeader;
192192
using Global::wFooter;
193-
193+
194194
// update internal screen dimensions
195195
if (reload_main_window)
196196
{
197197
rl_resize_terminal();
198198
endwin();
199199
refresh();
200200
}
201-
201+
202202
MainHeight = LINES-(Config.design == Design::Alternative ? 7 : 4);
203-
203+
204204
validateScreenSize();
205-
205+
206206
if (!Config.header_visibility)
207207
MainHeight += 2;
208208
if (!Config.statusbar_visibility)
209209
++MainHeight;
210-
210+
211211
setResizeFlags();
212-
212+
213213
applyToVisibleWindows(&BaseScreen::resize);
214-
214+
215215
if (Config.header_visibility || Config.design == Design::Alternative)
216216
wHeader->resize(COLS, HeaderHeight);
217-
217+
218218
FooterStartY = LINES-(Config.statusbar_visibility ? 2 : 1);
219219
wFooter->moveTo(0, FooterStartY);
220220
wFooter->resize(COLS, Config.statusbar_visibility ? 2 : 1);
221-
221+
222222
applyToVisibleWindows(&BaseScreen::refresh);
223-
223+
224224
Status::Changes::elapsedTime(false);
225225
Status::Changes::playerState();
226226
// Note: routines for drawing separator if alternative user
@@ -301,6 +301,36 @@ BaseAction *get(const std::string &name)
301301
return result;
302302
}
303303

304+
UpdateEnvironment::UpdateEnvironment()
305+
: BaseAction(Type::UpdateEnvironment, "update_environment")
306+
, m_past(boost::posix_time::from_time_t(0))
307+
{ }
308+
309+
void UpdateEnvironment::run(bool update_timer, bool refresh_window)
310+
{
311+
using Global::Timer;
312+
313+
// update timer, status if necessary etc.
314+
Status::trace(update_timer, true);
315+
316+
// header stuff
317+
if ((myScreen == myPlaylist || myScreen == myBrowser || myScreen == myLyrics)
318+
&& (Timer - m_past > boost::posix_time::milliseconds(500))
319+
)
320+
{
321+
drawHeader();
322+
m_past = Timer;
323+
}
324+
325+
if (refresh_window)
326+
myScreen->refreshWindow();
327+
}
328+
329+
void UpdateEnvironment::run()
330+
{
331+
run(true, true);
332+
}
333+
304334
bool MouseEvent::canBeRun()
305335
{
306336
return Config.mouse_support;
@@ -2427,6 +2457,7 @@ void populateActions()
24272457
AvailableActions[static_cast<size_t>(a->type())] = a;
24282458
};
24292459
insert_action(new Actions::Dummy());
2460+
insert_action(new Actions::UpdateEnvironment());
24302461
insert_action(new Actions::MouseEvent());
24312462
insert_action(new Actions::ScrollUp());
24322463
insert_action(new Actions::ScrollDown());

src/actions.h

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#ifndef NCMPCPP_ACTIONS_H
2222
#define NCMPCPP_ACTIONS_H
2323

24+
#include <boost/date_time/posix_time/posix_time_types.hpp>
2425
#include <boost/format.hpp>
2526
#include <map>
2627
#include <string>
@@ -34,7 +35,7 @@ namespace Actions {
3435
enum class Type
3536
{
3637
MacroUtility = 0,
37-
Dummy, MouseEvent, ScrollUp, ScrollDown, ScrollUpArtist, ScrollUpAlbum,
38+
Dummy, UpdateEnvironment, MouseEvent, ScrollUp, ScrollDown, ScrollUpArtist, ScrollUpAlbum,
3839
ScrollDownArtist, ScrollDownAlbum, PageUp, PageDown, MoveHome, MoveEnd,
3940
ToggleInterface, JumpToParentDirectory, PressEnter, PressSpace, PreviousColumn,
4041
NextColumn, MasterScreen, SlaveScreen, VolumeUp, VolumeDown, DeletePlaylistItems,
@@ -122,6 +123,18 @@ struct Dummy: BaseAction
122123
virtual void run() OVERRIDE { }
123124
};
124125

126+
struct UpdateEnvironment: BaseAction
127+
{
128+
UpdateEnvironment();
129+
130+
void run(bool update_status, bool refresh_window);
131+
132+
private:
133+
boost::posix_time::ptime m_past;
134+
135+
virtual void run() OVERRIDE;
136+
};
137+
125138
struct MouseEvent: BaseAction
126139
{
127140
MouseEvent(): BaseAction(Type::MouseEvent, "mouse_event")
@@ -134,9 +147,8 @@ struct MouseEvent: BaseAction
134147
virtual bool canBeRun() OVERRIDE;
135148
virtual void run() OVERRIDE;
136149

137-
private:
138-
MEVENT m_mouse_event;
139-
MEVENT m_old_mouse_event;
150+
MEVENT m_mouse_event;
151+
MEVENT m_old_mouse_event;
140152
};
141153

142154
struct ScrollUp: BaseAction

src/ncmpcpp.cpp

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,9 @@ int main(int argc, char **argv)
162162
bool key_pressed = false;
163163
auto input = NC::Key::None;
164164
auto connect_attempt = boost::posix_time::from_time_t(0);
165-
auto past = boost::posix_time::from_time_t(0);
165+
auto update_environment = static_cast<Actions::UpdateEnvironment &>(
166+
Actions::get(Actions::Type::UpdateEnvironment)
167+
);
166168

167169
while (!Actions::ExitMainLoop)
168170
{
@@ -189,30 +191,17 @@ int main(int argc, char **argv)
189191
Status::handleClientError(e);
190192
}
191193
}
192-
193-
// update timer, status if necessary etc.
194-
Status::trace(!key_pressed, true);
195194

196195
if (run_resize_screen)
197196
{
198197
Actions::resizeScreen(true);
199198
run_resize_screen = false;
200199
}
201-
202-
// header stuff
203-
if ((myScreen == myPlaylist || myScreen == myBrowser || myScreen == myLyrics)
204-
&& (Timer - past > boost::posix_time::milliseconds(500))
205-
)
206-
{
207-
drawHeader();
208-
past = Timer;
209-
}
210-
211-
if (key_pressed)
212-
myScreen->refreshWindow();
200+
201+
update_environment.run(!key_pressed, key_pressed);
202+
213203
input = readKey(*wFooter);
214204
key_pressed = input != NC::Key::None;
215-
216205
if (!key_pressed)
217206
continue;
218207

0 commit comments

Comments
 (0)