Skip to content

Commit 95085d3

Browse files
committed
initial commit
0 parents  commit 95085d3

File tree

11 files changed

+446
-0
lines changed

11 files changed

+446
-0
lines changed

.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
build/
2+
*.o
3+
.lock-wscript
4+
tags
5+
.*.swp
6+
*~

README

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
* The following package is needed to build this:
2+
3+
nodejs-dev libwebkitnode-dev
4+
5+
* Build:
6+
7+
node-waf configure build
8+
9+
* Run sample:
10+
11+
node tests/helloworld.js tests/testfs.html

lib/index.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
//
2+
// Copyright Intel Corp. All rights reserved. Permission is hereby
3+
// granted, free of charge, to any person obtaining a copy of this
4+
// software and associated documentation files (the "Software"), to
5+
// deal in the Software without restriction, including without
6+
// limitation the rights to use, copy, modify, merge, publish,
7+
// distribute, sublicense, and/or sell copies of the Software, and to
8+
// permit persons to whom the Software is furnished to do so, subject
9+
// to the following conditions:
10+
11+
// The above copyright notice and this permission notice shall be
12+
// included in all copies or substantial portions of the Software.
13+
14+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15+
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16+
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17+
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
18+
// BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
19+
// ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20+
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
// SOFTWARE.
22+
23+
24+
var nwebkit = require('../build/Release/nwebkit');
25+
26+
nwebkit.init = function(options) {
27+
process.nextTick(function() {
28+
nwebkit._init(module, options);
29+
});
30+
};
31+
32+
global.require = require;
33+
global.process = process;
34+
module.exports = nwebkit;

package.json

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"author": "Roger Wang <roger.wang@intel.com>",
3+
"name": "node-webkit",
4+
"description": "WebKit running in NodeJS",
5+
"version": "0.0.1",
6+
"repository": {
7+
"type": "git",
8+
"url": "git://github.com/rogerwang/node-webkit.git"
9+
},
10+
"main": "./lib",
11+
"dependencies": {},
12+
"devDependencies": {}
13+
}

src/main.cc

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
//
2+
// Copyright Intel Corp. All rights reserved. Permission is hereby
3+
// granted, free of charge, to any person obtaining a copy of this
4+
// software and associated documentation files (the "Software"), to
5+
// deal in the Software without restriction, including without
6+
// limitation the rights to use, copy, modify, merge, publish,
7+
// distribute, sublicense, and/or sell copies of the Software, and to
8+
// permit persons to whom the Software is furnished to do so, subject
9+
// to the following conditions:
10+
11+
// The above copyright notice and this permission notice shall be
12+
// included in all copies or substantial portions of the Software.
13+
14+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15+
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16+
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17+
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
18+
// BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
19+
// ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20+
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
// SOFTWARE.
22+
23+
24+
#include <errno.h>
25+
#include <gtk/gtk.h>
26+
#include <stdlib.h>
27+
#include <string.h>
28+
#include <webkit/webkit.h>
29+
#include <ev.h>
30+
31+
static void destroy_cb(GtkWidget* widget, GtkWidget* window)
32+
{
33+
ev_unref(EV_DEFAULT_UC);
34+
}
35+
36+
37+
static gchar* filename_to_url(const char* filename)
38+
{
39+
if (!g_file_test(filename, G_FILE_TEST_EXISTS))
40+
return 0;
41+
42+
GFile *gfile = g_file_new_for_path(filename);
43+
gchar *file_url = g_file_get_uri(gfile);
44+
g_object_unref(gfile);
45+
46+
return file_url;
47+
}
48+
49+
static void title_change_cb (WebKitWebView* webview,
50+
GParamSpec* pspec,
51+
GtkWidget* window)
52+
{
53+
const gchar* title = webkit_web_view_get_title (WEBKIT_WEB_VIEW (webview));
54+
if (title)
55+
gtk_window_set_title (GTK_WINDOW (window), title);
56+
}
57+
58+
namespace nwebkit {
59+
void nwebkit_view_init(const char* uri, int width, int height)
60+
{
61+
WebKitWebView *webview;
62+
gtk_init (NULL, NULL);
63+
64+
GtkWidget *window;
65+
gchar *url = filename_to_url(uri);
66+
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
67+
if (!width || !height)
68+
gtk_window_set_default_size(GTK_WINDOW(window), 800, 600);
69+
else
70+
gtk_window_set_default_size(GTK_WINDOW(window), width, height);
71+
72+
webview = WEBKIT_WEB_VIEW(webkit_web_view_new());
73+
GtkWidget *scrolled_win = gtk_scrolled_window_new (NULL, NULL);
74+
gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW(scrolled_win),
75+
GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
76+
77+
gtk_container_add (GTK_CONTAINER(scrolled_win), GTK_WIDGET(webview));
78+
gtk_container_add (GTK_CONTAINER (window), scrolled_win);
79+
80+
g_signal_connect (window, "destroy", G_CALLBACK(destroy_cb), NULL);
81+
g_signal_connect (webview, "notify::title",
82+
G_CALLBACK(title_change_cb), window);
83+
84+
webkit_web_view_load_uri(webview, url ? url : uri);
85+
g_free(url);
86+
87+
gtk_widget_grab_focus(GTK_WIDGET(webview));
88+
gtk_widget_show_all(window);
89+
ev_ref(EV_DEFAULT_UC);
90+
}
91+
92+
}

src/nwebkit.cc

Lines changed: 205 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,205 @@
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

Comments
 (0)