screenshot operator now adds file extension in the file selector and has its own...
[blender.git] / source / blender / editors / screen / screendump.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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
19  * All rights reserved.
20  *
21  * Contributor(s): Blender Foundation
22  *
23  * ***** END GPL LICENSE BLOCK *****
24  * Making screendumps.
25  */
26
27 /** \file blender/editors/screen/screendump.c
28  *  \ingroup edscr
29  */
30
31
32 #include <string.h>
33
34 #include "MEM_guardedalloc.h"
35
36 #include "BLI_blenlib.h"
37 #include "BLI_utildefines.h"
38
39 #include "IMB_imbuf_types.h"
40 #include "IMB_imbuf.h"
41
42 #include "DNA_scene_types.h"
43 #include "DNA_screen_types.h"
44 #include "DNA_space_types.h"
45 #include "DNA_userdef_types.h"
46
47 #include "BKE_context.h"
48 #include "BKE_global.h"
49 #include "BKE_main.h"
50 #include "BKE_image.h"
51 #include "BKE_report.h"
52 #include "BKE_writeavi.h"
53
54 #include "BIF_gl.h"
55
56 #include "RNA_access.h"
57 #include "RNA_define.h"
58
59 #include "UI_interface.h"
60
61 #include "WM_types.h"
62 #include "WM_api.h"
63
64 #include "PIL_time.h"
65
66 #include "ED_screen_types.h"
67
68 #include "screen_intern.h"
69
70 typedef struct ScreenshotData {
71         unsigned int *dumprect;
72         int dumpsx, dumpsy;
73         rcti crop;
74
75         ImageFormatData im_format;
76 } ScreenshotData;
77
78 /* get shot from frontbuffer */
79 static unsigned int *screenshot(bContext *C, int *dumpsx, int *dumpsy)
80 {
81         wmWindow *win= CTX_wm_window(C);
82         int x=0, y=0;
83         unsigned int *dumprect= NULL;
84         
85         x= 0;
86         y= 0;
87         *dumpsx= win->sizex;
88         *dumpsy= win->sizey;
89
90         if (*dumpsx && *dumpsy) {
91                 
92                 dumprect= MEM_mallocN(sizeof(int) * (*dumpsx) * (*dumpsy), "dumprect");
93                 glReadBuffer(GL_FRONT);
94                 glReadPixels(x, y, *dumpsx, *dumpsy, GL_RGBA, GL_UNSIGNED_BYTE, dumprect);
95                 glFinish();
96                 glReadBuffer(GL_BACK);
97         }
98
99         return dumprect;
100 }
101
102 /* call from both exec and invoke */
103 static int screenshot_data_create(bContext *C, wmOperator *op)
104 {
105         unsigned int *dumprect;
106         int dumpsx, dumpsy;
107
108         /* do redraw so we don't show popups/menus */
109         WM_redraw_windows(C);
110         
111         dumprect= screenshot(C, &dumpsx, &dumpsy);
112
113         if (dumprect) {
114                 ScreenshotData *scd= MEM_callocN(sizeof(ScreenshotData), "screenshot");
115                 ScrArea *sa= CTX_wm_area(C);
116                 
117                 scd->dumpsx= dumpsx;
118                 scd->dumpsy= dumpsy;
119                 scd->dumprect= dumprect;
120                 if (sa) {
121                         scd->crop= sa->totrct;
122                 }
123
124                 BKE_imformat_defaults(&scd->im_format);
125
126                 op->customdata = scd;
127
128                 return TRUE;
129         }
130         else {
131                 op->customdata= NULL;
132                 return FALSE;
133         }
134 }
135
136 static void screenshot_data_free(wmOperator *op)
137 {
138         ScreenshotData *scd= op->customdata;
139
140         if (scd) {
141                 if (scd->dumprect)
142                         MEM_freeN(scd->dumprect);
143                 MEM_freeN(scd);
144                 op->customdata= NULL;
145         }
146 }
147
148 static void screenshot_crop(ImBuf *ibuf, rcti crop)
149 {
150         unsigned int *to= ibuf->rect;
151         unsigned int *from= ibuf->rect + crop.ymin*ibuf->x + crop.xmin;
152         int y, cropw= crop.xmax - crop.xmin, croph = crop.ymax - crop.ymin;
153
154         if (cropw > 0 && croph > 0) {
155                 for (y=0; y<croph; y++, to+=cropw, from+=ibuf->x)
156                         memmove(to, from, sizeof(unsigned int)*cropw);
157
158                 ibuf->x= cropw;
159                 ibuf->y= croph;
160         }
161 }
162
163 static int screenshot_exec(bContext *C, wmOperator *op)
164 {
165         ScreenshotData *scd= op->customdata;
166
167         if (scd == NULL) {
168                 /* when running exec directly */
169                 screenshot_data_create(C, op);
170                 scd= op->customdata;
171         }
172
173         if (scd) {
174                 if (scd->dumprect) {
175                         ImBuf *ibuf;
176                         char path[FILE_MAX];
177
178                         RNA_string_get(op->ptr, "filepath", path);
179                         BLI_path_abs(path, G.main->name);
180
181                         /* operator ensures the extension */
182                         ibuf= IMB_allocImBuf(scd->dumpsx, scd->dumpsy, 24, 0);
183                         ibuf->rect= scd->dumprect;
184
185                         /* crop to show only single editor */
186                         if (!RNA_boolean_get(op->ptr, "full"))
187                                 screenshot_crop(ibuf, scd->crop);
188
189                         if (scd->im_format.planes == R_IMF_PLANES_BW) {
190                                 /* bw screenshot? - users will notice if it fails! */
191                                 IMB_color_to_bw(ibuf);
192                         }
193                         BKE_write_ibuf(ibuf, path, &scd->im_format);
194
195                         IMB_freeImBuf(ibuf);
196                 }
197         }
198
199         screenshot_data_free(op);
200         return OPERATOR_FINISHED;
201 }
202
203 static int screenshot_invoke(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
204 {
205         if (screenshot_data_create(C, op)) {
206                 if (RNA_struct_property_is_set(op->ptr, "filepath"))
207                         return screenshot_exec(C, op);
208
209                 /* extension is added by 'screenshot_check' after */
210                 RNA_string_set(op->ptr, "filepath", G.relbase_valid ? G.main->name : "//screen");
211                 
212                 WM_event_add_fileselect(C, op);
213         
214                 return OPERATOR_RUNNING_MODAL;
215         }       
216         return OPERATOR_CANCELLED;
217 }
218
219 static int screenshot_check(bContext *UNUSED(C), wmOperator *op)
220 {
221         ScreenshotData *scd = op->customdata;
222         return WM_operator_filesel_ensure_ext_imtype(op, scd->im_format.imtype);
223 }
224
225 static int screenshot_cancel(bContext *UNUSED(C), wmOperator *op)
226 {
227         screenshot_data_free(op);
228         return OPERATOR_CANCELLED;
229 }
230
231 static int screenshot_draw_check_prop(PointerRNA *UNUSED(ptr), PropertyRNA *prop)
232 {
233         const char *prop_id = RNA_property_identifier(prop);
234
235         return !(strcmp(prop_id, "filepath") == 0);
236 }
237
238 static void screenshot_draw(bContext *UNUSED(C), wmOperator *op)
239 {
240         uiLayout *layout = op->layout;
241         ScreenshotData *scd = op->customdata;
242         PointerRNA ptr;
243
244         /* image template */
245         RNA_pointer_create(NULL, &RNA_ImageFormatSettings, &scd->im_format, &ptr);
246         uiTemplateImageSettings(layout, &ptr);
247
248         /* main draw call */
249         RNA_pointer_create(NULL, op->type->srna, op->properties, &ptr);
250         uiDefAutoButsRNA(layout, &ptr, screenshot_draw_check_prop, '\0');
251 }
252
253
254 void SCREEN_OT_screenshot(wmOperatorType *ot)
255 {
256         ot->name = "Save Screenshot"; /* weak: opname starting with 'save' makes filewindow give save-over */
257         ot->idname = "SCREEN_OT_screenshot";
258         
259         ot->invoke = screenshot_invoke;
260         ot->check = screenshot_check;
261         ot->exec = screenshot_exec;
262         ot->cancel = screenshot_cancel;
263         ot->ui = screenshot_draw;
264         ot->poll = WM_operator_winactive;
265         
266         ot->flag = 0;
267         
268         WM_operator_properties_filesel(ot, FOLDERFILE|IMAGEFILE, FILE_SPECIAL, FILE_SAVE, WM_FILESEL_FILEPATH, FILE_DEFAULTDISPLAY);
269         RNA_def_boolean(ot->srna, "full", 1, "Full Screen", "");
270 }
271
272 /* *************** screenshot movie job ************************* */
273
274 typedef struct ScreenshotJob {
275         Main *bmain;
276         Scene *scene;
277         unsigned int *dumprect;
278         int x, y, dumpsx, dumpsy;
279         short *stop;
280         short *do_update;
281         ReportList reports;
282 } ScreenshotJob;
283
284
285 static void screenshot_freejob(void *sjv)
286 {
287         ScreenshotJob *sj= sjv;
288         
289         if (sj->dumprect)
290                 MEM_freeN(sj->dumprect);
291         
292         MEM_freeN(sj);
293 }
294
295
296 /* called before redraw notifiers, copies a new dumprect */
297 static void screenshot_updatejob(void *sjv)
298 {
299         ScreenshotJob *sj= sjv;
300         unsigned int *dumprect;
301         
302         if (sj->dumprect==NULL) {
303                 dumprect= MEM_mallocN(sizeof(int) * sj->dumpsx * sj->dumpsy, "dumprect");
304                 glReadPixels(sj->x, sj->y, sj->dumpsx, sj->dumpsy, GL_RGBA, GL_UNSIGNED_BYTE, dumprect);
305                 glFinish();
306                 
307                 sj->dumprect= dumprect;
308         }
309 }
310
311
312 /* only this runs inside thread */
313 static void screenshot_startjob(void *sjv, short *stop, short *do_update, float *UNUSED(progress))
314 {
315         ScreenshotJob *sj= sjv;
316         RenderData rd= sj->scene->r;
317         bMovieHandle *mh= BKE_get_movie_handle(sj->scene->r.im_format.imtype);
318         
319         /* we need this as local variables for renderdata */
320         rd.frs_sec= U.scrcastfps;
321         rd.frs_sec_base= 1.0f;
322         
323         if (BKE_imtype_is_movie(rd.im_format.imtype)) {
324                 if (!mh->start_movie(sj->scene, &rd, sj->dumpsx, sj->dumpsy, &sj->reports)) {
325                         printf("screencast job stopped\n");
326                         return;
327                 }
328         }
329         else
330                 mh= NULL;
331         
332         sj->stop= stop;
333         sj->do_update= do_update;
334         
335         *do_update= 1; // wait for opengl rect
336         
337         while (*stop==0) {
338                 
339                 if (sj->dumprect) {
340                         
341                         if (mh) {
342                                 if (mh->append_movie(&rd, rd.sfra, rd.cfra, (int *)sj->dumprect,
343                                                     sj->dumpsx, sj->dumpsy, &sj->reports))
344                                 {
345                                         BKE_reportf(&sj->reports, RPT_INFO, "Appended frame: %d", rd.cfra);
346                                         printf("Appended frame %d\n", rd.cfra);
347                                 }
348                                 else {
349                                         break;
350                                 }
351                         }
352                         else {
353                                 ImBuf *ibuf= IMB_allocImBuf(sj->dumpsx, sj->dumpsy, rd.im_format.planes, 0);
354                                 char name[FILE_MAX];
355                                 int ok;
356                                 
357                                 BKE_makepicstring(name, rd.pic, sj->bmain->name, rd.cfra, rd.im_format.imtype, rd.scemode & R_EXTENSION, TRUE);
358                                 
359                                 ibuf->rect= sj->dumprect;
360                                 ok= BKE_write_ibuf(ibuf, name, &rd.im_format);
361                                 
362                                 if (ok==0) {
363                                         printf("Write error: cannot save %s\n", name);
364                                         BKE_reportf(&sj->reports, RPT_INFO, "Write error: cannot save %s\n", name);
365                                         break;
366                                 }
367                                 else {
368                                         printf("Saved file: %s\n", name);
369                                         BKE_reportf(&sj->reports, RPT_INFO, "Saved file: %s", name);
370                                 }
371                                 
372                                 /* imbuf knows which rects are not part of ibuf */
373                                 IMB_freeImBuf(ibuf);    
374                         }
375                         
376                         MEM_freeN(sj->dumprect);
377                         sj->dumprect= NULL;
378                         
379                         *do_update= 1;
380                         
381                         rd.cfra++;
382
383                 }
384                 else 
385                         PIL_sleep_ms(U.scrcastwait);
386         }
387         
388         if (mh)
389                 mh->end_movie();
390
391         BKE_report(&sj->reports, RPT_INFO, "Screencast job stopped");
392 }
393
394 static int screencast_exec(bContext *C, wmOperator *op)
395 {
396         bScreen *screen= CTX_wm_screen(C);
397         wmJob *steve= WM_jobs_get(CTX_wm_manager(C), CTX_wm_window(C), screen, "Screencast", 0);
398         ScreenshotJob *sj= MEM_callocN(sizeof(ScreenshotJob), "screenshot job");
399
400         /* setup sj */
401         if (RNA_boolean_get(op->ptr, "full")) {
402                 wmWindow *win= CTX_wm_window(C);
403                 sj->x= 0;
404                 sj->y= 0;
405                 sj->dumpsx= win->sizex;
406                 sj->dumpsy= win->sizey;
407         } 
408         else {
409                 ScrArea *curarea= CTX_wm_area(C);
410                 sj->x= curarea->totrct.xmin;
411                 sj->y= curarea->totrct.ymin;
412                 sj->dumpsx= curarea->totrct.xmax - sj->x;
413                 sj->dumpsy= curarea->totrct.ymax - sj->y;
414         }
415         sj->bmain= CTX_data_main(C);
416         sj->scene= CTX_data_scene(C);
417
418         BKE_reports_init(&sj->reports, RPT_PRINT);
419
420         /* setup job */
421         WM_jobs_customdata(steve, sj, screenshot_freejob);
422         WM_jobs_timer(steve, 0.1, 0, NC_SCREEN|ND_SCREENCAST);
423         WM_jobs_callbacks(steve, screenshot_startjob, NULL, screenshot_updatejob, NULL);
424         
425         WM_jobs_start(CTX_wm_manager(C), steve);
426         
427         WM_event_add_notifier(C, NC_SCREEN|ND_SCREENCAST, screen);
428         
429         return OPERATOR_FINISHED;
430 }
431
432 void SCREEN_OT_screencast(wmOperatorType *ot)
433 {
434         ot->name = "Make Screencast";
435         ot->idname = "SCREEN_OT_screencast";
436         
437         ot->invoke = WM_operator_confirm;
438         ot->exec = screencast_exec;
439         ot->poll = WM_operator_winactive;
440         
441         ot->flag = 0;
442         
443         RNA_def_property(ot->srna, "filepath", PROP_STRING, PROP_FILEPATH);
444         RNA_def_boolean(ot->srna, "full", 1, "Full Screen", "");
445 }
446
447
448