Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
michael@0 | 2 | /* vim:expandtab:shiftwidth=4:tabstop=4: |
michael@0 | 3 | */ |
michael@0 | 4 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 5 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 6 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 7 | |
michael@0 | 8 | #include <sys/types.h> |
michael@0 | 9 | #include <unistd.h> |
michael@0 | 10 | #include <fcntl.h> |
michael@0 | 11 | #include <errno.h> |
michael@0 | 12 | #include <gdk/gdk.h> |
michael@0 | 13 | #include "nsAppShell.h" |
michael@0 | 14 | #include "nsWindow.h" |
michael@0 | 15 | #include "prlog.h" |
michael@0 | 16 | #include "prenv.h" |
michael@0 | 17 | #include "mozilla/HangMonitor.h" |
michael@0 | 18 | #include "mozilla/unused.h" |
michael@0 | 19 | #include "GeckoProfiler.h" |
michael@0 | 20 | |
michael@0 | 21 | using mozilla::unused; |
michael@0 | 22 | |
michael@0 | 23 | #define NOTIFY_TOKEN 0xFA |
michael@0 | 24 | |
michael@0 | 25 | #ifdef PR_LOGGING |
michael@0 | 26 | PRLogModuleInfo *gWidgetLog = nullptr; |
michael@0 | 27 | PRLogModuleInfo *gWidgetFocusLog = nullptr; |
michael@0 | 28 | PRLogModuleInfo *gWidgetDragLog = nullptr; |
michael@0 | 29 | PRLogModuleInfo *gWidgetDrawLog = nullptr; |
michael@0 | 30 | #endif |
michael@0 | 31 | |
michael@0 | 32 | static GPollFunc sPollFunc; |
michael@0 | 33 | |
michael@0 | 34 | // Wrapper function to disable hang monitoring while waiting in poll(). |
michael@0 | 35 | static gint |
michael@0 | 36 | PollWrapper(GPollFD *ufds, guint nfsd, gint timeout_) |
michael@0 | 37 | { |
michael@0 | 38 | mozilla::HangMonitor::Suspend(); |
michael@0 | 39 | profiler_sleep_start(); |
michael@0 | 40 | gint result = (*sPollFunc)(ufds, nfsd, timeout_); |
michael@0 | 41 | profiler_sleep_end(); |
michael@0 | 42 | mozilla::HangMonitor::NotifyActivity(); |
michael@0 | 43 | return result; |
michael@0 | 44 | } |
michael@0 | 45 | |
michael@0 | 46 | /*static*/ gboolean |
michael@0 | 47 | nsAppShell::EventProcessorCallback(GIOChannel *source, |
michael@0 | 48 | GIOCondition condition, |
michael@0 | 49 | gpointer data) |
michael@0 | 50 | { |
michael@0 | 51 | nsAppShell *self = static_cast<nsAppShell *>(data); |
michael@0 | 52 | |
michael@0 | 53 | unsigned char c; |
michael@0 | 54 | unused << read(self->mPipeFDs[0], &c, 1); |
michael@0 | 55 | NS_ASSERTION(c == (unsigned char) NOTIFY_TOKEN, "wrong token"); |
michael@0 | 56 | |
michael@0 | 57 | self->NativeEventCallback(); |
michael@0 | 58 | return TRUE; |
michael@0 | 59 | } |
michael@0 | 60 | |
michael@0 | 61 | nsAppShell::~nsAppShell() |
michael@0 | 62 | { |
michael@0 | 63 | if (mTag) |
michael@0 | 64 | g_source_remove(mTag); |
michael@0 | 65 | if (mPipeFDs[0]) |
michael@0 | 66 | close(mPipeFDs[0]); |
michael@0 | 67 | if (mPipeFDs[1]) |
michael@0 | 68 | close(mPipeFDs[1]); |
michael@0 | 69 | } |
michael@0 | 70 | |
michael@0 | 71 | nsresult |
michael@0 | 72 | nsAppShell::Init() |
michael@0 | 73 | { |
michael@0 | 74 | #ifdef PR_LOGGING |
michael@0 | 75 | if (!gWidgetLog) |
michael@0 | 76 | gWidgetLog = PR_NewLogModule("Widget"); |
michael@0 | 77 | if (!gWidgetFocusLog) |
michael@0 | 78 | gWidgetFocusLog = PR_NewLogModule("WidgetFocus"); |
michael@0 | 79 | if (!gWidgetDragLog) |
michael@0 | 80 | gWidgetDragLog = PR_NewLogModule("WidgetDrag"); |
michael@0 | 81 | if (!gWidgetDrawLog) |
michael@0 | 82 | gWidgetDrawLog = PR_NewLogModule("WidgetDraw"); |
michael@0 | 83 | #endif |
michael@0 | 84 | |
michael@0 | 85 | if (!sPollFunc) { |
michael@0 | 86 | sPollFunc = g_main_context_get_poll_func(nullptr); |
michael@0 | 87 | g_main_context_set_poll_func(nullptr, &PollWrapper); |
michael@0 | 88 | } |
michael@0 | 89 | |
michael@0 | 90 | if (PR_GetEnv("MOZ_DEBUG_PAINTS")) |
michael@0 | 91 | gdk_window_set_debug_updates(TRUE); |
michael@0 | 92 | |
michael@0 | 93 | int err = pipe(mPipeFDs); |
michael@0 | 94 | if (err) |
michael@0 | 95 | return NS_ERROR_OUT_OF_MEMORY; |
michael@0 | 96 | |
michael@0 | 97 | GIOChannel *ioc; |
michael@0 | 98 | GSource *source; |
michael@0 | 99 | |
michael@0 | 100 | // make the pipe nonblocking |
michael@0 | 101 | |
michael@0 | 102 | int flags = fcntl(mPipeFDs[0], F_GETFL, 0); |
michael@0 | 103 | if (flags == -1) |
michael@0 | 104 | goto failed; |
michael@0 | 105 | err = fcntl(mPipeFDs[0], F_SETFL, flags | O_NONBLOCK); |
michael@0 | 106 | if (err == -1) |
michael@0 | 107 | goto failed; |
michael@0 | 108 | flags = fcntl(mPipeFDs[1], F_GETFL, 0); |
michael@0 | 109 | if (flags == -1) |
michael@0 | 110 | goto failed; |
michael@0 | 111 | err = fcntl(mPipeFDs[1], F_SETFL, flags | O_NONBLOCK); |
michael@0 | 112 | if (err == -1) |
michael@0 | 113 | goto failed; |
michael@0 | 114 | |
michael@0 | 115 | ioc = g_io_channel_unix_new(mPipeFDs[0]); |
michael@0 | 116 | source = g_io_create_watch(ioc, G_IO_IN); |
michael@0 | 117 | g_io_channel_unref(ioc); |
michael@0 | 118 | g_source_set_callback(source, (GSourceFunc)EventProcessorCallback, this, nullptr); |
michael@0 | 119 | g_source_set_can_recurse(source, TRUE); |
michael@0 | 120 | mTag = g_source_attach(source, nullptr); |
michael@0 | 121 | g_source_unref(source); |
michael@0 | 122 | |
michael@0 | 123 | return nsBaseAppShell::Init(); |
michael@0 | 124 | failed: |
michael@0 | 125 | close(mPipeFDs[0]); |
michael@0 | 126 | close(mPipeFDs[1]); |
michael@0 | 127 | mPipeFDs[0] = mPipeFDs[1] = 0; |
michael@0 | 128 | return NS_ERROR_FAILURE; |
michael@0 | 129 | } |
michael@0 | 130 | |
michael@0 | 131 | void |
michael@0 | 132 | nsAppShell::ScheduleNativeEventCallback() |
michael@0 | 133 | { |
michael@0 | 134 | unsigned char buf[] = { NOTIFY_TOKEN }; |
michael@0 | 135 | unused << write(mPipeFDs[1], buf, 1); |
michael@0 | 136 | } |
michael@0 | 137 | |
michael@0 | 138 | bool |
michael@0 | 139 | nsAppShell::ProcessNextNativeEvent(bool mayWait) |
michael@0 | 140 | { |
michael@0 | 141 | return g_main_context_iteration(nullptr, mayWait); |
michael@0 | 142 | } |