source: trunk/libdjvu/DjVuGlobal.cpp @ 206

Last change on this file since 206 was 206, checked in by Eugene Romanenko, 14 years ago

DJVU plugin: djvulibre updated to version 3.5.19

File size: 7.6 KB
Line 
1//C-  -*- C++ -*-
2//C- -------------------------------------------------------------------
3//C- DjVuLibre-3.5
4//C- Copyright (c) 2002  Leon Bottou and Yann Le Cun.
5//C- Copyright (c) 2001  AT&T
6//C-
7//C- This software is subject to, and may be distributed under, the
8//C- GNU General Public License, either Version 2 of the license,
9//C- or (at your option) any later version. The license should have
10//C- accompanied the software or you may obtain a copy of the license
11//C- from the Free Software Foundation at http://www.fsf.org .
12//C-
13//C- This program is distributed in the hope that it will be useful,
14//C- but WITHOUT ANY WARRANTY; without even the implied warranty of
15//C- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16//C- GNU General Public License for more details.
17//C-
18//C- DjVuLibre-3.5 is derived from the DjVu(r) Reference Library from
19//C- Lizardtech Software.  Lizardtech Software has authorized us to
20//C- replace the original DjVu(r) Reference Library notice by the following
21//C- text (see doc/lizard2002.djvu and doc/lizardtech2007.djvu):
22//C-
23//C-  ------------------------------------------------------------------
24//C- | DjVu (r) Reference Library (v. 3.5)
25//C- | Copyright (c) 1999-2001 LizardTech, Inc. All Rights Reserved.
26//C- | The DjVu Reference Library is protected by U.S. Pat. No.
27//C- | 6,058,214 and patents pending.
28//C- |
29//C- | This software is subject to, and may be distributed under, the
30//C- | GNU General Public License, either Version 2 of the license,
31//C- | or (at your option) any later version. The license should have
32//C- | accompanied the software or you may obtain a copy of the license
33//C- | from the Free Software Foundation at http://www.fsf.org .
34//C- |
35//C- | The computer code originally released by LizardTech under this
36//C- | license and unmodified by other parties is deemed "the LIZARDTECH
37//C- | ORIGINAL CODE."  Subject to any third party intellectual property
38//C- | claims, LizardTech grants recipient a worldwide, royalty-free,
39//C- | non-exclusive license to make, use, sell, or otherwise dispose of
40//C- | the LIZARDTECH ORIGINAL CODE or of programs derived from the
41//C- | LIZARDTECH ORIGINAL CODE in compliance with the terms of the GNU
42//C- | General Public License.   This grant only confers the right to
43//C- | infringe patent claims underlying the LIZARDTECH ORIGINAL CODE to
44//C- | the extent such infringement is reasonably necessary to enable
45//C- | recipient to make, have made, practice, sell, or otherwise dispose
46//C- | of the LIZARDTECH ORIGINAL CODE (or portions thereof) and not to
47//C- | any greater extent that may be necessary to utilize further
48//C- | modifications or combinations.
49//C- |
50//C- | The LIZARDTECH ORIGINAL CODE is provided "AS IS" WITHOUT WARRANTY
51//C- | OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
52//C- | TO ANY WARRANTY OF NON-INFRINGEMENT, OR ANY IMPLIED WARRANTY OF
53//C- | MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
54//C- +------------------------------------------------------------------
55//
56// $Id: DjVuGlobal.cpp,v 1.8 2007/03/25 20:48:30 leonb Exp $
57// $Name: release_3_5_19 $
58
59#ifdef HAVE_CONFIG_H
60# include "config.h"
61#endif
62#if NEED_GNUG_PRAGMAS
63# pragma implementation
64#endif
65
66/** This file impliments the DjVuProgressTask elements.  The memory
67    functions are implimented in a separate file, because only the memory
68    functions should be compiled with out overloading of the memory functions.
69 */
70 
71
72#ifdef NEED_DJVU_PROGRESS
73#include "DjVuGlobal.h"
74
75
76// ----------------------------------------
77
78#include "GOS.h"
79#include "GThreads.h"
80#include "GException.h"
81#include "GContainer.h"
82#include <stdlib.h>
83#include <stdio.h>
84#include <string.h>
85
86#define INITIAL  500
87#define INTERVAL 250
88
89class DjVuProgressTask::Data : public GPEnabled
90{
91public:
92  djvu_progress_callback *callback;
93  DjVuProgressTask *head;
94  const char *gtask;
95  unsigned long lastsigdate;
96  Data(djvu_progress_callback *_callback):
97    callback(_callback), head(0), gtask(0), lastsigdate(0) {}
98};
99
100 
101static GPMap<void *,DjVuProgressTask::Data> &
102get_map(void)
103{
104  static GPMap<void *,DjVuProgressTask::Data> xmap;
105  return xmap;
106}
107
108djvu_progress_callback *
109DjVuProgressTask::set_callback(djvu_progress_callback *_callback)
110{ 
111  djvu_progress_callback *retval=0;
112  if(_callback)
113  {
114    GMap<void *,GP<DjVuProgressTask::Data> > &map=get_map();
115    void *threadID=GThread::current();
116    if(map.contains(threadID))
117    {
118      DjVuProgressTask::Data &data=*(map[threadID]);
119      retval=data.callback;
120      data.callback=_callback;
121      data.head=0;
122      data.gtask=0;
123      data.lastsigdate=0;
124    }else
125    {
126      map[threadID]=new Data(_callback);
127    }
128  }else
129  {
130    GMap<void *,GP<DjVuProgressTask::Data> > &map=get_map();
131    void *threadID=GThread::current();
132    if(map.contains(threadID))
133    {
134      DjVuProgressTask::Data &data=*(map[threadID]);
135      retval=data.callback;
136      data.callback=0;
137      data.head=0;
138      data.gtask=0;
139      data.lastsigdate=0;
140      map.del(threadID);
141    }
142  }
143  return retval;
144}
145
146DjVuProgressTask::DjVuProgressTask(const char *xtask,int nsteps)
147  : task(xtask),parent(0), nsteps(nsteps), runtostep(0), gdata(0), data(0)
148{
149  //  gtask=task;
150  {
151    GMap<void *,GP<DjVuProgressTask::Data> > &map=get_map();
152    void *threadID=GThread::current();
153    if(map.contains(threadID))
154    {
155      gdata=new GP<Data>;
156      Data &d=*(data=((*(GP<Data> *)gdata)=map[threadID]));
157      if(d.callback)
158      {
159        unsigned long curdate = GOS::ticks();
160        startdate = curdate;
161        if (!d.head)
162          d.lastsigdate = curdate + INITIAL;
163        parent = d.head;
164        d.head = this;
165      }
166    }
167  }
168}
169
170DjVuProgressTask::~DjVuProgressTask()
171{
172  if (data && data->callback)
173  {
174    if (data->head != this)
175      G_THROW( ERR_MSG("DjVuGlobal.not_compatible") );
176    data->head = parent;
177    if (!parent)
178    {
179      unsigned long curdate = GOS::ticks();
180      if((*(data->callback))(data->gtask?data->gtask:"",curdate-startdate, curdate-startdate))
181      {
182        G_THROW("INTERRUPT");
183      }
184    }
185  }
186  delete (GP<Data> *)gdata;
187}
188
189void
190DjVuProgressTask::run(int tostep)
191{
192  if(data)
193  {
194    data->gtask=task;
195    if ((data->callback)&&(tostep>runtostep))
196    {
197      unsigned long curdate = GOS::ticks();
198      if (curdate > data->lastsigdate + INTERVAL)
199        signal(curdate, curdate);
200      runtostep = tostep;
201    }
202  }
203}
204
205void
206DjVuProgressTask::signal(unsigned long curdate, unsigned long estdate)
207{
208  int inprogress = runtostep;
209  if (inprogress > nsteps)
210    inprogress = nsteps;
211  if (inprogress > 0)
212    {
213      const unsigned long enddate = startdate+
214        (unsigned long)(((float)(estdate-startdate) * (float)nsteps) / (float)inprogress);
215      if (parent)
216      {
217        parent->signal(curdate, enddate);
218      }
219      else if (data && data->callback && curdate<enddate)
220      {
221        if((*(data->callback))(data->gtask?data->gtask:"",curdate-startdate, enddate-startdate))
222        {
223          G_THROW("INTERRUPT");
224        }
225        data->lastsigdate = curdate;
226      }
227    }
228}
229
230// Progress callback
231//
232djvu_progress_callback *
233djvu_set_progress_callback( djvu_progress_callback *callback )
234{
235   return DjVuProgressTask::set_callback(callback);
236}
237
238int djvu_supports_progress_callback(void) {return 1;}
239
240#else
241
242#ifndef HAS_DJVU_PROGRESS_TYPEDEF
243extern "C"
244{
245  void *djvu_set_progress_callback(void *);
246  int djvu_supports_progress_callback(void);
247}
248void *djvu_set_progress_callback(void *) { return 0; }
249int djvu_supports_progress_callback(void) {return 0;}
250#else
251int djvu_supports_progress_callback(void) {return 0;}
252djvu_progress_callback *
253djvu_set_progress_callback( djvu_progress_callback *) { return 0; }
254#endif
255
256#endif
257
Note: See TracBrowser for help on using the repository browser.