Fix syntax for ID keyword.
[blender-staging.git] / source / blender / editors / screen / screendump.c
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19  *
20  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
21  * All rights reserved.
22  *
23  * Contributor(s): Blender Foundation
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  * Making screendumps.
27  */
28
29 #include <string.h>
30
31 #include "MEM_guardedalloc.h"
32
33 #include "BLI_blenlib.h"
34
35 #include "IMB_imbuf_types.h"
36 #include "IMB_imbuf.h"
37
38 #include "DNA_scene_types.h"
39 #include "DNA_screen_types.h"
40 #include "DNA_space_types.h"
41 #include "DNA_userdef_types.h"
42
43 #include "BKE_context.h"
44 #include "BKE_global.h"
45 #include "BKE_image.h"
46 #include "BKE_report.h"
47 #include "BKE_utildefines.h"
48 #include "BKE_writeavi.h"
49
50 #include "BIF_gl.h"
51
52 #include "RNA_access.h"
53 #include "RNA_define.h"
54
55 #include "WM_types.h"
56 #include "WM_api.h"
57
58 #include "PIL_time.h"
59
60 #include "ED_fileselect.h"
61 #include "ED_screen.h"
62 #include "ED_screen_types.h"
63
64 #include "screen_intern.h"
65
66 typedef struct ScreenshotData {
67         unsigned int *dumprect;
68         int dumpsx, dumpsy;
69 } ScreenshotData;
70
71 static int screenshot_exec(bContext *C, wmOperator *op)
72 {
73         ScreenshotData *scd= op->customdata;
74         
75         if(scd && scd->dumprect) {
76                 Scene *scene= CTX_data_scene(C);
77                 ImBuf *ibuf;
78                 char path[FILE_MAX];
79         
80                 RNA_string_get(op->ptr, "path", path);
81         
82                 strcpy(G.ima, path);
83                 BLI_path_abs(path, G.sce);
84                 
85                 /* BKE_add_image_extension() checks for if extension was already set */
86                 if(scene->r.scemode & R_EXTENSION) 
87                         if(strlen(path)<FILE_MAXDIR+FILE_MAXFILE-5)
88                                 BKE_add_image_extension(path, scene->r.imtype);
89                 
90                 ibuf= IMB_allocImBuf(scd->dumpsx, scd->dumpsy, 24, 0, 0);
91                 ibuf->rect= scd->dumprect;
92                 
93                 if(scene->r.planes == 8) IMB_cspace(ibuf, rgb_to_bw);
94                 
95                 BKE_write_ibuf(scene, ibuf, path, scene->r.imtype, scene->r.subimtype, scene->r.quality);
96
97                 IMB_freeImBuf(ibuf);
98
99                 MEM_freeN(scd->dumprect);
100                 MEM_freeN(scd);
101                 op->customdata= NULL;
102         }
103         return OPERATOR_FINISHED;
104 }
105
106 /* get shot from frontbuffer */
107 static unsigned int *screenshot(bContext *C, int *dumpsx, int *dumpsy, int fscreen)
108 {
109         wmWindow *win= CTX_wm_window(C);
110         ScrArea *curarea= CTX_wm_area(C);
111         int x=0, y=0;
112         unsigned int *dumprect= NULL;
113         
114         if(fscreen) {   /* full screen */
115                 x= 0;
116                 y= 0;
117                 *dumpsx= win->sizex;
118                 *dumpsy= win->sizey;
119         } 
120         else {
121                 x= curarea->totrct.xmin;
122                 y= curarea->totrct.ymin;
123                 *dumpsx= curarea->totrct.xmax-x;
124                 *dumpsy= curarea->totrct.ymax-y;
125         }
126
127         if (*dumpsx && *dumpsy) {
128                 
129                 dumprect= MEM_mallocN(sizeof(int) * (*dumpsx) * (*dumpsy), "dumprect");
130                 glReadBuffer(GL_FRONT);
131                 glReadPixels(x, y, *dumpsx, *dumpsy, GL_RGBA, GL_UNSIGNED_BYTE, dumprect);
132                 glFinish();
133                 glReadBuffer(GL_BACK);
134         }
135
136         return dumprect;
137 }
138
139
140 static int screenshot_invoke(bContext *C, wmOperator *op, wmEvent *event)
141 {
142         unsigned int *dumprect;
143         int dumpsx, dumpsy;
144         
145         dumprect= screenshot(C, &dumpsx, &dumpsy, RNA_boolean_get(op->ptr, "full"));
146         if(dumprect) {
147                 ScreenshotData *scd= MEM_callocN(sizeof(ScreenshotData), "screenshot");
148                 
149                 scd->dumpsx= dumpsx;
150                 scd->dumpsy= dumpsy;
151                 scd->dumprect= dumprect;
152                 op->customdata= scd;
153                 
154                 if(RNA_property_is_set(op->ptr, "path"))
155                         return screenshot_exec(C, op);
156                 
157                 RNA_string_set(op->ptr, "path", G.ima);
158                 
159                 WM_event_add_fileselect(C, op);
160         
161                 return OPERATOR_RUNNING_MODAL;
162         }       
163         return OPERATOR_CANCELLED;
164 }
165
166
167 void SCREEN_OT_screenshot(wmOperatorType *ot)
168 {
169         ot->name= "Save Screenshot"; /* weak: opname starting with 'save' makes filewindow give save-over */
170         ot->idname= "SCREEN_OT_screenshot";
171         
172         ot->invoke= screenshot_invoke;
173         ot->exec= screenshot_exec;
174         ot->poll= WM_operator_winactive;
175         
176         ot->flag= 0;
177         
178         WM_operator_properties_filesel(ot, FOLDERFILE|IMAGEFILE, FILE_SPECIAL, FILE_SAVE);
179         RNA_def_boolean(ot->srna, "full", 1, "Full Screen", "");
180 }
181
182 /* *************** screenshot movie job ************************* */
183
184 typedef struct ScreenshotJob {
185         Scene *scene;
186         unsigned int *dumprect;
187         int x, y, dumpsx, dumpsy;
188         short *stop;
189         short *do_update;
190         ReportList reports;
191 } ScreenshotJob;
192
193
194 static void screenshot_freejob(void *sjv)
195 {
196         ScreenshotJob *sj= sjv;
197         
198         if(sj->dumprect)
199                 MEM_freeN(sj->dumprect);
200         
201         MEM_freeN(sj);
202 }
203
204
205 /* called before redraw notifiers, copies a new dumprect */
206 static void screenshot_updatejob(void *sjv)
207 {
208         ScreenshotJob *sj= sjv;
209         unsigned int *dumprect;
210         
211         if(sj->dumprect==NULL) {
212                 dumprect= MEM_mallocN(sizeof(int) * sj->dumpsx * sj->dumpsy, "dumprect");
213                 glReadPixels(sj->x, sj->y, sj->dumpsx, sj->dumpsy, GL_RGBA, GL_UNSIGNED_BYTE, dumprect);
214                 glFinish();
215                 
216                 sj->dumprect= dumprect;
217         }
218 }
219
220
221 /* only this runs inside thread */
222 static void screenshot_startjob(void *sjv, short *stop, short *do_update)
223 {
224         ScreenshotJob *sj= sjv;
225         RenderData rd= sj->scene->r;
226         bMovieHandle *mh= BKE_get_movie_handle(sj->scene->r.imtype);
227         int cfra= 1;
228         
229         /* we need this as local variables for renderdata */
230         rd.frs_sec= U.scrcastfps;
231         rd.frs_sec_base= 1.0f;
232         
233         if(BKE_imtype_is_movie(rd.imtype)) {
234                 if(!mh->start_movie(sj->scene, &rd, sj->dumpsx, sj->dumpsy, &sj->reports)) {
235                         printf("screencast job stopped\n");
236                         return;
237                 }
238         }
239         else
240                 mh= NULL;
241         
242         sj->stop= stop;
243         sj->do_update= do_update;
244         
245         *do_update= 1; // wait for opengl rect
246         
247         while(*stop==0) {
248                 
249                 if(sj->dumprect) {
250                         
251                         if(mh) {
252                                 if(mh->append_movie(&rd, cfra, (int *)sj->dumprect, sj->dumpsx, sj->dumpsy, &sj->reports)) {
253                                         BKE_reportf(&sj->reports, RPT_INFO, "Appended frame: %d", cfra);
254                                         printf("Appended frame %d\n", cfra);
255                                 } else
256                                         break;
257                         }
258                         else {
259                                 ImBuf *ibuf= IMB_allocImBuf(sj->dumpsx, sj->dumpsy, rd.planes, 0, 0);
260                                 char name[FILE_MAXDIR+FILE_MAXFILE];
261                                 int ok;
262                                 
263                                 BKE_makepicstring(name, rd.pic, cfra, rd.imtype, rd.scemode & R_EXTENSION);
264                                 
265                                 ibuf->rect= sj->dumprect;
266                                 ok= BKE_write_ibuf(sj->scene, ibuf, name, rd.imtype, rd.subimtype, rd.quality);
267                                 
268                                 if(ok==0) {
269                                         printf("Write error: cannot save %s\n", name);
270                                         BKE_reportf(&sj->reports, RPT_INFO, "Write error: cannot save %s\n", name);
271                                         break;
272                                 }
273                                 else {
274                                         printf("Saved file: %s\n", name);
275                                         BKE_reportf(&sj->reports, RPT_INFO, "Saved file: %s", name);
276                                 }
277                                 
278                 /* imbuf knows which rects are not part of ibuf */
279                                 IMB_freeImBuf(ibuf);    
280                         }
281                         
282                         MEM_freeN(sj->dumprect);
283                         sj->dumprect= NULL;
284                         
285                         *do_update= 1;
286                         
287                         cfra++;
288
289                 }
290                 else 
291                         PIL_sleep_ms(U.scrcastwait);
292         }
293         
294         if(mh)
295                 mh->end_movie();
296
297         BKE_report(&sj->reports, RPT_INFO, "Screencast job stopped");
298 }
299
300 static int screencast_exec(bContext *C, wmOperator *op)
301 {
302         bScreen *screen= CTX_wm_screen(C);
303         wmJob *steve= WM_jobs_get(CTX_wm_manager(C), CTX_wm_window(C), screen, 0);
304         ScreenshotJob *sj= MEM_callocN(sizeof(ScreenshotJob), "screenshot job");
305
306         /* setup sj */
307         if(RNA_boolean_get(op->ptr, "full")) {
308                 wmWindow *win= CTX_wm_window(C);
309                 sj->x= 0;
310                 sj->y= 0;
311                 sj->dumpsx= win->sizex;
312                 sj->dumpsy= win->sizey;
313         } 
314         else {
315                 ScrArea *curarea= CTX_wm_area(C);
316                 sj->x= curarea->totrct.xmin;
317                 sj->y= curarea->totrct.ymin;
318                 sj->dumpsx= curarea->totrct.xmax - sj->x;
319                 sj->dumpsy= curarea->totrct.ymax - sj->y;
320         }
321         sj->scene= CTX_data_scene(C);
322
323         BKE_reports_init(&sj->reports, RPT_PRINT);
324
325         /* setup job */
326         WM_jobs_customdata(steve, sj, screenshot_freejob);
327         WM_jobs_timer(steve, 0.1, 0, NC_SCREEN|ND_SCREENCAST);
328         WM_jobs_callbacks(steve, screenshot_startjob, NULL, screenshot_updatejob);
329         
330         WM_jobs_start(CTX_wm_manager(C), steve);
331         
332         WM_event_add_notifier(C, NC_SCREEN|ND_SCREENCAST, screen);
333         
334         return OPERATOR_FINISHED;
335 }
336
337 void SCREEN_OT_screencast(wmOperatorType *ot)
338 {
339         ot->name= "Make Screencast";
340         ot->idname= "SCREEN_OT_screencast";
341         
342         ot->invoke= WM_operator_confirm;
343         ot->exec= screencast_exec;
344         ot->poll= WM_operator_winactive;
345         
346         ot->flag= 0;
347         
348         RNA_def_property(ot->srna, "path", PROP_STRING, PROP_FILEPATH);
349         RNA_def_boolean(ot->srna, "full", 1, "Full Screen", "");
350 }
351
352
353