This commit makes it possible to select the sort mode whenever we invoke an operator.
[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 #include "BLI_math.h"
39
40 #include "IMB_imbuf_types.h"
41 #include "IMB_imbuf.h"
42
43 #include "DNA_scene_types.h"
44 #include "DNA_screen_types.h"
45 #include "DNA_space_types.h"
46 #include "DNA_userdef_types.h"
47
48 #include "BKE_context.h"
49 #include "BKE_global.h"
50 #include "BKE_main.h"
51 #include "BKE_image.h"
52 #include "BKE_report.h"
53 #include "BKE_writeavi.h"
54
55 #include "BIF_gl.h"
56 #include "BIF_glutil.h"
57
58 #include "RNA_access.h"
59 #include "RNA_define.h"
60
61 #include "UI_interface.h"
62
63 #include "WM_types.h"
64 #include "WM_api.h"
65
66 #include "PIL_time.h"
67
68
69 #include "screen_intern.h"
70
71 typedef struct ScreenshotData {
72         unsigned int *dumprect;
73         int dumpsx, dumpsy;
74         rcti crop;
75
76         ImageFormatData im_format;
77 } ScreenshotData;
78
79 static void screenshot_read_pixels(int x, int y, int w, int h, unsigned char *rect)
80 {
81         int i;
82
83         glReadPixels(x, y, w, h, GL_RGBA, GL_UNSIGNED_BYTE, rect);
84         glFinish();
85
86         /* clear alpha, it is not set to a meaningful value in opengl */
87         for (i = 0, rect += 3; i < w * h; i++, rect += 4)
88                 *rect = 255;
89 }
90
91 /* get shot from frontbuffer */
92 static unsigned int *screenshot(bContext *C, int *dumpsx, int *dumpsy)
93 {
94         wmWindow *win = CTX_wm_window(C);
95         int x = 0, y = 0;
96         unsigned int *dumprect = NULL;
97
98         x = 0;
99         y = 0;
100         *dumpsx = WM_window_pixels_x(win);
101         *dumpsy = WM_window_pixels_y(win);
102
103         if (*dumpsx && *dumpsy) {
104                 
105                 dumprect = MEM_mallocN(sizeof(int) * (*dumpsx) * (*dumpsy), "dumprect");
106                 glReadBuffer(GL_FRONT);
107                 screenshot_read_pixels(x, y, *dumpsx, *dumpsy, (unsigned char *)dumprect);
108                 glReadBuffer(GL_BACK);
109         }
110
111         return dumprect;
112 }
113
114 /* call from both exec and invoke */
115 static int screenshot_data_create(bContext *C, wmOperator *op)
116 {
117         unsigned int *dumprect;
118         int dumpsx, dumpsy;
119
120         /* do redraw so we don't show popups/menus */
121         WM_redraw_windows(C);
122         
123         dumprect = screenshot(C, &dumpsx, &dumpsy);
124
125         if (dumprect) {
126                 ScreenshotData *scd = MEM_callocN(sizeof(ScreenshotData), "screenshot");
127                 ScrArea *sa = CTX_wm_area(C);
128                 
129                 scd->dumpsx = dumpsx;
130                 scd->dumpsy = dumpsy;
131                 scd->dumprect = dumprect;
132                 if (sa) {
133                         scd->crop = sa->totrct;
134                 }
135
136                 BKE_imformat_defaults(&scd->im_format);
137
138                 op->customdata = scd;
139
140                 return true;
141         }
142         else {
143                 op->customdata = NULL;
144                 return false;
145         }
146 }
147
148 static void screenshot_data_free(wmOperator *op)
149 {
150         ScreenshotData *scd = op->customdata;
151
152         if (scd) {
153                 if (scd->dumprect)
154                         MEM_freeN(scd->dumprect);
155                 MEM_freeN(scd);
156                 op->customdata = NULL;
157         }
158 }
159
160 static void screenshot_crop(ImBuf *ibuf, rcti crop)
161 {
162         unsigned int *to = ibuf->rect;
163         unsigned int *from = ibuf->rect + crop.ymin * ibuf->x + crop.xmin;
164         int crop_x = BLI_rcti_size_x(&crop);
165         int crop_y = BLI_rcti_size_y(&crop);
166         int y;
167
168         if (crop_x > 0 && crop_y > 0) {
169                 for (y = 0; y < crop_y; y++, to += crop_x, from += ibuf->x)
170                         memmove(to, from, sizeof(unsigned int) * crop_x);
171
172                 ibuf->x = crop_x;
173                 ibuf->y = crop_y;
174         }
175 }
176
177 static int screenshot_exec(bContext *C, wmOperator *op)
178 {
179         ScreenshotData *scd = op->customdata;
180
181         if (scd == NULL) {
182                 /* when running exec directly */
183                 screenshot_data_create(C, op);
184                 scd = op->customdata;
185         }
186
187         if (scd) {
188                 if (scd->dumprect) {
189                         ImBuf *ibuf;
190                         char path[FILE_MAX];
191
192                         RNA_string_get(op->ptr, "filepath", path);
193                         BLI_path_abs(path, G.main->name);
194
195                         /* operator ensures the extension */
196                         ibuf = IMB_allocImBuf(scd->dumpsx, scd->dumpsy, 24, 0);
197                         ibuf->rect = scd->dumprect;
198
199                         /* crop to show only single editor */
200                         if (!RNA_boolean_get(op->ptr, "full"))
201                                 screenshot_crop(ibuf, scd->crop);
202
203                         if (scd->im_format.planes == R_IMF_PLANES_BW) {
204                                 /* bw screenshot? - users will notice if it fails! */
205                                 IMB_color_to_bw(ibuf);
206                         }
207                         BKE_imbuf_write(ibuf, path, &scd->im_format);
208
209                         IMB_freeImBuf(ibuf);
210                 }
211         }
212
213         screenshot_data_free(op);
214         return OPERATOR_FINISHED;
215 }
216
217 static int screenshot_invoke(bContext *C, wmOperator *op, const wmEvent *UNUSED(event))
218 {
219         if (screenshot_data_create(C, op)) {
220                 if (RNA_struct_property_is_set(op->ptr, "filepath"))
221                         return screenshot_exec(C, op);
222
223                 /* extension is added by 'screenshot_check' after */
224                 RNA_string_set(op->ptr, "filepath", G.relbase_valid ? G.main->name : "//screen");
225                 
226                 WM_event_add_fileselect(C, op);
227         
228                 return OPERATOR_RUNNING_MODAL;
229         }
230         return OPERATOR_CANCELLED;
231 }
232
233 static bool screenshot_check(bContext *UNUSED(C), wmOperator *op)
234 {
235         ScreenshotData *scd = op->customdata;
236         return WM_operator_filesel_ensure_ext_imtype(op, &scd->im_format);
237 }
238
239 static void screenshot_cancel(bContext *UNUSED(C), wmOperator *op)
240 {
241         screenshot_data_free(op);
242 }
243
244 static bool screenshot_draw_check_prop(PointerRNA *UNUSED(ptr), PropertyRNA *prop)
245 {
246         const char *prop_id = RNA_property_identifier(prop);
247
248         return !(STREQ(prop_id, "filepath"));
249 }
250
251 static void screenshot_draw(bContext *UNUSED(C), wmOperator *op)
252 {
253         uiLayout *layout = op->layout;
254         ScreenshotData *scd = op->customdata;
255         PointerRNA ptr;
256
257         /* image template */
258         RNA_pointer_create(NULL, &RNA_ImageFormatSettings, &scd->im_format, &ptr);
259         uiTemplateImageSettings(layout, &ptr, false);
260
261         /* main draw call */
262         RNA_pointer_create(NULL, op->type->srna, op->properties, &ptr);
263         uiDefAutoButsRNA(layout, &ptr, screenshot_draw_check_prop, '\0');
264 }
265
266 static int screenshot_poll(bContext *C)
267 {
268         if (G.background)
269                 return false;
270
271         return WM_operator_winactive(C);
272 }
273
274 void SCREEN_OT_screenshot(wmOperatorType *ot)
275 {
276         ot->name = "Save Screenshot"; /* weak: opname starting with 'save' makes filewindow give save-over */
277         ot->idname = "SCREEN_OT_screenshot";
278         ot->description = "Capture a picture of the active area or whole Blender window";
279         
280         ot->invoke = screenshot_invoke;
281         ot->check = screenshot_check;
282         ot->exec = screenshot_exec;
283         ot->cancel = screenshot_cancel;
284         ot->ui = screenshot_draw;
285         ot->poll = screenshot_poll;
286         
287         ot->flag = 0;
288         
289         WM_operator_properties_filesel(ot, FILE_TYPE_FOLDER | FILE_TYPE_IMAGE, FILE_SPECIAL, FILE_SAVE,
290                                        WM_FILESEL_FILEPATH, FILE_DEFAULTDISPLAY, FILE_SORT_ALPHA);
291         RNA_def_boolean(ot->srna, "full", 1, "Full Screen",
292                         "Capture the whole window (otherwise only capture the active area)");
293 }
294
295 /* *************** screenshot movie job ************************* */
296
297 typedef struct ScreenshotJob {
298         Main *bmain;
299         Scene *scene;
300         wmWindowManager *wm;
301         unsigned int *dumprect;
302         int x, y, dumpsx, dumpsy;
303         const short *stop;
304         const short *do_update;
305         ReportList reports;
306
307         bMovieHandle *movie_handle;
308         void *movie_ctx;
309 } ScreenshotJob;
310
311
312 static void screenshot_freejob(void *sjv)
313 {
314         ScreenshotJob *sj = sjv;
315         
316         if (sj->dumprect)
317                 MEM_freeN(sj->dumprect);
318
319         if (sj->movie_handle) {
320                 bMovieHandle *mh = sj->movie_handle;
321                 mh->end_movie(sj->movie_ctx);
322                 mh->context_free(sj->movie_ctx);
323         }
324
325         MEM_freeN(sj);
326 }
327
328
329 /* called before redraw notifiers, copies a new dumprect */
330 static void screenshot_updatejob(void *sjv)
331 {
332         ScreenshotJob *sj = sjv;
333         unsigned int *dumprect;
334         
335         if (sj->dumprect == NULL) {
336                 dumprect = MEM_mallocN(sizeof(int) * sj->dumpsx * sj->dumpsy, "dumprect");
337                 screenshot_read_pixels(sj->x, sj->y, sj->dumpsx, sj->dumpsy, (unsigned char *)dumprect);
338                 
339                 sj->dumprect = dumprect;
340         }
341 }
342
343
344 /* only this runs inside thread */
345 static void screenshot_startjob(void *sjv, short *stop, short *do_update, float *UNUSED(progress))
346 {
347         ScreenshotJob *sj = sjv;
348         RenderData rd = sj->scene->r;
349         bMovieHandle *mh = NULL;
350
351         /* we need this as local variables for renderdata */
352         rd.frs_sec = U.scrcastfps;
353         rd.frs_sec_base = 1.0f;
354         
355         if (BKE_imtype_is_movie(rd.im_format.imtype)) {
356                 mh = BKE_movie_handle_get(sj->scene->r.im_format.imtype);
357                 sj->movie_ctx = mh->context_create();
358                 sj->movie_handle = mh;
359
360                 if (!mh->start_movie(sj->movie_ctx, sj->scene, &rd, sj->dumpsx, sj->dumpsy, &sj->reports, false, "")) {
361                         printf("screencast job stopped\n");
362                         return;
363                 }
364         }
365         
366         sj->stop = stop;
367         sj->do_update = do_update;
368         
369         *do_update = true; /* wait for opengl rect */
370         
371         while (*stop == 0) {
372                 
373                 if (sj->dumprect) {
374                         
375                         if (mh) {
376                                 if (mh->append_movie(sj->movie_ctx, &rd, rd.sfra, rd.cfra, (int *)sj->dumprect,
377                                                      sj->dumpsx, sj->dumpsy, "", &sj->reports))
378                                 {
379                                         BKE_reportf(&sj->reports, RPT_INFO, "Appended frame: %d", rd.cfra);
380                                         printf("Appended frame %d\n", rd.cfra);
381                                 }
382                                 else {
383                                         break;
384                                 }
385                         }
386                         else {
387                                 ImBuf *ibuf = IMB_allocImBuf(sj->dumpsx, sj->dumpsy, rd.im_format.planes, 0);
388                                 char name[FILE_MAX];
389                                 int ok;
390                                 
391                                 BKE_image_path_from_imformat(
392                                         name, rd.pic, sj->bmain->name, rd.cfra,
393                                         &rd.im_format, (rd.scemode & R_EXTENSION) != 0, true, NULL);
394                                 
395                                 ibuf->rect = sj->dumprect;
396                                 ok = BKE_imbuf_write(ibuf, name, &rd.im_format);
397                                 
398                                 if (ok == 0) {
399                                         printf("Write error: cannot save %s\n", name);
400                                         BKE_reportf(&sj->reports, RPT_INFO, "Write error: cannot save %s", name);
401                                         break;
402                                 }
403                                 else {
404                                         printf("Saved file: %s\n", name);
405                                         BKE_reportf(&sj->reports, RPT_INFO, "Saved file: %s", name);
406                                 }
407                                 
408                                 /* imbuf knows which rects are not part of ibuf */
409                                 IMB_freeImBuf(ibuf);
410                         }
411                         
412                         MEM_freeN(sj->dumprect);
413                         sj->dumprect = NULL;
414                         
415                         *do_update = true;
416                         
417                         rd.cfra++;
418
419                 }
420                 else 
421                         PIL_sleep_ms(U.scrcastwait);
422         }
423         
424         if (mh) {
425                 mh->end_movie(sj->movie_ctx);
426                 mh->context_free(sj->movie_ctx);
427                 sj->movie_handle = NULL;
428         }
429
430         BKE_report(&sj->reports, RPT_INFO, "Screencast job stopped");
431 }
432
433 /* Helper callback for drawing the cursor itself */
434 static void screencast_draw_cursor(bContext *UNUSED(C), int x, int y, void *UNUSED(p_ptr))
435 {
436         
437         glPushMatrix();
438         
439         glTranslatef((float)x, (float)y, 0.0f);
440         
441         
442         glEnable(GL_LINE_SMOOTH);
443         glEnable(GL_BLEND);
444         
445         glColor4ub(0, 0, 0, 32);
446         glutil_draw_filled_arc(0.0, M_PI * 2.0, 20, 40);
447         
448         glColor4ub(255, 255, 255, 128);
449         glutil_draw_lined_arc(0.0, M_PI * 2.0, 20, 40);
450         
451         glDisable(GL_BLEND);
452         glDisable(GL_LINE_SMOOTH);
453         
454         glPopMatrix();
455 }
456
457 /* Turn brush cursor in 3D view on/off */
458 static void screencast_cursor_toggle(wmWindowManager *wm, short enable)
459 {
460         static void *cursor = NULL;
461         
462         if (cursor && !enable) {
463                 /* clear cursor */
464                 WM_paint_cursor_end(wm, cursor);
465                 cursor = NULL;
466         }
467         else if (enable) {
468                 /* enable cursor */
469                 cursor = WM_paint_cursor_activate(wm, NULL, screencast_draw_cursor, NULL);
470         }
471 }
472
473 static void screenshot_endjob(void *sjv)
474 {
475         ScreenshotJob *sj = sjv;
476         
477         screencast_cursor_toggle(sj->wm, 0);
478 }
479
480
481 static int screencast_exec(bContext *C, wmOperator *op)
482 {
483         wmWindowManager *wm = CTX_wm_manager(C);
484         wmWindow *win = CTX_wm_window(C);
485         bScreen *screen = CTX_wm_screen(C);
486         wmJob *wm_job;
487         ScreenshotJob *sj;
488
489         /* if called again, stop the running job */
490         if (WM_jobs_test(wm, screen, WM_JOB_TYPE_SCREENCAST))
491                 WM_jobs_stop(wm, screen, screenshot_startjob);
492         
493         wm_job = WM_jobs_get(wm, win, screen, "Screencast", 0, WM_JOB_TYPE_SCREENCAST);
494         sj = MEM_callocN(sizeof(ScreenshotJob), "screenshot job");
495         
496         /* setup sj */
497         if (RNA_boolean_get(op->ptr, "full")) {
498                 sj->x = 0;
499                 sj->y = 0;
500                 sj->dumpsx = WM_window_pixels_x(win);
501                 sj->dumpsy = WM_window_pixels_y(win);
502         }
503         else {
504                 ScrArea *curarea = CTX_wm_area(C);
505                 sj->x = curarea->totrct.xmin;
506                 sj->y = curarea->totrct.ymin;
507                 sj->dumpsx = curarea->totrct.xmax - sj->x;
508                 sj->dumpsy = curarea->totrct.ymax - sj->y;
509         }
510         sj->bmain = CTX_data_main(C);
511         sj->scene = CTX_data_scene(C);
512         sj->wm = wm;
513         
514         BKE_reports_init(&sj->reports, RPT_PRINT);
515
516         /* setup job */
517         WM_jobs_customdata_set(wm_job, sj, screenshot_freejob);
518         WM_jobs_timer(wm_job, 0.1, 0, NC_SCREEN | ND_SCREENCAST);
519         WM_jobs_callbacks(wm_job, screenshot_startjob, NULL, screenshot_updatejob, screenshot_endjob);
520         
521         WM_jobs_start(sj->wm, wm_job);
522         
523         screencast_cursor_toggle(sj->wm, 1);
524         
525         WM_event_add_notifier(C, NC_SCREEN | ND_SCREENCAST, screen);
526         
527         return OPERATOR_FINISHED;
528 }
529
530 void SCREEN_OT_screencast(wmOperatorType *ot)
531 {
532         ot->name = "Make Screencast";
533         ot->idname = "SCREEN_OT_screencast";
534         ot->description = "Capture a video of the active area or whole Blender window";
535         
536         ot->invoke = WM_operator_confirm;
537         ot->exec = screencast_exec;
538         ot->poll = screenshot_poll;  /* shared poll */
539         
540         ot->flag = 0;
541         
542         RNA_def_property(ot->srna, "filepath", PROP_STRING, PROP_FILEPATH);
543         RNA_def_boolean(ot->srna, "full", 1, "Full Screen",
544                         "Capture the whole window (otherwise only capture the active area)");
545 }