Debian Codegraph @

blinkenlights.c

raw symbols ◀ ▶ SCIP
2 Debian patches touch this file
1 /* ---------------------- blinkenlights~ -------------------------------------- */
2 /* */
3 /* Blinkenlights is a BL movies player but also a pixel grid */
4 /* Written by Yves Degoyon ([email protected]). */
5 /* */
6 /* This program is free software; you can redistribute it and/or */
7 /* modify it under the terms of the GNU General Public License */
8 /* as published by the Free Software Foundation; either version 2 */
9 /* of the License, or (at your option) any later version. */
10 /* */
11 /* See file LICENSE for further informations on licensing terms. */
12 /* */
13 /* This program is distributed in the hope that it will be useful, */
14 /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
15 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
16 /* GNU General Public License for more details. */
17 /* */
18 /* You should have received a copy of the GNU General Public License */
19 /* along with this program; if not, write to the Free Software */
20 /* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
21 /* */
22 /* Based on PureData by Miller Puckette and others. */
23 /* */
24 /* Currently listening : */
25 /* Pop Group : We Are All Prostitutes */
26 /* Culturcide : Bruce */
27 /* ---------------------------------------------------------------------------- */
28
29 //added functions:
30 //"static void blinkenlights_findframes(t_blinkenlights *x)"
31 //this is to remember the frame positions in the .blm file
32
33 //"static void blinkenlights_goto(t_blinkenlights* x, t_float frame)"
34 //with a "goto $1" message, you can stratch the blm film. note that the
35 //range of $1 is 0 to 1
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" /* standard pd stuff */
54 #include "g_canvas.h" /* some pd's graphical functions */
55
56 /* support older Pd versions without sys_fopen(), sys_fclose(), and, sys_close() */
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; /* number of pixels ( width ) */
113 t_int x_height; /* number of pixels ( height) */
114 t_int x_xsize; /* x size of each pixel */
115 t_int x_ysize; /* y size of each pixel */
116 char *x_background; /* color of the background #RRGGBB */
117 char *x_foreground; /* color of the foreground #RRGGBB */
118 t_int x_ecanvas; /* flag that indicates if the canvas has been created */
119 t_glist *x_glist; /* graphic context */
120 FILE *x_filed; /* file descriptor */
121 t_int x_timer; /* timer read from bl movie */
122 t_int *x_frame; /* frame contents */
123 t_clock *x_clock; /* clock used for reading frames */
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 /* clean up */
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 /* close the current movie */
473 static void blinkenlights_close(t_blinkenlights *x)
474 {
475 /* closing previous file descriptor */
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 /* read the next frame */
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 //post( "blinkenlights: being readframe:>%s<", lineread );
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 // skip header and empty lines
514 while ( lineread[0] == '#' || lineread[0] == '\n' || lineread[0] == '\0' )
515 {
516
517 //post( "blinkenlights : skipped line : >%s<", lineread );
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 // post( "blinkenlights : setting timer to %d", x->x_timer );
536 }
537
538 // read the contents of one frame
539
540 // when reading first frame the height and width are read from the file
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 // post( "blinkenlights : lineread : %s", lineread );
556
557 nwidth = strlen( lineread )-1; // because of the carriage return
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 /* ascii value for '0' */;
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 // post( "pixoff %d %d", pint+1, flineno );
582 blinkenlights_pixoff( x, pint+1, flineno );
583 break;
584 case 1:
585 // post( "pixon %d %d", pint+1, flineno );
586 blinkenlights_pixon( x, pint+1, flineno );
587 break;
588 default:
589 // post("blinkenlights : wrong value found for pixel : %d (c=%c)", newvalue, *(lineread+pint) );
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 //--chun's functions begin here...
617 //-------------------------------------------------------------------------
618 /* remember all the frame positions */
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 //char *lineread = (char*) getbytes( BL_MAX_LENGTH );
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 // post( "blinkenlights : lineread : %s", lineread );
670
671 nwidth = strlen( lineread )-1; // because of the carriage return
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 /* ascii value for '0' */;
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 // post( "pixoff %d %d", pint+1, flineno );
696 blinkenlights_pixoff( x, pint+1, flineno );
697 break;
698 case 1:
699 // post( "pixon %d %d", pint+1, flineno );
700 blinkenlights_pixon( x, pint+1, flineno );
701 break;
702 default:
703 // post("blinkenlights : wrong value found for pixel : %d (c=%c)", newvalue, *(lineread+pint) );
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 //post("frame %d", x->frame_inc);
734 }
735
736 static void blinkenlights_timer2(t_blinkenlights* x, t_float timer)
737 {
738 x->x_timer2 = timer;
739 //post("frame %d", x->frame_inc);
740 }
741
742 /* open movie */
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 /* closing previous file descriptor */
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 // don't read the first frame when open..
775 //blinkenlights_readframe(x);
776 blinkenlights_findframes(x);
777 }
778
779 /* play frames */
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 /* play frames2 */
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 /* play movie */
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 /* vj movie */
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 /* stop movie */
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 /* jump to next frame */
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 }