1 /* ncmpc (Ncurses MPD Client)
2 * (c) 2004-2017 The Music Player Daemon Project
3 * Project homepage: http://musicpd.org
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.
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.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 #include "screen_interface.h"
22 #include "screen_list.h"
23 #include "screen_status.h"
27 #include "mpdclient.h"
29 #include "player_command.h"
30 #include "screen_help.h"
31 #include "screen_queue.h"
32 #include "screen_file.h"
33 #include "screen_artist.h"
34 #include "screen_search.h"
35 #include "screen_song.h"
36 #include "screen_keydef.h"
37 #include "screen_lyrics.h"
38 #include "screen_outputs.h"
39 #include "screen_chat.h"
41 #include <mpd/client.h>
50 struct screen screen
= {
51 .current_page
= &screen_queue
,
54 static const struct screen_functions
*mode_fn_prev
= &screen_queue
;
57 screen_switch(const struct screen_functions
*sf
, struct mpdclient
*c
)
61 if (sf
== screen
.current_page
)
64 mode_fn_prev
= screen
.current_page
;
66 /* close the old mode */
67 if (screen
.current_page
->close
!= NULL
)
68 screen
.current_page
->close();
70 /* get functions for the new mode */
71 screen
.current_page
= sf
;
73 /* open the new mode */
77 screen_paint(c
, true);
81 screen_swap(struct mpdclient
*c
, const struct mpd_song
*song
)
86 { /* just a hack to make the ifdefs less ugly */ }
87 #ifdef ENABLE_SONG_SCREEN
88 if (mode_fn_prev
== &screen_song
)
89 screen_song_switch(c
, song
);
91 #ifdef ENABLE_LYRICS_SCREEN
92 else if (mode_fn_prev
== &screen_lyrics
)
93 screen_lyrics_switch(c
, song
, true);
96 screen_switch(mode_fn_prev
, c
);
99 screen_switch(mode_fn_prev
, c
);
103 find_configured_screen(const char *name
)
107 for (i
= 0; options
.screen_list
[i
] != NULL
; ++i
)
108 if (strcmp(options
.screen_list
[i
], name
) == 0)
115 screen_next_mode(struct mpdclient
*c
, int offset
)
117 int max
= g_strv_length(options
.screen_list
);
119 /* find current screen */
120 int current
= find_configured_screen(screen_get_name(screen
.current_page
));
121 int next
= current
+ offset
;
127 const struct screen_functions
*sf
=
128 screen_lookup_name(options
.screen_list
[next
]);
130 screen_switch(sf
, c
);
134 screen_update(struct mpdclient
*c
)
137 static bool was_connected
;
138 static bool initialized
= false;
140 static bool random_enabled
;
143 static unsigned crossfade
;
145 /* print a message if mpd status has changed */
146 if ((c
->events
& MPD_IDLE_OPTIONS
) && c
->status
!= NULL
) {
148 repeat
= mpd_status_get_repeat(c
->status
);
149 random_enabled
= mpd_status_get_random(c
->status
);
150 single
= mpd_status_get_single(c
->status
);
151 consume
= mpd_status_get_consume(c
->status
);
152 crossfade
= mpd_status_get_crossfade(c
->status
);
156 if (repeat
!= mpd_status_get_repeat(c
->status
))
157 screen_status_printf(mpd_status_get_repeat(c
->status
) ?
158 _("Repeat mode is on") :
159 _("Repeat mode is off"));
161 if (random_enabled
!= mpd_status_get_random(c
->status
))
162 screen_status_printf(mpd_status_get_random(c
->status
) ?
163 _("Random mode is on") :
164 _("Random mode is off"));
166 if (single
!= mpd_status_get_single(c
->status
))
167 screen_status_printf(mpd_status_get_single(c
->status
) ?
168 /* "single" mode means
173 _("Single mode is on") :
174 _("Single mode is off"));
176 if (consume
!= mpd_status_get_consume(c
->status
))
177 screen_status_printf(mpd_status_get_consume(c
->status
) ?
178 /* "consume" mode means
179 that MPD removes each
182 _("Consume mode is on") :
183 _("Consume mode is off"));
185 if (crossfade
!= mpd_status_get_crossfade(c
->status
))
186 screen_status_printf(_("Crossfade %d seconds"),
187 mpd_status_get_crossfade(c
->status
));
189 repeat
= mpd_status_get_repeat(c
->status
);
190 random_enabled
= mpd_status_get_random(c
->status
);
191 single
= mpd_status_get_single(c
->status
);
192 consume
= mpd_status_get_consume(c
->status
);
193 crossfade
= mpd_status_get_crossfade(c
->status
);
196 if ((c
->events
& MPD_IDLE_DATABASE
) != 0 && was_connected
&&
197 mpdclient_is_connected(c
))
198 screen_status_printf(_("Database updated"));
199 was_connected
= mpdclient_is_connected(c
);
202 /* update the main window */
203 if (screen
.current_page
->update
!= NULL
)
204 screen
.current_page
->update(c
);
206 screen_paint(c
, false);
211 screen_get_mouse_event(struct mpdclient
*c
, unsigned long *bstate
, int *row
)
215 /* retrieve the mouse event from curses */
221 /* calculate the selected row in the list window */
222 *row
= event
.y
- screen
.title_bar
.window
.rows
;
223 /* copy button state bits */
224 *bstate
= event
.bstate
;
225 /* if button 2 was pressed switch screen */
226 if (event
.bstate
& BUTTON2_CLICKED
) {
227 screen_cmd(c
, CMD_SCREEN_NEXT
);
236 screen_cmd(struct mpdclient
*c
, command_t cmd
)
239 if (screen
.welcome_source_id
!= 0) {
240 g_source_remove(screen
.welcome_source_id
);
241 screen
.welcome_source_id
= 0;
245 if (screen
.current_page
->cmd
!= NULL
&&
246 screen
.current_page
->cmd(c
, cmd
))
249 if (handle_player_command(c
, cmd
))
253 case CMD_TOGGLE_FIND_WRAP
:
254 options
.find_wrap
= !options
.find_wrap
;
255 screen_status_printf(options
.find_wrap
?
256 _("Find mode: Wrapped") :
257 _("Find mode: Normal"));
259 case CMD_TOGGLE_AUTOCENTER
:
260 options
.auto_center
= !options
.auto_center
;
261 screen_status_printf(options
.auto_center
?
262 _("Auto center mode: On") :
263 _("Auto center mode: Off"));
265 case CMD_SCREEN_UPDATE
:
266 screen_paint(c
, true);
268 case CMD_SCREEN_PREVIOUS
:
269 screen_next_mode(c
, -1);
271 case CMD_SCREEN_NEXT
:
272 screen_next_mode(c
, 1);
274 case CMD_SCREEN_PLAY
:
275 screen_switch(&screen_queue
, c
);
277 case CMD_SCREEN_FILE
:
278 screen_switch(&screen_browse
, c
);
280 #ifdef ENABLE_HELP_SCREEN
281 case CMD_SCREEN_HELP
:
282 screen_switch(&screen_help
, c
);
285 #ifdef ENABLE_SEARCH_SCREEN
286 case CMD_SCREEN_SEARCH
:
287 screen_switch(&screen_search
, c
);
290 #ifdef ENABLE_ARTIST_SCREEN
291 case CMD_SCREEN_ARTIST
:
292 screen_switch(&screen_artist
, c
);
295 #ifdef ENABLE_SONG_SCREEN
296 case CMD_SCREEN_SONG
:
297 screen_switch(&screen_song
, c
);
300 #ifdef ENABLE_KEYDEF_SCREEN
301 case CMD_SCREEN_KEYDEF
:
302 screen_switch(&screen_keydef
, c
);
305 #ifdef ENABLE_LYRICS_SCREEN
306 case CMD_SCREEN_LYRICS
:
307 screen_switch(&screen_lyrics
, c
);
310 #ifdef ENABLE_OUTPUTS_SCREEN
311 case CMD_SCREEN_OUTPUTS
:
312 screen_switch(&screen_outputs
, c
);
315 #ifdef ENABLE_CHAT_SCREEN
316 case CMD_SCREEN_CHAT
:
317 screen_switch(&screen_chat
, c
);
320 case CMD_SCREEN_SWAP
:
321 screen_swap(c
, NULL
);