plevent.c revision 11031e1cb3ad94545b12935c386593919864c1ba
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is mozilla.org Code.
*
* The Initial Developer of the Original Code is
* Netscape Communications Corporation.
* Portions created by the Initial Developer are Copyright (C) 1998
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
*
* Alternatively, the contents of this file may be used under the terms of
* either of the GNU General Public License Version 2 or later (the "GPL"),
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
#if defined(_WIN32)
#include <windows.h>
#endif
#if defined(XP_OS2)
#define INCL_DOS
#define INCL_DOSERRORS
#define INCL_WIN
#include <os2.h>
#define DefWindowProc WinDefWindowProc
#endif /* XP_OS2 */
#include "nspr.h"
#include "plevent.h"
#if !defined(WIN32)
#include <errno.h>
#include <stddef.h>
#if !defined(XP_OS2)
#include <unistd.h>
#endif /* !XP_OS2 */
#endif /* !Win32 */
#if defined(XP_UNIX)
/* for fcntl */
#include <fcntl.h>
#endif
#if defined(XP_BEOS)
#endif
#if defined(XP_MACOSX)
#if defined(MOZ_WIDGET_COCOA)
#include <CoreFoundation/CoreFoundation.h>
#define MAC_USE_CFRUNLOOPSOURCE
#elif defined(TARGET_CARBON)
/* #include <CarbonEvents.h> */
/* #define MAC_USE_CARBON_EVENT */
#include <CoreFoundation/CoreFoundation.h>
#define MAC_USE_CFRUNLOOPSOURCE
#endif
#endif
#include "private/pprthred.h"
#if defined(VMS)
/*
** On OpenVMS, XtAppAddInput doesn't want a regular fd, instead it
** wants an event flag. So, we don't create and use a pipe for
** notification of when an event queue has something ready, instead
** we use an event flag. Shouldn't be a problem if we only have
** a few event queues.
*/
#include <starlet.h>
#include <stsdef.h>
#endif /* VMS */
#if defined(_WIN32)
/* Comment out the following USE_TIMER define to prevent
* WIN32 from using a WIN32 native timer for PLEvent notification.
* With USE_TIMER defined we will use a timer when pending input
* or paint events are starved, otherwise it will use a posted
* WM_APP msg for PLEvent notification.
*/
#define USE_TIMER
/* Threshold defined in milliseconds for determining when the input
* and paint events have been held in the WIN32 msg queue too long
*/
#define INPUT_STARVATION_LIMIT 50
/* The paint starvation limit is set to the smallest value which
* does not cause performance degradation while running page load tests
*/
#define PAINT_STARVATION_LIMIT 750
/* The WIN9X paint starvation limit is larger because it was
* determined that the following value was required to prevent performance
* degradation on page load tests for WIN98/95 only.
*/
#define WIN9X_PAINT_STARVATION_LIMIT 3000
#define TIMER_ID 0
/* If _md_PerformanceSetting <=0 then no event starvation otherwise events will be starved */
static PRInt32 _md_PerformanceSetting = 0;
static PRUint32 _md_StarvationDelay = 0;
static PRUint32 _md_SwitchTime = 0;
#endif
/*******************************************************************************
* Private Stuff
******************************************************************************/
/*
** EventQueueType -- Defines notification type for an event queue
**
*/
typedef enum {
EventQueueIsNative = 1,
struct PLEventQueue {
const char* name;
#if defined(_WIN32)
#endif
#if defined(VMS)
int efn;
#else
#endif
void* idFuncClosure;
#if defined(MAC_USE_CFRUNLOOPSOURCE)
#elif defined(MAC_USE_CARBON_EVENT)
#endif
#endif
};
#define PR_EVENT_PTR(_qp) \
#if defined(XP_OS2)
#else
#endif /* OS2 */
static char *_pr_eventWindowClass = "XPCOM:EventWindow";
#endif /* Win32, OS2 */
#if defined(_WIN32)
static LPCTSTR _md_GetEventQueuePropName() {
if (!atom) {
}
return MAKEINTATOM(atom);
}
#endif
#if defined(MAC_USE_CARBON_EVENT)
enum {
};
#endif
/*******************************************************************************
* Event Queue Operations
******************************************************************************/
/*
** _pl_CreateEventQueue() -- Create the event queue
**
**
*/
{
#if defined(_WIN32)
#endif
#endif
if ( qtype == EventQueueIsNative ) {
}
return self;
return NULL;
}
{
}
{
}
{
}
{
}
static void PR_CALLBACK
{
}
PR_IMPLEMENT(void)
{
/* destroy undelivered events */
/* destroying the monitor also destroys the name */
}
{
return PR_FAILURE;
#endif
/* insert event into thread's event queue: */
}
if (err != PR_SUCCESS)
goto error;
}
/*
* This may fall on deaf ears if we're really notifying the native
* thread, and no one has called PL_WaitForEvent (or PL_EventLoop):
*/
return err;
}
PR_IMPLEMENT(void*)
{
void* result;
return NULL;
/* Handle the case where the thread requesting the event handling
* is also the thread that's supposed to do the handling. */
}
else {
int i, entryCount;
return NULL;
}
return NULL;
}
/* We need temporarily to give up our event queue monitor if
we're holding it, otherwise, the thread we're going to wait
for notification from won't be able to enter it to process
the event. */
if (entryCount) {
for (i = 0; i < entryCount; i++)
}
/* wait for event to be handled or destroyed */
}
if (entryCount) {
for (i = 0; i < entryCount; i++)
}
}
/* For synchronous events, they're destroyed here on the caller's
thread before the result is returned. See PL_HandleEvent. */
return result;
}
{
return NULL;
!self->processingEvents &&
0 == _pl_GetEventCount(self) )
{
}
if (err)
goto done;
/* then grab the event and return it: */
}
done:
return event;
}
{
return PR_FALSE;
return result;
}
PR_IMPLEMENT(void)
{
return;
}
}
static void PR_CALLBACK
{
}
else {
}
}
else {
}
}
PR_IMPLEMENT(void)
{
return;
("$$$ revoking events for owner %0x", owner));
/*
** First we enter the monitor so that no one else can post any events
** to the queue:
*/
/*
** Discard any pending events for this owner:
*/
#ifdef DEBUG
{
}
}
#endif /* DEBUG */
("$$$ revoking events for owner %0x", owner));
}
static PRInt32
{
count++;
}
return count;
}
PR_IMPLEMENT(void)
{
return;
if (self->processingEvents) {
return;
}
/* Only process the events that are already in the queue, and
* not any new events that get added. Do this by counting the
* number of events currently in the queue
*/
while (count-- > 0) {
break;
}
if (count <= 0) {
}
else {
}
}
}
/*******************************************************************************
* Event Operations
******************************************************************************/
PR_IMPLEMENT(void)
{
#ifdef PL_POST_TIMINGS
#endif
#endif
}
PR_IMPLEMENT(void*)
{
}
PR_IMPLEMENT(void)
{
void* result;
return;
/* This event better not be on an event queue anymore. */
}
else {
/* For asynchronous events, they're destroyed by the event-handler
thread. See PR_PostSynchronousEvent. */
}
}
#ifdef PL_POST_TIMINGS
static long s_eventCount = 0;
static long s_totalTime = 0;
#endif
PR_IMPLEMENT(void)
{
return;
/* This event better not be on an event queue anymore. */
#ifdef PL_POST_TIMINGS
s_eventCount++;
#endif
}
PR_IMPLEMENT(void)
{
return;
/* Only the owner is allowed to dequeue events because once the
client has put it in the queue, they have no idea whether it's
been processed and destroyed or not. */
#if 0
/* I do not think that we need to do this anymore.
if we do not acknowledge and this is the only
only event in the queue, any calls to process
the eventQ will be effective noop.
*/
#endif
}
PR_IMPLEMENT(void)
{
#if defined(_WIN32)
return;
}
if (_md_PerformanceSetting == 0) {
/* Switched from allowing event starvation to no event starvation so grab
the current time to determine when to actually switch to using timers
instead of posted WM_APP messages. */
}
#endif
}
/*******************************************************************************
* Pure Event Queues
*
* For when you're only processing PLEvents and there is no native
* select, thread messages, or AppleEvents.
******************************************************************************/
{
return NULL;
if ((err == PR_FAILURE)
&& (PR_PENDING_INTERRUPT_ERROR == PR_GetError())) break;
}
return event;
}
PR_IMPLEMENT(void)
{
return;
while (PR_TRUE) {
/* This can only happen if the current thread is interrupted */
return;
}
}
}
/*******************************************************************************
* Native Event Queues
*
* For when you need to call select, or WaitNextEvent, and yet also want
* to handle PLEvents.
******************************************************************************/
static PRStatus
{
#if defined(VMS)
unsigned int status;
self->idFuncClosure = 0;
if (!$VMS_STATUS_SUCCESS(status))
return PR_FAILURE;
return PR_SUCCESS;
int err;
int flags;
self->idFuncClosure = 0;
if (err != 0) {
return PR_FAILURE;
}
/* make the pipe nonblocking */
if (flags == -1) {
goto failed;
}
if (err == -1) {
goto failed;
}
if (flags == -1) {
goto failed;
}
if (err == -1) {
goto failed;
}
return PR_SUCCESS;
return PR_FAILURE;
/* hook up to the nsToolkit queue, however the appshell
* isn't necessairly started, so we might have to create
* the queue ourselves
*/
char portname[64];
char semname[64];
(long unsigned) self->handlerThread);
(long unsigned) self->handlerThread);
{
/* create port
*/
/* We don't use the sem, but it has to be there
*/
create_sem(0, semname);
}
return PR_SUCCESS;
#else
return PR_SUCCESS;
#endif
}
static void
{
#if defined(VMS)
{
unsigned int status;
}
}
/* DestroyWindow doesn't do anything when called from a non ui thread. Since
* self->eventReceiverWindow was created on the ui thread, it must be destroyed
* on the ui thread.
*/
#elif defined(MAC_USE_CFRUNLOOPSOURCE)
#elif defined(MAC_USE_CARBON_EVENT)
if (comparator) {
}
#endif
}
#if defined(_WIN32)
static PRUint32 _md_InputTime = 0;
static PRUint32 _md_PaintTime = 0;
/* last mouse location */
static POINT _md_LastMousePos;
/*******************************************************************************
* Timer callback function. Timers are used on WIN32 instead of APP events
* when there are pending UI events because APP events can cause the GUI to lockup
* because posted messages are processed before other messages.
******************************************************************************/
{
}
static void _md_DetermineOSType()
{
GetVersionEx(&os);
}
}
static PRUint32 _md_GetPaintStarvationLimit()
{
if (! _md_IsOSSet) {
}
if (_md_IsWIN9X) {
return WIN9X_PAINT_STARVATION_LIMIT;
}
return PAINT_STARVATION_LIMIT;
}
/*
* Determine if an event is being starved (i.e the starvation limit has
* been exceeded.
* Note: this function uses the current setting and updates the contents
* of the wasPending and lastTime arguments
*
* ispending: PR_TRUE if the event is currently pending
* starvationLimit: Threshold defined in milliseconds for determining when
* the event has been held in the queue too long
* wasPending: PR_TRUE if the last time _md_EventIsStarved was called
* the event was pending. This value is updated within
* this function.
* lastTime: Holds the last time the event was in the queue.
* This value is updated within this function
* returns: PR_TRUE if the event is starved, PR_FALSE otherwise
*/
{
if (*wasPending && isPending) {
/*
* It was pending previously and the event is still
* pending so check to see if the elapsed time is
* over the limit which indicates the event was starved
*/
return PR_TRUE; /* pending and over the limit */
}
return PR_FALSE; /* pending but within the limit */
}
if (isPending) {
/*
* was_pending must be false so record the current time
* so the elapsed time can be computed the next time this
* function is called
*/
*lastTime = currentTime;
*wasPending = PR_TRUE;
return PR_FALSE;
}
/* Event is no longer pending */
*wasPending = PR_FALSE;
return PR_FALSE;
}
/* Determines if the there is a pending Mouse or input event */
{
/* Return immediately there aren't any pending input or paints. */
if (qstatus == 0) {
return PR_FALSE;
}
/* Is there anything other than a QS_MOUSEMOVE pending? */
if ((qstatus & QS_MOUSEBUTTON) ||
return PR_TRUE;
}
/*
* Mouse moves need extra processing to determine if the mouse
* pointer actually changed location because Windows automatically
* generates WM_MOVEMOVE events when a new window is created which
* we need to filter out.
*/
if (qstatus & QS_MOUSEMOVE) {
if ((_md_LastMousePos.x == cursorPos.x) &&
(_md_LastMousePos.y == cursorPos.y)) {
return PR_FALSE; /* This is a fake mouse move */
}
/* Real mouse move */
_md_LastMousePos.x = cursorPos.x;
_md_LastMousePos.y = cursorPos.y;
return PR_TRUE;
}
return PR_FALSE;
}
static PRStatus
{
#ifdef USE_TIMER
/* Since calls to set the _md_PerformanceSetting can be nested
* only performance setting values <= 0 will potentially trigger
* the use of a timer.
*/
if ((_md_PerformanceSetting <= 0) &&
return PR_SUCCESS;
}
/* Check for starved input */
now )) {
/*
* Use a timer for notification. Timers have the lowest priority.
* They are not processed until all other events have been processed.
* This allows any starved paints and input to be processed.
*/
/*
* Clear any pending paint. _md_WasInputPending was cleared in
* _md_EventIsStarved.
*/
return PR_SUCCESS;
}
now) ) {
/*
* Use a timer for notification. Timers have the lowest priority.
* They are not processed until all other events have been processed.
* This allows any starved paints and input to be processed
*/
/*
* Clear any pending input. _md_WasPaintPending was cleared in
* _md_EventIsStarved.
*/
return PR_SUCCESS;
}
/*
* Nothing is being starved so post a message instead of using a timer.
* Posted messages are processed before other messages so they have the
* highest priority.
*/
#endif
return PR_SUCCESS;
}/* --- end _pl_NativeNotify() --- */
#endif
#if defined(XP_OS2)
static PRStatus
{
}/* --- end _pl_NativeNotify() --- */
#endif /* XP_OS2 */
#if defined(VMS)
/* Just set the event flag */
static PRStatus
{
unsigned int status;
("_pl_NativeNotify: self=%p efn=%d",
}/* --- end _pl_NativeNotify() --- */
static PRStatus
{
#define NOTIFY_TOKEN 0xFA
unsigned char buf[] = { NOTIFY_TOKEN };
("_pl_NativeNotify: self=%p",
self));
if (count == 1)
return PR_SUCCESS;
return PR_SUCCESS;
return PR_FAILURE;
}/* --- end _pl_NativeNotify() --- */
#endif /* defined(XP_UNIX) && !defined(XP_MACOSX) */
#if defined(XP_BEOS)
struct ThreadInterfaceData
{
void *data;
};
static PRStatus
{
struct ThreadInterfaceData id;
return PR_SUCCESS; /* Is this correct? */
}
#endif /* XP_BEOS */
#if defined(XP_MACOSX)
static PRStatus
{
#if defined(MAC_USE_CFRUNLOOPSOURCE)
#elif defined(MAC_USE_CARBON_EVENT)
return PR_FAILURE;
}
return PR_FAILURE;
#endif
return PR_SUCCESS;
}
#endif /* defined(XP_MACOSX) */
static PRStatus
{
#ifdef XP_OS2
#else
#endif
/*
* only remove msg when we've been called directly by
* PL_ProcessPendingEvents, not when we've been called by
* the window proc because the window proc will remove the
* msg for us.
*/
("_pl_AcknowledgeNativeNotify: self=%p", self));
#ifdef XP_OS2
#else
}
#endif
}
return PR_SUCCESS;
("_pl_AcknowledgeNativeNotify: self=%p efn=%d",
/*
** If this is the last entry, then clear the event flag. Also make sure
** the flag is cleared on any spurious wakeups.
*/
return PR_SUCCESS;
unsigned char c;
("_pl_AcknowledgeNativeNotify: self=%p",
self));
/* consume the byte NativeNotify put in our pipe: */
return PR_SUCCESS;
return PR_SUCCESS;
return PR_FAILURE;
#else
/* nothing to do on the other platforms */
return PR_SUCCESS;
#endif
}
{
return -1;
#if defined(VMS)
#else
return -1; /* other platforms don't handle this (yet) */
#endif
}
{
}
{
}
#if defined(_WIN32)
/*
** Global Instance handle...
** In Win32 this is the module handle of the DLL.
**
*/
static HINSTANCE _pr_hInstance;
#endif
#if defined(_WIN32)
/*
** Initialization routine for the DLL...
*/
{
switch (dwReason)
{
case DLL_PROCESS_ATTACH:
break;
case DLL_THREAD_ATTACH:
break;
case DLL_THREAD_DETACH:
break;
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}
#endif
#ifdef XP_OS2
#else
#endif
{
if (_pr_PostEventMsgId == uMsg )
{
#ifdef XP_OS2
return MRFROMLONG(TRUE);
#else
return TRUE;
#endif
}
}
static PRBool isInitialized;
static PRCallOnceType once;
/*
** InitWinEventLib() -- Create the Windows initialization lock
**
*/
static PRStatus InitEventLib( void )
{
initLock = PR_NewLock();
}
#endif /* Win32, OS2 */
#if defined(_WIN32)
/*
** _md_CreateEventQueue() -- ModelDependent initializer
*/
{
/*
** If this is the first call to PL_InitializeEventsLib(),
** make the call to InitWinEventLib() to create the initLock.
**
** Then lock the initializer lock to insure that
** we have exclusive control over the initialization sequence.
**
*/
/* Register the windows message for XPCOM Event notification */
/* Register the class for the event receiver window */
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
RegisterClass(&wc);
}
/* Create the event receiver window */
"XPCOM:EventReceiver",
0, 0, 0, 10, 10,
NULL);
/* Set a property which can be used to retrieve the event queue
* within the _md_TimerProc callback
*/
return;
} /* end _md_CreateEventQueue() */
#endif /* Winxx */
#if defined(XP_OS2)
/*
** _md_CreateEventQueue() -- ModelDependent initializer
*/
{
/* Must have HMQ for this & can't assume we already have appshell */
{
/* Set our app to be a PM app before attempting Win calls */
hab = WinInitialize(0);
}
if( !_pr_PostEventMsgId)
{
WinRegisterClass( 0 /* hab_current */,
0, 0);
"XPCOM_PostEvent");
}
"", 0,
0, 0, 0, 0,
0,
NULL,
NULL);
return;
} /* end _md_CreateEventQueue() */
#endif /* XP_OS2 */
/*
** _md_CreateEventQueue() -- ModelDependent initializer
*/
{
/* there's really nothing special to do here,
** the guts of the unix stuff is in the setupnativenotify
** and related functions.
*/
return;
} /* end _md_CreateEventQueue() */
#endif /* (defined(XP_UNIX) && !defined(XP_MACOSX)) || defined(XP_BEOS) */
#if defined(MAC_USE_CFRUNLOOPSOURCE)
static void _md_EventReceiverProc(void *info)
{
}
#elif defined(MAC_USE_CARBON_EVENT)
/*
** _md_CreateEventQueue() -- ModelDependent initializer
*/
void* userData)
{
{
{
return noErr;
}
}
return eventNotHandledErr;
}
void *inCompareData)
{
{
}
return match;
}
#endif /* defined(MAC_USE_CARBON_EVENT) */
#if defined(XP_MACOSX)
{
#if defined(MAC_USE_CFRUNLOOPSOURCE)
CFRunLoopSourceContext sourceContext = { 0 };
sourceContext.version = 0;
/* make a run loop source */
eventQueue->mRunLoopSource = CFRunLoopSourceCreate(kCFAllocatorDefault, 0 /* order */, &sourceContext);
/* and add it to the run loop */
#elif defined(MAC_USE_CARBON_EVENT)
if (eventQueue->eventHandlerUPP)
{
}
#endif
} /* end _md_CreateEventQueue() */
#endif /* defined(XP_MACOSX) */
/* extra functions for unix */
{
return -1;
if (aSelf->processingEvents) {
return 0;
}
/* Only process the events that are already in the queue, and
* not any new events that get added. Do this by counting the
* number of events currently in the queue
*/
if (fullCount == 0) {
return 0;
}
while (fullCount-- > 0) {
/* peek at the next event */
break;
break;
}
count++;
}
/* if full count still had items left then there's still items left
in the queue. Let the native notify token stay. */
if (fullCount <= 0) {
}
}
return count;
}
PR_IMPLEMENT(void)
void *aClosure)
{
}
PR_IMPLEMENT(void)
{
aSelf->idFuncClosure = 0;
}
#endif /* defined(XP_UNIX) && !defined(XP_MACOSX) */
/* --- end plevent.c --- */