move code responsible for screen resize to SIGWINCH handler
[ncmpcpp.git] / src / server_info.cpp
blobf4818402064cb27b0b14143855a7e2d311ee713c
1 /***************************************************************************
2 * Copyright (C) 2008-2009 by Andrzej Rybczak *
3 * electricityispower@gmail.com *
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 ***************************************************************************/
21 #include <iomanip>
23 #include "global.h"
24 #include "server_info.h"
26 using Global::MainHeight;
27 using Global::MainStartY;
28 using Global::myScreen;
29 using Global::myOldScreen;
31 ServerInfo *myServerInfo = new ServerInfo;
33 void ServerInfo::Init()
35 SetDimensions();
36 w = new Scrollpad((COLS-itsWidth)/2, (MainHeight-itsHeight)/2+MainStartY, itsWidth, itsHeight, "MPD server info", Config.main_color, Config.window_border);
38 Mpd.GetURLHandlers(itsURLHandlers);
39 Mpd.GetTagTypes(itsTagTypes);
41 isInitialized = 1;
44 void ServerInfo::SwitchTo()
46 if (myScreen == this)
48 myOldScreen->SwitchTo();
49 return;
51 if (MainHeight < 5)
53 ShowMessage("Screen is too small to display this window!");
54 return;
57 if (!isInitialized)
58 Init();
60 // Resize() can fall back to old screen, so we need it updated
61 myOldScreen = myScreen;
63 if (hasToBeResized)
64 Resize();
66 myScreen = this;
67 w->Window::Clear();
70 void ServerInfo::Resize()
72 SetDimensions();
73 if (itsHeight < 5) // screen too low to display this window
74 return myOldScreen->SwitchTo();
75 w->Resize(itsWidth, itsHeight);
76 w->MoveTo((COLS-itsWidth)/2, (MainHeight-itsHeight)/2+MainStartY);
77 if (myOldScreen && myOldScreen->hasToBeResized) // resize background window
79 myOldScreen->Resize();
80 myOldScreen->Refresh();
82 hasToBeResized = 0;
85 std::basic_string<my_char_t> ServerInfo::Title()
87 return myOldScreen->Title();
90 void ServerInfo::Update()
92 static time_t now = 0, past;
93 time(&past);
94 if (past <= now)
95 return;
96 time(&now);
98 Mpd.UpdateStats();
99 w->Clear(0);
101 *w << fmtBold << U("Version: ") << fmtBoldEnd << U("0.") << std::fixed << std::setprecision(1) << Mpd.Version() << '\n';
102 *w << fmtBold << U("Uptime: ") << fmtBoldEnd;
103 ShowTime(*w, Mpd.Uptime(), 1);
104 *w << '\n';
105 *w << fmtBold << U("Time playing: ") << fmtBoldEnd << MPD::Song::ShowTime(Mpd.PlayTime()) << '\n';
106 *w << '\n';
107 *w << fmtBold << U("Total playtime: ") << fmtBoldEnd;
108 ShowTime(*w, Mpd.DBPlayTime(), 1);
109 *w << '\n';
110 *w << fmtBold << U("Artist names: ") << fmtBoldEnd << Mpd.NumberOfArtists() << '\n';
111 *w << fmtBold << U("Album names: ") << fmtBoldEnd << Mpd.NumberOfAlbums() << '\n';
112 *w << fmtBold << U("Songs in database: ") << fmtBoldEnd << Mpd.NumberOfSongs() << '\n';
113 *w << '\n';
114 *w << fmtBold << U("Last DB update: ") << fmtBoldEnd << Timestamp(Mpd.DBUpdateTime()) << '\n';
115 *w << '\n';
116 *w << fmtBold << U("URL Handlers:") << fmtBoldEnd;
117 for (MPD::TagList::const_iterator it = itsURLHandlers.begin(); it != itsURLHandlers.end(); ++it)
118 *w << (it != itsURLHandlers.begin() ? U(", ") : U(" ")) << *it;
119 *w << U("\n\n");
120 *w << fmtBold << U("Tag Types:") << fmtBoldEnd;
121 for (MPD::TagList::const_iterator it = itsTagTypes.begin(); it != itsTagTypes.end(); ++it)
122 *w << (it != itsTagTypes.begin() ? U(", ") : U(" ")) << *it;
124 w->Flush();
125 w->Refresh();
128 void ServerInfo::SetDimensions()
130 itsWidth = COLS*0.6;
131 itsHeight = std::min(size_t(LINES*0.7), MainHeight);