1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
|
/* valagdbusmodule.vala
*
* Copyright (C) 2010 Jürg Billeter
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* Author:
* Jürg Billeter <j@bitron.ch>
*/
public class Vala.GDBusModule : GVariantModule {
public static string? get_dbus_name (TypeSymbol symbol) {
return symbol.get_attribute_string ("DBus", "name");
}
public static string get_dbus_name_for_member (Symbol symbol) {
var dbus_name = symbol.get_attribute_string ("DBus", "name");
if (dbus_name != null) {
return dbus_name;
}
return Symbol.lower_case_to_camel_case (symbol.name);
}
public static bool is_dbus_no_reply (Method m) {
return m.get_attribute_bool ("DBus", "no_reply");
}
public override void visit_error_domain (ErrorDomain edomain) {
var edomain_dbus_name = get_dbus_name (edomain);
if (edomain_dbus_name == null) {
base.visit_error_domain (edomain);
return;
}
generate_error_domain_declaration (edomain, cfile);
if (!edomain.is_internal_symbol ()) {
generate_error_domain_declaration (edomain, header_file);
}
if (!edomain.is_private_symbol ()) {
generate_error_domain_declaration (edomain, internal_header_file);
}
var error_entries = new CCodeInitializerList ();
foreach (ErrorCode ecode in edomain.get_codes ()) {
var ecode_dbus_name = get_dbus_name (ecode);
if (ecode_dbus_name == null) {
ecode_dbus_name = Symbol.lower_case_to_camel_case (ecode.name.down ());
}
var error_entry = new CCodeInitializerList ();
error_entry.append (new CCodeIdentifier (get_ccode_name (ecode)));
error_entry.append (new CCodeConstant ("\"%s.%s\"".printf (edomain_dbus_name, ecode_dbus_name)));
error_entries.append (error_entry);
}
var cdecl = new CCodeDeclaration ("const GDBusErrorEntry");
cdecl.add_declarator (new CCodeVariableDeclarator (get_ccode_lower_case_name (edomain) + "_entries[]", error_entries));
cdecl.modifiers = CCodeModifiers.STATIC;
cfile.add_constant_declaration (cdecl);
string quark_fun_name = get_ccode_lower_case_prefix (edomain) + "quark";
var cquark_fun = new CCodeFunction (quark_fun_name, get_ccode_name (gquark_type.data_type));
push_function (cquark_fun);
string quark_name = "%squark_volatile".printf (get_ccode_lower_case_prefix (edomain));
ccode.add_declaration ("gsize", new CCodeVariableDeclarator (quark_name, new CCodeConstant ("0")), CCodeModifiers.STATIC | CCodeModifiers.VOLATILE);
var register_call = new CCodeFunctionCall (new CCodeIdentifier ("g_dbus_error_register_error_domain"));
register_call.add_argument (new CCodeConstant ("\"" + get_ccode_lower_case_name (edomain) + "-quark\""));
register_call.add_argument (new CCodeUnaryExpression (CCodeUnaryOperator.ADDRESS_OF, new CCodeIdentifier (quark_name)));
register_call.add_argument (new CCodeIdentifier (get_ccode_lower_case_name (edomain) + "_entries"));
var nentries = new CCodeFunctionCall (new CCodeIdentifier ("G_N_ELEMENTS"));
nentries.add_argument (new CCodeIdentifier (get_ccode_lower_case_name (edomain) + "_entries"));
register_call.add_argument (nentries);
ccode.add_expression (register_call);
ccode.add_return (new CCodeCastExpression (new CCodeIdentifier (quark_name), "GQuark"));
pop_function ();
cfile.add_function (cquark_fun);
}
bool is_file_descriptor (DataType type) {
if (type is ObjectType) {
if (type.data_type.get_full_name () == "GLib.UnixInputStream" ||
type.data_type.get_full_name () == "GLib.UnixOutputStream" ||
type.data_type.get_full_name () == "GLib.Socket") {
return true;
}
}
return false;
}
public bool dbus_method_uses_file_descriptor (Method method) {
foreach (Parameter param in method.get_parameters ()) {
if (is_file_descriptor (param.variable_type)) {
return true;
}
}
if (is_file_descriptor (method.return_type)) {
return true;
}
return false;
}
CCodeExpression? get_file_descriptor (DataType type, CCodeExpression expr) {
if (type is ObjectType) {
if (type.data_type.get_full_name () == "GLib.UnixInputStream") {
var result = new CCodeFunctionCall (new CCodeIdentifier ("g_unix_input_stream_get_fd"));
result.add_argument (expr);
return result;
} else if (type.data_type.get_full_name () == "GLib.UnixOutputStream") {
var result = new CCodeFunctionCall (new CCodeIdentifier ("g_unix_output_stream_get_fd"));
result.add_argument (expr);
return result;
} else if (type.data_type.get_full_name () == "GLib.Socket") {
var result = new CCodeFunctionCall (new CCodeIdentifier ("g_socket_get_fd"));
result.add_argument (expr);
return result;
}
}
return null;
}
public void send_dbus_value (DataType type, CCodeExpression builder_expr, CCodeExpression expr, Symbol? sym) {
var fd = get_file_descriptor (type, expr);
if (fd != null) {
// add file descriptor to the file descriptor list
var fd_append = new CCodeFunctionCall (new CCodeIdentifier ("g_unix_fd_list_append"));
fd_append.add_argument (new CCodeIdentifier ("_fd_list"));
fd_append.add_argument (fd);
fd_append.add_argument (new CCodeConstant ("NULL"));
// add index to file descriptor to gvariant
var builder_add = new CCodeFunctionCall (new CCodeIdentifier ("g_variant_builder_add"));
builder_add.add_argument (new CCodeUnaryExpression (CCodeUnaryOperator.ADDRESS_OF, builder_expr));
builder_add.add_argument (new CCodeConstant ("\"h\""));
builder_add.add_argument (fd_append);
ccode.add_expression (builder_add);
} else {
write_expression (type, builder_expr, expr, sym);
}
}
CCodeExpression? create_from_file_descriptor (DataType type, CCodeExpression expr) {
if (type is ObjectType) {
if (type.data_type.get_full_name () == "GLib.UnixInputStream") {
var result = new CCodeFunctionCall (new CCodeIdentifier ("g_unix_input_stream_new"));
result.add_argument (expr);
result.add_argument (new CCodeConstant ("TRUE"));
return new CCodeCastExpression (result, "GUnixInputStream *");
} else if (type.data_type.get_full_name () == "GLib.UnixOutputStream") {
var result = new CCodeFunctionCall (new CCodeIdentifier ("g_unix_output_stream_new"));
result.add_argument (expr);
result.add_argument (new CCodeConstant ("TRUE"));
return new CCodeCastExpression (result, "GUnixOutputStream *");
} else if (type.data_type.get_full_name () == "GLib.Socket") {
var result = new CCodeFunctionCall (new CCodeIdentifier ("g_socket_new_from_fd"));
result.add_argument (expr);
result.add_argument (new CCodeConstant ("NULL"));
return result;
}
}
return null;
}
public void receive_dbus_value (DataType type, CCodeExpression message_expr, CCodeExpression iter_expr, CCodeExpression target_expr, Symbol? sym, CCodeExpression? error_expr = null, out bool may_fail = null) {
var fd_list = new CCodeFunctionCall (new CCodeIdentifier ("g_dbus_message_get_unix_fd_list"));
fd_list.add_argument (message_expr);
var fd = new CCodeFunctionCall (new CCodeIdentifier ("g_unix_fd_list_get"));
fd.add_argument (fd_list);
fd.add_argument (new CCodeIdentifier ("_fd_index"));
fd.add_argument (new CCodeConstant ("NULL"));
var stream = create_from_file_descriptor (type, fd);
if (stream != null) {
var get_fd = new CCodeFunctionCall (new CCodeIdentifier ("g_variant_iter_next"));
get_fd.add_argument (new CCodeUnaryExpression (CCodeUnaryOperator.ADDRESS_OF, iter_expr));
get_fd.add_argument (new CCodeConstant ("\"h\""));
get_fd.add_argument (new CCodeUnaryExpression (CCodeUnaryOperator.ADDRESS_OF, new CCodeIdentifier ("_fd_index")));
ccode.add_expression (get_fd);
ccode.add_assignment (target_expr, stream);
may_fail = false;
} else {
read_expression (type, iter_expr, target_expr, sym, error_expr, out may_fail);
}
}
}
|