most keyboard events working again, but no scrub
[blender.git] / source / blender / windowmanager / intern / wm_playanim.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
17  *
18  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
19  * All rights reserved.
20  *
21  * The Original Code is: all of this file.
22  *
23  * Contributor(s): Campbell Barton
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  */
27
28 /** \file blender/windowmanager/intern/wm_playanim.c
29  *  \ingroup wm
30  *
31  * \note This file uses ghost directly and none of the WM definitions.
32  *       this could be made into its own module, alongside creator/
33  */
34
35 #include <sys/types.h>
36 #include <fcntl.h>
37 #include <stdlib.h>
38 #include <string.h>
39
40 #ifndef WIN32
41 #  include <unistd.h>
42 #  include <sys/times.h>
43 #  include <sys/wait.h>
44 #else
45 #  include <io.h>
46 #endif
47 #include "MEM_guardedalloc.h"
48
49 #include "PIL_time.h"
50
51 #include <math.h>
52
53 #include "BLI_blenlib.h"
54 #include "BLI_math.h"
55
56 #include "IMB_imbuf_types.h"
57 #include "IMB_imbuf.h"
58
59 #include "BKE_blender.h"
60 #include "BKE_global.h"
61 #include "BKE_utildefines.h"
62
63 #include "BIF_gl.h"
64 #include "BIF_glutil.h"
65
66 #ifdef WITH_QUICKTIME
67 #  ifdef _WIN32
68 #    include <QTML.h>
69 #    include <Movies.h>
70 #  elif defined(__APPLE__)
71 #    include <QuickTime/Movies.h>
72 #  endif /* __APPLE__ */
73 #endif /* WITH_QUICKTIME */
74
75 #include "DNA_scene_types.h"
76 #include "BLI_utildefines.h"
77 #include "ED_datafiles.h" /* for fonts */
78 #include "wm_event_types.h"
79 #include "GHOST_C-api.h"
80 #include "BLF_api.h"
81
82 #define WINCLOSE -1
83 #define REDRAW -3
84 #define RESHAPE -2
85 #define WINQUIT -4
86
87 /* use */
88 typedef struct PlayState {
89         short direction;
90         short next;
91         short once;
92         short turbo;
93         short pingpong;
94         short noskip;
95         short sstep;
96         short pause;
97         short wait2;
98         short stopped;
99         short go;
100
101         struct PlayAnimPict *picture;
102
103         /* set once at the start */
104         int ibufx, ibufy;
105         int fontid;
106
107         /* saves passing args */
108         struct ImBuf *curframe_ibuf;
109 } PlayState;
110
111 /* ***************** gl_util.c ****************** */
112
113 static GHOST_SystemHandle g_system = NULL;
114 static void *g_window = NULL;
115
116 static int qualN = 0;
117
118 #define LSHIFT  (1 << 0)
119 #define RSHIFT  (1 << 1)
120 #define SHIFT   (LSHIFT | RSHIFT)
121 #define LALT    (1 << 2)
122 #define RALT    (1 << 3)
123 #define ALT (LALT | RALT)
124 #define LCTRL   (1 << 4)
125 #define RCTRL   (1 << 5)
126 #define LMOUSE  (1 << 16)
127 #define MMOUSE  (1 << 17)
128 #define RMOUSE  (1 << 18)
129 #define MOUSE   (LMOUSE | MMOUSE | RMOUSE)
130
131 unsigned short screen_qread(short *val, char *ascii);
132
133 void playanim_window_get_size(int *width_r, int *height_r)
134 {
135         GHOST_RectangleHandle bounds = GHOST_GetClientBounds(g_window);
136         *width_r = GHOST_GetWidthRectangle(bounds);
137         *height_r = GHOST_GetHeightRectangle(bounds);
138         GHOST_DisposeRectangle(bounds);
139 }
140
141 /* implementation */
142 static void playanim_event_qual_update(void)
143 {
144         int val;
145
146         /* Shift */
147         GHOST_GetModifierKeyState(g_system, GHOST_kModifierKeyLeftShift, &val);
148         if (val) qualN |=  LSHIFT;
149         else     qualN &= ~LSHIFT;
150
151         GHOST_GetModifierKeyState(g_system, GHOST_kModifierKeyRightShift, &val);
152         if (val) qualN |=  RSHIFT;
153         else     qualN &= ~RSHIFT;
154
155         /* Control */
156         GHOST_GetModifierKeyState(g_system, GHOST_kModifierKeyLeftControl, &val);
157         if (val) qualN |=  LCTRL;
158         else     qualN &= ~LCTRL;
159
160         GHOST_GetModifierKeyState(g_system, GHOST_kModifierKeyRightControl, &val);
161         if (val) qualN |=  RCTRL;
162         else     qualN &= ~RCTRL;
163
164         /* Alt */
165         GHOST_GetModifierKeyState(g_system, GHOST_kModifierKeyLeftAlt, &val);
166         if (val) qualN |=  LCTRL;
167         else     qualN &= ~LCTRL;
168
169         GHOST_GetModifierKeyState(g_system, GHOST_kModifierKeyRightAlt, &val);
170         if (val) qualN |=  RCTRL;
171         else     qualN &= ~RCTRL;
172
173         /* LMB */
174         GHOST_GetButtonState(g_system, GHOST_kButtonMaskLeft, &val);
175         if (val) qualN |=  LMOUSE;
176         else     qualN &= ~LMOUSE;
177
178         /* MMB */
179         GHOST_GetButtonState(g_system, GHOST_kButtonMaskMiddle, &val);
180         if (val) qualN |=  MMOUSE;
181         else     qualN &= ~MMOUSE;
182
183         /* RMB */
184         GHOST_GetButtonState(g_system, GHOST_kButtonMaskRight, &val);
185         if (val) qualN |=  RMOUSE;
186         else     qualN &= ~RMOUSE;
187 }
188
189 typedef struct PlayAnimPict {
190         struct PlayAnimPict *next, *prev;
191         char *mem;
192         int size;
193         char *name;
194         struct ImBuf *ibuf;
195         struct anim *anim;
196         int frame;
197         int IB_flags;
198 } PlayAnimPict;
199
200 static struct ListBase _picsbase = {NULL, NULL};
201 static struct ListBase *picsbase = &_picsbase;
202 static int fromdisk = FALSE;
203 static int fstep = 1;
204 static float zoomx = 1.0, zoomy = 1.0;
205 static double ptottime = 0.0, swaptime = 0.04;
206
207 static int pupdate_time(void)
208 {
209         static double ltime;
210         double time;
211
212         time = PIL_check_seconds_timer();
213
214         ptottime += (time - ltime);
215         ltime = time;
216         return (ptottime < 0);
217 }
218
219 static void toscreen(PlayAnimPict *picture, struct ImBuf *ibuf, int fontid)
220 {
221
222         if (ibuf == NULL) {
223                 printf("no ibuf !\n");
224                 return;
225         }
226         if (ibuf->rect == NULL && ibuf->rect_float) {
227                 IMB_rect_from_float(ibuf);
228                 imb_freerectfloatImBuf(ibuf);
229         }
230         if (ibuf->rect == NULL)
231                 return;
232
233         GHOST_ActivateWindowDrawingContext(g_window);
234
235         glRasterPos2f(0.0f, 0.0f);
236
237         glDrawPixels(ibuf->x, ibuf->y, GL_RGBA, GL_UNSIGNED_BYTE, ibuf->rect);
238
239         pupdate_time();
240
241         if (picture && (qualN & (SHIFT | LMOUSE)) && (fontid != -1)) {
242                 char str[32 + FILE_MAX];
243                 cpack(-1);
244 //              glRasterPos2f(0.02f, 0.03f);
245                 BLI_snprintf(str, sizeof(str), "%s | %.2f frames/s", picture->name, fstep / swaptime);
246 //              BMF_DrawString(font, str);
247
248                 BLF_enable(fontid, BLF_ASPECT);
249                 BLF_aspect(fontid, 1.0f / ibuf->x, 1.0f / ibuf->y, 1.0f);
250                 BLF_position(fontid, 0.02f, 0.03f, 0.0f);
251                 BLF_draw(fontid, str, 256); // XXX
252                 printf("Drawing text '%s'\n", str);
253         }
254
255         GHOST_SwapWindowBuffers(g_window);
256 }
257
258 static void build_pict_list(char *first, int totframes, int fstep, int fontid)
259 {
260         char *mem, filepath[FILE_MAX];
261 //      short val;
262         PlayAnimPict *picture = NULL;
263         struct ImBuf *ibuf = NULL;
264         char str[32 + FILE_MAX];
265         struct anim *anim;
266
267         if (IMB_isanim(first)) {
268                 anim = IMB_open_anim(first, IB_rect, 0);
269                 if (anim) {
270                         int pic;
271                         ibuf = IMB_anim_absolute(anim, 0, IMB_TC_NONE, IMB_PROXY_NONE);
272                         if (ibuf) {
273                                 toscreen(NULL, ibuf, fontid);
274                                 IMB_freeImBuf(ibuf);
275                         }
276
277                         for (pic = 0; pic < IMB_anim_get_duration(anim, IMB_TC_NONE); pic++) {
278                                 picture = (PlayAnimPict *)MEM_callocN(sizeof(PlayAnimPict), "Pict");
279                                 picture->anim = anim;
280                                 picture->frame = pic;
281                                 picture->IB_flags = IB_rect;
282                                 BLI_snprintf(str, sizeof(str), "%s : %d", first, pic + 1);
283                                 picture->name = strdup(str);
284                                 BLI_addtail(picsbase, picture);
285                         }
286                 }
287                 else {
288                         printf("couldn't open anim %s\n", first);
289                 }
290         }
291         else {
292                 int count = 0;
293
294                 BLI_strncpy(filepath, first, sizeof(filepath));
295
296                 pupdate_time();
297                 ptottime = 1.0;
298
299                 /* O_DIRECT
300                  *
301                  * If set, all reads and writes on the resulting file descriptor will
302                  * be performed directly to or from the user program buffer, provided
303                  * appropriate size and alignment restrictions are met.  Refer to the
304                  * F_SETFL and F_DIOINFO commands in the fcntl(2) manual entry for
305                  * information about how to determine the alignment constraints.
306                  * O_DIRECT is a Silicon Graphics extension and is only supported on
307                  * local EFS and XFS file systems.
308                  */
309
310                 while (IMB_ispic(filepath) && totframes) {
311                         size_t size;
312                         int file;
313
314                         file = open(filepath, O_BINARY | O_RDONLY, 0);
315                         if (file < 0) return;
316                         picture = (PlayAnimPict *)MEM_callocN(sizeof(PlayAnimPict), "picture");
317                         if (picture == NULL) {
318                                 printf("Not enough memory for pict struct \n");
319                                 close(file);
320                                 return;
321                         }
322                         size = BLI_file_descriptor_size(file);
323
324                         if (size < 1) {
325                                 close(file);
326                                 MEM_freeN(picture);
327                                 return;
328                         }
329
330                         picture->size = size;
331                         picture->IB_flags = IB_rect;
332
333                         if (fromdisk == FALSE) {
334                                 mem = (char *)MEM_mallocN(size, "build pic list");
335                                 if (mem == NULL) {
336                                         printf("Couldn't get memory\n");
337                                         close(file);
338                                         MEM_freeN(picture);
339                                         return;
340                                 }
341
342                                 if (read(file, mem, size) != size) {
343                                         printf("Error while reading %s\n", filepath);
344                                         close(file);
345                                         MEM_freeN(picture);
346                                         MEM_freeN(mem);
347                                         return;
348                                 }
349                         }
350                         else {
351                                 mem = NULL;
352                         }
353
354                         picture->mem = mem;
355                         picture->name = strdup(filepath);
356                         close(file);
357                         BLI_addtail(picsbase, picture);
358                         count++;
359
360                         pupdate_time();
361
362                         if (ptottime > 1.0) {
363                                 if (picture->mem) {
364                                         ibuf = IMB_ibImageFromMemory((unsigned char *)picture->mem, picture->size,
365                                                                      picture->IB_flags, picture->name);
366                                 }
367                                 else {
368                                         ibuf = IMB_loadiffname(picture->name, picture->IB_flags);
369                                 }
370                                 if (ibuf) {
371                                         toscreen(picture, ibuf, fontid);
372                                         IMB_freeImBuf(ibuf);
373                                 }
374                                 pupdate_time();
375                                 ptottime = 0.0;
376                         }
377
378                         BLI_newname(filepath, +fstep);
379
380 #if 0 // XXX25
381                         while (qtest()) {
382                                 switch (qreadN(&val)) {
383                                         case ESCKEY:
384                                                 if (val) return;
385                                                 break;
386                                 }
387                         }
388 #endif
389                         totframes--;
390                 }
391         }
392         return;
393 }
394
395 static int ghost_event_proc(GHOST_EventHandle evt, GHOST_TUserDataPtr ps_void)
396 {
397         PlayState *ps = (PlayState *)ps_void;
398
399         GHOST_TEventType type = GHOST_GetEventType(evt);
400         int val;
401
402         playanim_event_qual_update();
403
404         /* convert ghost event into value keyboard or mouse */
405         val = ELEM(type, GHOST_kEventKeyDown, GHOST_kEventButtonDown);
406
407         if (ps->wait2 && ps->stopped) {
408                 ps->stopped = FALSE;
409         }
410
411         switch (type) {
412                 case GHOST_kEventKeyDown:
413                 case GHOST_kEventKeyUp:
414                 {
415                         GHOST_TEventKeyData *key_data;
416
417                         key_data = (GHOST_TEventKeyData *)GHOST_GetEventData(evt);
418                         switch (key_data->key) {
419                                 case GHOST_kKeyA:
420                                         if (val) ps->noskip = !ps->noskip;
421                                         break;
422                                 case GHOST_kKeyP:
423                                         if (val) ps->pingpong = !ps->pingpong;
424                                         break;
425                                 case GHOST_kKeyNumpad1:
426                                         if (val) swaptime = fstep / 60.0;
427                                         break;
428                                 case GHOST_kKeyNumpad2:
429                                         if (val) swaptime = fstep / 50.0;
430                                         break;
431                                 case GHOST_kKeyNumpad3:
432                                         if (val) swaptime = fstep / 30.0;
433                                         break;
434                                 case GHOST_kKeyNumpad4:
435                                         if (qualN & SHIFT)
436                                                 swaptime = fstep / 24.0;
437                                         else
438                                                 swaptime = fstep / 25.0;
439                                         break;
440                                 case GHOST_kKeyNumpad5:
441                                         if (val) swaptime = fstep / 20.0;
442                                         break;
443                                 case GHOST_kKeyNumpad6:
444                                         if (val) swaptime = fstep / 15.0;
445                                         break;
446                                 case GHOST_kKeyNumpad7:
447                                         if (val) swaptime = fstep / 12.0;
448                                         break;
449                                 case GHOST_kKeyNumpad8:
450                                         if (val) swaptime = fstep / 10.0;
451                                         break;
452                                 case GHOST_kKeyNumpad9:
453                                         if (val) swaptime = fstep / 6.0;
454                                         break;
455                                 case GHOST_kKeyLeftArrow:
456                                         if (val) {
457                                                 ps->sstep = TRUE;
458                                                 ps->wait2 = FALSE;
459                                                 if (qualN & SHIFT) {
460                                                         ps->picture = picsbase->first;
461                                                         ps->next = 0;
462                                                 }
463                                                 else {
464                                                         ps->next = -1;
465                                                 }
466                                         }
467                                         break;
468                                 case GHOST_kKeyDownArrow:
469                                         if (val) {
470                                                 ps->wait2 = FALSE;
471                                                 if (qualN & SHIFT) {
472                                                         ps->next = ps->direction = -1;
473                                                 }
474                                                 else {
475                                                         ps->next = -10;
476                                                         ps->sstep = TRUE;
477                                                 }
478                                         }
479                                         break;
480                                 case GHOST_kKeyRightArrow:
481                                         if (val) {
482                                                 ps->sstep = TRUE;
483                                                 ps->wait2 = FALSE;
484                                                 if (qualN & SHIFT) {
485                                                         ps->picture = picsbase->last;
486                                                         ps->next = 0;
487                                                 }
488                                                 else {
489                                                         ps->next = 1;
490                                                 }
491                                         }
492                                         break;
493                                 case GHOST_kKeyUpArrow:
494                                         if (val) {
495                                                 ps->wait2 = FALSE;
496                                                 if (qualN & SHIFT) {
497                                                         ps->next = ps->direction = 1;
498                                                 }
499                                                 else {
500                                                         ps->next = 10;
501                                                         ps->sstep = TRUE;
502                                                 }
503                                         }
504                                         break;
505
506                                 case GHOST_kKeySlash:
507                                 case GHOST_kKeyNumpadSlash:
508                                         if (val) {
509                                                 if (qualN & SHIFT) {
510                                                         if (ps->curframe_ibuf)
511                                                                 printf(" Name: %s | Speed: %.2f frames/s\n", ps->curframe_ibuf->name, fstep / swaptime);
512                                                 }
513                                                 else {
514                                                         swaptime = fstep / 5.0;
515                                                 }
516                                         }
517                                         break;
518 #if 0
519                                 case LEFTMOUSE:
520                                 case MOUSEX:
521                                         if (qualN & LMOUSE) {
522                                                 int sizex, sizey;
523                                                 int i;
524                                                 playanim_window_get_size(&sizex, &sizey);
525                                                 ps->picture = picsbase->first;
526                                                 i = 0;
527                                                 while (ps->picture) {
528                                                         i++;
529                                                         ps->picture = ps->picture->next;
530                                                 }
531                                                 i = (i * val) / sizex;
532                                                 ps->picture = picsbase->first;
533                                                 for (; i > 0; i--) {
534                                                         if (ps->picture->next == NULL) break;
535                                                         ps->picture = ps->picture->next;
536                                                 }
537                                                 ps->sstep = TRUE;
538                                                 ps->wait2 = FALSE;
539                                                 ps->next = 0;
540                                         }
541                                         break;
542 #endif
543                                 case GHOST_kKeyEqual:
544                                         if (val) {
545                                                 if (qualN & SHIFT) {
546                                                         ps->pause++;
547                                                         printf("pause:%d\n", ps->pause);
548                                                 }
549                                                 else {
550                                                         swaptime /= 1.1;
551                                                 }
552                                         }
553                                         break;
554                                 case GHOST_kKeyMinus:
555                                         if (val) {
556                                                 if (qualN & SHIFT) {
557                                                         ps->pause--;
558                                                         printf("pause:%d\n", ps->pause);
559                                                 }
560                                                 else {
561                                                         swaptime *= 1.1;
562                                                 }
563                                         }
564                                         break;
565                                 case GHOST_kKeyNumpad0:
566                                         if (val) {
567                                                 if (ps->once) {
568                                                         ps->once = ps->wait2 = FALSE;
569                                                 }
570                                                 else {
571                                                         ps->picture = NULL;
572                                                         ps->once = TRUE;
573                                                         ps->wait2 = FALSE;
574                                                 }
575                                         }
576                                         break;
577                                 case GHOST_kKeyEnter:
578                                 case GHOST_kKeyNumpadEnter:
579                                         if (val) {
580                                                 ps->wait2 = ps->sstep = FALSE;
581                                         }
582                                         break;
583                                 case GHOST_kKeyNumpadPeriod:
584                                         if (val) {
585                                                 if (ps->sstep) ps->wait2 = FALSE;
586                                                 else {
587                                                         ps->sstep = TRUE;
588                                                         ps->wait2 = !ps->wait2;
589                                                 }
590                                         }
591                                         break;
592                                 case GHOST_kKeyNumpadPlus:
593                                         if (val == 0) break;
594                                         zoomx += 2.0;
595                                         zoomy += 2.0;
596                                         /* no break??? - is this intentional? - campbell XXX25 */
597                                 case GHOST_kKeyNumpadMinus:
598                                 {
599                                         int sizex, sizey;
600                                         /* int ofsx, ofsy; */ /* UNUSED */
601
602                                         if (val == 0) break;
603                                         if (zoomx > 1.0) zoomx -= 1.0;
604                                         if (zoomy > 1.0) zoomy -= 1.0;
605                                         // playanim_window_get_position(&ofsx, &ofsy);
606                                         playanim_window_get_size(&sizex, &sizey);
607                                         /* ofsx += sizex / 2; */ /* UNUSED */
608                                         /* ofsy += sizey / 2; */ /* UNUSED */
609                                         sizex = zoomx * ps->ibufx;
610                                         sizey = zoomy * ps->ibufy;
611                                         /* ofsx -= sizex / 2; */ /* UNUSED */
612                                         /* ofsy -= sizey / 2; */ /* UNUSED */
613                                         // window_set_position(g_window,sizex,sizey);
614                                         GHOST_SetClientSize(g_window, sizex, sizey);
615                                         break;
616                                 }
617                                 case GHOST_kKeyEsc:
618                                         ps->go = FALSE;
619                                         break;
620                                 default:
621                                         break;
622                         }
623                         break;
624                 }
625                 case GHOST_kEventWindowSize:
626                 case GHOST_kEventWindowMove:
627                 {
628                         int sizex, sizey;
629
630                         playanim_window_get_size(&sizex, &sizey);
631                         GHOST_ActivateWindowDrawingContext(g_window);
632
633                         glViewport(0, 0, sizex, sizey);
634                         glScissor(0, 0, sizex, sizey);
635
636                         zoomx = (float) sizex / ps->ibufx;
637                         zoomy = (float) sizey / ps->ibufy;
638                         zoomx = floor(zoomx + 0.5);
639                         zoomy = floor(zoomy + 0.5);
640                         if (zoomx < 1.0) zoomx = 1.0;
641                         if (zoomy < 1.0) zoomy = 1.0;
642
643                         sizex = zoomx * ps->ibufx;
644                         sizey = zoomy * ps->ibufy;
645
646                         glPixelZoom(zoomx, zoomy);
647                         glEnable(GL_DITHER);
648                         ptottime = 0.0;
649                         toscreen(ps->picture, ps->curframe_ibuf, ps->fontid);
650                         //XXX25                         while (qtest()) qreadN(&val);
651
652                         break;
653                 }
654                 case GHOST_kEventQuit:
655                 case GHOST_kEventWindowClose:
656                 {
657                         ps->go = FALSE;
658                         break;
659                 }
660                 default:
661                         /* quiet warnings */
662                         break;
663         }
664
665         return 1;
666 }
667
668 void playanim_window_open(const char *title, int posx, int posy, int sizex, int sizey, int start_maximized)
669 {
670         GHOST_TWindowState inital_state;
671         GHOST_TUns32 scr_w, scr_h;
672
673         GHOST_GetMainDisplayDimensions(g_system, &scr_w, &scr_h);
674
675         posy = (scr_h - posy - sizey);
676
677         if (start_maximized == G_WINDOWSTATE_FULLSCREEN)
678                 inital_state = start_maximized ? GHOST_kWindowStateFullScreen : GHOST_kWindowStateNormal;
679         else
680                 inital_state = start_maximized ? GHOST_kWindowStateMaximized : GHOST_kWindowStateNormal;
681 #if defined(__APPLE__) && !defined(GHOST_COCOA)
682         {
683                 extern int macPrefState; /* creator.c */
684                 initial_state += macPrefState;
685         }
686 #endif
687
688         g_window = GHOST_CreateWindow(g_system,
689                                       title,
690                                       posx, posy, sizex, sizey,
691                                       inital_state,
692                                       GHOST_kDrawingContextTypeOpenGL,
693                                       FALSE /* no stereo */, FALSE);
694
695         //if (ghostwin) {
696         //if (win) {
697         // GHOST_SetWindowUserData(ghostwin, win);
698         //} else {
699         //      GHOST_DisposeWindow(g_system, ghostwin);
700         //}
701         //}
702 }
703
704
705 void playanim(int argc, const char **argv)
706 {
707         struct ImBuf *ibuf = NULL;
708         char filepath[FILE_MAX];
709         GHOST_TUns32 maxwinx, maxwiny;
710         /* short c233 = FALSE, yuvx = FALSE; */ /* UNUSED */
711         int i;
712         /* This was done to disambiguate the name for use under c++. */
713         struct anim *anim = NULL;
714         int start_x = 0, start_y = 0;
715         int sfra = -1;
716         int efra = -1;
717         int totblock;
718
719         PlayState ps = {0};
720
721         /* ps.doubleb   = TRUE;*/ /* UNUSED */
722         ps.go        = TRUE;
723         ps.direction = TRUE;
724         ps.next      = TRUE;
725         ps.once      = FALSE;
726         ps.turbo     = FALSE;
727         ps.pingpong  = FALSE;
728         ps.noskip    = FALSE;
729         ps.sstep     = FALSE;
730         ps.pause     = FALSE;
731         ps.wait2     = FALSE;
732         ps.stopped   = FALSE;
733         ps.picture   = NULL;
734         /* resetmap = FALSE */
735
736         ps.fontid = -1;
737
738         while (argc > 1) {
739                 if (argv[1][0] == '-') {
740                         switch (argv[1][1]) {
741                                 case 'm':
742                                         fromdisk = TRUE;
743                                         break;
744                                 case 'p':
745                                         if (argc > 3) {
746                                                 start_x = atoi(argv[2]);
747                                                 start_y = atoi(argv[3]);
748                                                 argc -= 2;
749                                                 argv += 2;
750                                         }
751                                         else {
752                                                 printf("too few arguments for -p (need 2): skipping\n");
753                                         }
754                                         break;
755                                 case 'f':
756                                         if (argc > 3) {
757                                                 double fps = atof(argv[2]);
758                                                 double fps_base = atof(argv[3]);
759                                                 if (fps == 0.0) {
760                                                         fps = 1;
761                                                         printf("invalid fps,"
762                                                                "forcing 1\n");
763                                                 }
764                                                 swaptime = fps_base / fps;
765                                                 argc -= 2;
766                                                 argv += 2;
767                                         }
768                                         else {
769                                                 printf("too few arguments for -f (need 2): skipping\n");
770                                         }
771                                         break;
772                                 case 's':
773                                         sfra = MIN2(MAXFRAME, MAX2(1, atoi(argv[2]) ));
774                                         argc--;
775                                         argv++;
776                                         break;
777                                 case 'e':
778                                         efra = MIN2(MAXFRAME, MAX2(1, atoi(argv[2]) ));
779                                         argc--;
780                                         argv++;
781                                         break;
782                                 case 'j':
783                                         fstep = MIN2(MAXFRAME, MAX2(1, atoi(argv[2])));
784                                         swaptime *= fstep;
785                                         argc--;
786                                         argv++;
787                                         break;
788                                 default:
789                                         printf("unknown option '%c': skipping\n", argv[1][1]);
790                                         break;
791                         }
792                         argc--;
793                         argv++;
794                 }
795                 else {
796                         break;
797                 }
798         }
799
800 #ifdef WITH_QUICKTIME
801 #if defined(_WIN32) || defined(__APPLE__) && !defined(GHOST_COCOA)
802         /* Initialize QuickTime */
803 #ifndef noErr
804 #define noErr 0
805 #endif
806
807 #ifdef _WIN32
808         if (InitializeQTML(0) != noErr)
809                 G.have_quicktime = FALSE;
810         else
811                 G.have_quicktime = TRUE;
812 #endif /* _WIN32 */
813         if (EnterMovies() != noErr)
814                 G.have_quicktime = FALSE;
815         else
816 #endif /* _WIN32 || __APPLE__  && !defined(GHOST_COCOA)*/
817         G.have_quicktime = TRUE;
818 #endif /* WITH_QUICKTIME */
819
820         if (argc > 1) {
821                 BLI_strncpy(filepath, argv[1], sizeof(filepath));
822         }
823         else {
824                 BLI_current_working_dir(filepath, sizeof(filepath));
825                 BLI_add_slash(filepath);
826         }
827
828         if (IMB_isanim(filepath)) {
829                 anim = IMB_open_anim(filepath, IB_rect, 0);
830                 if (anim) {
831                         ibuf = IMB_anim_absolute(anim, 0, IMB_TC_NONE, IMB_PROXY_NONE);
832                         IMB_close_anim(anim);
833                         anim = NULL;
834                 }
835         }
836         else if (!IMB_ispic(filepath)) {
837                 exit(1);
838         }
839
840         if (ibuf == NULL) {
841                 ibuf = IMB_loadiffname(filepath, IB_rect);
842         }
843
844         if (ibuf == NULL) {
845                 printf("couldn't open %s\n", filepath);
846                 exit(1);
847         }
848
849 #if 0 //XXX25
850         #if !defined(WIN32) && !defined(__APPLE__)
851         if (fork()) exit(0);
852         #endif
853 #endif //XXX25
854
855         /* XXX, fixme zr */
856         {
857 //              extern void add_to_mainqueue(wmWindow *win, void *user_data, short evt, short val, char ascii);
858
859                 GHOST_EventConsumerHandle consumer = GHOST_CreateEventConsumer(ghost_event_proc, &ps);
860
861                 g_system = GHOST_CreateSystem();
862                 GHOST_AddEventConsumer(g_system, consumer);
863
864
865
866                 playanim_window_open("Blender:Anim", start_x, start_y, ibuf->x, ibuf->y, 0);
867 //XXX25         window_set_handler(g_window, add_to_mainqueue, NULL);
868
869                 glMatrixMode(GL_PROJECTION);
870                 glLoadIdentity();
871                 glOrtho(0.0f, 1.0f, 0.0f, 1.0f, -1.0f, 1.0f);
872                 glMatrixMode(GL_MODELVIEW);
873         }
874
875         GHOST_GetMainDisplayDimensions(g_system, &maxwinx, &maxwiny);
876
877         //GHOST_ActivateWindowDrawingContext(g_window);
878
879         /* initialize the font */
880         BLF_init(11, 72);
881         ps.fontid = BLF_load_mem("monospace", (unsigned char *)datatoc_bmonofont_ttf, datatoc_bmonofont_ttf_size);
882         BLF_size(ps.fontid, 11, 72);
883
884         ps.ibufx = ibuf->x;
885         ps.ibufy = ibuf->y;
886
887         if (maxwinx % ibuf->x) maxwinx = ibuf->x * (1 + (maxwinx / ibuf->x));
888         if (maxwiny % ibuf->y) maxwiny = ibuf->y * (1 + (maxwiny / ibuf->y));
889
890         glClearColor(0.0, 0.0, 0.0, 0.0);
891         glClear(GL_COLOR_BUFFER_BIT);
892
893         GHOST_SwapWindowBuffers(g_window);
894
895         if (sfra == -1 || efra == -1) {
896                 /* one of the frames was invalid, just use all images */
897                 sfra = 1;
898                 efra = MAXFRAME;
899         }
900
901         build_pict_list(filepath, (efra - sfra) + 1, fstep, ps.fontid);
902
903         for (i = 2; i < argc; i++) {
904                 BLI_strncpy(filepath, argv[i], sizeof(filepath));
905                 build_pict_list(filepath, (efra - sfra) + 1, fstep, ps.fontid);
906         }
907
908         IMB_freeImBuf(ibuf);
909         ibuf = NULL;
910
911         pupdate_time();
912         ptottime = 0;
913
914         while (ps.go) {
915                 if (ps.pingpong)
916                         ps.direction = -ps.direction;
917
918                 if (ps.direction == 1) {
919                         ps.picture = picsbase->first;
920                 }
921                 else {
922                         ps.picture = picsbase->last;
923                 }
924
925                 if (ps.picture == NULL) {
926                         printf("couldn't find pictures\n");
927                         ps.go = FALSE;
928                 }
929                 if (ps.pingpong) {
930                         if (ps.direction == 1) {
931                                 ps.picture = ps.picture->next;
932                         }
933                         else {
934                                 ps.picture = ps.picture->prev;
935                         }
936                 }
937                 if (ptottime > 0.0) ptottime = 0.0;
938
939                 while (ps.picture) {
940                         if (ibuf != NULL && ibuf->ftype == 0) IMB_freeImBuf(ibuf);
941
942                         if (ps.picture->ibuf) {
943                                 ibuf = ps.picture->ibuf;
944                         }
945                         else if (ps.picture->anim) {
946                                 ibuf = IMB_anim_absolute(ps.picture->anim, ps.picture->frame, IMB_TC_NONE, IMB_PROXY_NONE);
947                         }
948                         else if (ps.picture->mem) {
949                                 ibuf = IMB_ibImageFromMemory((unsigned char *) ps.picture->mem, ps.picture->size,
950                                                              ps.picture->IB_flags, ps.picture->name);
951                         }
952                         else {
953                                 ibuf = IMB_loadiffname(ps.picture->name, ps.picture->IB_flags);
954                         }
955
956                         if (ibuf) {
957                                 BLI_strncpy(ibuf->name, ps.picture->name, sizeof(ibuf->name));
958
959 #ifdef _WIN32
960                                 GHOST_SetTitle(g_window, picture->name);
961 #endif
962
963                                 while (pupdate_time()) PIL_sleep_ms(1);
964                                 ptottime -= swaptime;
965                                 toscreen(ps.picture, ibuf, ps.fontid);
966                         } /* else deleten */
967                         else {
968                                 printf("error: can't play this image type\n");
969                                 exit(0);
970                         }
971
972                         if (ps.once) {
973                                 if (ps.picture->next == NULL) {
974                                         ps.wait2 = TRUE;
975                                 }
976                                 else if (ps.picture->prev == NULL) {
977                                         ps.wait2 = TRUE;
978                                 }
979                         }
980
981                         ps.next = ps.direction;
982
983
984                         {
985                                 int hasevent = GHOST_ProcessEvents(g_system, 0);
986                                 if (hasevent) {
987                                         GHOST_DispatchEvents(g_system);
988                                 }
989                         }
990
991                         ps.wait2 = ps.sstep;
992
993                         if (ps.wait2 == 0 && ps.stopped == 0) {
994                                 ps.stopped = TRUE;
995                         }
996
997                         pupdate_time();
998
999                         if (ps.picture && ps.next) {
1000                                 /* always at least set one step */
1001                                 while (ps.picture) {
1002                                         if (ps.next < 0) {
1003                                                 ps.picture = ps.picture->prev;
1004                                         }
1005                                         else {
1006                                                 ps.picture = ps.picture->next;
1007                                         }
1008
1009                                         if (ps.once && ps.picture != NULL) {
1010                                                 if (ps.picture->next == NULL) {
1011                                                         ps.wait2 = TRUE;
1012                                                 }
1013                                                 else if (ps.picture->prev == NULL) {
1014                                                         ps.wait2 = TRUE;
1015                                                 }
1016                                         }
1017
1018                                         if (ps.wait2 || ptottime < swaptime || ps.turbo || ps.noskip) break;
1019                                         ptottime -= swaptime;
1020                                 }
1021                                 if (ps.picture == NULL && ps.sstep) {
1022                                         if (ps.next < 0) {
1023                                                 ps.picture = picsbase->last;
1024                                         }
1025                                         else if (ps.next > 0) {
1026                                                 ps.picture = picsbase->first;
1027                                         }
1028                                 }
1029                         }
1030                         if (ps.go == FALSE) {
1031                                 break;
1032                         }
1033                 }
1034         }
1035         ps.picture = picsbase->first;
1036         anim = NULL;
1037         while (ps.picture) {
1038                 if (ps.picture && ps.picture->anim && (anim != ps.picture->anim)) {
1039                         // to prevent divx crashes
1040                         anim = ps.picture->anim;
1041                         IMB_close_anim(anim);
1042                 }
1043
1044                 if (ps.picture->ibuf) {
1045                         IMB_freeImBuf(ps.picture->ibuf);
1046                 }
1047                 if (ps.picture->mem) {
1048                         MEM_freeN(ps.picture->mem);
1049                 }
1050
1051                 ps.picture = ps.picture->next;
1052         }
1053 #ifdef WITH_QUICKTIME
1054 #if defined(_WIN32) || defined(__APPLE__) && !defined(GHOST_COCOA)
1055         if (G.have_quicktime) {
1056                 ExitMovies();
1057 #ifdef _WIN32
1058                 TerminateQTML();
1059 #endif /* _WIN32 */
1060         }
1061 #endif /* _WIN32 || __APPLE__ && !defined(GHOST_COCOA) */
1062 #endif /* WITH_QUICKTIME */
1063
1064         /* cleanup */
1065         if (ibuf) IMB_freeImBuf(ibuf);
1066         BLI_freelistN(picsbase);
1067 #if 0 // XXX25
1068         free_blender();
1069 #else
1070         /* we still miss freeing a lot!,
1071          * but many areas could skip initialization too for anim play */
1072         IMB_exit();
1073         BLF_exit();
1074 #endif
1075         GHOST_DisposeWindow(g_system, g_window);
1076
1077         totblock = MEM_get_memory_blocks_in_use();
1078         if (totblock != 0) {
1079                 printf("Error Totblock: %d\n", totblock);
1080                 MEM_printmemlist();
1081         }
1082 }