r553: Modern gccs require __attribute__((used)) for variables used only in assembly.
[cinelerra_cv/mob.git] / cinelerra / mainindexes.C
blob36ec75c986f2208d3c67f53e914688257fcac6e9
1 #include "asset.h"
2 #include "defaults.h"
3 #include "edl.h"
4 #include "filesystem.h"
5 #include "indexfile.h"
6 #include "condition.h"
7 #include "language.h"
8 #include "loadfile.h"
9 #include "guicast.h"
10 #include "mainindexes.h"
11 #include "mainprogress.h"
12 #include "mwindow.h"
13 #include "mwindowgui.h"
15 #include <string.h>
18 MainIndexes::MainIndexes(MWindow *mwindow)
19  : Thread()
21         set_synchronous(1);
22         this->mwindow = mwindow;
23         input_lock = new Condition(0, "MainIndexes::input_lock");
24         next_lock = new Mutex("MainIndexes::next_lock");
25         interrupt_lock = new Condition(1, "MainIndexes::interrupt_lock");
26         interrupt_flag = 0;
27         done = 0;
28         indexfile = new IndexFile(mwindow);
31 MainIndexes::~MainIndexes()
33         mwindow->mainprogress->cancelled = 1;
34         stop_loop();
35         delete indexfile;
36         delete next_lock;
37         delete input_lock;
38         delete interrupt_lock;
41 void MainIndexes::add_next_asset(Asset *asset)
43         next_lock->lock("MainIndexes::add_next_asset");
45 // Test current asset
46         IndexFile indexfile(mwindow);
48 //printf("MainIndexes::add_next_asset 1 %s\n", asset->path);
49         if(!indexfile.open_index(asset))
50         {
51 //printf("MainIndexes::add_next_asset 2\n");
52                 asset->index_status = INDEX_READY;
53                 indexfile.close_index();
54         }
55         else
56 // Put copy of asset in stack, not the real thing.
57         {
58 //printf("MainIndexes::add_next_asset 3\n");
59                 Asset *new_asset = new Asset;
60                 *new_asset = *asset;
61 // If the asset existed and was overwritten, the status will be READY.
62                 new_asset->index_status = INDEX_NOTTESTED;
63                 next_assets.append(new_asset);
64         }
66         next_lock->unlock();
69 void MainIndexes::delete_current_assets()
71         current_assets.remove_all_objects();
74 void MainIndexes::start_loop()
76         interrupt_flag = 0;
77         start();
80 void MainIndexes::stop_loop()
82         interrupt_flag = 1;
83         done = 1;
84         input_lock->unlock();
85         interrupt_lock->unlock();
86         Thread::join();
90 void MainIndexes::start_build()
92 //printf("MainIndexes::start_build 1\n");
93         interrupt_flag = 0;
94 // Locked up when indexes were already being built and an asset was 
95 // pasted.
96 //      interrupt_lock.lock();
97         input_lock->unlock();
100 void MainIndexes::interrupt_build()
102 //printf("MainIndexes::interrupt_build 1\n");
103         interrupt_flag = 1;
104         indexfile->interrupt_index();
105 //printf("MainIndexes::interrupt_build 2\n");
106         interrupt_lock->lock("MainIndexes::interrupt_build");
107 //printf("MainIndexes::interrupt_build 3\n");
108         interrupt_lock->unlock();
109 //printf("MainIndexes::interrupt_build 4\n");
112 void MainIndexes::load_next_assets()
114         delete_current_assets();
116 // Transfer from new list
117         next_lock->lock("MainIndexes::load_next_assets");
118         for(int i = 0; i < next_assets.total; i++)
119                 current_assets.append(next_assets.values[i]);
121 // Clear pointers from new list only
122         next_assets.remove_all();
123         next_lock->unlock();
127 void MainIndexes::run()
129         while(!done)
130         {
131 // Wait for new assets to be released
132                 input_lock->lock("MainIndexes::run 1");
133                 if(done) return;
134                 interrupt_lock->lock("MainIndexes::run 2");
135                 load_next_assets();
136                 interrupt_flag = 0;
143 // test index of each asset
144                 MainProgressBar *progress = 0;
145                 for(int i = 0; i < current_assets.total && !interrupt_flag; i++)
146                 {
147                         Asset *current_asset = current_assets.values[i];
148 //printf("MainIndexes::run 3 %s %d %d\n", current_asset->path, current_asset->index_status, current_asset->audio_data);
150                         if(current_asset->index_status == INDEX_NOTTESTED && 
151                                 current_asset->audio_data)
152                         {
153 //printf("MainIndexes::run 4\n");
156 // Doesn't exist.
157 // Try to create index now.
158                                 if(indexfile->open_index(current_asset))
159                                 {
160 //printf("MainIndexes::run 5 %p %s %p %p\n", current_asset, current_asset->path, mwindow, mwindow->mainprogress);
161                                         if(!progress)
162                                         {
163                                                 if(mwindow->gui) mwindow->gui->lock_window("MainIndexes::run 1");
164                                                 progress = mwindow->mainprogress->start_progress(_("Building Indexes..."), 1);
165                                                 if(mwindow->gui) mwindow->gui->unlock_window();
166                                         }
168 //printf("MainIndexes::run 5 %p %s\n", current_asset, current_asset->path);
170                                         indexfile->create_index(current_asset, progress);
171 //printf("MainIndexes::run 6 %p %s\n", current_asset, current_asset->path);
172                                         if(progress->is_cancelled()) interrupt_flag = 1;
173 //printf("MainIndexes::run 7 %p %s\n", current_asset, current_asset->path);
174                                 }
175                                 else
176 // Exists.  Update real thing.
177                                 {
178 //printf("MainIndexes::run 8\n");
179                                         if(current_asset->index_status == INDEX_NOTTESTED)
180                                         {
181                                                 current_asset->index_status = INDEX_READY;
182                                                 if(mwindow->gui) mwindow->gui->lock_window("MainIndexes::run 2");
183                                                 mwindow->edl->set_index_file(current_asset);
184                                                 if(mwindow->gui) mwindow->gui->unlock_window();
185                                         }
186                                         indexfile->close_index();
187                                 }
190 //printf("MainIndexes::run 8\n");
191                         }
192 //printf("MainIndexes::run 9\n");
193                 }
195                 if(progress)     // progress box is only created when an index is built
196                 {
197                         if(mwindow->gui) mwindow->gui->lock_window("MainIndexes::run 3");
198                         progress->stop_progress();
199                         delete progress;
200                         if(mwindow->gui) mwindow->gui->unlock_window();
201                         progress = 0;
202                 }
209                 interrupt_lock->unlock();
210         }