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