2007-03-22 10:30:00 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
|
|
|
/* vim:expandtab:shiftwidth=4:tabstop=4:
|
|
|
|
*/
|
2012-05-21 04:12:37 -07:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <errno.h>
|
2009-01-02 23:37:52 -08:00
|
|
|
#include <gdk/gdk.h>
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsAppShell.h"
|
2008-10-18 18:34:18 -07:00
|
|
|
#include "nsWindow.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "prlog.h"
|
|
|
|
#include "prenv.h"
|
2011-11-14 08:07:42 -08:00
|
|
|
#include "mozilla/HangMonitor.h"
|
2013-08-15 03:14:31 -07:00
|
|
|
#include "mozilla/unused.h"
|
2014-03-28 13:08:22 -07:00
|
|
|
#include "GeckoProfiler.h"
|
2014-06-17 21:11:00 -07:00
|
|
|
#include "nsIPowerManagerService.h"
|
|
|
|
#ifdef MOZ_ENABLE_DBUS
|
|
|
|
#include "WakeLockListener.h"
|
|
|
|
#endif
|
2013-08-15 03:14:31 -07:00
|
|
|
|
|
|
|
using mozilla::unused;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
#define NOTIFY_TOKEN 0xFA
|
|
|
|
|
|
|
|
#ifdef PR_LOGGING
|
2012-07-30 07:20:58 -07:00
|
|
|
PRLogModuleInfo *gWidgetLog = nullptr;
|
|
|
|
PRLogModuleInfo *gWidgetFocusLog = nullptr;
|
|
|
|
PRLogModuleInfo *gWidgetDragLog = nullptr;
|
|
|
|
PRLogModuleInfo *gWidgetDrawLog = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
#endif
|
|
|
|
|
2011-11-14 08:07:42 -08:00
|
|
|
static GPollFunc sPollFunc;
|
|
|
|
|
|
|
|
// Wrapper function to disable hang monitoring while waiting in poll().
|
|
|
|
static gint
|
|
|
|
PollWrapper(GPollFD *ufds, guint nfsd, gint timeout_)
|
|
|
|
{
|
|
|
|
mozilla::HangMonitor::Suspend();
|
2014-03-28 13:08:22 -07:00
|
|
|
profiler_sleep_start();
|
2011-11-14 08:07:42 -08:00
|
|
|
gint result = (*sPollFunc)(ufds, nfsd, timeout_);
|
2014-03-28 13:08:22 -07:00
|
|
|
profiler_sleep_end();
|
2011-11-14 08:07:42 -08:00
|
|
|
mozilla::HangMonitor::NotifyActivity();
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
/*static*/ gboolean
|
|
|
|
nsAppShell::EventProcessorCallback(GIOChannel *source,
|
|
|
|
GIOCondition condition,
|
|
|
|
gpointer data)
|
|
|
|
{
|
2007-07-08 00:08:04 -07:00
|
|
|
nsAppShell *self = static_cast<nsAppShell *>(data);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
unsigned char c;
|
2013-08-15 03:14:31 -07:00
|
|
|
unused << read(self->mPipeFDs[0], &c, 1);
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_ASSERTION(c == (unsigned char) NOTIFY_TOKEN, "wrong token");
|
|
|
|
|
|
|
|
self->NativeEventCallback();
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsAppShell::~nsAppShell()
|
|
|
|
{
|
|
|
|
if (mTag)
|
|
|
|
g_source_remove(mTag);
|
|
|
|
if (mPipeFDs[0])
|
|
|
|
close(mPipeFDs[0]);
|
|
|
|
if (mPipeFDs[1])
|
|
|
|
close(mPipeFDs[1]);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsAppShell::Init()
|
|
|
|
{
|
|
|
|
#ifdef PR_LOGGING
|
|
|
|
if (!gWidgetLog)
|
|
|
|
gWidgetLog = PR_NewLogModule("Widget");
|
|
|
|
if (!gWidgetFocusLog)
|
|
|
|
gWidgetFocusLog = PR_NewLogModule("WidgetFocus");
|
2009-06-14 17:48:52 -07:00
|
|
|
if (!gWidgetDragLog)
|
|
|
|
gWidgetDragLog = PR_NewLogModule("WidgetDrag");
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!gWidgetDrawLog)
|
|
|
|
gWidgetDrawLog = PR_NewLogModule("WidgetDraw");
|
|
|
|
#endif
|
|
|
|
|
2014-06-17 21:11:00 -07:00
|
|
|
#ifdef MOZ_ENABLE_DBUS
|
|
|
|
nsCOMPtr<nsIPowerManagerService> powerManagerService =
|
|
|
|
do_GetService(POWERMANAGERSERVICE_CONTRACTID);
|
|
|
|
|
|
|
|
if (powerManagerService) {
|
|
|
|
powerManagerService->AddWakeLockListener(WakeLockListener::GetSingleton());
|
|
|
|
} else {
|
|
|
|
NS_WARNING("Failed to retrieve PowerManagerService, wakelocks will be broken!");
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2011-11-14 08:07:42 -08:00
|
|
|
if (!sPollFunc) {
|
2013-10-08 11:47:37 -07:00
|
|
|
sPollFunc = g_main_context_get_poll_func(nullptr);
|
|
|
|
g_main_context_set_poll_func(nullptr, &PollWrapper);
|
2011-11-14 08:07:42 -08:00
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
if (PR_GetEnv("MOZ_DEBUG_PAINTS"))
|
|
|
|
gdk_window_set_debug_updates(TRUE);
|
|
|
|
|
|
|
|
int err = pipe(mPipeFDs);
|
|
|
|
if (err)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
2012-11-01 16:29:20 -07:00
|
|
|
GIOChannel *ioc;
|
|
|
|
GSource *source;
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
// make the pipe nonblocking
|
|
|
|
|
|
|
|
int flags = fcntl(mPipeFDs[0], F_GETFL, 0);
|
|
|
|
if (flags == -1)
|
|
|
|
goto failed;
|
|
|
|
err = fcntl(mPipeFDs[0], F_SETFL, flags | O_NONBLOCK);
|
|
|
|
if (err == -1)
|
|
|
|
goto failed;
|
|
|
|
flags = fcntl(mPipeFDs[1], F_GETFL, 0);
|
|
|
|
if (flags == -1)
|
|
|
|
goto failed;
|
|
|
|
err = fcntl(mPipeFDs[1], F_SETFL, flags | O_NONBLOCK);
|
|
|
|
if (err == -1)
|
|
|
|
goto failed;
|
|
|
|
|
|
|
|
ioc = g_io_channel_unix_new(mPipeFDs[0]);
|
2012-11-01 16:29:20 -07:00
|
|
|
source = g_io_create_watch(ioc, G_IO_IN);
|
2007-03-22 10:30:00 -07:00
|
|
|
g_io_channel_unref(ioc);
|
2012-11-01 16:29:20 -07:00
|
|
|
g_source_set_callback(source, (GSourceFunc)EventProcessorCallback, this, nullptr);
|
|
|
|
g_source_set_can_recurse(source, TRUE);
|
|
|
|
mTag = g_source_attach(source, nullptr);
|
|
|
|
g_source_unref(source);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
return nsBaseAppShell::Init();
|
|
|
|
failed:
|
|
|
|
close(mPipeFDs[0]);
|
|
|
|
close(mPipeFDs[1]);
|
|
|
|
mPipeFDs[0] = mPipeFDs[1] = 0;
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsAppShell::ScheduleNativeEventCallback()
|
|
|
|
{
|
|
|
|
unsigned char buf[] = { NOTIFY_TOKEN };
|
2013-08-15 03:14:31 -07:00
|
|
|
unused << write(mPipeFDs[1], buf, 1);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
|
|
|
nsAppShell::ProcessNextNativeEvent(bool mayWait)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2013-10-08 11:47:37 -07:00
|
|
|
return g_main_context_iteration(nullptr, mayWait);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|