Skip to content

Commit

Permalink
Damn loop
Browse files Browse the repository at this point in the history
  • Loading branch information
tim-smart committed Oct 10, 2010
1 parent cab0178 commit a975088
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 109 deletions.
133 changes: 77 additions & 56 deletions src/ngtk.cc
Expand Up @@ -30,14 +30,17 @@ struct econtext {
};

static void timer_cb (EV_P_ ev_timer *w, int revents) {
printf("%s\n", "timer_cd");
/* nop */
}

static void io_cb (EV_P_ ev_io *w, int revents) {
printf("%s\n", "io_cd");
/* nop */
}

static void prepare_cb (EV_P_ ev_prepare *w, int revents) {
printf("%s\n", "prepare_cb");
struct econtext *ctx = (struct econtext *)(((char *)w) - offsetof (struct econtext, pw));
gint timeout;
int i;
Expand All @@ -47,68 +50,72 @@ static void prepare_cb (EV_P_ ev_prepare *w, int revents) {
g_main_context_prepare (ctx->gc, &ctx->maxpri);

while (ctx->afd < (ctx->nfd = g_main_context_query (
ctx->gc,
ctx->maxpri,
&timeout,
ctx->pfd,
ctx->afd))) {
ctx->gc,
ctx->maxpri,
&timeout,
ctx->pfd,
ctx->afd))
)
{
free (ctx->pfd);
free (ctx->iow);

ctx->afd = 1;
while (ctx->afd < ctx->nfd)
ctx->afd <<= 1;

ctx->pfd = (GPollFD*)malloc (ctx->afd * sizeof (GPollFD));
ctx->iow = (ev_io*)malloc (ctx->afd * sizeof (ev_io));
ctx->pfd = (GPollFD*) malloc (ctx->afd * sizeof (GPollFD));
ctx->iow = (ev_io*) malloc (ctx->afd * sizeof (ev_io));
}

for (i = 0; i < ctx->nfd; ++i) {
GPollFD *pfd = ctx->pfd + i;
ev_io *iow = ctx->iow + i;
for (i = 0; i < ctx->nfd; ++i)
{
GPollFD *pfd = ctx->pfd + i;
ev_io *iow = ctx->iow + i;

pfd->revents = 0;
pfd->revents = 0;

ev_io_init (
ev_io_init (
iow,
io_cb,
pfd->fd,
(pfd->events & G_IO_IN ? EV_READ : 0)
| (pfd->events & G_IO_OUT ? EV_WRITE : 0)
);
iow->data = (void *)pfd;
ev_set_priority (iow, EV_MINPRI);
ev_io_start (EV_A iow);
}
| (pfd->events & G_IO_OUT ? EV_WRITE : 0)
);
iow->data = (void *)pfd;
ev_set_priority (iow, EV_MINPRI);
ev_io_start (EV_A iow);
}

if (timeout >= 0)
{
ev_timer_set (&ctx->tw, timeout * 1e-3, 0.);
ev_timer_start (EV_A &ctx->tw);
}
{
ev_timer_set (&ctx->tw, timeout * 1e-3, 0.);
ev_timer_start (EV_A &ctx->tw);
}
}

static void check_cb (EV_P_ ev_check *w, int revents) {
printf("%s\n", "check_cb");
struct econtext *ctx = (struct econtext *)(((char *)w) - offsetof (struct econtext, cw));
int i;

for (i = 0; i < ctx->nfd; ++i)
{
ev_io *iow = ctx->iow + i;
{
ev_io *iow = ctx->iow + i;

if (ev_is_pending (iow))
{
GPollFD *pfd = ctx->pfd + i;
int revents = ev_clear_pending (EV_A iow);

pfd->revents |= pfd->events &
((revents & EV_READ ? G_IO_IN : 0)
| (revents & EV_WRITE ? G_IO_OUT : 0));
}
if (ev_is_pending (iow))
{
GPollFD *pfd = ctx->pfd + i;
int revents = ev_clear_pending (EV_A iow);

ev_io_stop (EV_A iow);
pfd->revents |= pfd->events &
((revents & EV_READ ? G_IO_IN : 0)
| (revents & EV_WRITE ? G_IO_OUT : 0));
}

ev_io_stop (EV_A iow);
}

if (ev_is_active (&ctx->tw))
ev_timer_stop (EV_A &ctx->tw);

Expand All @@ -117,31 +124,18 @@ static void check_cb (EV_P_ ev_check *w, int revents) {

static struct econtext default_context;

extern "C" void init(Handle<Object> target) {
HandleScope scope;
//static Handle<Value> GtkInit (const Arguments &args) {
//HandleScope scope;

GMainContext *gc = g_main_context_default();
struct econtext *ctx = &default_context;

ctx->gc = g_main_context_ref(gc);
ctx->nfd = 0;
ctx->afd = 0;
ctx->iow = 0;
ctx->pfd = 0;

ev_prepare_init (&ctx->pw, prepare_cb);
ev_set_priority (&ctx->pw, EV_MINPRI);
ev_prepare_start (EV_DEFAULT_ &ctx->pw);
//gtk_init(NULL, NULL);

ev_check_init (&ctx->cw, check_cb);
ev_set_priority (&ctx->cw, EV_MAXPRI);
ev_check_start (EV_DEFAULT_ &ctx->cw);
//return Undefined();
//}

ev_init (&ctx->tw, timer_cb);
ev_set_priority (&ctx->tw, EV_MINPRI);
extern "C" void init(Handle<Object> target) {
HandleScope scope;

// Have to init here, otherwise we segfault :/
gtk_init(NULL, NULL);
printf("%s\n", "init");

// Position constants.
NGTK_DEFINE_CONSTANT(target, "WIN_POS_NONE", GTK_WIN_POS_NONE);
Expand Down Expand Up @@ -188,7 +182,34 @@ extern "C" void init(Handle<Object> target) {
Button::Initialize(target);
Entry::Initialize(target);

Loop::Initialize(target);
//Loop::Initialize(target);

gtk_init(NULL, NULL);

GMainContext *gc = g_main_context_default();
struct econtext *ctx = &default_context;

ctx->gc = g_main_context_ref(gc);
ctx->nfd = 0;
ctx->afd = 0;
ctx->iow = 0;
ctx->pfd = 0;

ev_prepare_init (&ctx->pw, prepare_cb);
ev_set_priority (&ctx->pw, EV_MINPRI);
ev_prepare_start (EV_DEFAULT_UC_ &ctx->pw);
ev_unref(EV_DEFAULT_UC);

ev_check_init (&ctx->cw, check_cb);
ev_set_priority (&ctx->cw, EV_MAXPRI);
ev_check_start (EV_DEFAULT_ &ctx->cw);
ev_unref(EV_DEFAULT_UC);

ev_init (&ctx->tw, timer_cb);
ev_set_priority (&ctx->tw, EV_MINPRI);


printf("%s\n", "init done");
}

} // namespace ngtk
18 changes: 6 additions & 12 deletions src/ngtk_entry.cc
Expand Up @@ -39,11 +39,9 @@ Entry::Entry (void) {
Handle<Value> Entry::SetText (const Arguments &args) {
HandleScope scope;

if (args[0]->IsString()) {
GtkWidget *entry = Entry::Data(args.This());
GtkWidget *entry = Entry::Data(args.This());

gtk_entry_set_text(GTK_ENTRY(entry), *String::Utf8Value(args[0]->ToString()));
}
gtk_entry_set_text(GTK_ENTRY(entry), *String::Utf8Value(args[0]->ToString()));

return args.This();
}
Expand All @@ -61,11 +59,9 @@ Handle<Value> Entry::GetText (const Arguments &args) {
Handle<Value> Entry::AppendText (const Arguments &args) {
HandleScope scope;

if (args[0]->IsString()) {
GtkWidget *entry = Entry::Data(args.This());
GtkWidget *entry = Entry::Data(args.This());

gtk_entry_append_text(GTK_ENTRY(entry), *String::Utf8Value(args[0]->ToString()));
}
gtk_entry_append_text(GTK_ENTRY(entry), *String::Utf8Value(args[0]->ToString()));

return args.This();
}
Expand All @@ -74,11 +70,9 @@ Handle<Value> Entry::AppendText (const Arguments &args) {
Handle<Value> Entry::PrependText (const Arguments &args) {
HandleScope scope;

if (args[0]->IsString()) {
GtkWidget *entry = Entry::Data(args.This());
GtkWidget *entry = Entry::Data(args.This());

gtk_entry_prepend_text(GTK_ENTRY(entry), *String::Utf8Value(args[0]->ToString()));
}
gtk_entry_prepend_text(GTK_ENTRY(entry), *String::Utf8Value(args[0]->ToString()));

return args.This();
}
Expand Down
4 changes: 1 addition & 3 deletions src/ngtk_window.cc
Expand Up @@ -206,9 +206,7 @@ Handle<Value> Window::SetOpacity (const Arguments &args) {
if (args[0]->IsNumber()) {
GtkWidget *window = Window::Data(args.This());

gdouble opacity = args[0]->NumberValue();

gtk_window_set_opacity(GTK_WINDOW(window), opacity);
gtk_window_set_opacity(GTK_WINDOW(window), args[0]->NumberValue());
}

return args.This();
Expand Down
88 changes: 50 additions & 38 deletions test.js
@@ -1,40 +1,52 @@
console.log('test.js');
var gtk = require('./build/default/gtk');

var loop = new gtk.Loop();

var window = new gtk.Window();
var entry = new gtk.Entry();

entry.setText('A teeheehee!');
entry.setVisibility(false);

//var button = new gtk.Button();
//button.setLabel('Test Button');

//button.on('clicked', function () {
//console.log('clicked');
//var dialog = new gtk.MessageDialog(window, gtk.DIALOG_DESTROY_WITH_PARENT,
//gtk.MESSAGE_INFO);
//dialog.show();
//});

//window.add(button);
window.add(entry);

window.setTitle('Node');
window.setResizable(true);
window.setDefaultSize();

window.on('destroy', function () {
console.log(window.getOpacity());
console.log(window.getPosition());
console.log(window.getSize());
console.log(window.getResizable(true));
console.log(window.getTitle());
console.log('OMG');
loop.quit();
var fs = require('fs');

process.nextTick(function () {
//gtk.gtkInit();

var window = new gtk.Window();
var entry = new gtk.Entry();

entry.setText('A teeheehee!');
entry.setVisibility(true);

entry.on('changed', function () {
setTimeout(function () {
console.log(entry);
}, 2000);
console.log('changed');
});

var button = new gtk.Button();
button.setLabel('Test Button');

button.on('clicked', function () {
console.log('clicked');
var dialog = new gtk.MessageDialog(window, gtk.DIALOG_DESTROY_WITH_PARENT,
gtk.MESSAGE_INFO, gtk.BUTTONS_OK);
dialog.show();
});

//window.add(button);
window.add(entry);

window.setTitle('Node');
window.setResizable(true);
window.setDefaultSize();

window.on('destroy', function () {
console.log(window.getOpacity());
console.log(window.getPosition());
console.log(window.getSize());
console.log(window.getResizable(true));
console.log(window.getTitle());
console.log('OMG');
//loop.quit();
});

window.show();
});

window.show();

loop.run();
setInterval(function () {
console.log('hi');
}, 1000);

0 comments on commit a975088

Please sign in to comment.