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