1 /***************************************************************************
2 * Copyright (C) 2008-2021 by Andrzej Rybczak *
3 * andrzej@rybczak.net *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
19 ***************************************************************************/
20
21 #include "screens/outputs.h"
22
23 #ifdef ENABLE_OUTPUTS
24
25 #include "curses/menu_impl.h"
26 #include "charset.h"
27 #include "display.h"
28 #include "global.h"
29 #include "helpers.h"
30 #include "settings.h"
31 #include "status.h"
32 #include "statusbar.h"
33 #include "title.h"
34 #include "screens/screen_switcher.h"
35
36 using Global::MainHeight;
37 using Global::MainStartY;
38 using Global::myScreen;
39
40 Outputs *myOutputs;
41
Outputs()42 Outputs::Outputs()
43 : Screen(NC::Menu<MPD::Output>(0, MainStartY, COLS, MainHeight, "", Config.main_color, NC::Border()))
44 {
45 w.cyclicScrolling(Config.use_cyclic_scrolling);
46 w.centeredCursor(Config.centered_cursor);
47 setHighlightFixes(w);
48 w.setItemDisplayer([](NC::Menu<MPD::Output> &menu) {
49 auto &output = menu.drawn()->value();
50 if (output.enabled())
51 menu << NC::Format::Bold;
52 menu << Charset::utf8ToLocale(output.name());
53 if (output.enabled())
54 menu << NC::Format::NoBold;
55 });
56 }
57
switchTo()58 void Outputs::switchTo()
59 {
60 SwitchTo::execute(this);
61 drawHeader();
62 }
63
resize()64 void Outputs::resize()
65 {
66 size_t x_offset, width;
67 getWindowResizeParams(x_offset, width);
68 w.resize(width, MainHeight);
69 w.moveTo(x_offset, MainStartY);
70 hasToBeResized = 0;
71 }
72
title()73 std::wstring Outputs::title()
74 {
75 return L"Outputs";
76 }
77
mouseButtonPressed(MEVENT me)78 void Outputs::mouseButtonPressed(MEVENT me)
79 {
80 if (w.empty() || !w.hasCoords(me.x, me.y) || size_t(me.y) >= w.size())
81 return;
82 if (me.bstate & BUTTON1_PRESSED || me.bstate & BUTTON3_PRESSED)
83 {
84 w.Goto(me.y);
85 if (me.bstate & BUTTON3_PRESSED)
86 toggleOutput();
87 }
88 else
89 Screen<WindowType>::mouseButtonPressed(me);
90 }
91
fetchList()92 void Outputs::fetchList()
93 {
94 w.clear();
95 for (MPD::OutputIterator out = Mpd.GetOutputs(), end; out != end; ++out)
96 w.addItem(std::move(*out));
97 }
98
toggleOutput()99 void Outputs::toggleOutput()
100 {
101 if (w.current()->value().enabled())
102 {
103 Mpd.DisableOutput(w.choice());
104 Statusbar::printf("Output \"%s\" disabled", w.current()->value().name());
105 }
106 else
107 {
108 Mpd.EnableOutput(w.choice());
109 Statusbar::printf("Output \"%s\" enabled", w.current()->value().name());
110 }
111 }
112
113 #endif // ENABLE_OUTPUTS
114