Fix #19361: BGE keyboard sensor allowed to set mouse events
[blender-staging.git] / source / blender / editors / sound / sound_ops.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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19  *
20  * The Original Code is Copyright (C) 2007 Blender Foundation.
21  * All rights reserved.
22  *
23  * 
24  * Contributor(s): Blender Foundation
25  *
26  * ***** END GPL LICENSE BLOCK *****
27  */
28
29 #include <string.h>
30 #include <stdlib.h>
31 #include <stdio.h>
32
33 #include "DNA_packedFile_types.h"
34 #include "DNA_scene_types.h"
35 #include "DNA_space_types.h"
36 #include "DNA_sound_types.h"
37 #include "DNA_sequence_types.h"
38 #include "DNA_windowmanager_types.h"
39
40 #include "BKE_context.h"
41 #include "BKE_global.h"
42 #include "BKE_main.h"
43 #include "BKE_report.h"
44 #include "BKE_packedFile.h"
45 #include "BKE_sound.h"
46 #include "BKE_utildefines.h"
47
48 #include "BLI_blenlib.h"
49
50 #include "ED_sound.h"
51
52 #include "RNA_access.h"
53 #include "RNA_define.h"
54 #include "RNA_enum_types.h"
55
56 #include "UI_interface.h"
57
58 #include "WM_api.h"
59 #include "WM_types.h"
60
61 #include "AUD_C-API.h"
62
63 #include "sound_intern.h"
64
65 /******************** open sound operator ********************/
66
67 static int open_exec(bContext *C, wmOperator *op)
68 {
69         char path[FILE_MAX];
70         bSound *sound;
71         AUD_SoundInfo info;
72
73         RNA_string_get(op->ptr, "path", path);
74
75         sound = sound_new_file(CTX_data_main(C), path);
76
77         if (sound==NULL || sound->handle == NULL) {
78                 BKE_report(op->reports, RPT_ERROR, "Unsupported audio format");
79                 return OPERATOR_CANCELLED;
80         }
81
82         info = AUD_getInfo(sound->handle);
83
84         if (info.specs.format == AUD_FORMAT_INVALID) {
85                 sound_delete(C, sound);
86                 BKE_report(op->reports, RPT_ERROR, "Unsupported audio format");
87                 return OPERATOR_CANCELLED;
88         }
89
90         if (RNA_boolean_get(op->ptr, "cache")) {
91                 sound_cache(sound, 0);
92         }
93
94         return OPERATOR_FINISHED;
95 }
96
97 static int open_invoke(bContext *C, wmOperator *op, wmEvent *event)
98 {
99         return WM_operator_filesel(C, op, event);
100 }
101
102 void SOUND_OT_open(wmOperatorType *ot)
103 {
104         /* identifiers */
105         ot->name= "Open Sound";
106         ot->idname= "SOUND_OT_open";
107         ot->description= "Load a sound file into blender";
108
109         /* api callbacks */
110         ot->exec= open_exec;
111         ot->invoke= open_invoke;
112
113         /* flags */
114         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
115
116         /* properties */
117         WM_operator_properties_filesel(ot, FOLDERFILE|SOUNDFILE|MOVIEFILE, FILE_SPECIAL);
118         RNA_def_boolean(ot->srna, "cache", FALSE, "Cache", "Cache the sound in memory.");
119 }
120
121 /* ******************************************************* */
122
123 static int sound_poll(bContext *C)
124 {
125         Editing* ed = CTX_data_scene(C)->ed;
126
127         if(!ed || !ed->act_seq || ed->act_seq->type != SEQ_SOUND)
128                 return 0;
129
130         return 1;
131 }
132 /********************* pack operator *********************/
133
134 static int pack_exec(bContext *C, wmOperator *op)
135 {
136         Editing* ed = CTX_data_scene(C)->ed;
137         bSound* sound;
138
139         if(!ed || !ed->act_seq || ed->act_seq->type != SEQ_SOUND)
140                 return OPERATOR_CANCELLED;
141
142         sound = ed->act_seq->sound;
143
144         if(!sound || sound->packedfile)
145                 return OPERATOR_CANCELLED;
146
147         sound->packedfile= newPackedFile(op->reports, sound->name);
148         sound_load(CTX_data_main(C), sound);
149
150         return OPERATOR_FINISHED;
151 }
152
153 void SOUND_OT_pack(wmOperatorType *ot)
154 {
155         /* identifiers */
156         ot->name= "Pack Sound";
157         ot->idname= "SOUND_OT_pack";
158
159         /* api callbacks */
160         ot->exec= pack_exec;
161         ot->poll= sound_poll;
162
163         /* flags */
164         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
165 }
166
167 /********************* unpack operator *********************/
168
169 // XXX this function is in image_ops.c too, exactly the same, should be moved to a generally accessible position
170 static void unpack_menu(bContext *C, char *opname, char *abs_name, char *folder, PackedFile *pf)
171 {
172         uiPopupMenu *pup;
173         uiLayout *layout;
174         char line[FILE_MAX + 100];
175         char local_name[FILE_MAXDIR + FILE_MAX], fi[FILE_MAX];
176
177         strcpy(local_name, abs_name);
178         BLI_splitdirstring(local_name, fi);
179         sprintf(local_name, "//%s/%s", folder, fi);
180
181         pup= uiPupMenuBegin(C, "Unpack file", 0);
182         layout= uiPupMenuLayout(pup);
183
184         uiItemEnumO(layout, "Remove Pack", 0, opname, "method", PF_REMOVE);
185
186         if(strcmp(abs_name, local_name)) {
187                 switch(checkPackedFile(local_name, pf)) {
188                         case PF_NOFILE:
189                                 sprintf(line, "Create %s", local_name);
190                                 uiItemEnumO(layout, line, 0, opname, "method", PF_WRITE_LOCAL);
191                                 break;
192                         case PF_EQUAL:
193                                 sprintf(line, "Use %s (identical)", local_name);
194                                 uiItemEnumO(layout, line, 0, opname, "method", PF_USE_LOCAL);
195                                 break;
196                         case PF_DIFFERS:
197                                 sprintf(line, "Use %s (differs)", local_name);
198                                 uiItemEnumO(layout, line, 0, opname, "method", PF_USE_LOCAL);
199                                 sprintf(line, "Overwrite %s", local_name);
200                                 uiItemEnumO(layout, line, 0, opname, "method", PF_WRITE_LOCAL);
201                                 break;
202                 }
203         }
204
205         switch(checkPackedFile(abs_name, pf)) {
206                 case PF_NOFILE:
207                         sprintf(line, "Create %s", abs_name);
208                         uiItemEnumO(layout, line, 0, opname, "method", PF_WRITE_ORIGINAL);
209                         break;
210                 case PF_EQUAL:
211                         sprintf(line, "Use %s (identical)", abs_name);
212                         uiItemEnumO(layout, line, 0, opname, "method", PF_USE_ORIGINAL);
213                         break;
214                 case PF_DIFFERS:
215                         sprintf(line, "Use %s (differs)", local_name);
216                         uiItemEnumO(layout, line, 0, opname, "method", PF_USE_ORIGINAL);
217                         sprintf(line, "Overwrite %s", local_name);
218                         uiItemEnumO(layout, line, 0, opname, "method", PF_WRITE_ORIGINAL);
219                         break;
220         }
221
222         uiPupMenuEnd(C, pup);
223 }
224
225 static int unpack_exec(bContext *C, wmOperator *op)
226 {
227         int method= RNA_enum_get(op->ptr, "method");
228         Editing* ed = CTX_data_scene(C)->ed;
229         bSound* sound;
230
231         if(!ed || !ed->act_seq || ed->act_seq->type != SEQ_SOUND)
232                 return OPERATOR_CANCELLED;
233
234         sound = ed->act_seq->sound;
235
236         if(!sound || !sound->packedfile)
237                 return OPERATOR_CANCELLED;
238
239         if(G.fileflags & G_AUTOPACK)
240                 BKE_report(op->reports, RPT_WARNING, "AutoPack is enabled, so image will be packed again on file save.");
241
242         unpackSound(op->reports, sound, method);
243
244         return OPERATOR_FINISHED;
245 }
246
247 static int unpack_invoke(bContext *C, wmOperator *op, wmEvent *event)
248 {
249         Editing* ed = CTX_data_scene(C)->ed;
250         bSound* sound;
251
252         if(!ed || !ed->act_seq || ed->act_seq->type != SEQ_SOUND)
253                 return OPERATOR_CANCELLED;
254
255         sound = ed->act_seq->sound;
256
257         if(!sound || !sound->packedfile)
258                 return OPERATOR_CANCELLED;
259
260         if(G.fileflags & G_AUTOPACK)
261                 BKE_report(op->reports, RPT_WARNING, "AutoPack is enabled, so image will be packed again on file save.");
262
263         unpack_menu(C, "SOUND_OT_unpack", sound->name, "audio", sound->packedfile);
264
265         return OPERATOR_FINISHED;
266 }
267
268 void SOUND_OT_unpack(wmOperatorType *ot)
269 {
270         /* identifiers */
271         ot->name= "Unpack Sound";
272         ot->idname= "SOUND_OT_unpack";
273
274         /* api callbacks */
275         ot->exec= unpack_exec;
276         ot->invoke= unpack_invoke;
277         ot->poll= sound_poll;
278
279         /* flags */
280         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
281
282         /* properties */
283         RNA_def_enum(ot->srna, "method", unpack_method_items, PF_USE_LOCAL, "Method", "How to unpack.");
284 }
285
286 /* ******************************************************* */
287
288 void ED_operatortypes_sound(void)
289 {
290         WM_operatortype_append(SOUND_OT_open);
291         WM_operatortype_append(SOUND_OT_pack);
292         WM_operatortype_append(SOUND_OT_unpack);
293 }