A modern Music Player Daemon based on Rockbox open source high quality audio player
libadwaita
audio
rust
zig
deno
mpris
rockbox
mpd
1
2#ifdef ROCKBOX
3#include "plugin.h"
4#include "../../pdbox.h"
5#else /* ROCKBOX */
6#include <sys/socket.h>
7#include <netinet/in.h>
8#include <netinet/tcp.h>
9#include <netdb.h>
10#include <stdio.h>
11#endif /* ROCKBOX */
12
13#include "m_pd.h"
14#include "m_imp.h"
15
16static t_class *ipod_class = 0;
17
18typedef struct _ipod
19{
20 t_object x_obj;
21 t_symbol* x_what;
22} t_ipod;
23
24static t_ipod* ipod;
25static t_int x_fd = -1;
26
27
28
29static void ipod_connect(void)
30{
31#ifdef ROCKBOX
32 if (x_fd >= 0)
33 {
34 error("ipod_connect: already connected");
35 return;
36 }
37 else
38 {
39 x_fd++;
40 }
41#else /* ROCKBOX */
42 struct sockaddr_in server;
43 struct hostent *hp;
44 int sockfd;
45 int portno = 3334;
46 char hostname[] = "127.0.0.1";
47 int intarg;
48 if (x_fd >= 0)
49 {
50 error("ipod_connect: already connected");
51 return;
52 }
53
54 /* create a socket */
55 sockfd = socket(AF_INET, SOCK_DGRAM, 0);
56
57 if (sockfd < 0)
58 {
59 sys_sockerror("socket");
60 return;
61 }
62
63 /* connect socket using hostname provided in command line */
64
65 server.sin_family = AF_INET;
66 hp = gethostbyname(hostname);
67 if (hp == 0)
68 {
69 post("bad host?\n");
70 return;
71 }
72
73 memcpy((char *)&server.sin_addr, (char *)hp->h_addr, hp->h_length);
74
75 server.sin_port = htons((u_short)portno);
76 if (connect(sockfd, (struct sockaddr *) &server, sizeof (server)) < 0)
77 {
78 sys_sockerror("connecting stream socket");
79 sys_closesocket(sockfd);
80 return;
81 }
82 post("connected %s %d",hostname,portno);
83 x_fd = sockfd;
84#endif /* ROCKBOX */
85}
86
87
88
89static void ipod_bang(t_ipod *x)
90{
91 static char sendme[200];
92#ifdef ROCKBOX
93 snprintf(sendme, sizeof(sendme), "%s bang;\n", x->x_what->s_name);
94 SEND_FROM_CORE(sendme);
95#else /* ROCKBOX */
96 sprintf(sendme,"%s bang;\n",x->x_what->s_name);
97 send(x_fd,sendme,strlen(sendme),0);
98#endif /*ROCKBOX */
99
100// if (x->x_sym->s_thing) pd_bang(x->x_sym->s_thing);
101}
102
103static void ipod_float(t_ipod *x, t_float f)
104{
105 static char sendme[200];
106#ifdef ROCKBOX
107 char f_buf[32];
108
109 ftoan(f, f_buf, sizeof(f_buf)-1);
110 strcpy(sendme, x->x_what->s_name);
111 strcat(sendme, " ");
112 strcat(sendme, f_buf);
113
114 SEND_FROM_CORE(sendme);
115#else /* ROCKBOX */
116 sprintf(sendme,"%s %f;\n",x->x_what->s_name,f);
117 send(x_fd,sendme,strlen(sendme),0);
118#endif /* ROCKBOX */
119
120// post("forwarding float %s",x->x_what->s_name);
121// if (x->x_sym->s_thing) pd_float(x->x_sym->s_thing, f);
122}
123
124static void *ipod_new(t_symbol* what)
125{
126 t_ipod *x = (t_ipod *)pd_new(ipod_class);
127 post("new ipod %s",what->s_name);
128 x->x_what = what;
129 return (x);
130}
131
132static void ipod_setup(void)
133{
134 ipod_class = class_new(gensym("ipod"), (t_newmethod)ipod_new, 0,
135 sizeof(t_ipod), 0, A_DEFSYM, 0);
136 class_addbang(ipod_class, ipod_bang);
137 class_addfloat(ipod_class, ipod_float);
138 ipod_connect();
139}
140
141void pd_checkgui(t_pd *x, t_symbol *s)
142{
143 if (!strncmp(s->s_name,"pod_",4))
144 if (!strcmp((*x)->c_name->s_name,"gatom") ||
145 !strcmp((*x)->c_name->s_name,"vsl") ||
146 !strcmp((*x)->c_name->s_name,"hsl") ||
147 !strcmp((*x)->c_name->s_name,"bng") ||
148 !strcmp((*x)->c_name->s_name,"vradio") ||
149 !strcmp((*x)->c_name->s_name,"hradio")) {
150
151 post("binding %s to %s",s->s_name,(*x)->c_name->s_name);
152 if (!ipod_class) ipod_setup();
153 ipod = ipod_new(s);
154 pd_bind(&ipod->x_obj.ob_pd,s);
155 }
156}
157