forked from clayrisser/gtk-node
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
136 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/* | ||
* Copyright (c) 2015-2016 Jasper St. Pierre, Andrea Giammarchi & Contributors | ||
* Permission is hereby granted, free of charge, to any person obtaining a | ||
* copy of this software and associated documentation files (the "Software"), | ||
* to deal in the Software without restriction, including without limitation | ||
* the rights to use, copy, modify, merge, publish, distribute, sublicense, | ||
* and/or sell copies of the Software, and to permit persons to whom the | ||
* Software is furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice (including the next | ||
* paragraph) shall be included in all copies or substantial portions of the | ||
* Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | ||
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | ||
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER | ||
* DEALINGS IN THE SOFTWARE. | ||
*/ | ||
#include <glib.h> | ||
#include <uv.h> | ||
#include <stdbool.h> | ||
#include <stdio.h> | ||
#include "index.h" | ||
|
||
struct uv_loop_source { | ||
GSource source; | ||
uv_loop_t *loop; | ||
}; | ||
|
||
static gboolean uv_loop_source_prepare (GSource *base, int *timeout) { | ||
struct uv_loop_source *source = (struct uv_loop_source *) base; | ||
uv_update_time (source->loop); | ||
|
||
bool loop_alive = uv_loop_alive (source->loop); | ||
|
||
/* If the loop is dead, we can simply sleep forever until a GTK+ source | ||
* (presumably) wakes us back up again. */ | ||
if (!loop_alive) { | ||
return FALSE; | ||
} | ||
|
||
/* Otherwise, check the timeout. If the timeout is 0, that means we're | ||
* ready to go. Otherwise, keep sleeping until the timeout happens again. */ | ||
int t = uv_backend_timeout (source->loop); | ||
*timeout = t; | ||
|
||
if (t == 0) | ||
return TRUE; | ||
else | ||
return FALSE; | ||
} | ||
|
||
static gboolean uv_loop_source_dispatch (GSource *base, GSourceFunc callback, gpointer user_data) { | ||
struct uv_loop_source *source = (struct uv_loop_source *) base; | ||
uv_run (source->loop, UV_RUN_NOWAIT); | ||
return G_SOURCE_CONTINUE; | ||
} | ||
|
||
static GSourceFuncs uv_loop_source_funcs = { | ||
uv_loop_source_prepare, | ||
NULL, | ||
uv_loop_source_dispatch, | ||
NULL, | ||
NULL, | ||
NULL, | ||
}; | ||
|
||
static GSource *uv_loop_source_new (uv_loop_t *loop) { | ||
struct uv_loop_source *source = (struct uv_loop_source *) g_source_new (&uv_loop_source_funcs, sizeof (*source)); | ||
source->loop = loop; | ||
g_source_add_unix_fd (&source->source, | ||
uv_backend_fd (loop), | ||
(GIOCondition) (G_IO_IN | G_IO_OUT | G_IO_ERR)); | ||
return &source->source; | ||
} | ||
|
||
void startLoop() { | ||
GSource *source = uv_loop_source_new (uv_default_loop ()); | ||
g_source_attach (source, NULL); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
#ifndef GTK_LOOP_HEADER | ||
#define GTK_LOOP_HEADER | ||
|
||
void startLoop(); | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import ffi from 'ffi'; | ||
import path from 'path'; | ||
import ref from 'ref'; | ||
|
||
/** | ||
* GTK runs applications in a GTK event loop. | ||
* This is problematic in a NodeJS environment as | ||
* a GTK application will block the calling | ||
* NodeJS function. | ||
* To solve this, we can nest the NodeJS LibUV event | ||
* loop in the GTK event loop so that NodeJS events will | ||
* continue to tick along concurrently with GTK events. | ||
* @credit To WebReflection's prior work on NodeGTK | ||
* bindings that solved this issue. | ||
* @credit https://github.com/WebReflection/node-gtk/blob/master/src/loop.cc | ||
*/ | ||
const loop = ffi.Library(path.resolve(__dirname, 'index'), { | ||
startLoop: ['void', []], | ||
}); | ||
|
||
export default loop; |