michael@0: /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ michael@0: /* vim:expandtab:shiftwidth=4:tabstop=4: michael@0: */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include "nsAppShell.h" michael@0: #include "nsWindow.h" michael@0: #include "prlog.h" michael@0: #include "prenv.h" michael@0: #include "mozilla/HangMonitor.h" michael@0: #include "mozilla/unused.h" michael@0: #include "GeckoProfiler.h" michael@0: michael@0: using mozilla::unused; michael@0: michael@0: #define NOTIFY_TOKEN 0xFA michael@0: michael@0: #ifdef PR_LOGGING michael@0: PRLogModuleInfo *gWidgetLog = nullptr; michael@0: PRLogModuleInfo *gWidgetFocusLog = nullptr; michael@0: PRLogModuleInfo *gWidgetDragLog = nullptr; michael@0: PRLogModuleInfo *gWidgetDrawLog = nullptr; michael@0: #endif michael@0: michael@0: static GPollFunc sPollFunc; michael@0: michael@0: // Wrapper function to disable hang monitoring while waiting in poll(). michael@0: static gint michael@0: PollWrapper(GPollFD *ufds, guint nfsd, gint timeout_) michael@0: { michael@0: mozilla::HangMonitor::Suspend(); michael@0: profiler_sleep_start(); michael@0: gint result = (*sPollFunc)(ufds, nfsd, timeout_); michael@0: profiler_sleep_end(); michael@0: mozilla::HangMonitor::NotifyActivity(); michael@0: return result; michael@0: } michael@0: michael@0: /*static*/ gboolean michael@0: nsAppShell::EventProcessorCallback(GIOChannel *source, michael@0: GIOCondition condition, michael@0: gpointer data) michael@0: { michael@0: nsAppShell *self = static_cast(data); michael@0: michael@0: unsigned char c; michael@0: unused << read(self->mPipeFDs[0], &c, 1); michael@0: NS_ASSERTION(c == (unsigned char) NOTIFY_TOKEN, "wrong token"); michael@0: michael@0: self->NativeEventCallback(); michael@0: return TRUE; michael@0: } michael@0: michael@0: nsAppShell::~nsAppShell() michael@0: { michael@0: if (mTag) michael@0: g_source_remove(mTag); michael@0: if (mPipeFDs[0]) michael@0: close(mPipeFDs[0]); michael@0: if (mPipeFDs[1]) michael@0: close(mPipeFDs[1]); michael@0: } michael@0: michael@0: nsresult michael@0: nsAppShell::Init() michael@0: { michael@0: #ifdef PR_LOGGING michael@0: if (!gWidgetLog) michael@0: gWidgetLog = PR_NewLogModule("Widget"); michael@0: if (!gWidgetFocusLog) michael@0: gWidgetFocusLog = PR_NewLogModule("WidgetFocus"); michael@0: if (!gWidgetDragLog) michael@0: gWidgetDragLog = PR_NewLogModule("WidgetDrag"); michael@0: if (!gWidgetDrawLog) michael@0: gWidgetDrawLog = PR_NewLogModule("WidgetDraw"); michael@0: #endif michael@0: michael@0: if (!sPollFunc) { michael@0: sPollFunc = g_main_context_get_poll_func(nullptr); michael@0: g_main_context_set_poll_func(nullptr, &PollWrapper); michael@0: } michael@0: michael@0: if (PR_GetEnv("MOZ_DEBUG_PAINTS")) michael@0: gdk_window_set_debug_updates(TRUE); michael@0: michael@0: int err = pipe(mPipeFDs); michael@0: if (err) michael@0: return NS_ERROR_OUT_OF_MEMORY; michael@0: michael@0: GIOChannel *ioc; michael@0: GSource *source; michael@0: michael@0: // make the pipe nonblocking michael@0: michael@0: int flags = fcntl(mPipeFDs[0], F_GETFL, 0); michael@0: if (flags == -1) michael@0: goto failed; michael@0: err = fcntl(mPipeFDs[0], F_SETFL, flags | O_NONBLOCK); michael@0: if (err == -1) michael@0: goto failed; michael@0: flags = fcntl(mPipeFDs[1], F_GETFL, 0); michael@0: if (flags == -1) michael@0: goto failed; michael@0: err = fcntl(mPipeFDs[1], F_SETFL, flags | O_NONBLOCK); michael@0: if (err == -1) michael@0: goto failed; michael@0: michael@0: ioc = g_io_channel_unix_new(mPipeFDs[0]); michael@0: source = g_io_create_watch(ioc, G_IO_IN); michael@0: g_io_channel_unref(ioc); michael@0: g_source_set_callback(source, (GSourceFunc)EventProcessorCallback, this, nullptr); michael@0: g_source_set_can_recurse(source, TRUE); michael@0: mTag = g_source_attach(source, nullptr); michael@0: g_source_unref(source); michael@0: michael@0: return nsBaseAppShell::Init(); michael@0: failed: michael@0: close(mPipeFDs[0]); michael@0: close(mPipeFDs[1]); michael@0: mPipeFDs[0] = mPipeFDs[1] = 0; michael@0: return NS_ERROR_FAILURE; michael@0: } michael@0: michael@0: void michael@0: nsAppShell::ScheduleNativeEventCallback() michael@0: { michael@0: unsigned char buf[] = { NOTIFY_TOKEN }; michael@0: unused << write(mPipeFDs[1], buf, 1); michael@0: } michael@0: michael@0: bool michael@0: nsAppShell::ProcessNextNativeEvent(bool mayWait) michael@0: { michael@0: return g_main_context_iteration(nullptr, mayWait); michael@0: }