| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
#include <sys/types.h> |
| 39 |
#include <string.h> |
| 40 |
#include <stdio.h> |
| 41 |
#include <errno.h> |
| 42 |
#include <stdlib.h> |
| 43 |
#include <fcntl.h> |
| 44 |
#include <sys/stat.h> |
| 45 |
#ifndef __APPLE__ |
| 46 |
#include <malloc.h> |
| 47 |
#endif |
| 48 |
#include <ctype.h> |
| 49 |
#include <unistd.h> |
| 50 |
#include <time.h> |
| 51 |
#include <sys/time.h> |
| 52 |
|
| 53 |
#include "m_pd.h" |
| 54 |
#include "g_canvas.h" |
| 55 |
|
| 56 |
|
| 57 |
#if PD_MAJOR_VERSION == 0 && PD_MINOR_VERSION < 44 |
| 58 |
#define sys_fopen fopen |
| 59 |
#define sys_fclose fclose |
| 60 |
#define sys_close close |
| 61 |
#endif |
| 62 |
|
| 63 |
static char *blinkenlights_version = "blinkenlights: a blinkenlights movies player version 0.2 ( bugs @ [email protected] and [email protected] )"; |
| 64 |
|
| 65 |
static int guidebug=0; |
| 66 |
|
| 67 |
#define SYS_VGUI2(a,b) if (guidebug) \ |
| 68 |
post(a,b);\ |
| 69 |
sys_vgui(a,b) |
| 70 |
|
| 71 |
#define SYS_VGUI3(a,b,c) if (guidebug) \ |
| 72 |
post(a,b,c);\ |
| 73 |
sys_vgui(a,b,c) |
| 74 |
|
| 75 |
#define SYS_VGUI4(a,b,c,d) if (guidebug) \ |
| 76 |
post(a,b,c,d);\ |
| 77 |
sys_vgui(a,b,c,d) |
| 78 |
|
| 79 |
#define SYS_VGUI5(a,b,c,d,e) if (guidebug) \ |
| 80 |
post(a,b,c,d,e);\ |
| 81 |
sys_vgui(a,b,c,d,e) |
| 82 |
|
| 83 |
#define SYS_VGUI6(a,b,c,d,e,f) if (guidebug) \ |
| 84 |
post(a,b,c,d,e,f);\ |
| 85 |
sys_vgui(a,b,c,d,e,f) |
| 86 |
|
| 87 |
#define SYS_VGUI7(a,b,c,d,e,f,g) if (guidebug) \ |
| 88 |
post(a,b,c,d,e,f,g);\ |
| 89 |
sys_vgui(a,b,c,d,e,f,g) |
| 90 |
|
| 91 |
#define SYS_VGUI8(a,b,c,d,e,f,g,h) if (guidebug) \ |
| 92 |
post(a,b,c,d,e,f,g,h);\ |
| 93 |
sys_vgui(a,b,c,d,e,f,g,h) |
| 94 |
|
| 95 |
#define SYS_VGUI9(a,b,c,d,e,f,g,h,i) if (guidebug) \ |
| 96 |
post(a,b,c,d,e,f,g,h,i);\ |
| 97 |
sys_vgui(a,b,c,d,e,f,g,h,i) |
| 98 |
|
| 99 |
#define SYS_VGUI10(a,b,c,d,e,f,g,h,i,j) if (guidebug) \ |
| 100 |
post(a,b,c,d,e,f,g,h,i,j);\ |
| 101 |
sys_vgui(a,b,c,d,e,f,g,h,i,j) |
| 102 |
|
| 103 |
#define BL_BACKGROUND_COLOR "#000000" |
| 104 |
#define BL_FOREGROUND_COLOR "#00FF00" |
| 105 |
#define BL_MAX_LENGTH 1024 |
| 106 |
|
| 107 |
static t_class *blinkenlights_class; |
| 108 |
|
| 109 |
typedef struct _blinkenlights |
| 110 |
{ |
| 111 |
t_object x_obj; |
| 112 |
t_int x_width; |
| 113 |
t_int x_height; |
| 114 |
t_int x_xsize; |
| 115 |
t_int x_ysize; |
| 116 |
char *x_background; |
| 117 |
char *x_foreground; |
| 118 |
t_int x_ecanvas; |
| 119 |
t_glist *x_glist; |
| 120 |
FILE *x_filed; |
| 121 |
t_int x_timer; |
| 122 |
t_int *x_frame; |
| 123 |
t_clock *x_clock; |
| 124 |
t_outlet *outlet_bang; |
| 125 |
t_int frame_no; |
| 126 |
t_int frame_pos[BL_MAX_LENGTH]; |
| 127 |
t_clock *x_clock2; |
| 128 |
t_int x_timer2; |
| 129 |
t_float frame_inc; |
| 130 |
|
| 131 |
} t_blinkenlights; |
| 132 |
|
| 133 |
static void blinkenlights_close(t_blinkenlights *x); |
| 134 |
|
| 135 |
|
| 136 |
static void blinkenlights_free(t_blinkenlights *x) |
| 137 |
{ |
| 138 |
post( "blinkenlights : freeing colors" ); |
| 139 |
if ( x->x_background ) freebytes( x->x_background, 8 ); |
| 140 |
if ( x->x_foreground ) freebytes( x->x_foreground, 8 ); |
| 141 |
post( "blinkenlights : closing file" ); |
| 142 |
blinkenlights_close(x); |
| 143 |
post( "blinkenlights : cancelling clock" ); |
| 144 |
if ( x->x_clock != NULL ) |
| 145 |
{ |
| 146 |
clock_unset( x->x_clock ); |
| 147 |
clock_free( x->x_clock ); |
| 148 |
} |
| 149 |
if ( x->x_clock2 != NULL ) |
| 150 |
{ |
| 151 |
clock_unset( x->x_clock2 ); |
| 152 |
clock_free( x->x_clock2 ); |
| 153 |
} |
| 154 |
post( "blinkenlights : done" ); |
| 155 |
} |
| 156 |
|
| 157 |
static void *blinkenlights_new(t_float fwidth, t_float fheight, t_float fxpixsize, t_float fypixsize ) |
| 158 |
{ |
| 159 |
t_blinkenlights *x = (t_blinkenlights *)pd_new(blinkenlights_class); |
| 160 |
if ( fwidth <= 0 ) |
| 161 |
{ |
| 162 |
post( "blinkenlights: wrong creation argument : width : %f", fwidth ); |
| 163 |
return NULL; |
| 164 |
} |
| 165 |
if ( fheight <= 0 ) |
| 166 |
{ |
| 167 |
post( "blinkenlights: wrong creation argument : height : %f", fheight ); |
| 168 |
return NULL; |
| 169 |
} |
| 170 |
if ( fxpixsize <= 0 ) |
| 171 |
{ |
| 172 |
post( "blinkenlights: wrong creation argument : x pixel size : %f", fxpixsize ); |
| 173 |
return NULL; |
| 174 |
} |
| 175 |
if ( fypixsize <= 0 ) |
| 176 |
{ |
| 177 |
post( "blinkenlights: wrong creation argument : y pixel size : %f", fypixsize ); |
| 178 |
return NULL; |
| 179 |
} |
| 180 |
x->x_width = (int) fwidth; |
| 181 |
x->x_height = (int) fheight; |
| 182 |
x->x_xsize = (int) fxpixsize; |
| 183 |
x->x_ysize = (int) fypixsize; |
| 184 |
x->x_ecanvas = 0; |
| 185 |
x->x_filed = NULL; |
| 186 |
x->x_frame = NULL; |
| 187 |
x->x_clock = NULL; |
| 188 |
x->x_glist = (t_glist *) canvas_getcurrent(); |
| 189 |
x->x_background = (char*) getbytes( 8 ); |
| 190 |
strncpy( x->x_background, BL_BACKGROUND_COLOR, 7 ); |
| 191 |
x->x_background[7] = '\0'; |
| 192 |
x->x_foreground = (char*) getbytes( 8 ); |
| 193 |
strncpy( x->x_foreground, BL_FOREGROUND_COLOR, 7 ); |
| 194 |
x->x_foreground[7] = '\0'; |
| 195 |
x->x_timer2 = 40; |
| 196 |
x->frame_inc = 0; |
| 197 |
|
| 198 |
x->outlet_bang = outlet_new(&x->x_obj, &s_bang); |
| 199 |
|
| 200 |
return(x); |
| 201 |
} |
| 202 |
|
| 203 |
static void blinkenlights_draw_new(t_blinkenlights* x) |
| 204 |
{ |
| 205 |
t_canvas *canvas=glist_getcanvas(x->x_glist); |
| 206 |
t_int xi, yi; |
| 207 |
|
| 208 |
SYS_VGUI4("toplevel .x%lx -width %d -height %d -borderwidth 0 -background #000000\n", x, |
| 209 |
x->x_width*x->x_xsize, x->x_height*x->x_ysize ); |
| 210 |
SYS_VGUI2("frame .x%lx.m -relief raised -bd 2\n", x); |
| 211 |
SYS_VGUI2("wm title .x%lx blinkenlights\n", x); |
| 212 |
|
| 213 |
SYS_VGUI4("canvas .x%lx.c -width %d -height %d\n", |
| 214 |
x, |
| 215 |
x->x_width*x->x_xsize, x->x_height*x->x_ysize ); |
| 216 |
x->x_ecanvas = 1; |
| 217 |
|
| 218 |
for ( xi=1; xi<=x->x_width; xi++ ) |
| 219 |
{ |
| 220 |
for ( yi=1; yi<=x->x_height; yi++ ) |
| 221 |
{ |
| 222 |
SYS_VGUI10(".x%lx.c create rectangle %d %d %d %d -fill %s -outline #555555 -tags %xPIX%.5d%.5d\n", |
| 223 |
x, |
| 224 |
(xi-1)*x->x_xsize, (yi-1)*x->x_ysize, |
| 225 |
xi*x->x_xsize, yi*x->x_ysize, |
| 226 |
x->x_background, |
| 227 |
x, xi, yi); |
| 228 |
} |
| 229 |
} |
| 230 |
|
| 231 |
SYS_VGUI2("pack .x%lx.c -side left -expand 1 -fill both\n", x); |
| 232 |
SYS_VGUI2("pack .x%lx.m -side top -fill x\n", x); |
| 233 |
SYS_VGUI2("wm geometry .x%lx +0+0\n", x); |
| 234 |
SYS_VGUI2("wm geometry .x%lx +0+0\n", x); |
| 235 |
|
| 236 |
} |
| 237 |
|
| 238 |
static void blinkenlights_create(t_blinkenlights* x) |
| 239 |
{ |
| 240 |
|
| 241 |
if ( x->x_ecanvas ) |
| 242 |
{ |
| 243 |
post("blinkenlights : create : canvas already exists" ); |
| 244 |
return; |
| 245 |
} |
| 246 |
|
| 247 |
blinkenlights_draw_new(x); |
| 248 |
|
| 249 |
} |
| 250 |
|
| 251 |
static void blinkenlights_erase(t_blinkenlights* x) |
| 252 |
{ |
| 253 |
t_canvas *canvas=glist_getcanvas(x->x_glist); |
| 254 |
t_int xi, yi; |
| 255 |
|
| 256 |
for ( xi=1; xi<=x->x_width; xi++ ) |
| 257 |
{ |
| 258 |
for ( yi=1; yi<=x->x_height; yi++ ) |
| 259 |
{ |
| 260 |
SYS_VGUI5(".x%lx.c delete %xPIX%.5d%.5d\n", x, x, xi, yi); |
| 261 |
} |
| 262 |
} |
| 263 |
SYS_VGUI2("destroy .x%lx\n", x); |
| 264 |
x->x_ecanvas=0; |
| 265 |
} |
| 266 |
|
| 267 |
static void blinkenlights_destroy(t_blinkenlights* x) |
| 268 |
{ |
| 269 |
if ( !x->x_ecanvas ) |
| 270 |
{ |
| 271 |
post("blinkenlights : destroy : canvas does not exist" ); |
| 272 |
return; |
| 273 |
} |
| 274 |
|
| 275 |
blinkenlights_erase(x); |
| 276 |
} |
| 277 |
|
| 278 |
static void blinkenlights_width(t_blinkenlights* x, t_float fwidth) |
| 279 |
{ |
| 280 |
if ( fwidth <= 0 ) |
| 281 |
{ |
| 282 |
post( "blinkenlights~: wrong width : ignored" ); |
| 283 |
return; |
| 284 |
} |
| 285 |
else |
| 286 |
{ |
| 287 |
if ( x->x_width == (int)fwidth ) return; |
| 288 |
x->x_width=(int)fwidth; |
| 289 |
} |
| 290 |
if (x->x_ecanvas) blinkenlights_erase( x ); |
| 291 |
blinkenlights_draw_new( x ); |
| 292 |
} |
| 293 |
|
| 294 |
static void blinkenlights_height(t_blinkenlights* x, t_float fheight) |
| 295 |
{ |
| 296 |
if ( fheight <= 0 ) |
| 297 |
{ |
| 298 |
post( "blinkenlights~: wrong height : ignored" ); |
| 299 |
return; |
| 300 |
} |
| 301 |
else |
| 302 |
{ |
| 303 |
if ( x->x_height == (int)fheight ) return; |
| 304 |
x->x_height=(int)fheight; |
| 305 |
} |
| 306 |
if (x->x_ecanvas) blinkenlights_erase( x ); |
| 307 |
blinkenlights_draw_new( x ); |
| 308 |
} |
| 309 |
|
| 310 |
static void blinkenlights_xsize(t_blinkenlights* x, t_float fxsize) |
| 311 |
{ |
| 312 |
if ( fxsize <= 0 ) |
| 313 |
{ |
| 314 |
post( "blinkenlights~: wrong x pixel size : ignored" ); |
| 315 |
return; |
| 316 |
} |
| 317 |
else |
| 318 |
{ |
| 319 |
if ( x->x_xsize == (int)fxsize ) return; |
| 320 |
x->x_xsize=(int)fxsize; |
| 321 |
} |
| 322 |
if (x->x_ecanvas) blinkenlights_erase( x ); |
| 323 |
blinkenlights_draw_new( x ); |
| 324 |
} |
| 325 |
|
| 326 |
static void blinkenlights_ysize(t_blinkenlights* x, t_float fysize) |
| 327 |
{ |
| 328 |
if ( fysize <= 0 ) |
| 329 |
{ |
| 330 |
post( "blinkenlights~: wrong y pixel size : ignored" ); |
| 331 |
return; |
| 332 |
} |
| 333 |
else |
| 334 |
{ |
| 335 |
if ( x->x_ysize == (int)fysize ) return; |
| 336 |
x->x_ysize=(int)fysize; |
| 337 |
} |
| 338 |
if (x->x_ecanvas) blinkenlights_erase( x ); |
| 339 |
blinkenlights_draw_new( x ); |
| 340 |
} |
| 341 |
|
| 342 |
static void blinkenlights_background(t_blinkenlights* x, t_float fR, t_float fG, t_float fB) |
| 343 |
{ |
| 344 |
if ( fR <0 || fR>255 ) |
| 345 |
{ |
| 346 |
post("blinkenlights : wrong color component : fR : %d", fR); |
| 347 |
return; |
| 348 |
} |
| 349 |
if ( fG <0 || fG>255 ) |
| 350 |
{ |
| 351 |
post("blinkenlights : wrong color component : fG : %d", fG); |
| 352 |
return; |
| 353 |
} |
| 354 |
if ( fB <0 || fB>255 ) |
| 355 |
{ |
| 356 |
post("blinkenlights : wrong color component : fB : %d", fB); |
| 357 |
return; |
| 358 |
} |
| 359 |
sprintf( x->x_background, "#%.2x%.2x%.2x", (int)fR, (int)fG, (int)fB ); |
| 360 |
post("blinkenlights : background color set to : %s", x->x_background ); |
| 361 |
} |
| 362 |
|
| 363 |
static void blinkenlights_foreground(t_blinkenlights* x, t_float fR, t_float fG, t_float fB) |
| 364 |
{ |
| 365 |
if ( fR <0 || fR>255 ) |
| 366 |
{ |
| 367 |
post("blinkenlights : wrong color component : fR : %d", fR); |
| 368 |
return; |
| 369 |
} |
| 370 |
if ( fG <0 || fG>255 ) |
| 371 |
{ |
| 372 |
post("blinkenlights : wrong color component : fG : %d", fG); |
| 373 |
return; |
| 374 |
} |
| 375 |
if ( fB <0 || fB>255 ) |
| 376 |
{ |
| 377 |
post("blinkenlights : wrong color component : fB : %d", fB); |
| 378 |
return; |
| 379 |
} |
| 380 |
sprintf( x->x_foreground, "#%.2x%.2x%.2x", (int)fR, (int)fG, (int)fB ); |
| 381 |
post("blinkenlights : foreground color set to : %s", x->x_foreground ); |
| 382 |
} |
| 383 |
|
| 384 |
static void blinkenlights_pixon(t_blinkenlights* x, t_float fX, t_float fY) |
| 385 |
{ |
| 386 |
if ( !x->x_ecanvas ) |
| 387 |
{ |
| 388 |
post("blinkenlights : pixon : canvas does not exist" ); |
| 389 |
return; |
| 390 |
} |
| 391 |
if ( fX<1 || fX>x->x_width ) |
| 392 |
{ |
| 393 |
post("blinkenlights : pixon : wrong x coordinate : %d : should be in [1,%d]", (int)fX, x->x_width ); |
| 394 |
return; |
| 395 |
} |
| 396 |
if ( fY<1 || fY>x->x_height ) |
| 397 |
{ |
| 398 |
post("blinkenlights : pixon : wrong y coordinate : %d : should be in [1,%d]", (int)fY, x->x_height ); |
| 399 |
return; |
| 400 |
} |
| 401 |
SYS_VGUI6(".x%lx.c itemconfigure %xPIX%.5d%.5d -fill %s\n", x, x, (int)fX, (int)fY, x->x_foreground ); |
| 402 |
} |
| 403 |
|
| 404 |
static void blinkenlights_pixoff(t_blinkenlights* x, t_float fX, t_float fY) |
| 405 |
{ |
| 406 |
if ( !x->x_ecanvas ) |
| 407 |
{ |
| 408 |
post("blinkenlights : pixoff : canvas does not exist" ); |
| 409 |
return; |
| 410 |
} |
| 411 |
if ( fX<1 || fX>x->x_width ) |
| 412 |
{ |
| 413 |
post("blinkenlights : pixoff : wrong x coordinate : %d : should be in [1,%d]", (int)fX, x->x_width ); |
| 414 |
return; |
| 415 |
} |
| 416 |
if ( fY<1 || fY>x->x_height ) |
| 417 |
{ |
| 418 |
post("blinkenlights : pixoff : wrong y coordinate : %d : should be in [1,%d]", (int)fY, x->x_height ); |
| 419 |
return; |
| 420 |
} |
| 421 |
SYS_VGUI6(".x%lx.c itemconfigure %xPIX%.5d%.5d -fill %s\n", x, x, (int)fX, (int)fY, x->x_background ); |
| 422 |
} |
| 423 |
|
| 424 |
static void blinkenlights_pixel(t_blinkenlights* x, t_float fX, t_float fY, t_float fR, t_float fG, t_float fB) |
| 425 |
{ |
| 426 |
if ( !x->x_ecanvas ) |
| 427 |
{ |
| 428 |
post("blinkenlights : pixel : canvas does not exist" ); |
| 429 |
return; |
| 430 |
} |
| 431 |
if ( fX<1 || fX>x->x_width ) |
| 432 |
{ |
| 433 |
post("blinkenlights : pixel : wrong x coordinate : %d : should be in [1,%d]", (int)fX, x->x_width ); |
| 434 |
return; |
| 435 |
} |
| 436 |
if ( fY<1 || fY>x->x_height ) |
| 437 |
{ |
| 438 |
post("blinkenlights : pixel : wrong y coordinate : %d : should be in [1,%d]", (int)fY, x->x_height ); |
| 439 |
return; |
| 440 |
} |
| 441 |
if ( fR <0 || fR>255 ) |
| 442 |
{ |
| 443 |
post("blinkenlights : pixel : wrong color component : fR : %d", fR); |
| 444 |
return; |
| 445 |
} |
| 446 |
if ( fG <0 || fG>255 ) |
| 447 |
{ |
| 448 |
post("blinkenlights : pixel : wrong color component : fG : %d", fG); |
| 449 |
return; |
| 450 |
} |
| 451 |
if ( fB <0 || fB>255 ) |
| 452 |
{ |
| 453 |
post("blinkenlights : pixel : wrong color component : fB : %d", fB); |
| 454 |
return; |
| 455 |
} |
| 456 |
SYS_VGUI8(".x%lx.c itemconfigure %xPIX%.5d%.5d -fill #%.2X%.2X%.2X\n", x, x, (int)fX, (int)fY, (int)fR, (int)fG, (int)fB ); |
| 457 |
} |
| 458 |
|
| 459 |
static void blinkenlights_clear(t_blinkenlights* x) |
| 460 |
{ |
| 461 |
t_int xi, yi; |
| 462 |
|
| 463 |
for ( xi=1; xi<=x->x_width; xi++ ) |
| 464 |
{ |
| 465 |
for ( yi=1; yi<=x->x_height; yi++ ) |
| 466 |
{ |
| 467 |
blinkenlights_pixoff( x, xi, yi ); |
| 468 |
} |
| 469 |
} |
| 470 |
} |
| 471 |
|
| 472 |
|
| 473 |
static void blinkenlights_close(t_blinkenlights *x) |
| 474 |
{ |
| 475 |
|
| 476 |
if ( x->x_filed != NULL ) |
| 477 |
{ |
| 478 |
if(fclose(x->x_filed) < 0) |
| 479 |
{ |
| 480 |
perror( "blinkenlights : closing file" ); |
| 481 |
} |
| 482 |
x->x_filed = NULL; |
| 483 |
} |
| 484 |
if ( x->x_frame ) |
| 485 |
{ |
| 486 |
freebytes( x->x_frame, x->x_width*x->x_height*sizeof(t_int) ); |
| 487 |
x->x_frame = NULL; |
| 488 |
} |
| 489 |
} |
| 490 |
|
| 491 |
|
| 492 |
static void blinkenlights_readframe(t_blinkenlights *x) |
| 493 |
{ |
| 494 |
char *lineread = (char*) getbytes( BL_MAX_LENGTH ); |
| 495 |
t_int flineno = 0; |
| 496 |
t_int width, height, nwidth; |
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
if ( !x->x_ecanvas ) |
| 501 |
{ |
| 502 |
post("blinkenlights : next : canvas does not exist" ); |
| 503 |
return; |
| 504 |
} |
| 505 |
|
| 506 |
if ( x->x_filed == NULL ) |
| 507 |
{ |
| 508 |
post( "blinkenlights : no file is opened for reading a frame" ); |
| 509 |
blinkenlights_close(x); |
| 510 |
return; |
| 511 |
} |
| 512 |
|
| 513 |
|
| 514 |
while ( lineread[0] == '#' || lineread[0] == '\n' || lineread[0] == '\0' ) |
| 515 |
{ |
| 516 |
|
| 517 |
|
| 518 |
if ( fgets( lineread, BL_MAX_LENGTH, x->x_filed ) == 0 ) |
| 519 |
{ |
| 520 |
post( "blinkenlights : end of file detected : looping..." ); |
| 521 |
fseek( x->x_filed, 0L, SEEK_SET ); |
| 522 |
outlet_bang(x->outlet_bang); |
| 523 |
} |
| 524 |
} |
| 525 |
|
| 526 |
if ( lineread[0] != '@' ) |
| 527 |
{ |
| 528 |
post( "blinkenlights : format error : should find a time lime here : @XXX : got : >%s<", lineread ); |
| 529 |
blinkenlights_close(x); |
| 530 |
return; |
| 531 |
} |
| 532 |
else |
| 533 |
{ |
| 534 |
x->x_timer = atoi( lineread+1 ); |
| 535 |
|
| 536 |
} |
| 537 |
|
| 538 |
|
| 539 |
|
| 540 |
|
| 541 |
height = 0; |
| 542 |
width = 0; |
| 543 |
while ( 1 ) |
| 544 |
{ |
| 545 |
|
| 546 |
if ( fgets( lineread, BL_MAX_LENGTH, x->x_filed ) == NULL ) |
| 547 |
{ |
| 548 |
post( "blinkenlights : EOF not expected here !!! "); |
| 549 |
blinkenlights_close(x); |
| 550 |
return; |
| 551 |
} |
| 552 |
else |
| 553 |
{ |
| 554 |
if ( (lineread[0] == '\0') || (lineread[0] == '#') || (lineread[0] == '\n') ) break; |
| 555 |
|
| 556 |
|
| 557 |
nwidth = strlen( lineread )-1; |
| 558 |
flineno++; |
| 559 |
height = flineno; |
| 560 |
if ( ( nwidth != width ) && ( width != 0 ) ) |
| 561 |
{ |
| 562 |
post( "blinkenlights : weird file : width has changed (nwidth=%d) (width=%d)", nwidth, width ); |
| 563 |
blinkenlights_close( x ); |
| 564 |
return; |
| 565 |
} |
| 566 |
width = nwidth; |
| 567 |
if ( x->x_frame != NULL ) |
| 568 |
{ |
| 569 |
t_int pint = 0; |
| 570 |
t_int newvalue; |
| 571 |
|
| 572 |
while ( pint < width ) |
| 573 |
{ |
| 574 |
newvalue = (int) *(lineread+pint) - 48 ; |
| 575 |
if ( newvalue != *(x->x_frame+(flineno-1)*x->x_width+pint ) ) |
| 576 |
{ |
| 577 |
*(x->x_frame+(flineno-1)*x->x_width+pint ) = newvalue; |
| 578 |
switch ( newvalue ) |
| 579 |
{ |
| 580 |
case 0: |
| 581 |
|
| 582 |
blinkenlights_pixoff( x, pint+1, flineno ); |
| 583 |
break; |
| 584 |
case 1: |
| 585 |
|
| 586 |
blinkenlights_pixon( x, pint+1, flineno ); |
| 587 |
break; |
| 588 |
default: |
| 589 |
|
| 590 |
break; |
| 591 |
} |
| 592 |
} |
| 593 |
pint++; |
| 594 |
} |
| 595 |
} |
| 596 |
if ( x->x_frame == NULL ) x->x_height++; |
| 597 |
} |
| 598 |
} |
| 599 |
if ( x->x_frame == NULL ) |
| 600 |
{ |
| 601 |
if ( x->x_filed != NULL ) if ( fseek(x->x_filed, 0L, SEEK_SET) < 0 ) |
| 602 |
{ |
| 603 |
post( "blinkenlights : could not rewind file" ); |
| 604 |
blinkenlights_close( x ); |
| 605 |
return; |
| 606 |
} |
| 607 |
blinkenlights_width(x, width); |
| 608 |
blinkenlights_height(x, height); |
| 609 |
x->x_frame = ( t_int* ) getbytes( x->x_width*x->x_height*sizeof(t_int) ); |
| 610 |
blinkenlights_readframe(x); |
| 611 |
} |
| 612 |
|
| 613 |
if ( lineread ) freebytes( lineread, BL_MAX_LENGTH ); |
| 614 |
} |
| 615 |
|
| 616 |
|
| 617 |
|
| 618 |
|
| 619 |
static void blinkenlights_findframes(t_blinkenlights *x) |
| 620 |
{ |
| 621 |
int i =0; |
| 622 |
|
| 623 |
x->frame_no = 0; |
| 624 |
|
| 625 |
for(i=0;; i++) |
| 626 |
{ |
| 627 |
char *lineread = (char*) getbytes( BL_MAX_LENGTH ); |
| 628 |
|
| 629 |
fgets( lineread, BL_MAX_LENGTH, x->x_filed ); |
| 630 |
if(strlen(lineread) == 0) break; |
| 631 |
|
| 632 |
if(lineread[0] == '@') |
| 633 |
{ |
| 634 |
x->frame_pos[x->frame_no] = ftell(x->x_filed); |
| 635 |
x->frame_no++; |
| 636 |
} |
| 637 |
if (lineread) freebytes( lineread, BL_MAX_LENGTH ); |
| 638 |
} |
| 639 |
fseek( x->x_filed, 0L, SEEK_SET ); |
| 640 |
post("the end:: %d frames!", x->frame_no); |
| 641 |
} |
| 642 |
|
| 643 |
|
| 644 |
static void blinkenlights_goto(t_blinkenlights* x) |
| 645 |
{ |
| 646 |
|
| 647 |
char lineread[BL_MAX_LENGTH]; |
| 648 |
int current_frame = x->frame_pos[(int)(x->frame_inc * (x->frame_no-1))]; |
| 649 |
int i, n, width, newvalue, height, nwidth; |
| 650 |
|
| 651 |
t_int flineno = 0; |
| 652 |
|
| 653 |
fseek(x->x_filed, current_frame, SEEK_SET); |
| 654 |
|
| 655 |
height = 0; |
| 656 |
width = 0; |
| 657 |
while ( 1 ) |
| 658 |
{ |
| 659 |
|
| 660 |
if ( fgets( lineread, BL_MAX_LENGTH, x->x_filed ) == NULL ) |
| 661 |
{ |
| 662 |
post( "blinkenlights : EOF not expected here !!! "); |
| 663 |
blinkenlights_close(x); |
| 664 |
return; |
| 665 |
} |
| 666 |
else |
| 667 |
{ |
| 668 |
if ( (lineread[0] == '\0') || (lineread[0] == '#') || (lineread[0] == '\n') ) break; |
| 669 |
|
| 670 |
|
| 671 |
nwidth = strlen( lineread )-1; |
| 672 |
flineno++; |
| 673 |
height = flineno; |
| 674 |
if ( ( nwidth != width ) && ( width != 0 ) ) |
| 675 |
{ |
| 676 |
post( "blinkenlights : weird file : width has changed (nwidth=%d) (width=%d)", nwidth, width ); |
| 677 |
blinkenlights_close( x ); |
| 678 |
return; |
| 679 |
} |
| 680 |
width = nwidth; |
| 681 |
if ( x->x_frame != NULL ) |
| 682 |
{ |
| 683 |
t_int pint = 0; |
| 684 |
t_int newvalue; |
| 685 |
|
| 686 |
while ( pint < width ) |
| 687 |
{ |
| 688 |
newvalue = (int) *(lineread+pint) - 48 ; |
| 689 |
if ( newvalue != *(x->x_frame+(flineno-1)*x->x_width+pint ) ) |
| 690 |
{ |
| 691 |
*(x->x_frame+(flineno-1)*x->x_width+pint ) = newvalue; |
| 692 |
switch ( newvalue ) |
| 693 |
{ |
| 694 |
case 0: |
| 695 |
|
| 696 |
blinkenlights_pixoff( x, pint+1, flineno ); |
| 697 |
break; |
| 698 |
case 1: |
| 699 |
|
| 700 |
blinkenlights_pixon( x, pint+1, flineno ); |
| 701 |
break; |
| 702 |
default: |
| 703 |
|
| 704 |
break; |
| 705 |
} |
| 706 |
} |
| 707 |
pint++; |
| 708 |
} |
| 709 |
} |
| 710 |
if ( x->x_frame == NULL ) x->x_height++; |
| 711 |
} |
| 712 |
} |
| 713 |
if ( x->x_frame == NULL ) |
| 714 |
{ |
| 715 |
if ( x->x_filed != NULL ) if ( fseek(x->x_filed, 0L, SEEK_SET) < 0 ) |
| 716 |
{ |
| 717 |
post( "blinkenlights : could not rewind file" ); |
| 718 |
blinkenlights_close( x ); |
| 719 |
return; |
| 720 |
} |
| 721 |
blinkenlights_width(x, width); |
| 722 |
blinkenlights_height(x, height); |
| 723 |
x->x_frame = ( t_int* ) getbytes( x->x_width*x->x_height*sizeof(t_int) ); |
| 724 |
blinkenlights_readframe(x); |
| 725 |
} |
| 726 |
|
| 727 |
} |
| 728 |
|
| 729 |
static void blinkenlights_frame_pos(t_blinkenlights* x, t_float pos) |
| 730 |
{ |
| 731 |
if(pos > 1 | pos < 0) post ("dude, don't be crazy!"); |
| 732 |
else x->frame_inc = pos;; |
| 733 |
|
| 734 |
} |
| 735 |
|
| 736 |
static void blinkenlights_timer2(t_blinkenlights* x, t_float timer) |
| 737 |
{ |
| 738 |
x->x_timer2 = timer; |
| 739 |
|
| 740 |
} |
| 741 |
|
| 742 |
|
| 743 |
static void blinkenlights_open(t_blinkenlights *x, t_symbol *sfile) |
| 744 |
{ |
| 745 |
if ( !x->x_ecanvas ) |
| 746 |
{ |
| 747 |
post("blinkenlights : open : canvas does not exist" ); |
| 748 |
return; |
| 749 |
} |
| 750 |
|
| 751 |
|
| 752 |
|
| 753 |
if ( x->x_filed != NULL ) |
| 754 |
{ |
| 755 |
if(fclose(x->x_filed) < 0) |
| 756 |
{ |
| 757 |
perror( "blinkenlights : closing file" ); |
| 758 |
} |
| 759 |
x->x_filed = NULL; |
| 760 |
} |
| 761 |
if ( x->x_frame ) |
| 762 |
{ |
| 763 |
blinkenlights_clear(x); |
| 764 |
} |
| 765 |
|
| 766 |
|
| 767 |
|
| 768 |
if ( ( x->x_filed = sys_fopen( sfile->s_name, "r" ) ) == NULL ) |
| 769 |
{ |
| •770 |
pd_error(x, "blinkenlights : cannot open >%s<", sfile->s_name); |
| 771 |
return; |
| 772 |
} |
| 773 |
post( "blinkenlights : opened >%s<", sfile->s_name); |
| 774 |
|
| 775 |
|
| 776 |
blinkenlights_findframes(x); |
| 777 |
} |
| 778 |
|
| 779 |
|
| 780 |
static void blinkenlights_playframes(t_blinkenlights *x) |
| 781 |
{ |
| 782 |
blinkenlights_readframe( x ); |
| 783 |
clock_delay( x->x_clock, (double)x->x_timer ); |
| 784 |
} |
| 785 |
|
| 786 |
|
| 787 |
static void blinkenlights_playframes2(t_blinkenlights *x) |
| 788 |
{ |
| 789 |
blinkenlights_goto(x); |
| 790 |
clock_delay( x->x_clock2, (double)x->x_timer2 ); |
| 791 |
} |
| 792 |
|
| 793 |
|
| 794 |
static void blinkenlights_play(t_blinkenlights *x) |
| 795 |
{ |
| 796 |
if ( !x->x_ecanvas ) |
| 797 |
{ |
| 798 |
post("blinkenlights : play : canvas does not exist" ); |
| 799 |
return; |
| 800 |
} |
| 801 |
|
| 802 |
if ( x->x_filed == NULL ) |
| 803 |
{ |
| 804 |
post( "blinkenlights : no file is opened for playing" ); |
| 805 |
blinkenlights_close(x); |
| 806 |
return; |
| 807 |
} |
| 808 |
|
| 809 |
if ( x->x_clock == NULL ) x->x_clock = clock_new( x, (t_method)blinkenlights_playframes); |
| 810 |
clock_delay( x->x_clock, (double)x->x_timer ); |
| 811 |
} |
| 812 |
|
| 813 |
|
| 814 |
static void blinkenlights_vj(t_blinkenlights *x, t_float start_vj) |
| 815 |
{ |
| 816 |
if ( !x->x_ecanvas ) |
| 817 |
{ |
| 818 |
post("blinkenlights : play : canvas does not exist" ); |
| 819 |
return; |
| 820 |
} |
| 821 |
|
| 822 |
if ( x->x_filed == NULL ) |
| 823 |
{ |
| 824 |
post( "blinkenlights : no file is opened for playing" ); |
| 825 |
blinkenlights_close(x); |
| 826 |
return; |
| 827 |
} |
| 828 |
|
| 829 |
if(start_vj) |
| 830 |
{ |
| 831 |
if ( x->x_clock2 == NULL ) x->x_clock2 = clock_new( x, (t_method)blinkenlights_playframes2); |
| 832 |
clock_delay( x->x_clock2, (double)x->x_timer2 ); |
| 833 |
} |
| 834 |
else |
| 835 |
{ |
| 836 |
clock_unset( x->x_clock2 ); |
| 837 |
} |
| 838 |
} |
| 839 |
|
| 840 |
|
| 841 |
static void blinkenlights_stop(t_blinkenlights *x) |
| 842 |
{ |
| 843 |
if ( !x->x_ecanvas ) |
| 844 |
{ |
| 845 |
post("blinkenlights : play : canvas does not exist" ); |
| 846 |
return; |
| 847 |
} |
| 848 |
|
| 849 |
if ( x->x_filed == NULL ) |
| 850 |
{ |
| 851 |
post( "blinkenlights : no file is opened for playing" ); |
| 852 |
blinkenlights_close(x); |
| 853 |
return; |
| 854 |
} |
| 855 |
|
| 856 |
if ( x->x_clock != NULL ) |
| 857 |
{ |
| 858 |
clock_unset( x->x_clock ); |
| 859 |
} |
| 860 |
} |
| 861 |
|
| 862 |
|
| 863 |
static void blinkenlights_next(t_blinkenlights *x) |
| 864 |
{ |
| 865 |
blinkenlights_readframe(x); |
| 866 |
} |
| 867 |
|
| 868 |
void blinkenlights_setup(void) |
| 869 |
{ |
| •870 |
logpost(NULL, 4, "%s", blinkenlights_version); |
| 871 |
blinkenlights_class = class_new(gensym("blinkenlights"), (t_newmethod)blinkenlights_new, |
| 872 |
(t_method)blinkenlights_free, |
| 873 |
sizeof(t_blinkenlights), 0, A_DEFFLOAT, A_DEFFLOAT, A_DEFFLOAT, A_DEFFLOAT, 0); |
| 874 |
class_addmethod( blinkenlights_class, (t_method)blinkenlights_width, gensym("width"), A_FLOAT, 0); |
| 875 |
class_addmethod( blinkenlights_class, (t_method)blinkenlights_height, gensym("height"), A_FLOAT, 0); |
| 876 |
class_addmethod( blinkenlights_class, (t_method)blinkenlights_xsize, gensym("xsize"), A_FLOAT, 0); |
| 877 |
class_addmethod( blinkenlights_class, (t_method)blinkenlights_ysize, gensym("ysize"), A_FLOAT, 0); |
| 878 |
class_addmethod( blinkenlights_class, (t_method)blinkenlights_open, gensym("open"), A_SYMBOL, 0); |
| 879 |
class_addmethod( blinkenlights_class, (t_method)blinkenlights_create, gensym("create"), 0); |
| 880 |
class_addmethod( blinkenlights_class, (t_method)blinkenlights_destroy, gensym("destroy"), 0); |
| 881 |
class_addmethod( blinkenlights_class, (t_method)blinkenlights_next, gensym("next"), 0); |
| 882 |
class_addmethod( blinkenlights_class, (t_method)blinkenlights_play, gensym("play"), 0); |
| 883 |
class_addmethod( blinkenlights_class, (t_method)blinkenlights_stop, gensym("stop"), 0); |
| 884 |
class_addmethod( blinkenlights_class, (t_method)blinkenlights_frame_pos, gensym("goto"), A_FLOAT, 0); |
| 885 |
class_addmethod( blinkenlights_class, (t_method)blinkenlights_vj, gensym("vj"), A_FLOAT, 0); |
| 886 |
class_addmethod( blinkenlights_class, (t_method)blinkenlights_timer2, gensym("timer2"), A_FLOAT, 0); |
| 887 |
class_addmethod( blinkenlights_class, (t_method)blinkenlights_background, gensym("background"), A_FLOAT, A_FLOAT, A_FLOAT, 0); |
| 888 |
class_addmethod( blinkenlights_class, (t_method)blinkenlights_foreground, gensym("foreground"), A_FLOAT, A_FLOAT, A_FLOAT, 0); |
| 889 |
class_addmethod( blinkenlights_class, (t_method)blinkenlights_destroy, gensym("destroy"), 0); |
| 890 |
class_addmethod( blinkenlights_class, (t_method)blinkenlights_pixon, gensym("pixon"), A_FLOAT, A_FLOAT, 0); |
| 891 |
class_addmethod( blinkenlights_class, (t_method)blinkenlights_pixoff, gensym("pixoff"), A_FLOAT, A_FLOAT, 0); |
| 892 |
class_addmethod( blinkenlights_class, (t_method)blinkenlights_pixel, gensym("pixel"), A_FLOAT, A_FLOAT, A_FLOAT, A_FLOAT, A_FLOAT, 0); |
| 893 |
class_addmethod( blinkenlights_class, (t_method)blinkenlights_clear, gensym("clear"), 0); |
| 894 |
} |