code cleanup
[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 "wm_event_types.h"
78 #include "GHOST_C-api.h"
79
80 #define WINCLOSE -1
81 #define REDRAW -3
82 #define RESHAPE -2
83 #define WINQUIT -4
84
85 static int qtest(void)
86 {
87         return 0;
88 }
89
90 /* ***************** gl_util.c ****************** */
91
92 static GHOST_SystemHandle g_system = NULL;
93 static void *g_window = NULL;
94
95 static int qualN = 0;
96
97 #define LSHIFT  (1 << 0)
98 #define RSHIFT  (1 << 1)
99 #define SHIFT   (LSHIFT | RSHIFT)
100 #define LALT    (1 << 2)
101 #define RALT    (1 << 3)
102 #define ALT (LALT | RALT)
103 #define LCTRL   (1 << 4)
104 #define RCTRL   (1 << 5)
105 #define LMOUSE  (1 << 16)
106 #define MMOUSE  (1 << 17)
107 #define RMOUSE  (1 << 18)
108 #define MOUSE   (LMOUSE | MMOUSE | RMOUSE)
109
110 unsigned short screen_qread(short *val, char *ascii);
111
112 void playanim_window_get_size(int *width_r, int *height_r)
113 {
114         GHOST_RectangleHandle bounds = GHOST_GetClientBounds(g_window);
115         *width_r = GHOST_GetWidthRectangle(bounds);
116         *height_r = GHOST_GetHeightRectangle(bounds);
117         GHOST_DisposeRectangle(bounds);
118 }
119
120 /* implementation */
121 static int qreadN(short *val)
122 {
123 #if 0 // XXX25
124         char ascii;
125         int event = screen_qread(val, &ascii);
126 #else
127         int event = 123456789;
128 #endif
129
130         switch (event) {
131                 case LEFTMOUSE:
132                         if (*val) qualN |= LMOUSE;
133                         else qualN &= ~LMOUSE;
134                         break;
135                 case MIDDLEMOUSE:
136                         if (*val) qualN |= MMOUSE;
137                         else qualN &= ~MMOUSE;
138                         break;
139                 case RIGHTMOUSE:
140                         if (*val) qualN |= RMOUSE;
141                         else qualN &= ~RMOUSE;
142                         break;
143                 case LEFTSHIFTKEY:
144                         if (*val) qualN |= LSHIFT;
145                         else qualN &= ~LSHIFT;
146                         break;
147                 case RIGHTSHIFTKEY:
148                         if (*val) qualN |= RSHIFT;
149                         else qualN &= ~RSHIFT;
150                         break;
151                 case LEFTCTRLKEY:
152                         if (*val) qualN |= LCTRL;
153                         else qualN &= ~LCTRL;
154                         break;
155                 case RIGHTCTRLKEY:
156                         if (*val) qualN |= RCTRL;
157                         else qualN &= ~RCTRL;
158                         break;
159                 case LEFTALTKEY:
160                         if (*val) qualN |= LALT;
161                         else qualN &= ~LALT;
162                         break;
163                 case RIGHTALTKEY:
164                         if (*val) qualN |= RALT;
165                         else qualN &= ~RALT;
166                         break;
167         }
168
169         return(event);
170 }
171
172 typedef struct PlayAnimPict {
173         struct PlayAnimPict *next, *prev;
174         char *mem;
175         int size;
176         char *name;
177         struct ImBuf *ibuf;
178         struct anim *anim;
179         int frame;
180         int IB_flags;
181 } PlayAnimPict;
182
183 static struct ListBase _picsbase = {NULL, NULL};
184 static struct ListBase *picsbase = &_picsbase;
185 static int fromdisk = FALSE;
186 static int fstep = 1;
187 static float zoomx = 1.0, zoomy = 1.0;
188 static double ptottime = 0.0, swaptime = 0.04;
189
190 static int pupdate_time(void)
191 {
192         static double ltime;
193         double time;
194
195         time = PIL_check_seconds_timer();
196
197         ptottime += (time - ltime);
198         ltime = time;
199         return (ptottime < 0);
200 }
201
202 static void toscreen(PlayAnimPict *picture, struct ImBuf *ibuf)
203 {
204
205         if (ibuf == NULL) {
206                 printf("no ibuf !\n");
207                 return;
208         }
209         if (ibuf->rect == NULL && ibuf->rect_float) {
210                 IMB_rect_from_float(ibuf);
211                 imb_freerectfloatImBuf(ibuf);
212         }
213         if (ibuf->rect == NULL)
214                 return;
215
216         glRasterPos2f(0.0f, 0.0f);
217
218         glDrawPixels(ibuf->x, ibuf->y, GL_RGBA, GL_UNSIGNED_BYTE, ibuf->rect);
219
220         pupdate_time();
221
222         if (picture && (qualN & (SHIFT | LMOUSE))) {
223                 char str[32 + FILE_MAX];
224                 cpack(-1);
225                 glRasterPos2f(0.02f, 0.03f);
226                 BLI_snprintf(str, sizeof(str), "%s | %.2f frames/s\n", picture->name, fstep / swaptime);
227 #if 0 // XXX25
228                 BMF_DrawString(G.fonts, str);
229 #endif
230         }
231
232         GHOST_SwapWindowBuffers(g_window);
233 }
234
235 static void build_pict_list(char *first, int totframes, int fstep)
236 {
237         int size, pic, file;
238         char *mem, filepath[FILE_MAX];
239 //      short val;
240         PlayAnimPict *picture = NULL;
241         struct ImBuf *ibuf = NULL;
242         int count = 0;
243         char str[32 + FILE_MAX];
244         struct anim *anim;
245
246         if (IMB_isanim(first)) {
247                 anim = IMB_open_anim(first, IB_rect, 0);
248                 if (anim) {
249                         ibuf = IMB_anim_absolute(anim, 0, IMB_TC_NONE, IMB_PROXY_NONE);
250                         if (ibuf) {
251                                 toscreen(NULL, ibuf);
252                                 IMB_freeImBuf(ibuf);
253                         }
254
255                         for (pic = 0; pic < IMB_anim_get_duration(anim, IMB_TC_NONE); pic++) {
256                                 picture = (PlayAnimPict *)MEM_callocN(sizeof(PlayAnimPict), "Pict");
257                                 picture->anim = anim;
258                                 picture->frame = pic;
259                                 picture->IB_flags = IB_rect;
260                                 BLI_snprintf(str, sizeof(str), "%s : %d", first, pic + 1);
261                                 picture->name = strdup(str);
262                                 BLI_addtail(picsbase, picture);
263                         }
264                 }
265                 else {
266                         printf("couldn't open anim %s\n", first);
267                 }
268         }
269         else {
270
271                 BLI_strncpy(filepath, first, sizeof(filepath));
272
273                 pupdate_time();
274                 ptottime = 1.0;
275
276                 /* O_DIRECT
277                  *
278                  * If set, all reads and writes on the resulting file descriptor will
279                  * be performed directly to or from the user program buffer, provided
280                  * appropriate size and alignment restrictions are met.  Refer to the
281                  * F_SETFL and F_DIOINFO commands in the fcntl(2) manual entry for
282                  * information about how to determine the alignment constraints.
283                  * O_DIRECT is a Silicon Graphics extension and is only supported on
284                  * local EFS and XFS file systems.
285                  */
286
287                 while (IMB_ispic(filepath) && totframes) {
288                         file = open(filepath, O_BINARY | O_RDONLY, 0);
289                         if (file < 0) return;
290                         picture = (PlayAnimPict *)MEM_callocN(sizeof(PlayAnimPict), "picture");
291                         if (picture == NULL) {
292                                 printf("Not enough memory for pict struct \n");
293                                 close(file);
294                                 return;
295                         }
296                         size = BLI_file_descriptor_size(file);
297
298                         if (size < 1) {
299                                 close(file);
300                                 MEM_freeN(picture);
301                                 return;
302                         }
303
304                         picture->size = size;
305                         picture->IB_flags = IB_rect;
306
307                         if (fromdisk == FALSE) {
308                                 mem = (char *)MEM_mallocN(size, "build pic list");
309                                 if (mem == NULL) {
310                                         printf("Couldn't get memory\n");
311                                         close(file);
312                                         MEM_freeN(picture);
313                                         return;
314                                 }
315
316                                 if (read(file, mem, size) != size) {
317                                         printf("Error while reading %s\n", filepath);
318                                         close(file);
319                                         MEM_freeN(picture);
320                                         MEM_freeN(mem);
321                                         return;
322                                 }
323                         }
324                         else {
325                                 mem = NULL;
326                         }
327
328                         picture->mem = mem;
329                         picture->name = strdup(filepath);
330                         close(file);
331                         BLI_addtail(picsbase, picture);
332                         count++;
333
334                         pupdate_time();
335
336                         if (ptottime > 1.0) {
337                                 if (picture->mem) {
338                                         ibuf = IMB_ibImageFromMemory((unsigned char *)picture->mem, picture->size,
339                                                                      picture->IB_flags, picture->name);
340                                 }
341                                 else {
342                                         ibuf = IMB_loadiffname(picture->name, picture->IB_flags);
343                                 }
344                                 if (ibuf) {
345                                         toscreen(picture, ibuf);
346                                         IMB_freeImBuf(ibuf);
347                                 }
348                                 pupdate_time();
349                                 ptottime = 0.0;
350                         }
351
352                         BLI_newname(filepath, +fstep);
353
354 #if 0 // XXX25
355                         while (qtest()) {
356                                 switch (qreadN(&val)) {
357                                         case ESCKEY:
358                                                 if (val) return;
359                                                 break;
360                                 }
361                         }
362 #endif
363                         totframes--;
364                 }
365         }
366         return;
367 }
368
369 static int ghost_event_proc(GHOST_EventHandle evt, GHOST_TUserDataPtr C_void_ptr)
370 {
371         (void)evt;
372         (void)C_void_ptr;
373
374         return 1;
375 }
376
377 void playanim_window_open(const char *title, int posx, int posy, int sizex, int sizey, int start_maximized)
378 {
379         GHOST_TWindowState inital_state;
380         GHOST_TUns32 scr_w, scr_h;
381
382         GHOST_GetMainDisplayDimensions(g_system, &scr_w, &scr_h);
383
384         posy = (scr_h - posy - sizey);
385
386         if (start_maximized == G_WINDOWSTATE_FULLSCREEN)
387                 inital_state = start_maximized ? GHOST_kWindowStateFullScreen : GHOST_kWindowStateNormal;
388         else
389                 inital_state = start_maximized ? GHOST_kWindowStateMaximized : GHOST_kWindowStateNormal;
390 #ifdef __APPLE__
391         inital_state += macPrefState;
392 #endif
393
394         g_window = GHOST_CreateWindow(g_system,
395                                       title,
396                                       posx, posy, sizex, sizey,
397                                       inital_state,
398                                       GHOST_kDrawingContextTypeOpenGL,
399                                       FALSE /* no stereo */, FALSE);
400
401         //if (ghostwin) {
402         //if (win) {
403         // GHOST_SetWindowUserData(ghostwin, win);
404         //} else {
405         //      GHOST_DisposeWindow(g_system, ghostwin);
406         //}
407         //}
408 }
409
410
411 void playanim(int argc, const char **argv)
412 {
413         struct ImBuf *ibuf = 0;
414         PlayAnimPict *picture = 0;
415         char filepath[FILE_MAX];
416         short val = 0, go = TRUE, ibufx = 0, ibufy = 0;
417         int event, stopped = FALSE;
418         GHOST_TUns32 maxwinx, maxwiny;
419         /* short c233 = FALSE, yuvx = FALSE; */ /* UNUSED */
420         short once = FALSE, sstep = FALSE, wait2 = FALSE, /*  resetmap = FALSE, */ pause = 0;
421         short pingpong = FALSE, direction = 1, next = 1, turbo = FALSE, /*  doubleb = TRUE, */ noskip = FALSE;
422         int sizex, sizey, ofsx, ofsy, i;
423         /* This was done to disambiguate the name for use under c++. */
424         struct anim *anim = 0;
425         int start_x = 0, start_y = 0;
426         int sfra = -1;
427         int efra = -1;
428         int totblock;
429
430         while (argc > 1) {
431                 if (argv[1][0] == '-') {
432                         switch (argv[1][1]) {
433                                 case 'm':
434                                         fromdisk = TRUE;
435                                         break;
436                                 case 'p':
437                                         if (argc > 3) {
438                                                 start_x = atoi(argv[2]);
439                                                 start_y = atoi(argv[3]);
440                                                 argc -= 2;
441                                                 argv += 2;
442                                         }
443                                         else {
444                                                 printf("too few arguments for -p (need 2): skipping\n");
445                                         }
446                                         break;
447                                 case 'f':
448                                         if (argc > 3) {
449                                                 double fps = atof(argv[2]);
450                                                 double fps_base = atof(argv[3]);
451                                                 if (fps == 0.0) {
452                                                         fps = 1;
453                                                         printf("invalid fps,"
454                                                                "forcing 1\n");
455                                                 }
456                                                 swaptime = fps_base / fps;
457                                                 argc -= 2;
458                                                 argv += 2;
459                                         }
460                                         else {
461                                                 printf("too few arguments for -f (need 2): skipping\n");
462                                         }
463                                         break;
464                                 case 's':
465                                         sfra = MIN2(MAXFRAME, MAX2(1, atoi(argv[2]) ));
466                                         argc--;
467                                         argv++;
468                                         break;
469                                 case 'e':
470                                         efra = MIN2(MAXFRAME, MAX2(1, atoi(argv[2]) ));
471                                         argc--;
472                                         argv++;
473                                         break;
474                                 case 'j':
475                                         fstep = MIN2(MAXFRAME, MAX2(1, atoi(argv[2])));
476                                         swaptime *= fstep;
477                                         argc--;
478                                         argv++;
479                                         break;
480                                 default:
481                                         printf("unknown option '%c': skipping\n", argv[1][1]);
482                                         break;
483                         }
484                         argc--;
485                         argv++;
486                 }
487                 else {
488                         break;
489                 }
490         }
491
492 #ifdef WITH_QUICKTIME
493 #if defined(_WIN32) || defined(__APPLE__)
494         /* Initialize QuickTime */
495 #ifndef noErr
496 #define noErr 0
497 #endif
498
499 #ifdef _WIN32
500         if (InitializeQTML(0) != noErr)
501                 G.have_quicktime = FALSE;
502         else
503                 G.have_quicktime = TRUE;
504 #endif /* _WIN32 */
505         if (EnterMovies() != noErr)
506                 G.have_quicktime = FALSE;
507         else
508 #endif /* _WIN32 || __APPLE__ */
509         G.have_quicktime = TRUE;
510 #endif /* WITH_QUICKTIME */
511
512         if (argc > 1) {
513                 BLI_strncpy(filepath, argv[1], sizeof(filepath));
514         }
515         else {
516                 BLI_current_working_dir(filepath, sizeof(filepath));
517                 BLI_add_slash(filepath);
518         }
519
520         if (IMB_isanim(filepath)) {
521                 anim = IMB_open_anim(filepath, IB_rect, 0);
522                 if (anim) {
523                         ibuf = IMB_anim_absolute(anim, 0, IMB_TC_NONE, IMB_PROXY_NONE);
524                         IMB_close_anim(anim);
525                         anim = NULL;
526                 }
527         }
528         else if (!IMB_ispic(filepath)) {
529                 exit(1);
530         }
531
532         if (ibuf == NULL) {
533                 ibuf = IMB_loadiffname(filepath, IB_rect);
534         }
535
536         if (ibuf == NULL) {
537                 printf("couldn't open %s\n", filepath);
538                 exit(1);
539         }
540
541 #if 0 //XXX25
542         #if !defined(WIN32) && !defined(__APPLE__)
543         if (fork()) exit(0);
544         #endif
545 #endif //XXX25
546
547         /* XXX, fixme zr */
548         {
549 //              extern void add_to_mainqueue(wmWindow *win, void *user_data, short evt, short val, char ascii);
550
551                 void *some_handle = NULL; // XXX25, fixme
552                 GHOST_EventConsumerHandle consumer = GHOST_CreateEventConsumer(ghost_event_proc, some_handle);
553
554                 g_system = GHOST_CreateSystem();
555                 GHOST_AddEventConsumer(g_system, consumer);
556
557
558
559                 playanim_window_open("Blender:Anim", start_x, start_y, ibuf->x, ibuf->y, 0);
560 //XXX25         window_set_handler(g_window, add_to_mainqueue, NULL);
561
562                 glMatrixMode(GL_PROJECTION);
563                 glLoadIdentity();
564                 glOrtho(0.0f, 1.0f, 0.0f, 1.0f, -1.0f, 1.0f);
565                 glMatrixMode(GL_MODELVIEW);
566         }
567
568         GHOST_GetMainDisplayDimensions(g_system, &maxwinx, &maxwiny);
569
570 #if 0 //XXX25
571         G.fonts = BMF_GetFont(BMF_kHelvetica10);
572 #endif
573
574         ibufx = ibuf->x;
575         ibufy = ibuf->y;
576
577         if (maxwinx % ibuf->x) maxwinx = ibuf->x * (1 + (maxwinx / ibuf->x));
578         if (maxwiny % ibuf->y) maxwiny = ibuf->y * (1 + (maxwiny / ibuf->y));
579
580         glClearColor(0.0, 0.0, 0.0, 0.0);
581         glClear(GL_COLOR_BUFFER_BIT);
582
583         GHOST_SwapWindowBuffers(g_window);
584
585         if (sfra == -1 || efra == -1) {
586                 /* one of the frames was invalid, just use all images */
587                 sfra = 1;
588                 efra = MAXFRAME;
589         }
590
591         build_pict_list(filepath, (efra - sfra) + 1, fstep);
592
593         for (i = 2; i < argc; i++) {
594                 BLI_strncpy(filepath, argv[i], sizeof(filepath));
595                 build_pict_list(filepath, (efra - sfra) + 1, fstep);
596         }
597
598         IMB_freeImBuf(ibuf);
599         ibuf = NULL;
600
601         pupdate_time();
602         ptottime = 0;
603
604         while (go) {
605                 if (pingpong) direction = -direction;
606
607                 if (direction == 1) {
608                         picture = picsbase->first;
609                 }
610                 else {
611                         picture = picsbase->last;
612                 }
613
614                 if (picture == NULL) {
615                         printf("couldn't find pictures\n");
616                         go = FALSE;
617                 }
618                 if (pingpong) {
619                         if (direction == 1) {
620                                 picture = picture->next;
621                         }
622                         else {
623                                 picture = picture->prev;
624                         }
625                 }
626                 if (ptottime > 0.0) ptottime = 0.0;
627
628                 while (picture) {
629                         if (ibuf != NULL && ibuf->ftype == 0) IMB_freeImBuf(ibuf);
630
631                         if (picture->ibuf) {
632                                 ibuf = picture->ibuf;
633                         }
634                         else if (picture->anim) {
635                                 ibuf = IMB_anim_absolute(picture->anim, picture->frame, IMB_TC_NONE, IMB_PROXY_NONE);
636                         }
637                         else if (picture->mem) {
638                                 ibuf = IMB_ibImageFromMemory((unsigned char *) picture->mem, picture->size,
639                                                              picture->IB_flags, picture->name);
640                         }
641                         else {
642                                 ibuf = IMB_loadiffname(picture->name, picture->IB_flags);
643                         }
644
645                         if (ibuf) {
646                                 BLI_strncpy(ibuf->name, picture->name, sizeof(ibuf->name));
647
648 #ifdef _WIN32
649                                 window_set_title(g_window, picture->name);
650 #endif
651
652                                 while (pupdate_time()) PIL_sleep_ms(1);
653                                 ptottime -= swaptime;
654                                 toscreen(picture, ibuf);
655                         } /* else deleten */
656                         else {
657                                 printf("error: can't play this image type\n");
658                                 exit(0);
659                         }
660
661                         if (once) {
662                                 if (picture->next == NULL) {
663                                         wait2 = TRUE;
664                                 }
665                                 else if (picture->prev == NULL) {
666                                         wait2 = TRUE;
667                                 }
668                         }
669
670                         next = direction;
671
672                         while ((qtest() != 0) || (wait2 != 0)) {
673                                 if (wait2 && stopped) {
674                                         stopped = FALSE;
675                                 }
676
677                                 event = qreadN(&val);
678                                 /* printf("%d %d\n", event, val); */
679
680                                 if (wait2) {
681                                         pupdate_time();
682                                         ptottime = 0;
683                                 }
684                                 switch (event) {
685                                         case AKEY:
686                                                 if (val)
687                                                         noskip = !noskip;
688                                                 break;
689                                         case PKEY:
690                                                 if (val)
691                                                         pingpong = !pingpong;
692                                                 break;
693                                         case SLASHKEY:
694                                                 if (val) {
695                                                         if (qualN & SHIFT) {
696                                                                 if (ibuf)
697                                                                         printf(" Name: %s | Speed: %.2f frames/s\n", ibuf->name, fstep / swaptime);
698                                                         }
699                                                         else {
700                                                                 swaptime = fstep / 5.0;
701                                                         }
702                                                 }
703                                                 break;
704                                         case LEFTARROWKEY:
705                                                 if (val) {
706                                                         sstep = TRUE;
707                                                         wait2 = FALSE;
708                                                         if (qualN & SHIFT) {
709                                                                 picture = picsbase->first;
710                                                                 next = 0;
711                                                         }
712                                                         else {
713                                                                 next = -1;
714                                                         }
715                                                 }
716                                                 break;
717                                         case DOWNARROWKEY:
718                                                 if (val) {
719                                                         wait2 = FALSE;
720                                                         if (qualN & SHIFT) {
721                                                                 next = direction = -1;
722                                                         }
723                                                         else {
724                                                                 next = -10;
725                                                                 sstep = TRUE;
726                                                         }
727                                                 }
728                                                 break;
729                                         case RIGHTARROWKEY:
730                                                 if (val) {
731                                                         sstep = TRUE;
732                                                         wait2 = FALSE;
733                                                         if (qualN & SHIFT) {
734                                                                 picture = picsbase->last;
735                                                                 next = 0;
736                                                         }
737                                                         else {
738                                                                 next = 1;
739                                                         }
740                                                 }
741                                                 break;
742                                         case UPARROWKEY:
743                                                 if (val) {
744                                                         wait2 = FALSE;
745                                                         if (qualN & SHIFT) {
746                                                                 next = direction = 1;
747                                                         }
748                                                         else {
749                                                                 next = 10;
750                                                                 sstep = TRUE;
751                                                         }
752                                                 }
753                                                 break;
754                                         case LEFTMOUSE:
755                                         case MOUSEX:
756                                                 if (qualN & LMOUSE) {
757                                                         playanim_window_get_size(&sizex, &sizey);
758                                                         picture = picsbase->first;
759                                                         i = 0;
760                                                         while (picture) {
761                                                                 i++;
762                                                                 picture = picture->next;
763                                                         }
764                                                         i = (i * val) / sizex;
765                                                         picture = picsbase->first;
766                                                         for (; i > 0; i--) {
767                                                                 if (picture->next == NULL) break;
768                                                                 picture = picture->next;
769                                                         }
770                                                         sstep = TRUE;
771                                                         wait2 = FALSE;
772                                                         next = 0;
773                                                 }
774                                                 break;
775                                                 go = FALSE;
776                                                 break;
777                                         case EQUALKEY:
778                                                 if (val) {
779                                                         if (qualN & SHIFT) {
780                                                                 pause++;
781                                                                 printf("pause:%d\n", pause);
782                                                         }
783                                                         else {
784                                                                 swaptime /= 1.1;
785                                                         }
786                                                 }
787                                                 break;
788                                         case MINUSKEY:
789                                                 if (val) {
790                                                         if (qualN & SHIFT) {
791                                                                 pause--;
792                                                                 printf("pause:%d\n", pause);
793                                                         }
794                                                         else {
795                                                                 swaptime *= 1.1;
796                                                         }
797                                                 }
798                                                 break;
799                                         case PAD0:
800                                                 if (val) {
801                                                         if (once) once = wait2 = FALSE;
802                                                         else {
803                                                                 picture = 0;
804                                                                 once = TRUE;
805                                                                 wait2 = FALSE;
806                                                         }
807                                                 }
808                                                 break;
809                                         case RETKEY:
810                                         case PADENTER:
811                                                 if (val) {
812                                                         wait2 = sstep = FALSE;
813                                                 }
814                                                 break;
815                                         case PADPERIOD:
816                                                 if (val) {
817                                                         if (sstep) wait2 = FALSE;
818                                                         else {
819                                                                 sstep = TRUE;
820                                                                 wait2 = !wait2;
821                                                         }
822                                                 }
823                                                 break;
824                                         case PAD1:
825                                                 swaptime = fstep / 60.0;
826                                                 break;
827                                         case PAD2:
828                                                 swaptime = fstep / 50.0;
829                                                 break;
830                                         case PAD3:
831                                                 swaptime = fstep / 30.0;
832                                                 break;
833                                         case PAD4:
834                                                 if (qualN & SHIFT)
835                                                         swaptime = fstep / 24.0;
836                                                 else
837                                                         swaptime = fstep / 25.0;
838                                                 break;
839                                         case PAD5:
840                                                 swaptime = fstep / 20.0;
841                                                 break;
842                                         case PAD6:
843                                                 swaptime = fstep / 15.0;
844                                                 break;
845                                         case PAD7:
846                                                 swaptime = fstep / 12.0;
847                                                 break;
848                                         case PAD8:
849                                                 swaptime = fstep / 10.0;
850                                                 break;
851                                         case PAD9:
852                                                 swaptime = fstep / 6.0;
853                                                 break;
854                                         case PADPLUSKEY:
855                                                 if (val == 0) break;
856                                                 zoomx += 2.0;
857                                                 zoomy += 2.0;
858                                         case PADMINUS:
859                                                 if (val == 0) break;
860                                                 if (zoomx > 1.0) zoomx -= 1.0;
861                                                 if (zoomy > 1.0) zoomy -= 1.0;
862                                                 // playanim_window_get_position(&ofsx, &ofsy);
863                                                 playanim_window_get_size(&sizex, &sizey);
864                                                 ofsx += sizex / 2;
865                                                 ofsy += sizey / 2;
866                                                 sizex = zoomx * ibufx;
867                                                 sizey = zoomy * ibufy;
868                                                 ofsx -= sizex / 2;
869                                                 ofsy -= sizey / 2;
870                                                 // window_set_position(g_window,sizex,sizey);
871                                                 GHOST_SetClientSize(g_window, sizex, sizey);
872                                                 break;
873                                         case RESHAPE:
874                                         case REDRAW:
875                                                 playanim_window_get_size(&sizey, &sizey);
876                                                 GHOST_ActivateWindowDrawingContext(g_window);
877
878                                                 glViewport(0, 0, sizex, sizey);
879                                                 glScissor(0, 0, sizex, sizey);
880
881                                                 zoomx = (float) sizex / ibufx;
882                                                 zoomy = (float) sizey / ibufy;
883                                                 zoomx = floor(zoomx + 0.5);
884                                                 zoomy = floor(zoomy + 0.5);
885                                                 if (zoomx < 1.0) zoomx = 1.0;
886                                                 if (zoomy < 1.0) zoomy = 1.0;
887
888                                                 sizex = zoomx * ibufx;
889                                                 sizey = zoomy * ibufy;
890
891                                                 glPixelZoom(zoomx, zoomy);
892                                                 glEnable(GL_DITHER);
893                                                 ptottime = 0.0;
894                                                 toscreen(picture, ibuf);
895                                                 while (qtest()) qreadN(&val);
896
897                                                 break;
898                                         case ESCKEY:
899                                         case WINCLOSE:
900                                         case WINQUIT:
901                                                 go = FALSE;
902                                                 break;
903                                 }
904                                 if (go == FALSE) break;
905                         }
906
907                         wait2 = sstep;
908
909                         if (wait2 == 0 && stopped == 0) {
910                                 stopped = TRUE;
911                         }
912
913                         pupdate_time();
914
915                         if (picture && next) {
916                                 /* always at least set one step */
917                                 while (picture) {
918                                         if (next < 0) {
919                                                 picture = picture->prev;
920                                         }
921                                         else {
922                                                 picture = picture->next;
923                                         }
924
925                                         if (once && picture != NULL) {
926                                                 if (picture->next == NULL) {
927                                                         wait2 = TRUE;
928                                                 }
929                                                 else if (picture->prev == NULL) {
930                                                         wait2 = TRUE;
931                                                 }
932                                         }
933
934                                         if (wait2 || ptottime < swaptime || turbo || noskip) break;
935                                         ptottime -= swaptime;
936                                 }
937                                 if (picture == NULL && sstep) {
938                                         if (next < 0) {
939                                                 picture = picsbase->last;
940                                         }
941                                         else if (next > 0) {
942                                                 picture = picsbase->first;
943                                         }
944                                 }
945                         }
946                         if (go == FALSE) break;
947                 }
948         }
949         picture = picsbase->first;
950         anim = NULL;
951         while (picture) {
952                 if (picture && picture->anim && (anim != picture->anim)) {
953                         // to prevent divx crashes
954                         anim = picture->anim;
955                         IMB_close_anim(anim);
956                 }
957                 if (picture->ibuf) IMB_freeImBuf(picture->ibuf);
958                 if (picture->mem) MEM_freeN(picture->mem);
959
960                 picture = picture->next;
961         }
962 #ifdef WITH_QUICKTIME
963 #if defined(_WIN32) || defined(__APPLE__)
964         if (G.have_quicktime) {
965                 ExitMovies();
966 #ifdef _WIN32
967                 TerminateQTML();
968 #endif /* _WIN32 */
969         }
970 #endif /* _WIN32 || __APPLE__ */
971 #endif /* WITH_QUICKTIME */
972
973         /* cleanup */
974         if (ibuf) IMB_freeImBuf(ibuf);
975         BLI_freelistN(picsbase);
976         free_blender();
977         GHOST_DisposeWindow(g_system, g_window);
978
979         totblock = MEM_get_memory_blocks_in_use();
980         if (totblock != 0) {
981                 printf("Error Totblock: %d\n", totblock);
982                 MEM_printmemlist();
983         }
984 }