Mercurial > audlegacy-plugins
annotate src/projectm/main.c @ 1035:711ec8d39ca6 trunk
[svn] Changed alarm and projectm plugins so they use auddrct now.
| author | magma |
|---|---|
| date | Sat, 12 May 2007 15:03:26 -0700 |
| parents | aaab0bede198 |
| children | 2cf4d0182a86 |
| 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 = { | |
| 76 NULL, // Handle, filled in by xmms | |
| 77 NULL, // Filename, filled in by xmms | |
| 78 0, // Session ID | |
| 79 "projectM v0.99", // description | |
| 80 2, // # of PCM channels for render_pcm() | |
| 81 0, // # of freq channels wanted for render_freq() | |
| 82 projectM_xmms_init, // Called when plugin is enabled | |
| 83 projectM_cleanup, // Called when plugin is disabled | |
| 84 projectM_about, // Show the about box | |
| 85 projectM_configure, // Show the configure box | |
| 86 NULL, // Called to disable plugin, filled in by xmms | |
| 87 projectM_playback_start, // Called when playback starts | |
| 88 projectM_playback_stop, // Called when playback stops | |
| 89 projectM_render_pcm, // Render the PCM data, must return quickly | |
| 90 projectM_render_freq // Render the freq data, must return quickly | |
| 91 }; | |
| 92 | |
| 93 // XMMS entry point | |
| 94 VisPlugin *get_vplugin_info(void) | |
| 95 { | |
| 96 return &projectM_vtable; | |
| 97 } | |
| 98 | |
| 99 // Our worker thread | |
| 100 SDL_Thread *worker_thread; | |
| 101 | |
| 102 SDL_mutex *mutex; | |
| 103 | |
| 104 SDL_sem *sem; | |
| 105 | |
| 106 SDL_Event event; | |
| 107 | |
| 108 SDL_Surface *screen; | |
| 109 //SDL_RenderTarget *RenderTarget = NULL; | |
| 110 //GLuint RenderTargetTextureID; | |
| 111 | |
| 112 projectM_t *globalPM = NULL; | |
| 113 | |
| 114 int maxsamples=512; | |
| 115 | |
| 116 int texsize=512; | |
| 117 int gx=32,gy=24; | |
| 118 int wvw=640,wvh=480; | |
| 119 int fvw=1280,fvh=960; | |
| 120 int fps=30, fullscreen=0; | |
| 121 char *disp; | |
| 122 | |
| 123 int disable_projectm(void) { | |
| 124 projectM_vtable.disable_plugin(&projectM_vtable); | |
| 125 return 0; | |
| 126 } | |
| 127 | |
| 128 int get_xmms_title(void) { | |
| 129 static char check_title = 1; | |
| 130 static int last_pos; | |
| 131 static char *last_title = NULL; | |
| 132 int pos; | |
| 133 char *title = NULL; | |
| 134 | |
| 135 //Nice optimization, but we want the title no matter what so I can display it when the song changes | |
| 136 #if 0 | |
| 137 if(!(globalPM->showtitle%2)) { | |
| 138 /* Repeat less often when not showing title */ | |
| 139 return 1000; | |
| 140 } | |
| 141 #endif | |
| 142 | |
|
1035
711ec8d39ca6
[svn] Changed alarm and projectm plugins so they use auddrct now.
magma
parents:
663
diff
changeset
|
143 pos = audacious_drct_pl_get_pos(); |
| 358 | 144 /* Only check every 1 second for title change, otherwise check pos */ |
| 145 if(check_title || pos != last_pos) { | |
|
1035
711ec8d39ca6
[svn] Changed alarm and projectm plugins so they use auddrct now.
magma
parents:
663
diff
changeset
|
146 title = audacious_drct_pl_get_title(pos); |
| 358 | 147 if(title && (!last_title || strcmp(last_title,title))) { |
| 148 globalPM->title = title; | |
| 149 globalPM->drawtitle = 1; | |
| 150 g_free(last_title); | |
| 151 last_title = title; | |
| 152 } else if(title && last_title != title) { | |
| 153 /* New copy of last title */ | |
| 154 g_free(title); | |
| 155 } | |
| 156 check_title = !check_title; | |
| 157 } | |
| 158 last_pos = pos; | |
| 159 /* Repeat every 500ms */ | |
| 160 return 500; | |
| 161 } | |
| 162 | |
| 163 void worker_func() | |
| 164 { | |
| 165 char projectM_data[PATH_MAX]; | |
| 166 | |
| 167 SDL_TimerID title_timer = NULL; | |
| 168 | |
| 169 | |
| 170 read_config(); | |
| 171 | |
| 172 init_display(wvw,wvh,fullscreen); | |
| 173 | |
| 174 SDL_WM_SetCaption("projectM v0.99", "projectM v0.99"); | |
| 175 | |
| 176 | |
| 177 /** Initialise projectM */ | |
| 178 | |
| 179 globalPM = (projectM_t *)malloc( sizeof( projectM_t ) ); | |
| 180 | |
| 181 projectM_reset( globalPM ); | |
| 182 | |
| 183 globalPM->fullscreen = fullscreen; | |
| 184 globalPM->renderTarget->texsize = texsize; | |
| 185 globalPM->gx=gx; | |
| 186 globalPM->gy=gy; | |
| 187 globalPM->fps=fps; | |
| 188 globalPM->renderTarget->usePbuffers=0; | |
| 189 | |
| 190 strcpy(projectM_data, PROJECTM_DATADIR); | |
| 191 strcpy(projectM_data+strlen(PROJECTM_DATADIR), FONTS_DIR); | |
| 192 projectM_data[strlen(PROJECTM_DATADIR)+strlen(FONTS_DIR)]='\0'; | |
| 193 | |
| 194 globalPM->fontURL = (char *)malloc( sizeof( char ) * 512 ); | |
| 195 strcpy( globalPM->fontURL, projectM_data ); | |
| 196 | |
| 197 strcpy(projectM_data+strlen(PROJECTM_DATADIR), PRESETS_DIR); | |
| 198 projectM_data[strlen(PROJECTM_DATADIR)+strlen(PRESETS_DIR)]='\0'; | |
| 199 | |
| 200 globalPM->presetURL = (char *)malloc( sizeof( char ) * 512 ); | |
| 201 strcpy( globalPM->presetURL, projectM_data ); | |
| 202 | |
| 203 | |
| 204 projectM_init( globalPM ); | |
| 205 | |
| 206 projectM_resetGL( globalPM, wvw, wvh ); | |
| 207 | |
|
663
aaab0bede198
[svn] Cast those pointers properly, and do not use deprecated GTK+ functions.
chainsaw
parents:
358
diff
changeset
|
208 title_timer = SDL_AddTimer(500, (SDL_NewTimerCallback) get_xmms_title, NULL); |
| 358 | 209 |
| 210 /** Initialise the thread */ | |
| 211 | |
| 212 SDL_SemTryWait(sem); | |
| 213 while ( SDL_SemTryWait(sem) ) { | |
| 214 projectMEvent evt; | |
| 215 projectMKeycode key; | |
| 216 projectMModifier mod; | |
| 217 | |
| 218 /** Process SDL events */ | |
| 219 SDL_Event event; | |
| 220 while ( SDL_PollEvent( &event ) ) { | |
| 221 /** Translate into projectM codes and process */ | |
| 222 evt = sdl2pmEvent( event ); | |
| 223 key = sdl2pmKeycode( event.key.keysym.sym ); | |
| 224 mod = sdl2pmModifier( event.key.keysym.mod ); | |
| 225 | |
| 226 if ( evt == PROJECTM_KEYDOWN ) { | |
| 227 | |
| 228 | |
| 229 if(key == SDLK_f) | |
| 230 { | |
| 231 int w, h; | |
| 232 if (fullscreen == 0) { | |
| 233 w = fvw; | |
| 234 h = fvh; | |
| 235 } else { | |
| 236 w = wvw; | |
| 237 h = wvh; | |
| 238 } | |
| 239 globalPM->fullscreen = fullscreen ^= 1; | |
| 240 resize_display(w, h, fullscreen); | |
| 241 projectM_resetGL( globalPM, w, h ); | |
| 242 } | |
| 243 else key_handler(globalPM,evt,key,mod); | |
| 244 | |
| 245 } | |
| 246 else if ( evt == PROJECTM_VIDEORESIZE ) | |
| 247 { | |
| 248 wvw=event.resize.w; | |
| 249 wvh=event.resize.h; | |
| 250 resize_display(wvw, wvh, 0); | |
| 251 projectM_resetGL( globalPM, wvw, wvh ); | |
| 252 | |
| 253 } | |
| 254 else if ( evt == PROJECTM_VIDEOQUIT ) { | |
| 255 | |
|
663
aaab0bede198
[svn] Cast those pointers properly, and do not use deprecated GTK+ functions.
chainsaw
parents:
358
diff
changeset
|
256 (void) g_idle_add ((GSourceFunc) disable_projectm, NULL); |
| 358 | 257 /* if(quit_timer == NULL) |
| 258 quit_timer = SDL_AddTimer(500, disable_projectm, NULL);*/ | |
| 259 } | |
| 260 | |
| 261 } | |
| 262 | |
| 263 | |
| 264 /** Render the new frame */ | |
| 265 | |
| 266 renderFrame( globalPM ); | |
| 267 | |
| 268 SDL_GL_SwapBuffers(); | |
| 269 } | |
| 270 | |
| 271 | |
| 272 | |
| 273 printf("Worker thread: Exiting\n"); | |
| 274 if(title_timer) SDL_RemoveTimer(title_timer); | |
| 275 g_free(globalPM->title); | |
| 276 free(globalPM->presetURL); | |
| 277 free(globalPM->fontURL); | |
| 278 free(globalPM); | |
| 279 close_display(); | |
| 280 } | |
| 281 | |
| 282 static void projectM_xmms_init(void) | |
| 283 { | |
| 284 | |
| 285 printf("projectM plugin: Initializing\n"); | |
| 286 | |
| 287 SDL_EnableUNICODE(1); | |
| 288 | |
| 289 mutex = SDL_CreateMutex(); | |
| 290 | |
| 291 sem = SDL_CreateSemaphore(1); | |
| 292 | |
| 293 worker_thread = SDL_CreateThread ((void *) worker_func, NULL); | |
| 294 | |
| 295 } | |
| 296 | |
| 297 | |
| 298 | |
| 299 static void projectM_cleanup(void) | |
| 300 { | |
| 301 | |
| 302 SDL_SemPost(sem); | |
| 303 SDL_WaitThread(worker_thread, NULL); | |
| 304 | |
| 305 SDL_DestroySemaphore(sem); | |
| 306 printf("Destroy Semaphore\n"); | |
| 307 SDL_DestroyMutex(mutex); | |
| 308 printf("Destroy Mutex\n"); | |
| 309 | |
| 310 printf("projectM plugin: Cleanup completed\n"); | |
| 311 } | |
| 312 static void projectM_about(void) | |
| 313 { | |
| 314 printf("projectM plugin: About\n"); | |
| 315 } | |
| 316 static void projectM_configure(void) | |
| 317 { | |
| 318 printf("projectM plugin: Configure\n"); | |
| 319 } | |
| 320 static void projectM_playback_start(void) | |
| 321 { | |
| 322 printf("projectM plugin: Playback starting\n"); | |
| 323 } | |
| 324 static void projectM_playback_stop(void) | |
| 325 { | |
| 326 printf("projectM plugin: Playback stopping\n"); | |
| 327 } | |
| 328 static void projectM_render_pcm(gint16 pcm_data[2][512]) | |
| 329 { | |
| 330 | |
| 331 if (0 < SDL_SemValue(sem)) return; | |
| 332 SDL_mutexP(mutex); | |
| 333 | |
| 334 addPCM16Data(pcm_data,512); | |
| 335 | |
| 336 SDL_mutexV(mutex); | |
| 337 | |
| 338 } | |
| 339 | |
| 340 static void projectM_render_freq(gint16 freq_data[2][256]) | |
| 341 { | |
| 342 printf("NO GOOD\n"); | |
| 343 } | |
| 344 | |
| 345 | |
| 346 void read_config() | |
| 347 { | |
| 348 | |
| 349 int n; | |
| 350 | |
| 351 char num[80]; | |
| 352 FILE *in; | |
| 353 FILE *out; | |
| 354 | |
| 355 char* home; | |
| 356 char projectM_home[PATH_MAX]; | |
| 357 char projectM_config[PATH_MAX]; | |
| 358 | |
| 359 strcpy(projectM_config, PROJECTM_DATADIR); | |
| 360 strcpy(projectM_config+strlen(PROJECTM_DATADIR), CONFIG_FILE); | |
| 361 projectM_config[strlen(PROJECTM_DATADIR)+strlen(CONFIG_FILE)]='\0'; | |
| 362 | |
| 363 home=getenv("HOME"); | |
| 364 strcpy(projectM_home, home); | |
| 365 strcpy(projectM_home+strlen(home), "/.projectM/config"); | |
| 366 projectM_home[strlen(home)+strlen("/.projectM/config")]='\0'; | |
| 367 | |
| 368 | |
| 369 if ((in = fopen(projectM_home, "r")) != 0) | |
| 370 { | |
| 371 printf("reading ~/.projectM/config \n"); | |
| 372 } | |
| 373 else | |
| 374 { | |
| 375 printf("trying to create ~/.projectM/config \n"); | |
| 376 | |
| 377 strcpy(projectM_home, home); | |
| 378 strcpy(projectM_home+strlen(home), "/.projectM"); | |
| 379 projectM_home[strlen(home)+strlen("/.projectM")]='\0'; | |
| 380 mkdir(projectM_home,0755); | |
| 381 | |
| 382 strcpy(projectM_home, home); | |
| 383 strcpy(projectM_home+strlen(home), "/.projectM/config"); | |
| 384 projectM_home[strlen(home)+strlen("/.projectM/config")]='\0'; | |
| 385 | |
| 386 if((out = fopen(projectM_home,"w"))!=0) | |
| 387 { | |
| 388 | |
| 389 if ((in = fopen(projectM_config, "r")) != 0) | |
| 390 { | |
| 391 | |
| 392 while(fgets(num,80,in)!=NULL) | |
| 393 { | |
| 394 fputs(num,out); | |
| 395 } | |
| 396 fclose(in); | |
| 397 fclose(out); | |
| 398 | |
| 399 | |
| 400 if ((in = fopen(projectM_home, "r")) != 0) | |
| 401 { printf("created ~/.projectM/config successfully\n"); } | |
| 402 else{printf("This shouldn't happen, using implementation defualts\n");return;} | |
| 403 } | |
| 404 else{printf("Cannot find projectM default config, using implementation defaults\n");return;} | |
| 405 } | |
| 406 else | |
| 407 { | |
| 408 printf("Cannot create ~/.projectM/config, using default config file\n"); | |
| 409 if ((in = fopen(projectM_config, "r")) != 0) | |
| 410 { printf("Successfully opened default config file\n");} | |
| 411 else{ printf("Using implementation defaults, your system is really messed up, I'm suprised we even got this far\n"); return;} | |
| 412 | |
| 413 } | |
| 414 | |
| 415 } | |
| 416 | |
| 417 | |
| 418 | |
| 419 fgets(num, 80, in); fgets(num, 80, in); fgets(num, 80, in); | |
| 420 if(fgets(num, 80, in) != NULL) sscanf (num, "%d", &texsize); | |
| 421 | |
| 422 fgets(num, 80, in); | |
| 423 if(fgets(num, 80, in) != NULL) sscanf (num, "%d", &gx); | |
| 424 | |
| 425 fgets(num, 80, in); | |
| 426 if(fgets(num, 80, in) != NULL) sscanf (num, "%d", &gy); | |
| 427 | |
| 428 fgets(num, 80, in); | |
| 429 if(fgets(num, 80, in) != NULL) sscanf (num, "%d", &wvw); | |
| 430 | |
| 431 fgets(num, 80, in); | |
| 432 if(fgets(num, 80, in) != NULL) sscanf (num, "%d", &wvh); | |
| 433 | |
| 434 fgets(num, 80, in); | |
| 435 if(fgets(num, 80, in) != NULL) sscanf (num, "%d", &fvw); | |
| 436 | |
| 437 fgets(num, 80, in); | |
| 438 if(fgets(num, 80, in) != NULL) sscanf (num, "%d", &fvh); | |
| 439 | |
| 440 fgets(num, 80, in); | |
| 441 if(fgets(num, 80, in) != NULL) sscanf (num, "%d", &fps); | |
| 442 | |
| 443 fgets(num, 80, in); | |
| 444 if(fgets(num, 80, in) != NULL) sscanf (num, "%d", &fullscreen); | |
| 445 /* | |
| 446 fgets(num, 80, in); | |
| 447 fgets(num, 80, in); | |
| 448 | |
| 449 n=0; | |
| 450 while (num[n]!=' ' && num[n]!='\n' && n < 80 && num[n]!=EOF) | |
| 451 { | |
| 452 disp[n]=num[n]; | |
| 453 n++; | |
| 454 } | |
| 455 disp[n]=0; | |
| 456 | |
| 457 | |
| 458 // sprintf(disp,"%s",num ); | |
| 459 setenv("DISPLAY",disp,1); | |
| 460 printf("%s %d\n", disp,strlen(disp)); | |
| 461 setenv("LD_PRELOAD", "/usr/lib/tls/libGL.so.1.0.4496", 1); | |
| 462 */ | |
| 463 fclose(in); | |
| 464 | |
| 465 } |
