Mercurial > audlegacy-plugins
annotate src/projectm/main.c @ 1644:c0463e51c338
projectm: C99
author | William Pitcock <nenolod@atheme.org> |
---|---|
date | Fri, 07 Sep 2007 05:56:30 -0500 |
parents | 761e17b23e0c |
children | b180f83e4388 |
rev | line source |
---|---|
358 | 1 /* |
2 xmms-projectM v0.99 - xmms-projectm.sourceforge.net | |
3 -------------------------------------------------- | |
4 | |
5 Lead Developers: Carmelo Piccione (cep@andrew.cmu.edu) & | |
6 Peter Sperl (peter@sperl.com) | |
7 | |
8 We have also been advised by some professors at CMU, namely Roger B. Dannenberg. | |
9 http://www-2.cs.cmu.edu/~rbd/ | |
10 | |
11 The inspiration for this program was Milkdrop by Ryan Geiss. Obviously. | |
12 | |
13 This code is distributed under the GPL. | |
14 | |
15 | |
16 THANKS FOR THE CODE!!! | |
17 ------------------------------------------------- | |
18 The base for this program was andy@nobugs.org's XMMS plugin tutorial | |
19 http://www.xmms.org/docs/vis-plugin.html | |
20 | |
21 We used some FFT code by Takuya OOURA instead of XMMS' built-in fft code | |
22 fftsg.c - http://momonga.t.u-tokyo.ac.jp/~ooura/fft.html | |
23 | |
24 For font rendering we used GLF by Roman Podobedov | |
25 glf.c - http://astronomy.swin.edu.au/~pbourke/opengl/glf/ | |
26 | |
27 and some beat detection code was inspired by Frederic Patin @ | |
28 www.gamedev.net/reference/programming/features/beatdetection/ | |
29 | |
30 */ | |
31 | |
32 | |
33 #include <stdio.h> | |
34 #include <audacious/plugin.h> | |
35 #include <string.h> | |
36 #include <stdlib.h> | |
37 #include <gtk/gtk.h> | |
38 #include <audacious/util.h> | |
39 #include <SDL/SDL.h> | |
40 #include <SDL/SDL_thread.h> | |
41 #include <GL/gl.h> | |
42 #include <GL/glu.h> | |
1035
711ec8d39ca6
[svn] Changed alarm and projectm plugins so they use auddrct now.
magma
parents:
663
diff
changeset
|
43 #include <audacious/auddrct.h> |
358 | 44 #include <math.h> |
45 #include <sys/stat.h> | |
46 #include <sys/types.h> | |
47 | |
48 #include <projectM/projectM.h> | |
49 #include <projectM/console_interface.h> | |
50 #include "sdltoprojectM.h" | |
51 #include "video_init.h" | |
52 | |
53 #if HAVE_CONFIG_H | |
54 #include <config.h> | |
55 #endif | |
56 #define CONFIG_FILE "/config" | |
57 #define PRESETS_DIR "/presets" | |
58 #define FONTS_DIR "/fonts" | |
59 | |
60 // Forward declarations | |
61 static void projectM_xmms_init(void); | |
62 static void projectM_cleanup(void); | |
63 static void projectM_about(void); | |
64 static void projectM_configure(void); | |
65 static void projectM_playback_start(void); | |
66 static void projectM_playback_stop(void); | |
67 static void projectM_render_pcm(gint16 pcm_data[2][512]); | |
68 static void projectM_render_freq(gint16 pcm_data[2][256]); | |
69 void read_config(); | |
70 | |
71 | |
72 //extern preset_t * active_preset; | |
73 | |
74 // Callback functions | |
75 VisPlugin projectM_vtable = { | |
1644 | 76 .description = "projectM v0.99", // description |
77 .num_pcm_chs_wanted = 2, // # of PCM channels for render_pcm() | |
78 .num_freq_chs_wanted = 0, // # of freq channels wanted for render_freq() | |
79 .init = projectM_xmms_init, // Called when plugin is enabled | |
80 .cleanup = projectM_cleanup, // Called when plugin is disabled | |
81 .about = projectM_about, // Show the about box | |
82 .configure = projectM_configure, // Show the configure box | |
83 .playback_start = projectM_playback_start, // Called when playback starts | |
84 .playback_stop = projectM_playback_stop, // Called when playback stops | |
85 .render_pcm = projectM_render_pcm, // Render the PCM data, must return quickly | |
86 .render_freq = projectM_render_freq // Render the freq data, must return quickly | |
358 | 87 }; |
88 | |
1139
038298d9fbe3
[svn] - projectm: convert to plugin2 architecture. now SVN users have milkdrop back.
nenolod
parents:
1061
diff
changeset
|
89 VisPlugin *projectM_vplist[] = { &projectM_vtable, NULL }; |
038298d9fbe3
[svn] - projectm: convert to plugin2 architecture. now SVN users have milkdrop back.
nenolod
parents:
1061
diff
changeset
|
90 |
1395
761e17b23e0c
added Discovery plugin type
Cristi Magherusan <majeru@atheme-project.org>
parents:
1139
diff
changeset
|
91 DECLARE_PLUGIN(projectm, NULL, NULL, NULL, NULL, NULL, NULL, projectM_vplist,NULL); |
358 | 92 |
93 // Our worker thread | |
94 SDL_Thread *worker_thread; | |
95 | |
96 SDL_mutex *mutex; | |
97 | |
98 SDL_sem *sem; | |
99 | |
100 SDL_Event event; | |
101 | |
102 SDL_Surface *screen; | |
103 //SDL_RenderTarget *RenderTarget = NULL; | |
104 //GLuint RenderTargetTextureID; | |
105 | |
106 projectM_t *globalPM = NULL; | |
107 | |
108 int maxsamples=512; | |
109 | |
110 int texsize=512; | |
111 int gx=32,gy=24; | |
112 int wvw=640,wvh=480; | |
113 int fvw=1280,fvh=960; | |
114 int fps=30, fullscreen=0; | |
115 char *disp; | |
116 | |
117 int disable_projectm(void) { | |
118 projectM_vtable.disable_plugin(&projectM_vtable); | |
119 return 0; | |
120 } | |
121 | |
122 int get_xmms_title(void) { | |
123 static char check_title = 1; | |
124 static int last_pos; | |
125 static char *last_title = NULL; | |
126 int pos; | |
127 char *title = NULL; | |
128 | |
129 //Nice optimization, but we want the title no matter what so I can display it when the song changes | |
130 #if 0 | |
131 if(!(globalPM->showtitle%2)) { | |
132 /* Repeat less often when not showing title */ | |
133 return 1000; | |
134 } | |
135 #endif | |
136 | |
1035
711ec8d39ca6
[svn] Changed alarm and projectm plugins so they use auddrct now.
magma
parents:
663
diff
changeset
|
137 pos = audacious_drct_pl_get_pos(); |
358 | 138 /* Only check every 1 second for title change, otherwise check pos */ |
139 if(check_title || pos != last_pos) { | |
1035
711ec8d39ca6
[svn] Changed alarm and projectm plugins so they use auddrct now.
magma
parents:
663
diff
changeset
|
140 title = audacious_drct_pl_get_title(pos); |
358 | 141 if(title && (!last_title || strcmp(last_title,title))) { |
142 globalPM->title = title; | |
143 globalPM->drawtitle = 1; | |
144 g_free(last_title); | |
145 last_title = title; | |
146 } else if(title && last_title != title) { | |
147 /* New copy of last title */ | |
148 g_free(title); | |
149 } | |
150 check_title = !check_title; | |
151 } | |
152 last_pos = pos; | |
153 /* Repeat every 500ms */ | |
154 return 500; | |
155 } | |
156 | |
157 void worker_func() | |
158 { | |
159 char projectM_data[PATH_MAX]; | |
160 | |
161 SDL_TimerID title_timer = NULL; | |
162 | |
163 | |
164 read_config(); | |
165 | |
166 init_display(wvw,wvh,fullscreen); | |
167 | |
168 SDL_WM_SetCaption("projectM v0.99", "projectM v0.99"); | |
169 | |
170 | |
171 /** Initialise projectM */ | |
172 | |
173 globalPM = (projectM_t *)malloc( sizeof( projectM_t ) ); | |
174 | |
175 projectM_reset( globalPM ); | |
176 | |
177 globalPM->fullscreen = fullscreen; | |
178 globalPM->renderTarget->texsize = texsize; | |
179 globalPM->gx=gx; | |
180 globalPM->gy=gy; | |
181 globalPM->fps=fps; | |
182 globalPM->renderTarget->usePbuffers=0; | |
183 | |
184 strcpy(projectM_data, PROJECTM_DATADIR); | |
185 strcpy(projectM_data+strlen(PROJECTM_DATADIR), FONTS_DIR); | |
186 projectM_data[strlen(PROJECTM_DATADIR)+strlen(FONTS_DIR)]='\0'; | |
187 | |
188 globalPM->fontURL = (char *)malloc( sizeof( char ) * 512 ); | |
189 strcpy( globalPM->fontURL, projectM_data ); | |
190 | |
191 strcpy(projectM_data+strlen(PROJECTM_DATADIR), PRESETS_DIR); | |
192 projectM_data[strlen(PROJECTM_DATADIR)+strlen(PRESETS_DIR)]='\0'; | |
193 | |
194 globalPM->presetURL = (char *)malloc( sizeof( char ) * 512 ); | |
195 strcpy( globalPM->presetURL, projectM_data ); | |
196 | |
197 | |
198 projectM_init( globalPM ); | |
199 | |
200 projectM_resetGL( globalPM, wvw, wvh ); | |
201 | |
663
aaab0bede198
[svn] Cast those pointers properly, and do not use deprecated GTK+ functions.
chainsaw
parents:
358
diff
changeset
|
202 title_timer = SDL_AddTimer(500, (SDL_NewTimerCallback) get_xmms_title, NULL); |
358 | 203 |
204 /** Initialise the thread */ | |
205 | |
206 SDL_SemTryWait(sem); | |
207 while ( SDL_SemTryWait(sem) ) { | |
208 projectMEvent evt; | |
209 projectMKeycode key; | |
210 projectMModifier mod; | |
211 | |
212 /** Process SDL events */ | |
213 SDL_Event event; | |
214 while ( SDL_PollEvent( &event ) ) { | |
215 /** Translate into projectM codes and process */ | |
216 evt = sdl2pmEvent( event ); | |
217 key = sdl2pmKeycode( event.key.keysym.sym ); | |
218 mod = sdl2pmModifier( event.key.keysym.mod ); | |
219 | |
220 if ( evt == PROJECTM_KEYDOWN ) { | |
221 | |
222 | |
223 if(key == SDLK_f) | |
224 { | |
225 int w, h; | |
226 if (fullscreen == 0) { | |
227 w = fvw; | |
228 h = fvh; | |
229 } else { | |
230 w = wvw; | |
231 h = wvh; | |
232 } | |
233 globalPM->fullscreen = fullscreen ^= 1; | |
234 resize_display(w, h, fullscreen); | |
235 projectM_resetGL( globalPM, w, h ); | |
236 } | |
237 else key_handler(globalPM,evt,key,mod); | |
238 | |
239 } | |
240 else if ( evt == PROJECTM_VIDEORESIZE ) | |
241 { | |
242 wvw=event.resize.w; | |
243 wvh=event.resize.h; | |
244 resize_display(wvw, wvh, 0); | |
245 projectM_resetGL( globalPM, wvw, wvh ); | |
246 | |
247 } | |
248 else if ( evt == PROJECTM_VIDEOQUIT ) { | |
249 | |
663
aaab0bede198
[svn] Cast those pointers properly, and do not use deprecated GTK+ functions.
chainsaw
parents:
358
diff
changeset
|
250 (void) g_idle_add ((GSourceFunc) disable_projectm, NULL); |
358 | 251 /* if(quit_timer == NULL) |
252 quit_timer = SDL_AddTimer(500, disable_projectm, NULL);*/ | |
253 } | |
254 | |
255 } | |
256 | |
257 | |
258 /** Render the new frame */ | |
259 | |
260 renderFrame( globalPM ); | |
261 | |
262 SDL_GL_SwapBuffers(); | |
263 } | |
264 | |
265 | |
266 | |
267 printf("Worker thread: Exiting\n"); | |
268 if(title_timer) SDL_RemoveTimer(title_timer); | |
269 g_free(globalPM->title); | |
270 free(globalPM->presetURL); | |
271 free(globalPM->fontURL); | |
272 free(globalPM); | |
273 close_display(); | |
274 } | |
275 | |
276 static void projectM_xmms_init(void) | |
277 { | |
278 | |
279 printf("projectM plugin: Initializing\n"); | |
280 | |
281 SDL_EnableUNICODE(1); | |
282 | |
283 mutex = SDL_CreateMutex(); | |
284 | |
285 sem = SDL_CreateSemaphore(1); | |
286 | |
287 worker_thread = SDL_CreateThread ((void *) worker_func, NULL); | |
288 | |
289 } | |
290 | |
291 | |
292 | |
293 static void projectM_cleanup(void) | |
294 { | |
295 | |
296 SDL_SemPost(sem); | |
297 SDL_WaitThread(worker_thread, NULL); | |
298 | |
299 SDL_DestroySemaphore(sem); | |
300 printf("Destroy Semaphore\n"); | |
301 SDL_DestroyMutex(mutex); | |
302 printf("Destroy Mutex\n"); | |
303 | |
304 printf("projectM plugin: Cleanup completed\n"); | |
305 } | |
306 static void projectM_about(void) | |
307 { | |
308 printf("projectM plugin: About\n"); | |
309 } | |
310 static void projectM_configure(void) | |
311 { | |
312 printf("projectM plugin: Configure\n"); | |
313 } | |
314 static void projectM_playback_start(void) | |
315 { | |
316 printf("projectM plugin: Playback starting\n"); | |
317 } | |
318 static void projectM_playback_stop(void) | |
319 { | |
320 printf("projectM plugin: Playback stopping\n"); | |
321 } | |
322 static void projectM_render_pcm(gint16 pcm_data[2][512]) | |
323 { | |
324 | |
325 if (0 < SDL_SemValue(sem)) return; | |
326 SDL_mutexP(mutex); | |
327 | |
328 addPCM16Data(pcm_data,512); | |
329 | |
330 SDL_mutexV(mutex); | |
331 | |
332 } | |
333 | |
334 static void projectM_render_freq(gint16 freq_data[2][256]) | |
335 { | |
336 printf("NO GOOD\n"); | |
337 } | |
338 | |
339 | |
340 void read_config() | |
341 { | |
342 | |
343 int n; | |
344 | |
345 char num[80]; | |
346 FILE *in; | |
347 FILE *out; | |
348 | |
349 char* home; | |
350 char projectM_home[PATH_MAX]; | |
351 char projectM_config[PATH_MAX]; | |
352 | |
353 strcpy(projectM_config, PROJECTM_DATADIR); | |
354 strcpy(projectM_config+strlen(PROJECTM_DATADIR), CONFIG_FILE); | |
355 projectM_config[strlen(PROJECTM_DATADIR)+strlen(CONFIG_FILE)]='\0'; | |
356 | |
357 home=getenv("HOME"); | |
358 strcpy(projectM_home, home); | |
359 strcpy(projectM_home+strlen(home), "/.projectM/config"); | |
360 projectM_home[strlen(home)+strlen("/.projectM/config")]='\0'; | |
361 | |
362 | |
363 if ((in = fopen(projectM_home, "r")) != 0) | |
364 { | |
365 printf("reading ~/.projectM/config \n"); | |
366 } | |
367 else | |
368 { | |
369 printf("trying to create ~/.projectM/config \n"); | |
370 | |
371 strcpy(projectM_home, home); | |
372 strcpy(projectM_home+strlen(home), "/.projectM"); | |
373 projectM_home[strlen(home)+strlen("/.projectM")]='\0'; | |
374 mkdir(projectM_home,0755); | |
375 | |
376 strcpy(projectM_home, home); | |
377 strcpy(projectM_home+strlen(home), "/.projectM/config"); | |
378 projectM_home[strlen(home)+strlen("/.projectM/config")]='\0'; | |
379 | |
380 if((out = fopen(projectM_home,"w"))!=0) | |
381 { | |
382 | |
383 if ((in = fopen(projectM_config, "r")) != 0) | |
384 { | |
385 | |
386 while(fgets(num,80,in)!=NULL) | |
387 { | |
388 fputs(num,out); | |
389 } | |
390 fclose(in); | |
391 fclose(out); | |
392 | |
393 | |
394 if ((in = fopen(projectM_home, "r")) != 0) | |
395 { printf("created ~/.projectM/config successfully\n"); } | |
396 else{printf("This shouldn't happen, using implementation defualts\n");return;} | |
397 } | |
398 else{printf("Cannot find projectM default config, using implementation defaults\n");return;} | |
399 } | |
400 else | |
401 { | |
402 printf("Cannot create ~/.projectM/config, using default config file\n"); | |
403 if ((in = fopen(projectM_config, "r")) != 0) | |
404 { printf("Successfully opened default config file\n");} | |
405 else{ printf("Using implementation defaults, your system is really messed up, I'm suprised we even got this far\n"); return;} | |
406 | |
407 } | |
408 | |
409 } | |
410 | |
411 | |
412 | |
413 fgets(num, 80, in); fgets(num, 80, in); fgets(num, 80, in); | |
414 if(fgets(num, 80, in) != NULL) sscanf (num, "%d", &texsize); | |
415 | |
416 fgets(num, 80, in); | |
417 if(fgets(num, 80, in) != NULL) sscanf (num, "%d", &gx); | |
418 | |
419 fgets(num, 80, in); | |
420 if(fgets(num, 80, in) != NULL) sscanf (num, "%d", &gy); | |
421 | |
422 fgets(num, 80, in); | |
423 if(fgets(num, 80, in) != NULL) sscanf (num, "%d", &wvw); | |
424 | |
425 fgets(num, 80, in); | |
426 if(fgets(num, 80, in) != NULL) sscanf (num, "%d", &wvh); | |
427 | |
428 fgets(num, 80, in); | |
429 if(fgets(num, 80, in) != NULL) sscanf (num, "%d", &fvw); | |
430 | |
431 fgets(num, 80, in); | |
432 if(fgets(num, 80, in) != NULL) sscanf (num, "%d", &fvh); | |
433 | |
434 fgets(num, 80, in); | |
435 if(fgets(num, 80, in) != NULL) sscanf (num, "%d", &fps); | |
436 | |
437 fgets(num, 80, in); | |
438 if(fgets(num, 80, in) != NULL) sscanf (num, "%d", &fullscreen); | |
439 /* | |
440 fgets(num, 80, in); | |
441 fgets(num, 80, in); | |
442 | |
443 n=0; | |
444 while (num[n]!=' ' && num[n]!='\n' && n < 80 && num[n]!=EOF) | |
445 { | |
446 disp[n]=num[n]; | |
447 n++; | |
448 } | |
449 disp[n]=0; | |
450 | |
451 | |
452 // sprintf(disp,"%s",num ); | |
453 setenv("DISPLAY",disp,1); | |
454 printf("%s %d\n", disp,strlen(disp)); | |
455 setenv("LD_PRELOAD", "/usr/lib/tls/libGL.so.1.0.4496", 1); | |
456 */ | |
457 fclose(in); | |
458 | |
459 } |