Merged changes in the trunk up to revision 54110.
[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                 {
694                         g_WS.qual &= ~WS_QUAL_MOUSE;
695                         break;
696                 }
697                 case GHOST_kEventWindowSize:
698                 case GHOST_kEventWindowMove:
699                 {
700                         int sizex, sizey;
701
702                         playanim_window_get_size(&sizex, &sizey);
703                         GHOST_ActivateWindowDrawingContext(g_WS.ghost_window);
704
705                         glViewport(0, 0, sizex, sizey);
706                         glScissor(0, 0, sizex, sizey);
707
708                         zoomx = (float) sizex / ps->ibufx;
709                         zoomy = (float) sizey / ps->ibufy;
710                         zoomx = floor(zoomx + 0.5f);
711                         zoomy = floor(zoomy + 0.5f);
712                         if (zoomx < 1.0f) zoomx = 1.0f;
713                         if (zoomy < 1.0f) zoomy = 1.0f;
714
715                         sizex = zoomx * ps->ibufx;
716                         sizey = zoomy * ps->ibufy;
717
718                         glPixelZoom(zoomx, zoomy);
719                         glEnable(GL_DITHER);
720                         ptottime = 0.0;
721                         playanim_toscreen(ps->picture, ps->curframe_ibuf, ps->fontid, ps->fstep);
722
723                         break;
724                 }
725                 case GHOST_kEventQuit:
726                 case GHOST_kEventWindowClose:
727                 {
728                         ps->go = FALSE;
729                         break;
730                 }
731                 default:
732                         /* quiet warnings */
733                         break;
734         }
735
736         return 1;
737 }
738
739 static void playanim_window_open(const char *title, int posx, int posy, int sizex, int sizey, int start_maximized)
740 {
741         GHOST_TWindowState inital_state;
742         GHOST_TUns32 scr_w, scr_h;
743
744         GHOST_GetMainDisplayDimensions(g_WS.ghost_system, &scr_w, &scr_h);
745
746         posy = (scr_h - posy - sizey);
747
748         if (start_maximized == G_WINDOWSTATE_FULLSCREEN)
749                 inital_state = start_maximized ? GHOST_kWindowStateFullScreen : GHOST_kWindowStateNormal;
750         else
751                 inital_state = start_maximized ? GHOST_kWindowStateMaximized : GHOST_kWindowStateNormal;
752
753         g_WS.ghost_window = GHOST_CreateWindow(g_WS.ghost_system,
754                                                title,
755                                                posx, posy, sizex, sizey,
756                                                inital_state,
757                                                GHOST_kDrawingContextTypeOpenGL,
758                                                FALSE /* no stereo */, FALSE);
759 }
760
761 static void playanim_window_zoom(const PlayState *ps, const float zoom_offset)
762 {
763         int sizex, sizey;
764         /* int ofsx, ofsy; */ /* UNUSED */
765
766         if (zoomx + zoom_offset > 0.0f) zoomx += zoom_offset;
767         if (zoomy + zoom_offset > 0.0f) zoomy += zoom_offset;
768
769         // playanim_window_get_position(&ofsx, &ofsy);
770         playanim_window_get_size(&sizex, &sizey);
771         /* ofsx += sizex / 2; */ /* UNUSED */
772         /* ofsy += sizey / 2; */ /* UNUSED */
773         sizex = zoomx * ps->ibufx;
774         sizey = zoomy * ps->ibufy;
775         /* ofsx -= sizex / 2; */ /* UNUSED */
776         /* ofsy -= sizey / 2; */ /* UNUSED */
777         // window_set_position(g_WS.ghost_window,sizex,sizey);
778         GHOST_SetClientSize(g_WS.ghost_window, sizex, sizey);
779 }
780
781 void WM_main_playanim(int argc, const char **argv)
782 {
783         struct ImBuf *ibuf = NULL;
784         char filepath[FILE_MAX];
785         GHOST_TUns32 maxwinx, maxwiny;
786         /* short c233 = FALSE, yuvx = FALSE; */ /* UNUSED */
787         int i;
788         /* This was done to disambiguate the name for use under c++. */
789         struct anim *anim = NULL;
790         int start_x = 0, start_y = 0;
791         int sfra = -1;
792         int efra = -1;
793         int totblock;
794
795         PlayState ps = {0};
796
797         /* ps.doubleb   = TRUE;*/ /* UNUSED */
798         ps.go        = TRUE;
799         ps.direction = TRUE;
800         ps.next_frame = 1;
801         ps.once      = FALSE;
802         ps.turbo     = FALSE;
803         ps.pingpong  = FALSE;
804         ps.noskip    = FALSE;
805         ps.sstep     = FALSE;
806         ps.wait2     = FALSE;
807         ps.stopped   = FALSE;
808         ps.picture   = NULL;
809         /* resetmap = FALSE */
810
811         ps.fstep     = 1;
812
813         ps.fontid = -1;
814
815         while (argc > 1) {
816                 if (argv[1][0] == '-') {
817                         switch (argv[1][1]) {
818                                 case 'm':
819                                         fromdisk = TRUE;
820                                         break;
821                                 case 'p':
822                                         if (argc > 3) {
823                                                 start_x = atoi(argv[2]);
824                                                 start_y = atoi(argv[3]);
825                                                 argc -= 2;
826                                                 argv += 2;
827                                         }
828                                         else {
829                                                 printf("too few arguments for -p (need 2): skipping\n");
830                                         }
831                                         break;
832                                 case 'f':
833                                         if (argc > 3) {
834                                                 double fps = atof(argv[2]);
835                                                 double fps_base = atof(argv[3]);
836                                                 if (fps == 0.0) {
837                                                         fps = 1;
838                                                         printf("invalid fps,"
839                                                                "forcing 1\n");
840                                                 }
841                                                 swaptime = fps_base / fps;
842                                                 argc -= 2;
843                                                 argv += 2;
844                                         }
845                                         else {
846                                                 printf("too few arguments for -f (need 2): skipping\n");
847                                         }
848                                         break;
849                                 case 's':
850                                         sfra = MIN2(MAXFRAME, MAX2(1, atoi(argv[2]) ));
851                                         argc--;
852                                         argv++;
853                                         break;
854                                 case 'e':
855                                         efra = MIN2(MAXFRAME, MAX2(1, atoi(argv[2]) ));
856                                         argc--;
857                                         argv++;
858                                         break;
859                                 case 'j':
860                                         ps.fstep = MIN2(MAXFRAME, MAX2(1, atoi(argv[2])));
861                                         swaptime *= ps.fstep;
862                                         argc--;
863                                         argv++;
864                                         break;
865                                 default:
866                                         printf("unknown option '%c': skipping\n", argv[1][1]);
867                                         break;
868                         }
869                         argc--;
870                         argv++;
871                 }
872                 else {
873                         break;
874                 }
875         }
876
877         if (argc > 1) {
878                 BLI_strncpy(filepath, argv[1], sizeof(filepath));
879         }
880         else {
881                 BLI_current_working_dir(filepath, sizeof(filepath));
882                 BLI_add_slash(filepath);
883         }
884
885         if (IMB_isanim(filepath)) {
886                 /* OCIO_TODO: support different input color spaces */
887                 anim = IMB_open_anim(filepath, IB_rect, 0, NULL);
888                 if (anim) {
889                         ibuf = IMB_anim_absolute(anim, 0, IMB_TC_NONE, IMB_PROXY_NONE);
890                         IMB_close_anim(anim);
891                         anim = NULL;
892                 }
893         }
894         else if (!IMB_ispic(filepath)) {
895                 exit(1);
896         }
897
898         if (ibuf == NULL) {
899                 /* OCIO_TODO: support different input color space */
900                 ibuf = IMB_loadiffname(filepath, IB_rect, NULL);
901         }
902
903         if (ibuf == NULL) {
904                 printf("couldn't open %s\n", filepath);
905                 exit(1);
906         }
907
908 #if 0 //XXX25
909         #if !defined(WIN32) && !defined(__APPLE__)
910         if (fork()) exit(0);
911         #endif
912 #endif //XXX25
913
914         /* XXX, fixme zr */
915         {
916 //              extern void add_to_mainqueue(wmWindow *win, void *user_data, short evt, short val, char ascii);
917
918                 GHOST_EventConsumerHandle consumer = GHOST_CreateEventConsumer(ghost_event_proc, &ps);
919
920                 g_WS.ghost_system = GHOST_CreateSystem();
921                 GHOST_AddEventConsumer(g_WS.ghost_system, consumer);
922
923
924
925                 playanim_window_open("Blender:Anim", start_x, start_y, ibuf->x, ibuf->y, 0);
926 //XXX25         window_set_handler(g_WS.ghost_window, add_to_mainqueue, NULL);
927
928                 glMatrixMode(GL_PROJECTION);
929                 glLoadIdentity();
930                 glOrtho(0.0f, 1.0f, 0.0f, 1.0f, -1.0f, 1.0f);
931                 glMatrixMode(GL_MODELVIEW);
932         }
933
934         GHOST_GetMainDisplayDimensions(g_WS.ghost_system, &maxwinx, &maxwiny);
935
936         //GHOST_ActivateWindowDrawingContext(g_WS.ghost_window);
937
938         /* initialize the font */
939         BLF_init(11, 72);
940         ps.fontid = BLF_load_mem("monospace", (unsigned char *)datatoc_bmonofont_ttf, datatoc_bmonofont_ttf_size);
941         BLF_size(ps.fontid, 11, 72);
942
943         ps.ibufx = ibuf->x;
944         ps.ibufy = ibuf->y;
945
946         if (maxwinx % ibuf->x) maxwinx = ibuf->x * (1 + (maxwinx / ibuf->x));
947         if (maxwiny % ibuf->y) maxwiny = ibuf->y * (1 + (maxwiny / ibuf->y));
948
949         glClearColor(0.0, 0.0, 0.0, 0.0);
950         glClear(GL_COLOR_BUFFER_BIT);
951
952         GHOST_SwapWindowBuffers(g_WS.ghost_window);
953
954         if (sfra == -1 || efra == -1) {
955                 /* one of the frames was invalid, just use all images */
956                 sfra = 1;
957                 efra = MAXFRAME;
958         }
959
960         build_pict_list(filepath, (efra - sfra) + 1, ps.fstep, ps.fontid);
961
962         for (i = 2; i < argc; i++) {
963                 BLI_strncpy(filepath, argv[i], sizeof(filepath));
964                 build_pict_list(filepath, (efra - sfra) + 1, ps.fstep, ps.fontid);
965         }
966
967         IMB_freeImBuf(ibuf);
968         ibuf = NULL;
969
970         pupdate_time();
971         ptottime = 0;
972
973         /* newly added in 2.6x, without this images never get freed */
974 #define USE_IMB_CACHE
975
976         while (ps.go) {
977                 if (ps.pingpong)
978                         ps.direction = -ps.direction;
979
980                 if (ps.direction == 1) {
981                         ps.picture = picsbase.first;
982                 }
983                 else {
984                         ps.picture = picsbase.last;
985                 }
986
987                 if (ps.picture == NULL) {
988                         printf("couldn't find pictures\n");
989                         ps.go = FALSE;
990                 }
991                 if (ps.pingpong) {
992                         if (ps.direction == 1) {
993                                 ps.picture = ps.picture->next;
994                         }
995                         else {
996                                 ps.picture = ps.picture->prev;
997                         }
998                 }
999                 if (ptottime > 0.0) ptottime = 0.0;
1000
1001                 while (ps.picture) {
1002                         int hasevent;
1003 #ifndef USE_IMB_CACHE
1004                         if (ibuf != NULL && ibuf->ftype == 0) IMB_freeImBuf(ibuf);
1005 #endif
1006                         if (ps.picture->ibuf) {
1007                                 ibuf = ps.picture->ibuf;
1008                         }
1009                         else if (ps.picture->anim) {
1010                                 ibuf = IMB_anim_absolute(ps.picture->anim, ps.picture->frame, IMB_TC_NONE, IMB_PROXY_NONE);
1011                         }
1012                         else if (ps.picture->mem) {
1013                                 /* use correct colorspace here */
1014                                 ibuf = IMB_ibImageFromMemory((unsigned char *) ps.picture->mem, ps.picture->size,
1015                                                              ps.picture->IB_flags, NULL, ps.picture->name);
1016                         }
1017                         else {
1018                                 /* use correct colorspace here */
1019                                 ibuf = IMB_loadiffname(ps.picture->name, ps.picture->IB_flags, NULL);
1020                         }
1021
1022                         if (ibuf) {
1023
1024 #ifdef USE_IMB_CACHE
1025                                 ps.picture->ibuf = ibuf;
1026 #endif
1027
1028                                 BLI_strncpy(ibuf->name, ps.picture->name, sizeof(ibuf->name));
1029
1030                                 /* why only windows? (from 2.4x) - campbell */
1031 #ifdef _WIN32
1032                                 GHOST_SetTitle(g_WS.ghost_window, ps.picture->name);
1033 #endif
1034
1035                                 while (pupdate_time()) PIL_sleep_ms(1);
1036                                 ptottime -= swaptime;
1037                                 playanim_toscreen(ps.picture, ibuf, ps.fontid, ps.fstep);
1038                         } /* else deleten */
1039                         else {
1040                                 printf("error: can't play this image type\n");
1041                                 exit(0);
1042                         }
1043
1044                         if (ps.once) {
1045                                 if (ps.picture->next == NULL) {
1046                                         ps.wait2 = TRUE;
1047                                 }
1048                                 else if (ps.picture->prev == NULL) {
1049                                         ps.wait2 = TRUE;
1050                                 }
1051                         }
1052
1053                         ps.next_frame = ps.direction;
1054
1055
1056                         while ((hasevent = GHOST_ProcessEvents(g_WS.ghost_system, 0) || ps.wait2 != 0)) {
1057                                 if (hasevent) {
1058                                         GHOST_DispatchEvents(g_WS.ghost_system);
1059                                 }
1060                                 if (ps.wait2) {
1061                                         if (hasevent) {
1062                                                 if (ibuf) {
1063                                                         while (pupdate_time()) PIL_sleep_ms(1);
1064                                                         ptottime -= swaptime;
1065                                                         playanim_toscreen(ps.picture, ibuf, ps.fontid, ps.fstep);
1066                                                 }
1067                                         }
1068                                 }
1069                                 if (!ps.go) {
1070                                         break;
1071                                 }
1072                         }
1073
1074                         ps.wait2 = ps.sstep;
1075
1076                         if (ps.wait2 == 0 && ps.stopped == 0) {
1077                                 ps.stopped = TRUE;
1078                         }
1079
1080                         pupdate_time();
1081
1082                         if (ps.picture && ps.next_frame) {
1083                                 /* always at least set one step */
1084                                 while (ps.picture) {
1085                                         ps.picture = playanim_step(ps.picture, ps.next_frame);
1086
1087                                         if (ps.once && ps.picture != NULL) {
1088                                                 if (ps.picture->next == NULL) {
1089                                                         ps.wait2 = TRUE;
1090                                                 }
1091                                                 else if (ps.picture->prev == NULL) {
1092                                                         ps.wait2 = TRUE;
1093                                                 }
1094                                         }
1095
1096                                         if (ps.wait2 || ptottime < swaptime || ps.turbo || ps.noskip) break;
1097                                         ptottime -= swaptime;
1098                                 }
1099                                 if (ps.picture == NULL && ps.sstep) {
1100                                         ps.picture = playanim_step(ps.picture, ps.next_frame);
1101                                 }
1102                         }
1103                         if (ps.go == FALSE) {
1104                                 break;
1105                         }
1106                 }
1107         }
1108         ps.picture = picsbase.first;
1109         anim = NULL;
1110         while (ps.picture) {
1111                 if (ps.picture && ps.picture->anim && (anim != ps.picture->anim)) {
1112                         // to prevent divx crashes
1113                         anim = ps.picture->anim;
1114                         IMB_close_anim(anim);
1115                 }
1116
1117                 if (ps.picture->ibuf) {
1118                         IMB_freeImBuf(ps.picture->ibuf);
1119                 }
1120                 if (ps.picture->mem) {
1121                         MEM_freeN(ps.picture->mem);
1122                 }
1123
1124                 ps.picture = ps.picture->next;
1125         }
1126
1127         /* cleanup */
1128 #ifndef USE_IMB_CACHE
1129         if (ibuf) IMB_freeImBuf(ibuf);
1130 #endif
1131
1132         BLI_freelistN(&picsbase);
1133 #if 0 // XXX25
1134         free_blender();
1135 #else
1136         /* we still miss freeing a lot!,
1137          * but many areas could skip initialization too for anim play */
1138         IMB_exit();
1139         BKE_images_exit();
1140         BLF_exit();
1141 #endif
1142         GHOST_DisposeWindow(g_WS.ghost_system, g_WS.ghost_window);
1143
1144         totblock = MEM_get_memory_blocks_in_use();
1145         if (totblock != 0) {
1146                 /* prints many bAKey, bArgument's which are tricky to fix */
1147 #if 0
1148                 printf("Error Totblock: %d\n", totblock);
1149                 MEM_printmemlist();
1150 #endif
1151         }
1152 }