|
| 1 | +// |
| 2 | +// Copyright (c) 2010 Illarionov Oleg, Marc Lehmann and Tim Smart. |
| 3 | +// Copyright (c) 2011 Intel Corp. |
| 4 | +// |
| 5 | +// Permission is hereby granted, free of charge, to any person |
| 6 | +// obtaining a copy of this software and associated documentation |
| 7 | +// files (the "Software"), to deal in the Software without |
| 8 | +// restriction, including without limitation the rights to use, copy, |
| 9 | +// modify, merge, publish, distribute, sublicense, and/or sell copies |
| 10 | +// of the Software, and to permit persons to whom the Software is |
| 11 | +// furnished to do so, subject to the following conditions: |
| 12 | + |
| 13 | +// The above copyright notice and this permission notice shall be |
| 14 | +// included in all copies or substantial portions of the Software. |
| 15 | + |
| 16 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 17 | +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 18 | +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 19 | +// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS |
| 20 | +// BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN |
| 21 | +// ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 22 | +// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 23 | +// SOFTWARE. |
| 24 | + |
| 25 | +// The EV-Glib section of this source was derived from the EV::Glib |
| 26 | +// module which was relicensed with permission from Marc Lehmann. |
| 27 | + |
| 28 | +#include "nwebkit.h" |
| 29 | + |
| 30 | +#include <ev.h> |
| 31 | +#include <stdlib.h> |
| 32 | + |
| 33 | +namespace nwebkit { |
| 34 | + |
| 35 | +using namespace v8; |
| 36 | + |
| 37 | +struct econtext { |
| 38 | + GPollFD *pfd; |
| 39 | + ev_io *iow; |
| 40 | + int nfd, afd; |
| 41 | + gint maxpri; |
| 42 | + |
| 43 | + ev_prepare pw; |
| 44 | + ev_check cw; |
| 45 | + ev_timer tw; |
| 46 | + |
| 47 | + GMainContext *gc; |
| 48 | +}; |
| 49 | + |
| 50 | +static void timer_cb (EV_P_ ev_timer *w, int revents) { |
| 51 | + /* nop */ |
| 52 | +} |
| 53 | + |
| 54 | +static void io_cb (EV_P_ ev_io *w, int revents) { |
| 55 | + /* nop */ |
| 56 | +} |
| 57 | + |
| 58 | +static void prepare_cb (EV_P_ ev_prepare *w, int revents) { |
| 59 | + struct econtext *ctx = (struct econtext *)(((char *)w) - offsetof (struct econtext, pw)); |
| 60 | + gint timeout; |
| 61 | + int i; |
| 62 | + |
| 63 | + g_main_context_dispatch (ctx->gc); |
| 64 | + |
| 65 | + g_main_context_prepare (ctx->gc, &ctx->maxpri); |
| 66 | + |
| 67 | + while (ctx->afd < (ctx->nfd = g_main_context_query ( |
| 68 | + ctx->gc, |
| 69 | + ctx->maxpri, |
| 70 | + &timeout, |
| 71 | + ctx->pfd, |
| 72 | + ctx->afd)) |
| 73 | + ) |
| 74 | + { |
| 75 | + free (ctx->pfd); |
| 76 | + free (ctx->iow); |
| 77 | + |
| 78 | + ctx->afd = 1; |
| 79 | + while (ctx->afd < ctx->nfd) |
| 80 | + ctx->afd <<= 1; |
| 81 | + |
| 82 | + ctx->pfd = (GPollFD*) malloc (ctx->afd * sizeof (GPollFD)); |
| 83 | + ctx->iow = (ev_io*) malloc (ctx->afd * sizeof (ev_io)); |
| 84 | + } |
| 85 | + |
| 86 | + for (i = 0; i < ctx->nfd; ++i) |
| 87 | + { |
| 88 | + GPollFD *pfd = ctx->pfd + i; |
| 89 | + ev_io *iow = ctx->iow + i; |
| 90 | + |
| 91 | + pfd->revents = 0; |
| 92 | + |
| 93 | + ev_io_init ( |
| 94 | + iow, |
| 95 | + io_cb, |
| 96 | + pfd->fd, |
| 97 | + (pfd->events & G_IO_IN ? EV_READ : 0) |
| 98 | + | (pfd->events & G_IO_OUT ? EV_WRITE : 0) |
| 99 | + ); |
| 100 | + iow->data = (void *)pfd; |
| 101 | + ev_set_priority (iow, EV_MINPRI); |
| 102 | + ev_io_start (EV_A_ iow); |
| 103 | + } |
| 104 | + |
| 105 | + if (timeout >= 0) |
| 106 | + { |
| 107 | + ev_timer_set (&ctx->tw, timeout * 1e-3, 0.); |
| 108 | + ev_timer_start (EV_A_ &ctx->tw); |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +static void check_cb (EV_P_ ev_check *w, int revents) { |
| 113 | + |
| 114 | + struct econtext *ctx = (struct econtext *)(((char *)w) - offsetof (struct econtext, cw)); |
| 115 | + int i; |
| 116 | + |
| 117 | + for (i = 0; i < ctx->nfd; ++i) |
| 118 | + { |
| 119 | + ev_io *iow = ctx->iow + i; |
| 120 | + |
| 121 | + if (ev_is_pending (iow)) |
| 122 | + { |
| 123 | + GPollFD *pfd = ctx->pfd + i; |
| 124 | + int revents = ev_clear_pending (EV_A_ iow); |
| 125 | + |
| 126 | + pfd->revents |= pfd->events & |
| 127 | + ((revents & EV_READ ? G_IO_IN : 0) |
| 128 | + | (revents & EV_WRITE ? G_IO_OUT : 0)); |
| 129 | + } |
| 130 | + |
| 131 | + ev_io_stop (EV_A_ iow); |
| 132 | + } |
| 133 | + |
| 134 | + if (ev_is_active (&ctx->tw)) |
| 135 | + ev_timer_stop (EV_A_ &ctx->tw); |
| 136 | + |
| 137 | + if (ctx->nfd || GLIB_CHECK_VERSION (2, 30, 0)) |
| 138 | + // on glib 2.28 passing nfd as 0 will hang |
| 139 | + // if we don't check with nfd == 0 on 2.30, it will also have |
| 140 | + // issues printing lots of warnings |
| 141 | + g_main_context_check (ctx->gc, ctx->maxpri, ctx->pfd, ctx->nfd); |
| 142 | +} |
| 143 | + |
| 144 | +static struct econtext default_context; |
| 145 | + //extern "C" { |
| 146 | + void nwebkit_view_init(const char*, int, int); |
| 147 | + //} |
| 148 | + |
| 149 | +// Extracts a C string from a V8 Utf8Value. |
| 150 | +const char* ToCString(const v8::String::Utf8Value& value) { |
| 151 | + return *value ? *value : "<string conversion failed>"; |
| 152 | +} |
| 153 | + |
| 154 | +static inline v8::Local<v8::Value> CompileRun(const char* source) { |
| 155 | + return v8::Script::Compile(v8::String::New(source))->Run(); |
| 156 | +} |
| 157 | + |
| 158 | +static Handle<Value> GtkInit (const Arguments &args) { |
| 159 | + |
| 160 | + HandleScope scope; |
| 161 | + v8::Local<v8::Context> entered, current; |
| 162 | + Local<Object> thisObj = args.This(); |
| 163 | + Local<Object> options = Local<Object>::Cast(args[1]); |
| 164 | + |
| 165 | + v8::String::Utf8Value str(options->Get(v8::String::New("url"))); |
| 166 | + const char* cstr = ToCString(str); |
| 167 | + int width = options->Get(v8::String::New("width"))->Int32Value(); |
| 168 | + int height = options->Get(v8::String::New("height"))->Int32Value(); |
| 169 | + nwebkit_view_init(cstr, width, height); |
| 170 | + return Undefined(); |
| 171 | +} |
| 172 | + |
| 173 | +void init(Handle<Object> target) { |
| 174 | + HandleScope scope; |
| 175 | + |
| 176 | + if (!g_thread_supported()) |
| 177 | + g_thread_init(NULL); |
| 178 | + |
| 179 | + target->Set(v8::String::NewSymbol("_init"), |
| 180 | + v8::FunctionTemplate::New(GtkInit)->GetFunction()); |
| 181 | + |
| 182 | + GMainContext *gc = g_main_context_default(); |
| 183 | + struct econtext *ctx = &default_context; |
| 184 | + |
| 185 | + ctx->gc = g_main_context_ref(gc); |
| 186 | + ctx->nfd = 0; |
| 187 | + ctx->afd = 0; |
| 188 | + ctx->iow = 0; |
| 189 | + ctx->pfd = 0; |
| 190 | + |
| 191 | + ev_prepare_init (&ctx->pw, prepare_cb); |
| 192 | + ev_set_priority (&ctx->pw, EV_MINPRI); |
| 193 | + ev_prepare_start (EV_DEFAULT_UC_ &ctx->pw); |
| 194 | + ev_unref(EV_DEFAULT_UC); |
| 195 | + |
| 196 | + ev_check_init (&ctx->cw, check_cb); |
| 197 | + ev_set_priority (&ctx->cw, EV_MAXPRI); |
| 198 | + ev_check_start (EV_DEFAULT_UC_ &ctx->cw); |
| 199 | + ev_unref(EV_DEFAULT_UC); |
| 200 | + |
| 201 | + ev_init (&ctx->tw, timer_cb); |
| 202 | + ev_set_priority (&ctx->tw, EV_MINPRI); |
| 203 | +} |
| 204 | + NODE_MODULE(nwebkit, init) |
| 205 | +} // namespace nwebkit |
0 commit comments