37
37
* ***** END LICENSE BLOCK ***** */
39
const { utils: Cu } = Components;
39
41
var EXPORTED_SYMBOLS = [ "gio" ];
41
43
const GIO_LIBNAME = "gio-2.0";
42
44
const GIO_ABIS = [ 0 ];
44
const Cu = Components.utils;
46
46
Cu.import("resource://gre/modules/ctypes.jsm");
47
Cu.import("resource://ubufox/libs/ctypes-utils.jsm");
47
Cu.import("resource://ubufox/modules/utils.jsm");
48
48
Cu.import("resource://ubufox/libs/gobject.jsm");
49
49
Cu.import("resource://ubufox/libs/glib.jsm");
51
["LOG", "WARN", "ERROR"].forEach(function(aName) {
52
this.__defineGetter__(aName, function() {
53
Components.utils.import("resource://gre/modules/AddonLogging.jsm");
55
LogManager.getLogger("ubufox.gio", this);
60
var asyncCbHandlers = {};
61
var asyncCbIdSerial = 0;
63
function AsyncCbData(cb, ccb) {
51
function getString(aWrappee) {
52
let res = aWrappee.apply(null, Array.prototype.slice.call(arguments, 1));
53
return res.isNull() ? null : res.readString();
68
function newAsyncCbId() {
69
while (asyncCbHandlers[++asyncCbIdSerial]) {}
70
return asyncCbIdSerial;
56
function getOwnedString(aWrappee) {
59
res = aWrappee.apply(null, Array.prototype.slice.call(arguments, 1));
60
return res.isNull() ? null : res.readString();
61
} finally { glib.g_free(res); }
73
64
function gio_defines(lib) {
76
this.GBusType = ctypes.int;
77
this.G_BUS_TYPE_STARTER = -1;
78
this.G_BUS_TYPE_NONE = 0;
79
this.G_BUS_TYPE_SYSTEM = 1;
80
this.G_BUS_TYPE_SESSION = 2;
83
this.GDBusProxyFlags = ctypes.int;
84
this.G_DBUS_PROXY_FLAGS_NONE = 0;
85
this.G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES = (1<<0);
86
this.G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS = (1<<1);
87
this.G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START = (1<<2);
90
this.GDBusCallFlags = ctypes.int;
91
this.G_DBUS_CALL_FLAGS_NONE = 0;
92
this.G_DBUS_CALL_FLAGS_NO_AUTO_START = (1<<0);
95
this.GFileMonitorFlags = ctypes.int;
96
this.G_FILE_MONITOR_NONE = 0;
97
this.G_FILE_MONITOR_WATCH_MOUNTS = (1<<0);
98
this.G_FILE_MONITOR_SEND_MOVED = (1<<1);
101
this.GFileMonitorEvent = ctypes.int;
102
this.G_FILE_MONITOR_EVENT_CHANGED = 0;
103
this.G_FILE_MONITOR_EVENT_CHANGES_DONE_HINT = 1;
104
this.G_FILE_MONITOR_EVENT_DELETED = 2;
105
this.G_FILE_MONITOR_EVENT_CREATED = 3;
106
this.G_FILE_MONITOR_EVENT_ATTRIBUTE_CHANGED = 4;
107
this.G_FILE_MONITOR_EVENT_PRE_UNMOUNT = 5;
108
this.G_FILE_MONITOR_EVENT_UNMOUNTED = 6;
109
this.G_FILE_MONITOR_EVENT_MOVED = 7;
66
CTypesUtils.defineEnums(this, "GBusType", -1, [
73
CTypesUtils.defineFlags(this, "GDBusProxyFlags", 0, [
74
"G_DBUS_PROXY_FLAGS_NONE",
75
"G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES",
76
"G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS",
77
"G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START"
80
CTypesUtils.defineFlags(this, "GDBusCallFlags", 0, [
81
"G_DBUS_CALL_FLAGS_NONE",
82
"G_DBUS_CALL_FLAGS_NO_AUTO_START"
85
CTypesUtils.defineFlags(this, "GFileMonitorFlags", 0, [
86
"G_FILE_MONITOR_NONE",
87
"G_FILE_MONITOR_WATCH_MOUNTS",
88
"G_FILE_MONITOR_SEND_MOVED"
91
CTypesUtils.defineEnums(this, "GFileMonitorEvent", 0, [
92
"G_FILE_MONITOR_EVENT_CHANGED",
93
"G_FILE_MONITOR_EVENT_CHANGES_DONE_HINT",
94
"G_FILE_MONITOR_EVENT_DELETED",
95
"G_FILE_MONITOR_EVENT_CREATED",
96
"G_FILE_MONITOR_EVENT_ATTRIBUTE_CHANGED",
97
"G_FILE_MONITOR_EVENT_PRE_UNMOUNT",
98
"G_FILE_MONITOR_EVENT_UNMOUNTED",
99
"G_FILE_MONITOR_EVENT_MOVED"
112
this.G_IO_ERROR_DBUS_ERROR = 36;
103
CTypesUtils.defineSimple(this, "G_IO_ERROR_DBUS_ERROR", 36);
115
this.GDBusInterfaceInfo = ctypes.StructType("GDBusInterfaceInfo");
116
this.GCancellable = ctypes.StructType("GCancellable");
117
this.GAsyncResult = ctypes.StructType("GAsyncResult");
118
this.GDBusProxy = ctypes.StructType("GDBusProxy");
119
this.GFile = ctypes.StructType("GFile");
120
this.GFileMonitor = ctypes.StructType("GFileMonitor");
106
CTypesUtils.defineSimple(this, "GDBusInterfaceInfo",
107
ctypes.StructType("GDBusInterfaceInfo"));
108
CTypesUtils.defineSimple(this, "GCancellable",
109
ctypes.StructType("GCancellable"));
110
CTypesUtils.defineSimple(this, "GAsyncResult",
111
ctypes.StructType("GAsyncResult"));
112
CTypesUtils.defineSimple(this, "GDBusProxy",
113
ctypes.StructType("GDBusProxy"));
114
CTypesUtils.defineSimple(this, "GFile",
115
ctypes.StructType("GFile"));
116
CTypesUtils.defineSimple(this, "GFileMonitor",
117
ctypes.StructType("GFileMonitor"));
123
this.GAsyncReadyCallback = ctypes.FunctionType(ctypes.default_abi,
125
[gobject.GObject.ptr,
126
this.GAsyncResult.ptr,
120
CTypesUtils.defineSimple(this, "GAsyncReadyCallback",
121
ctypes.FunctionType(ctypes.default_abi,
123
[gobject.GObject.ptr,
124
this.GAsyncResult.ptr,
125
glib.gpointer]).ptr);
130
lib.lazy_bind("g_dbus_proxy_new_for_bus", ctypes.void_t, this.GBusType,
131
this.GDBusProxyFlags, this.GDBusInterfaceInfo.ptr,
132
glib.gchar.ptr, glib.gchar.ptr, glib.gchar.ptr,
133
this.GCancellable.ptr, this.GAsyncReadyCallback, glib.gpointer);
128
lib.lazy_bind_with_wrapper("g_dbus_proxy_new_for_bus", function(aWrappee,
136
var ccw = CTypesUtils.wrapCallback(aCallback,
137
{type: gio.GAsyncReadyCallback,
138
root: true, singleshot: true});
141
aWrappee(aBusType, aFlags, aInterfaceInfo, aName, aPath, aInterface,
142
aCancellable, ccw, null);
144
CTypesUtils.unrootCallback(aCallback);
147
}, ctypes.void_t, [this.GBusType, this.GDBusProxyFlags,
148
this.GDBusInterfaceInfo.ptr, glib.gchar.ptr,
149
glib.gchar.ptr, glib.gchar.ptr, this.GCancellable.ptr,
150
this.GAsyncReadyCallback, glib.gpointer]);
134
151
lib.lazy_bind("g_dbus_proxy_new_for_bus_finish", this.GDBusProxy.ptr,
135
this.GAsyncResult.ptr, glib.GError.ptr.ptr);
136
lib.lazy_bind("g_dbus_proxy_call", ctypes.void_t, this.GDBusProxy.ptr,
137
glib.gchar.ptr, glib.GVariant.ptr, this.GDBusCallFlags,
138
glib.gint, this.GCancellable.ptr, this.GAsyncReadyCallback,
152
[this.GAsyncResult.ptr, glib.GError.ptr.ptr]);
153
lib.lazy_bind_with_wrapper("g_dbus_proxy_call", function(aWrappee, aProxy,
158
var ccw = CTypesUtils.wrapCallback(aCallback,
159
{type: gio.GAsyncReadyCallback,
160
root: true, singleshot: true});
163
aWrappee(aProxy, aMethod, aParams, aFlags, aTimeout, aCancellable,
166
CTypesUtils.unrootCallback(aCallback);
169
}, ctypes.void_t, [this.GDBusProxy.ptr, glib.gchar.ptr, glib.GVariant.ptr,
170
this.GDBusCallFlags, glib.gint, this.GCancellable.ptr,
171
this.GAsyncReadyCallback, glib.gpointer]);
140
172
lib.lazy_bind("g_dbus_proxy_call_finish", glib.GVariant.ptr,
141
this.GDBusProxy.ptr, this.GAsyncResult.ptr, glib.GError.ptr.ptr);
142
lib.lazy_bind("g_dbus_proxy_get_name", glib.gchar.ptr, this.GDBusProxy.ptr);
143
lib.lazy_bind("g_dbus_proxy_get_interface_name", glib.gchar.ptr,
144
this.GDBusProxy.ptr);
145
lib.lazy_bind("g_dbus_proxy_get_object_path", glib.gchar.ptr, this.GDBusProxy.ptr);
173
[this.GDBusProxy.ptr, this.GAsyncResult.ptr,
174
glib.GError.ptr.ptr]);
175
lib.lazy_bind_with_wrapper("g_dbus_proxy_get_name", getString,
176
glib.gchar.ptr, [this.GDBusProxy.ptr]);
177
lib.lazy_bind_with_wrapper("g_dbus_proxy_get_interface_name", getString,
178
glib.gchar.ptr, [this.GDBusProxy.ptr]);
179
lib.lazy_bind_with_wrapper("g_dbus_proxy_get_object_path", getString,
180
glib.gchar.ptr, [this.GDBusProxy.ptr]);
146
181
lib.lazy_bind("g_io_error_quark", glib.GQuark);
147
lib.lazy_bind("g_dbus_error_get_remote_error", glib.gchar.ptr, glib.GError.ptr);
148
lib.lazy_bind("g_dbus_proxy_get_name_owner", glib.gchar.ptr, this.GDBusProxy.ptr);
149
lib.lazy_bind("g_file_new_for_path", this.GFile.ptr, ctypes.char.ptr);
150
lib.lazy_bind("g_file_monitor_file", this.GFileMonitor.ptr, this.GFile.ptr,
151
this.GFileMonitorFlags, this.GCancellable.ptr, glib.GError.ptr);
152
lib.lazy_bind("g_file_monitor_directory", this.GFileMonitor.ptr, this.GFile.ptr,
153
this.GFileMonitorFlags, this.GCancellable.ptr, glib.GError.ptr);
154
lib.lazy_bind("g_file_get_path", ctypes.char.ptr, this.GFile.ptr);
156
// Helpers to work around jsctypes limitations
157
this.GDbusProxyNewForBus = function(aType, aFlags, aInfo, aName,
158
aPath, aInterface, aCancellable,
160
let cb = function(aObject, aResult, aData) {
162
aCallback(aObject, aResult);
166
delete asyncCbHandlers[id];
168
let ccb = gio.GAsyncReadyCallback(cb);
170
var id = newAsyncCbId();
171
asyncCbHandlers[id] = new AsyncCbData(cb, ccb);
173
gio.g_dbus_proxy_new_for_bus(aType, aFlags, aInfo, aName, aPath,
174
aInterface, aCancellable, ccb, null);
177
this.GDbusProxyCall = function(aProxy, aMethod, aParams, aFlags, aTimeout,
178
aCancellable, aCallback) {
179
let cb = function(aObject, aResult, aData) {
181
aCallback(aObject, aResult);
185
delete asyncCbHandlers[id];
187
let ccb = gio.GAsyncReadyCallback(cb);
189
var id = newAsyncCbId();
190
asyncCbHandlers[id] = new AsyncCbData(cb, ccb);
192
gio.g_dbus_proxy_call(aProxy, aMethod, aParams, aFlags, aTimeout,
193
aCancellable, ccb, null);
196
this.G_IO_ERROR = this.g_io_error_quark();
182
lib.lazy_bind_with_wrapper("g_dbus_error_get_remote_error", getOwnedString,
183
glib.gchar.ptr, [glib.GError.ptr]);
184
lib.lazy_bind_with_wrapper("g_dbus_proxy_get_name_owner", getOwnedString,
185
glib.gchar.ptr, [this.GDBusProxy.ptr]);
186
lib.lazy_bind("g_file_new_for_path", this.GFile.ptr, [glib.gchar.ptr]);
187
lib.lazy_bind("g_file_monitor_file", this.GFileMonitor.ptr, [this.GFile.ptr,
188
this.GFileMonitorFlags, this.GCancellable.ptr,
190
lib.lazy_bind("g_file_monitor_directory", this.GFileMonitor.ptr,
191
[this.GFile.ptr, this.GFileMonitorFlags,
192
this.GCancellable.ptr, glib.GError.ptr]);
193
lib.lazy_bind_with_wrapper("g_file_get_path", getOwnedString,
194
glib.gchar.ptr, [this.GFile.ptr]);
196
CTypesUtils.defineSimple(this, "G_IO_ERROR", this.g_io_error_quark());
198
gobject.createSignal(this.GDBusProxy, "g-signal", ctypes.void_t,
199
[this.GDBusProxy.ptr, glib.gchar.ptr,
200
glib.gchar.ptr, glib.GVariant.ptr,
202
gobject.createSignal(this.GFileMonitor, "changed", ctypes.void_t,
203
[this.GFileMonitor.ptr, this.GFile.ptr,
204
this.GFile.ptr, this.GFileMonitorEvent,
199
new ctypes_library(GIO_LIBNAME, GIO_ABIS, gio_defines, this);
208
var gio = new CTypesUtils.newLibrary(GIO_LIBNAME, GIO_ABIS, gio_defines);