xaizek / pms (License: GPLv3+) (since 2018-12-07)
Older version of Practical Music Search written in C++.
Commit 632eeaaf7eb39561584372df640f41d1bcd6f908

Implement the start of song lists.
Author: Kim Tore Jensen
Author date (UTC): 2014-09-30 20:52
Committer name: Kim Tore Jensen
Committer date (UTC): 2014-09-30 20:52
Parent(s): ae1f4c2d00f8777eeed5a179bec70b3c27e38afa
Signing key:
Tree: bb77ec892865c0d27d11aaa9a392e7d6662b02d5
File Lines added Lines deleted
src/Makefile.am 1 1
src/mpd.c 32 6
src/mpd.h 4 5
src/pms.c 8 4
src/pms.h 4 0
src/song.c 0 6
src/song.h 0 4
src/songlist.c 70 0
src/songlist.h 10 6
File src/Makefile.am changed (mode: 100644) (index 2dd497c..ce98298)
1 1 bin_PROGRAMS = pms bin_PROGRAMS = pms
2 pms_SOURCES = pms.c curses.c console.c window.c topbar.c input.c mpd.c
2 pms_SOURCES = pms.c curses.c console.c window.c topbar.c input.c mpd.c songlist.c
3 3 pms_CC = @PTHREAD_CC@ pms_CC = @PTHREAD_CC@
4 4 pms_CFLAGS = @PTHREAD_CFLAGS@ pms_CFLAGS = @PTHREAD_CFLAGS@
5 5 pms_LDADD = @PTHREAD_LIBS@ @CURSES_LIB@ @libmpdclient_LIBS@ pms_LDADD = @PTHREAD_LIBS@ @CURSES_LIB@ @libmpdclient_LIBS@
File src/mpd.c changed (mode: 100644) (index 8fc1fb2..4df9ab1)
19 19
20 20 #include "pms.h" #include "pms.h"
21 21
22 extern struct pms_state_t * pms_state;
23
22 24 struct mpd_connection * pms_mpd_connect(const char * server, unsigned int port, unsigned int timeout) { struct mpd_connection * pms_mpd_connect(const char * server, unsigned int port, unsigned int timeout) {
23 25
24 26 enum mpd_error status; enum mpd_error status;
 
... ... struct mpd_connection * pms_mpd_connect(const char * server, unsigned int port,
45 47 return connection; return connection;
46 48 } }
47 49
48 void pms_mpd_get_status(struct mpd_connection * connection, struct pms_state_t * state) {
49 if (state->status) {
50 mpd_status_free(state->status);
50 void pms_mpd_get_status(struct mpd_connection * connection) {
51
52 if (pms_state->status) {
53 mpd_status_free(pms_state->status);
51 54 } }
52 state->status = mpd_run_status(connection);
55 pms_state->status = mpd_run_status(connection);
53 56 } }
54 57
55 void pms_handle_mpd_idle_update(struct mpd_connection * connection, struct pms_state_t * state, enum mpd_idle flags) {
58 enum mpd_error pms_mpd_get_playlist(struct mpd_connection * connection, struct songlist_t * songlist) {
59
60 struct mpd_song *song;
61
62 songlist_clear(songlist);
63 songlist_reserve(songlist, mpd_status_get_queue_length(pms_state->status));
64
65 if (!mpd_send_list_queue_meta(connection)) {
66 return -1;
67 }
68
69 while ((song = mpd_recv_song(connection)) != NULL) {
70 songlist_add(songlist, song);
71 }
72
73 return mpd_connection_get_error(connection);
74 }
75
76 void pms_handle_mpd_idle_update(struct mpd_connection * connection, enum mpd_idle flags) {
56 77
57 78 console("pms_handle_mpd_idle_update %d", flags); console("pms_handle_mpd_idle_update %d", flags);
58 79
 
... ... void pms_handle_mpd_idle_update(struct mpd_connection * connection, struct pms_s
82 103 } }
83 104
84 105 if (flags & (MPD_IDLE_QUEUE | MPD_IDLE_PLAYER | MPD_IDLE_MIXER | MPD_IDLE_OPTIONS)) { if (flags & (MPD_IDLE_QUEUE | MPD_IDLE_PLAYER | MPD_IDLE_MIXER | MPD_IDLE_OPTIONS)) {
85 pms_mpd_get_status(connection, state);
106 pms_mpd_get_status(connection);
107 }
108
109 if (flags & (MPD_IDLE_QUEUE)) {
110 // TODO: use mpd_send_queue_changes_meta()
111 pms_mpd_get_playlist(connection, pms_state->queue);
86 112 } }
87 113
88 114 topbar_draw(); topbar_draw();
File src/mpd.h changed (mode: 100644) (index d253068..f12125b)
19 19
20 20 #include <mpd/client.h> #include <mpd/client.h>
21 21
22 struct pms_state_t;
22 struct songlist_t;
23 23
24 24 struct mpd_connection * pms_mpd_connect(const char * server, unsigned int port, unsigned int timeout); struct mpd_connection * pms_mpd_connect(const char * server, unsigned int port, unsigned int timeout);
25
26 void pms_mpd_get_status(struct mpd_connection * connection, struct pms_state_t * state);
27
28 void pms_handle_mpd_idle_update(struct mpd_connection * connection, struct pms_state_t * state, enum mpd_idle flags);
25 void pms_mpd_get_status(struct mpd_connection * connection);
26 enum mpd_error pms_mpd_get_playlist(struct mpd_connection * connection, struct songlist_t * songlist);
27 void pms_handle_mpd_idle_update(struct mpd_connection * connection, enum mpd_idle flags);
File src/pms.c changed (mode: 100644) (index c1eb6dc..19707b3)
31 31 struct options_t * options = NULL; struct options_t * options = NULL;
32 32 struct pms_state_t * pms_state = NULL; struct pms_state_t * pms_state = NULL;
33 33
34 static pthread_mutex_t status_mutex = PTHREAD_MUTEX_INITIALIZER;
35
36 34 void debug(const char * format, ...) { void debug(const char * format, ...) {
37 35
38 36 va_list ap; va_list ap;
 
... ... static void signal_init() {
94 92 signal(SIGTERM, signal_kill); signal(SIGTERM, signal_kill);
95 93 } }
96 94
95 static void songlist_init() {
96 pms_state->queue = songlist_new();
97 pms_state->library = songlist_new();
98 }
99
97 100 int pms_get_pending_input_flags(struct mpd_connection * connection) { int pms_get_pending_input_flags(struct mpd_connection * connection) {
98 101
99 102 struct timeval tv; struct timeval tv;
 
... ... int main(int argc, char** argv) {
147 150 console_init(options->console_size); console_init(options->console_size);
148 151 signal_init(); signal_init();
149 152 input_reset(); input_reset();
153 songlist_init();
150 154 console("%s %s (c) 2006-2014 Kim Tore Jensen <%s>", PACKAGE_NAME, PACKAGE_VERSION, PACKAGE_BUGREPORT); console("%s %s (c) 2006-2014 Kim Tore Jensen <%s>", PACKAGE_NAME, PACKAGE_VERSION, PACKAGE_BUGREPORT);
151 155
152 156 while(pms_state->running) { while(pms_state->running) {
153 157
154 158 if (!connection) { if (!connection) {
155 159 if (connection = pms_mpd_connect(options->server, options->port, options->timeout)) { if (connection = pms_mpd_connect(options->server, options->port, options->timeout)) {
156 pms_handle_mpd_idle_update(connection, pms_state, -1);
160 pms_handle_mpd_idle_update(connection, -1);
157 161 } }
158 162 } }
159 163
 
... ... int main(int argc, char** argv) {
167 171 if (input_flags & PMS_HAS_INPUT_MPD) { if (input_flags & PMS_HAS_INPUT_MPD) {
168 172 flags = mpd_recv_idle(connection, true); flags = mpd_recv_idle(connection, true);
169 173 is_idle = false; is_idle = false;
170 pms_handle_mpd_idle_update(connection, pms_state, -1);
174 pms_handle_mpd_idle_update(connection, flags);
171 175 } }
172 176
173 177 if (input_flags & PMS_HAS_INPUT_STDIN) { if (input_flags & PMS_HAS_INPUT_STDIN) {
File src/pms.h changed (mode: 100644) (index 90d92f3..ac75316)
33 33 #include "topbar.h" #include "topbar.h"
34 34 #include "input.h" #include "input.h"
35 35 #include "mpd.h" #include "mpd.h"
36 #include "song.h"
37 #include "songlist.h"
36 38
37 39 #define PMS_EXIT_SUCCESS 0 #define PMS_EXIT_SUCCESS 0
38 40 #define PMS_EXIT_MEMORY 1 #define PMS_EXIT_MEMORY 1
 
... ... struct pms_state_t {
56 58 /* Set to false when shutting down. */ /* Set to false when shutting down. */
57 59 int running; int running;
58 60 struct mpd_status * status; struct mpd_status * status;
61 struct songlist_t * queue;
62 struct songlist_t * library;
59 63 }; };
60 64
61 65 /** /**
File src/song.c copied from file src/topbar.h (similarity 86%) (mode: 100644) (index 386bb3b..40b57f7)
16 16 * You should have received a copy of the GNU General Public License * You should have received a copy of the GNU General Public License
17 17 * along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 18 */ */
19
20 #include <mpd/client.h>
21
22 void topbar_draw();
23 const char * topbar_mode_str();
24 const char * topbar_playing_str();
File src/song.h copied from file src/topbar.h (similarity 89%) (mode: 100644) (index 386bb3b..2e648f6)
18 18 */ */
19 19
20 20 #include <mpd/client.h> #include <mpd/client.h>
21
22 void topbar_draw();
23 const char * topbar_mode_str();
24 const char * topbar_playing_str();
File src/songlist.c added (mode: 100644) (index 0000000..06d5121)
1 /* vi:set ts=4 sts=4 sw=4 et:
2 *
3 * Practical Music Search
4 * Copyright (c) 2006-2014 Kim Tore Jensen
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include <string.h>
21 #include <stdlib.h>
22 #include <assert.h>
23
24 #include "pms.h"
25
26 struct songlist_t * songlist_new() {
27
28 struct songlist_t * songlist;
29 if ((songlist = malloc(sizeof(struct songlist_t))) == NULL) {
30 fatal(PMS_EXIT_MEMORY, "Out of memory");
31 }
32 memset(songlist, 0, sizeof(*songlist));
33 return songlist;
34 }
35
36 void songlist_free(struct songlist_t * songlist) {
37
38 assert(songlist != NULL);
39 songlist_clear(songlist);
40 free(songlist);
41 }
42
43 void songlist_clear(struct songlist_t * songlist) {
44
45 while (songlist->count > 0) {
46 mpd_song_free(songlist->songs[--songlist->count]);
47 }
48
49 songlist_reserve(songlist, 0);
50 }
51
52 void songlist_reserve(struct songlist_t * songlist, unsigned long length) {
53
54 assert(length >= songlist->count);
55
56 if ((songlist->songs = realloc(songlist->songs, sizeof(struct mpd_song *)*length)) == NULL && length > 0) {
57 fatal(PMS_EXIT_MEMORY, "Out of memory");
58 }
59
60 songlist->capacity = length;
61 }
62
63 void songlist_add(struct songlist_t * songlist, struct mpd_song * song) {
64
65 if (songlist->capacity <= songlist->count) {
66 songlist_reserve(songlist, songlist->capacity*2);
67 }
68
69 songlist->songs[songlist->count++] = song;
70 }
File src/songlist.h copied from file src/mpd.h (similarity 67%) (mode: 100644) (index d253068..c5cd474)
19 19
20 20 #include <mpd/client.h> #include <mpd/client.h>
21 21
22 struct pms_state_t;
22 struct songlist_t {
23 unsigned long count;
24 unsigned long capacity;
25 struct mpd_song ** songs;
26 };
23 27
24 struct mpd_connection * pms_mpd_connect(const char * server, unsigned int port, unsigned int timeout);
25
26 void pms_mpd_get_status(struct mpd_connection * connection, struct pms_state_t * state);
27
28 void pms_handle_mpd_idle_update(struct mpd_connection * connection, struct pms_state_t * state, enum mpd_idle flags);
28 struct songlist_t * songlist_new();
29 void songlist_free(struct songlist_t * songlist);
30 void songlist_clear(struct songlist_t * songlist);
31 void songlist_reserve(struct songlist_t * s, unsigned long length);
32 void songlist_add(struct songlist_t * songlist, struct mpd_song * song);
Hints

Before first commit, do not forget to setup your git environment:
git config --global user.name "your_name_here"
git config --global user.email "your@email_here"

Clone this repository using HTTP(S):
git clone https://code.reversed.top/user/xaizek/pms

Clone this repository using ssh (do not forget to upload a key first):
git clone ssh://rocketgit@code.reversed.top/user/xaizek/pms

You are allowed to anonymously push to this repository.
This means that your pushed commits will automatically be transformed into a pull request:
... clone the repository ...
... make some changes and some commits ...
git push origin master