1 #include <glib.h>
2 #include "goom_config.h"
3 
4 #include <xmms/plugin.h>
5 #include <xmms/xmmsctrl.h>
6 
7 #include <stdlib.h>
8 #include <stdio.h>
9 #include <unistd.h>
10 #include <sys/time.h>
11 #include <sys/types.h>
12 #include <signal.h>
13 #include <string.h>
14 
15 static void plug_init (void);
16 static void plug_cleanup (void);
17 static void plug_render_pcm (gint16 data[2][512]);
18 
19 static int fd_in, fd_out;
20 static pid_t goom_pid = -1;
21 
22 static VisPlugin plug_vp = {
23     NULL,
24     NULL,
25     0,
26     "What A GOOM!! " VERSION,
27     2,
28     0,
29     plug_init,/* init */
30     plug_cleanup,/* cleanup */
31     NULL,/* about */
32     NULL,/* configure */
33     NULL,/* disable_plugin */
34     NULL,/* playback_start */
35     NULL,/* playback_stop */
36     plug_render_pcm, /* render_pcm */
37     NULL /* render_freq */
38 };
39 
40 VisPlugin *
get_vplugin_info(void)41 get_vplugin_info (void)
42 {
43     return &plug_vp;
44 }
45 
46 static void
plug_init(void)47 plug_init (void)
48 {
49     int fd[2];
50     pid_t pid;
51 
52     /* create a pipe */
53     if (pipe(fd) < 0) {
54         fprintf (stderr, "System Error\n");
55         /* TODO: en gtk? */
56         return;
57     }
58     fd_in  = fd[0];
59     fd_out = fd[1];
60 
61     /* load an executable */
62     pid = fork();
63 
64     /* todo look at the result */
65     if (pid == 0) {
66         dup2(fd_in, 0);
67 
68         execlp  ("goom2", "goom2", NULL, 0);
69         fprintf (stderr, "Unable to load goom...\n"); /* TODO: Message en gtk
70                                                          check the PATH */
71         exit (1);
72     }
73     if (pid == -1) {
74         /* erreur system : TODO -> dialog en gtk */
75     }
76     if (goom_pid != -1)
77         kill (goom_pid, SIGQUIT);
78     goom_pid = pid;
79 }
80 
sendIntToGoom(int i)81 static void sendIntToGoom(int i) {
82     write (fd_out, &i, sizeof(int));
83 }
84 
85 static void
plug_cleanup(void)86 plug_cleanup (void)
87 {
88     sendIntToGoom(2);
89     kill (goom_pid, SIGQUIT);
90     goom_pid = -1;
91 }
92 
93 static void
plug_render_pcm(gint16 data[2][512])94 plug_render_pcm (gint16 data[2][512])
95 {
96     fd_set rfds;
97     struct timeval tv;
98     int retval;
99 
100     tv.tv_sec = 0;
101     tv.tv_usec = 10000;
102 
103     FD_ZERO(&rfds);
104     FD_SET(fd_out, &rfds);
105     retval = select(fd_out+1, NULL, &rfds, NULL, &tv);
106     if (retval) {
107         /* send sound datas to goom */
108         {
109             sendIntToGoom(0);
110             write (fd_out, &data[0][0], 512*sizeof(gint16)*2);
111             fsync(fd_out);
112         }
113 
114         /* send song title to goom */
115         {
116             static int spos = -1;
117             int pos = xmms_remote_get_playlist_pos(plug_vp.xmms_session);
118             char title[2048];
119             if (spos != pos) {
120                 sendIntToGoom(1);
121                 strcpy(title, xmms_remote_get_playlist_title(plug_vp.xmms_session, pos));
122                 write (fd_out, &title[0], 2048);
123                 spos = pos;
124             }
125         }
126     }
127     else {
128         usleep(100);
129     }
130 }
131