~ubuntu-branches/ubuntu/precise/kompozer/precise

« back to all changes in this revision

Viewing changes to mozilla/widget/src/gtk/nsAppShell.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Anthony Yarusso
  • Date: 2007-08-27 01:11:03 UTC
  • Revision ID: james.westby@ubuntu.com-20070827011103-2jgf4s6532gqu2ka
Tags: upstream-0.7.10
ImportĀ upstreamĀ versionĀ 0.7.10

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- Mode: c++; tab-width: 2; indent-tabs-mode: nil; -*- */
 
2
/* ***** BEGIN LICENSE BLOCK *****
 
3
 * Version: NPL 1.1/GPL 2.0/LGPL 2.1
 
4
 *
 
5
 * The contents of this file are subject to the Netscape Public License
 
6
 * Version 1.1 (the "License"); you may not use this file except in
 
7
 * compliance with the License. You may obtain a copy of the License at
 
8
 * http://www.mozilla.org/NPL/
 
9
 *
 
10
 * Software distributed under the License is distributed on an "AS IS" basis,
 
11
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 
12
 * for the specific language governing rights and limitations under the
 
13
 * License.
 
14
 *
 
15
 * The Original Code is mozilla.org code.
 
16
 *
 
17
 * The Initial Developer of the Original Code is 
 
18
 * Netscape Communications Corporation.
 
19
 * Portions created by the Initial Developer are Copyright (C) 1998
 
20
 * the Initial Developer. All Rights Reserved.
 
21
 *
 
22
 * Contributor(s):
 
23
 *
 
24
 * Alternatively, the contents of this file may be used under the terms of
 
25
 * either the GNU General Public License Version 2 or later (the "GPL"), or 
 
26
 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 
27
 * in which case the provisions of the GPL or the LGPL are applicable instead
 
28
 * of those above. If you wish to allow use of your version of this file only
 
29
 * under the terms of either the GPL or the LGPL, and not to allow others to
 
30
 * use your version of this file under the terms of the NPL, indicate your
 
31
 * decision by deleting the provisions above and replace them with the notice
 
32
 * and other provisions required by the GPL or the LGPL. If you do not delete
 
33
 * the provisions above, a recipient may use your version of this file under
 
34
 * the terms of any one of the NPL, the GPL or the LGPL.
 
35
 *
 
36
 * ***** END LICENSE BLOCK ***** */
 
37
 
 
38
#include "prmon.h"
 
39
#include "plhash.h"
 
40
#include "nscore.h"
 
41
#include "nsCOMPtr.h"
 
42
#include "nsAppShell.h"
 
43
#include "nsIAppShell.h"
 
44
#include "nsIServiceManager.h"
 
45
#include "nsIEventQueueService.h"
 
46
#include "nsICmdLineService.h"
 
47
#include "nsGtkEventHandler.h"
 
48
#include <stdlib.h>
 
49
#include <gdk/gdkx.h>
 
50
 
 
51
#include "nsIWidget.h"
 
52
 
 
53
#include "glib.h"
 
54
#include "nsVoidArray.h"
 
55
 
 
56
static PRBool sInitialized = PR_FALSE;
 
57
static PLHashTable *sQueueHashTable = nsnull;
 
58
static PLHashTable *sCountHashTable = nsnull;
 
59
static nsVoidArray *sEventQueueList = nsnull;
 
60
 
 
61
struct OurGdkIOClosure {
 
62
  GdkInputFunction  function;
 
63
  gpointer          data;
 
64
};
 
65
 
 
66
static gboolean
 
67
our_gdk_io_invoke(GIOChannel* source, GIOCondition condition, gpointer data)
 
68
{
 
69
  OurGdkIOClosure* ioc = (OurGdkIOClosure*) data;
 
70
  if (ioc) {
 
71
    (*ioc->function)(ioc->data, g_io_channel_unix_get_fd(source),
 
72
                     GDK_INPUT_READ);
 
73
  }
 
74
  return TRUE;
 
75
}
 
76
 
 
77
static void
 
78
our_gdk_io_destroy(gpointer data)
 
79
{
 
80
#ifdef DEBUG_APPSHELL
 
81
  printf("our_gdk_io_destroy()\n");
 
82
#endif
 
83
  OurGdkIOClosure* ioc = (OurGdkIOClosure*) data;
 
84
  if (ioc) {
 
85
    g_free(ioc);
 
86
  }
 
87
}
 
88
 
 
89
static gint
 
90
our_gdk_input_add (gint              source,
 
91
                   GdkInputFunction  function,
 
92
                   gpointer          data,
 
93
                   gint              priority)
 
94
{
 
95
#ifdef DEBUG_APPSHELL
 
96
  printf("our_gdk_input_add()\n");
 
97
#endif
 
98
  guint result;
 
99
  OurGdkIOClosure *closure = g_new (OurGdkIOClosure, 1);
 
100
  GIOChannel *channel;
 
101
 
 
102
  closure->function = function;
 
103
  closure->data = data;
 
104
 
 
105
  channel = g_io_channel_unix_new (source);
 
106
  result = g_io_add_watch_full (channel, priority, G_IO_IN,
 
107
                                our_gdk_io_invoke,
 
108
                                closure, our_gdk_io_destroy);
 
109
  g_io_channel_unref (channel);
 
110
 
 
111
  return result;
 
112
}
 
113
 
 
114
// wrapper so we can call a macro
 
115
static unsigned long getNextRequest (void *aClosure) {
 
116
  return NextRequest(GDK_DISPLAY());
 
117
}
 
118
 
 
119
 
 
120
//-------------------------------------------------------------------------
 
121
//
 
122
// XPCOM CIDs
 
123
//
 
124
//-------------------------------------------------------------------------
 
125
static NS_DEFINE_CID(kEventQueueServiceCID, NS_EVENTQUEUESERVICE_CID);
 
126
static NS_DEFINE_CID(kCmdLineServiceCID, NS_COMMANDLINE_SERVICE_CID);
 
127
 
 
128
 
 
129
//-------------------------------------------------------------------------
 
130
//
 
131
// nsAppShell constructor
 
132
//
 
133
//-------------------------------------------------------------------------
 
134
nsAppShell::nsAppShell()
 
135
{
 
136
#ifdef DEBUG_APPSHELL
 
137
  printf("nsAppShell::nsAppShell()\n");
 
138
#endif
 
139
  if (!sEventQueueList)
 
140
    sEventQueueList = new nsVoidArray();
 
141
}
 
142
 
 
143
//-------------------------------------------------------------------------
 
144
//
 
145
// nsAppShell destructor
 
146
//
 
147
//-------------------------------------------------------------------------
 
148
nsAppShell::~nsAppShell()
 
149
{
 
150
#ifdef DEBUG_APPSHELL
 
151
  printf("nsAppShell::~nsAppShell()\n");
 
152
#endif
 
153
}
 
154
 
 
155
/* static */ void
 
156
nsAppShell::ReleaseGlobals()
 
157
{
 
158
  if (sQueueHashTable) {
 
159
    PL_HashTableDestroy(sQueueHashTable);
 
160
    sQueueHashTable = nsnull;
 
161
  }
 
162
  if (sCountHashTable) {
 
163
    PL_HashTableDestroy(sCountHashTable);
 
164
    sCountHashTable = nsnull;
 
165
  }
 
166
  if (sEventQueueList) {
 
167
    delete sEventQueueList;
 
168
    sEventQueueList = nsnull;
 
169
  }
 
170
}
 
171
 
 
172
//-------------------------------------------------------------------------
 
173
//
 
174
// nsISupports implementation macro
 
175
//
 
176
//-------------------------------------------------------------------------
 
177
 
 
178
NS_IMPL_ISUPPORTS1(nsAppShell, nsIAppShell)
 
179
 
 
180
static void event_processor_callback(gpointer data,
 
181
                                     gint source,
 
182
                                     GdkInputCondition condition)
 
183
{
 
184
  nsIEventQueue *eventQueue = (nsIEventQueue*)data;
 
185
  if (eventQueue)
 
186
      eventQueue->ProcessPendingEvents();
 
187
  
 
188
}
 
189
 
 
190
//-------------------------------------------------------------------------
 
191
//
 
192
// Create the application shell
 
193
//
 
194
//-------------------------------------------------------------------------
 
195
 
 
196
NS_IMETHODIMP nsAppShell::Create(int *bac, char **bav)
 
197
{
 
198
#ifdef DEBUG_APPSHELL
 
199
  printf("nsAppShell::Create()\n");
 
200
#endif
 
201
  if (sInitialized)
 
202
    return NS_OK;
 
203
 
 
204
  sInitialized = PR_TRUE;
 
205
 
 
206
  int argc = bac ? *bac : 0;
 
207
  char **argv = bav;
 
208
 
 
209
  nsresult rv;
 
210
 
 
211
  nsCOMPtr<nsICmdLineService> cmdLineArgs = do_GetService(kCmdLineServiceCID);
 
212
  if (cmdLineArgs) {
 
213
    rv = cmdLineArgs->GetArgc(&argc);
 
214
    if(NS_FAILED(rv))
 
215
      argc = bac ? *bac : 0;
 
216
 
 
217
    rv = cmdLineArgs->GetArgv(&argv);
 
218
    if(NS_FAILED(rv))
 
219
      argv = bav;
 
220
  }
 
221
 
 
222
  return NS_OK;
 
223
}
 
224
 
 
225
//-------------------------------------------------------------------------
 
226
//
 
227
// Spinup - do any preparation necessary for running a message loop
 
228
//
 
229
//-------------------------------------------------------------------------
 
230
NS_IMETHODIMP nsAppShell::Spinup()
 
231
{
 
232
  nsresult   rv = NS_OK;
 
233
 
 
234
#ifdef DEBUG_APPSHELL
 
235
  printf("nsAppShell::Spinup()\n");
 
236
#endif
 
237
 
 
238
  // Get the event queue service
 
239
  nsCOMPtr<nsIEventQueueService> eventQService = do_GetService(kEventQueueServiceCID, &rv);
 
240
 
 
241
  if (NS_FAILED(rv)) {
 
242
    NS_ASSERTION("Could not obtain event queue service", PR_FALSE);
 
243
    return rv;
 
244
  }
 
245
 
 
246
  //Get the event queue for the thread.
 
247
  rv = eventQService->GetThreadEventQueue(NS_CURRENT_THREAD, getter_AddRefs(mEventQueue));
 
248
  
 
249
  // If we got an event queue, use it.
 
250
  if (mEventQueue)
 
251
    goto done;
 
252
 
 
253
  // otherwise create a new event queue for the thread
 
254
  rv = eventQService->CreateThreadEventQueue();
 
255
  if (NS_FAILED(rv)) {
 
256
    NS_ASSERTION("Could not create the thread event queue", PR_FALSE);
 
257
    return rv;
 
258
  }
 
259
 
 
260
  // Ask again nicely for the event queue now that we have created one.
 
261
  rv = eventQService->GetThreadEventQueue(NS_CURRENT_THREAD, getter_AddRefs(mEventQueue));
 
262
 
 
263
  // XXX shouldn't this be automatic?
 
264
 done:
 
265
  ListenToEventQueue(mEventQueue, PR_TRUE);
 
266
 
 
267
  return rv;
 
268
}
 
269
 
 
270
//-------------------------------------------------------------------------
 
271
//
 
272
// Spindown - do any cleanup necessary for finishing a message loop
 
273
//
 
274
//-------------------------------------------------------------------------
 
275
NS_IMETHODIMP nsAppShell::Spindown()
 
276
{
 
277
#ifdef DEBUG_APPSHELL
 
278
  printf("nsAppShell::Spindown()\n");
 
279
#endif
 
280
  if (mEventQueue) {
 
281
    ListenToEventQueue(mEventQueue, PR_FALSE);
 
282
    mEventQueue->ProcessPendingEvents();
 
283
    mEventQueue = nsnull;
 
284
  }
 
285
  return NS_OK;
 
286
}
 
287
 
 
288
#ifdef NS_TRACE_MALLOC
 
289
#include "nsTraceMalloc.h"
 
290
 
 
291
static gint
 
292
tm_flush_logfiles(gpointer data)
 
293
{
 
294
  NS_TraceMallocFlushLogfiles();
 
295
  return 1;
 
296
}
 
297
#endif
 
298
 
 
299
//-------------------------------------------------------------------------
 
300
//
 
301
// Run
 
302
//
 
303
//-------------------------------------------------------------------------
 
304
NS_IMETHODIMP nsAppShell::Run()
 
305
{
 
306
  if (!mEventQueue)
 
307
    Spinup();
 
308
  
 
309
  if (!mEventQueue)
 
310
    return NS_ERROR_NOT_INITIALIZED;
 
311
 
 
312
#ifdef NS_TRACE_MALLOC
 
313
  gtk_idle_add(tm_flush_logfiles, nsnull);
 
314
#endif
 
315
 
 
316
  // kick up gtk_main.  this won't return until gtk_main_quit is called
 
317
  gtk_main();
 
318
 
 
319
  Spindown();
 
320
 
 
321
  return NS_OK; 
 
322
}
 
323
 
 
324
//-------------------------------------------------------------------------
 
325
//
 
326
// Exit a message handler loop
 
327
//
 
328
//-------------------------------------------------------------------------
 
329
 
 
330
NS_IMETHODIMP nsAppShell::Exit()
 
331
{
 
332
  gtk_main_quit();
 
333
  return NS_OK;
 
334
}
 
335
 
 
336
// does nothing. used by xp code with non-gtk expectations.
 
337
// this method will be removed once xp eventloops are working.
 
338
NS_IMETHODIMP nsAppShell::GetNativeEvent(PRBool &aRealEvent, void *& aEvent)
 
339
{
 
340
  aRealEvent = PR_FALSE;
 
341
  aEvent = 0;
 
342
 
 
343
  return NS_OK;
 
344
}
 
345
 
 
346
// simply executes one iteration of the event loop. used by xp code with
 
347
// non-gtk expectations.
 
348
// this method will be removed once xp eventloops are working.
 
349
NS_IMETHODIMP nsAppShell::DispatchNativeEvent(PRBool aRealEvent, void *aEvent)
 
350
{
 
351
  if (!mEventQueue)
 
352
    return NS_ERROR_NOT_INITIALIZED;
 
353
 
 
354
  g_main_iteration(PR_TRUE);
 
355
 
 
356
  return NS_OK;
 
357
}
 
358
 
 
359
#define NUMBER_HASH_KEY(_num) ((PLHashNumber) _num)
 
360
 
 
361
static PLHashNumber
 
362
IntHashKey(PRInt32 key)
 
363
{
 
364
  return NUMBER_HASH_KEY(key);
 
365
}
 
366
 
 
367
NS_IMETHODIMP nsAppShell::ListenToEventQueue(nsIEventQueue *aQueue,
 
368
                                             PRBool aListen)
 
369
{
 
370
#ifdef DEBUG_APPSHELL
 
371
  printf("ListenToEventQueue(%p, %d) this=%p\n", aQueue, aListen, this);
 
372
#endif
 
373
  if (!sQueueHashTable) {
 
374
    sQueueHashTable = PL_NewHashTable(3, (PLHashFunction)IntHashKey,
 
375
                                      PL_CompareValues, PL_CompareValues, 0, 0);
 
376
  }
 
377
  if (!sCountHashTable) {
 
378
    sCountHashTable = PL_NewHashTable(3, (PLHashFunction)IntHashKey,
 
379
                                      PL_CompareValues, PL_CompareValues, 0, 0);
 
380
  }    
 
381
 
 
382
  if (aListen) {
 
383
    /* add listener */
 
384
    PRInt32 key = aQueue->GetEventQueueSelectFD();
 
385
 
 
386
    /* only add if we arn't already in the table */
 
387
    if (!PL_HashTableLookup(sQueueHashTable, GINT_TO_POINTER(key))) {
 
388
      gint tag;
 
389
      tag = our_gdk_input_add(aQueue->GetEventQueueSelectFD(),
 
390
                              event_processor_callback,
 
391
                              aQueue,
 
392
                              G_PRIORITY_HIGH_IDLE);
 
393
      if (tag >= 0) {
 
394
        PL_HashTableAdd(sQueueHashTable, GINT_TO_POINTER(key), GINT_TO_POINTER(tag));
 
395
      }
 
396
      PLEventQueue *plqueue;
 
397
      aQueue->GetPLEventQueue(&plqueue);
 
398
      PL_RegisterEventIDFunc(plqueue, getNextRequest, 0);
 
399
      sEventQueueList->AppendElement(plqueue);
 
400
    }
 
401
    /* bump up the count */
 
402
    gint count = GPOINTER_TO_INT(PL_HashTableLookup(sCountHashTable, GINT_TO_POINTER(key)));
 
403
    PL_HashTableAdd(sCountHashTable, GINT_TO_POINTER(key), GINT_TO_POINTER(count+1));
 
404
  } else {
 
405
    /* remove listener */
 
406
    PRInt32 key = aQueue->GetEventQueueSelectFD();
 
407
    
 
408
    PLEventQueue *plqueue;
 
409
    aQueue->GetPLEventQueue(&plqueue);
 
410
    PL_UnregisterEventIDFunc(plqueue);
 
411
    sEventQueueList->RemoveElement(plqueue);
 
412
 
 
413
    gint count = GPOINTER_TO_INT(PL_HashTableLookup(sCountHashTable, GINT_TO_POINTER(key)));
 
414
    if (count - 1 == 0) {
 
415
      gint tag = GPOINTER_TO_INT(PL_HashTableLookup(sQueueHashTable, GINT_TO_POINTER(key)));
 
416
      if (tag > 0) {
 
417
        g_source_remove(tag);
 
418
        PL_HashTableRemove(sQueueHashTable, GINT_TO_POINTER(key));
 
419
      }
 
420
    }
 
421
    PL_HashTableAdd(sCountHashTable, GINT_TO_POINTER(key), GINT_TO_POINTER(count-1));
 
422
 
 
423
  }
 
424
 
 
425
  return NS_OK;
 
426
}
 
427
 
 
428
PRBool processQueue(void *aElement, void *aData)
 
429
{
 
430
  PLEventQueue *queue = (PLEventQueue *) aElement;
 
431
  unsigned int  id = NS_PTR_TO_INT32(aData);
 
432
  PL_ProcessEventsBeforeID(queue, id);
 
433
  return PR_TRUE;
 
434
}
 
435
 
 
436
void
 
437
nsAppShell::ProcessBeforeID(unsigned long aID)
 
438
{
 
439
  if (sEventQueueList)
 
440
    sEventQueueList->EnumerateForwards(processQueue, (void *)aID);
 
441
}