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

Put MPD functions in mpd.c.
Author: Kim Tore Jensen
Author date (UTC): 2014-09-30 17:23
Committer name: Kim Tore Jensen
Committer date (UTC): 2014-09-30 17:23
Parent(s): 030fa3524b1af1135a96f48133949fe88542ba93
Signing key:
Tree: ce192e1b0467d29fb32b19432d0424dd170bb36c
File Lines added Lines deleted
src/Makefile.am 1 1
src/input.h 1 1
src/mpd.c 91 0
src/mpd.h 7 3
src/pms.c 5 77
src/pms.h 1 0
File src/Makefile.am changed (mode: 100644) (index 9a8c846..2dd497c)
1 1 bin_PROGRAMS = pms bin_PROGRAMS = pms
2 pms_SOURCES = pms.c curses.c console.c window.c topbar.c input.c
2 pms_SOURCES = pms.c curses.c console.c window.c topbar.c input.c mpd.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/input.h changed (mode: 100644) (index a62eb20..7d9d26c)
... ... typedef struct {
40 40 void input_reset(); void input_reset();
41 41
42 42 /* /*
43 * Retrieve input
43 * Retrieve input from stdin. Will block until input is available.
44 44 */ */
45 45 command_t * input_get(); command_t * input_get();
46 46
File src/mpd.c added (mode: 100644) (index 0000000..0305d0d)
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 "pms.h"
21
22 struct mpd_connection * pms_mpd_connect(const char * server, unsigned int port, unsigned int timeout) {
23
24 enum mpd_error status;
25 const char * error_msg;
26 struct mpd_connection * connection;
27
28 console("Connecting to %s...", server);
29
30 connection = mpd_connection_new(server, port, timeout);
31 if (connection == NULL) {
32 fatal(PMS_EXIT_MEMORY, "mpd connect: out of memory\n");
33 }
34
35 status = mpd_connection_get_error(connection);
36 if (status == MPD_ERROR_SUCCESS) {
37 console("Connected to %s", server);
38 } else {
39 error_msg = mpd_connection_get_error_message(connection);
40 console("Error connecting to %s: error %d: %s", server, status, error_msg);
41 mpd_connection_free(connection);
42 connection = NULL;
43 }
44
45 return connection;
46 }
47
48 void pms_get_mpd_status(struct mpd_connection * connection, struct pms_state_t * state) {
49 if (state->status) {
50 mpd_status_free(state->status);
51 }
52 state->status = mpd_run_status(connection);
53 }
54
55 void pms_handle_mpd_idle_update(struct mpd_connection * connection, struct pms_state_t * state, enum mpd_idle flags) {
56
57 console("pms_handle_mpd_idle_update %d", flags);
58
59 if (flags & MPD_IDLE_DATABASE) {
60 console("Database has been updated.");
61 }
62 if (flags & MPD_IDLE_STORED_PLAYLIST) {
63 console("Stored playlists have been updated.");
64 }
65 if (flags & MPD_IDLE_QUEUE) {
66 console("The queue has been updated.");
67 }
68 if (flags & MPD_IDLE_PLAYER) {
69 console("Player state has changed.");
70 }
71 if (flags & MPD_IDLE_MIXER) {
72 console("Mixer parameters have changed.");
73 }
74 if (flags & MPD_IDLE_OUTPUT) {
75 console("Outputs have changed.");
76 }
77 if (flags & MPD_IDLE_OPTIONS) {
78 console("Options have changed.");
79 }
80 if (flags & MPD_IDLE_UPDATE) {
81 console("Database update has started or finished.");
82 }
83
84 if (flags & (MPD_IDLE_QUEUE | MPD_IDLE_PLAYER | MPD_IDLE_MIXER | MPD_IDLE_OPTIONS)) {
85 pms_get_mpd_status(connection, state);
86 }
87
88 topbar_draw();
89
90 }
91
File src/mpd.h copied from file src/topbar.h (similarity 70%) (mode: 100644) (index 386bb3b..245fe9e)
19 19
20 20 #include <mpd/client.h> #include <mpd/client.h>
21 21
22 void topbar_draw();
23 const char * topbar_mode_str();
24 const char * topbar_playing_str();
22 struct pms_state_t;
23
24 struct mpd_connection * pms_mpd_connect(const char * server, unsigned int port, unsigned int timeout);
25
26 void pms_get_mpd_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);
File src/pms.c changed (mode: 100644) (index 911a4dc..9c8fca3)
25 25 #include <sys/select.h> #include <sys/select.h>
26 26 #include <unistd.h> #include <unistd.h>
27 27 #include <errno.h> #include <errno.h>
28
28 29 #include "pms.h" #include "pms.h"
29 30
30 31 struct options_t * options = NULL; struct options_t * options = NULL;
 
... ... void shutdown() {
76 77 pms_state->running = 0; pms_state->running = 0;
77 78 } }
78 79
79 static struct mpd_connection * pms_mpd_connect() {
80
81 enum mpd_error status;
82 const char * error_msg;
83 struct mpd_connection * connection;
84
85 console("Connecting to %s...", options->server);
86
87 connection = mpd_connection_new(options->server, options->port, options->timeout);
88 if (connection == NULL) {
89 fatal(PMS_EXIT_MEMORY, "mpd connect: out of memory\n");
90 }
91
92 status = mpd_connection_get_error(connection);
93 if (status == MPD_ERROR_SUCCESS) {
94 console("Connected to %s", options->server);
95 } else {
96 error_msg = mpd_connection_get_error_message(connection);
97 console("Error connecting to %s: error %d: %s", options->server, status, error_msg);
98 mpd_connection_free(connection);
99 connection = NULL;
100 }
101
102 return connection;
103 }
104
105 static void pms_get_mpd_state(struct mpd_connection * connection) {
106
107 pms_status_lock();
108 if (pms_state->status) {
109 mpd_status_free(pms_state->status);
110 }
111 pms_state->status = mpd_run_status(connection);
112 pms_status_unlock();
113
114 }
115
116 static void pms_handle_mpd_idle_update(struct mpd_connection * connection, enum mpd_idle flags) {
117
118 console("pms_handle_mpd_idle_update %d", flags);
119
120 if (flags & MPD_IDLE_DATABASE) {
121 console("Database has been updated.");
122 }
123 if (flags & MPD_IDLE_STORED_PLAYLIST) {
124 console("Stored playlists have been updated.");
125 }
126 if (flags & MPD_IDLE_QUEUE) {
127 console("The queue has been updated.");
128 }
129 if (flags & MPD_IDLE_PLAYER) {
130 console("Player state has changed.");
131 }
132 if (flags & MPD_IDLE_MIXER) {
133 console("Mixer parameters have changed.");
134 }
135 if (flags & MPD_IDLE_OUTPUT) {
136 console("Outputs have changed.");
137 }
138 if (flags & MPD_IDLE_OPTIONS) {
139 console("Options have changed.");
140 }
141 if (flags & MPD_IDLE_UPDATE) {
142 console("Database update has started or finished.");
143 }
144
145 if (flags & (MPD_IDLE_QUEUE | MPD_IDLE_PLAYER | MPD_IDLE_MIXER | MPD_IDLE_OPTIONS)) {
146 pms_get_mpd_state(connection);
147 }
148
149 topbar_draw();
150
151 }
152
153 80 void signal_resize(int signal) { void signal_resize(int signal) {
154 81 console("Resized to %d x %d", LINES, COLS); console("Resized to %d x %d", LINES, COLS);
155 82 pms_curses_lock(); pms_curses_lock();
 
... ... static void signal_init() {
170 97 } }
171 98
172 99 int pms_get_pending_input_flags(struct mpd_connection * connection) { int pms_get_pending_input_flags(struct mpd_connection * connection) {
100
173 101 struct timeval tv; struct timeval tv;
174 102 int mpd_fd = 0; int mpd_fd = 0;
175 103 int retval; int retval;
 
... ... int main(int argc, char** argv) {
226 154 while(pms_state->running) { while(pms_state->running) {
227 155
228 156 if (!connection) { if (!connection) {
229 if (connection = pms_mpd_connect()) {
230 pms_handle_mpd_idle_update(connection, -1);
157 if (connection = pms_mpd_connect(options->server, options->port, options->timeout)) {
158 pms_handle_mpd_idle_update(connection, pms_state, -1);
231 159 } }
232 160 } }
233 161
 
... ... int main(int argc, char** argv) {
241 169 if (input_flags & PMS_HAS_INPUT_MPD) { if (input_flags & PMS_HAS_INPUT_MPD) {
242 170 flags = mpd_recv_idle(connection, true); flags = mpd_recv_idle(connection, true);
243 171 is_idle = false; is_idle = false;
244 pms_handle_mpd_idle_update(connection, flags);
172 pms_handle_mpd_idle_update(connection, pms_state, -1);
245 173 } }
246 174
247 175 if (input_flags & PMS_HAS_INPUT_STDIN) { if (input_flags & PMS_HAS_INPUT_STDIN) {
File src/pms.h changed (mode: 100644) (index dabaa58..13533ff)
32 32 #include "console.h" #include "console.h"
33 33 #include "topbar.h" #include "topbar.h"
34 34 #include "input.h" #include "input.h"
35 #include "mpd.h"
35 36
36 37 #define PMS_EXIT_SUCCESS 0 #define PMS_EXIT_SUCCESS 0
37 38 #define PMS_EXIT_MEMORY 1 #define PMS_EXIT_MEMORY 1
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