/* example-frontend.c - * * Copyright 2007-2008 OpenMoko, Inc. * Authored by Chia-I Wu * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program 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 General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ #include #include #include "frontend-marshal.h" #include "control-object-bindings.h" #include "control-viewport-bindings.h" #include "control-world-bindings.h" #include "diversity-control-bindings.h" #define DIVERSITY_DBUS_BUS DBUS_BUS_SESSION #define DIVERSITY_DBUS_SERVICE "org.openmoko.diversity" #define DIVERSITY_DBUS_PATH "/org/openmoko/diversity" #define DIVERSITY_DBUS_INTERFACE "org.openmoko.diversity" #define M_RADIUS (6371.0 * 1000.0) #define M_PI (3.14159) #define TO_RADICAL(l, r) ((l) / (r)) #define TO_DEGREES(l) (180.0 * TO_RADICAL((l), M_RADIUS) / M_PI) static GMainLoop *mainloop; static struct { DBusGConnection *connection; DBusGProxy *control; DBusGProxy *world; DBusGProxy *self; DBusGProxy *view; DBusGProxy *view_geom; } frontend; static void on_self_geometry_changed(DBusGProxy *proxy, gdouble lon, gdouble lat, gdouble width, gdouble height, void *data) { GError *error = NULL; gdouble l; l = TO_DEGREES(5000); g_print("%f %f %f %f\n", lon, lat, width, height); lon -= l / 2; lat -= l / 2; if (!org_openmoko_Diversity_Object_geometry_set(frontend.view_geom, lon, lat, l, l, &error)) { g_print("failed to move viewport: %s\n", error->message); return; } } static void on_viewport_object_added(void *ignore, const gchar *path, void *data) { g_print("ADDED %s\n", path); } static void on_viewport_object_removed(void *ignore, const gchar *path, void *data) { g_print("REMOVED %s\n", path); } static DBusGProxy *proxy_new_for_control(void) { DBusGProxy *proxy; gint major, minor, micro; GError *error = NULL; proxy = dbus_g_proxy_new_for_name(frontend.connection, DIVERSITY_DBUS_SERVICE, DIVERSITY_DBUS_PATH, DIVERSITY_DBUS_INTERFACE); if (!proxy) return NULL; g_print("Diversity version "); if (org_openmoko_Diversity_version(proxy, &major, &minor, µ, &error)) g_print("%d.%d.%d\n", major, minor, micro); else g_print("unknown: %s\n", error->message); return proxy; } static DBusGProxy *proxy_new_for_world(void) { return dbus_g_proxy_new_for_name(frontend.connection, DIVERSITY_DBUS_SERVICE, DIVERSITY_DBUS_PATH"/world", DIVERSITY_DBUS_INTERFACE".world"); } static DBusGProxy *proxy_new_for_self(void) { DBusGProxy *proxy; gchar *path; GError *error = NULL; if (!org_openmoko_Diversity_World_get_self(frontend.world, &path, &error)) { g_print("failed to get self: %s\n", error->message); g_error_free(error); return NULL; } proxy = dbus_g_proxy_new_for_name(frontend.connection, DIVERSITY_DBUS_SERVICE, path, DIVERSITY_DBUS_INTERFACE".object"); g_free(path); dbus_g_object_register_marshaller( _frontend_marshal_VOID__DOUBLE_DOUBLE_DOUBLE_DOUBLE, G_TYPE_NONE, G_TYPE_DOUBLE, G_TYPE_DOUBLE, G_TYPE_DOUBLE, G_TYPE_DOUBLE, G_TYPE_INVALID); dbus_g_proxy_add_signal(proxy, "GeometryChanged", G_TYPE_DOUBLE, G_TYPE_DOUBLE, G_TYPE_DOUBLE, G_TYPE_DOUBLE, G_TYPE_INVALID); dbus_g_proxy_connect_signal(proxy, "GeometryChanged", G_CALLBACK(on_self_geometry_changed), NULL, NULL); return proxy; } static DBusGProxy *proxy_new_for_view(void) { DBusGProxy *proxy; gchar *path; GError *error = NULL; if (!org_openmoko_Diversity_World_viewport_add(frontend.world, 0.0, 0.0, 0.0, 0.0, &path, &error)) { g_print("failed to get viewport: %s\n", error->message); g_error_free(error); return NULL; } g_print("path=%s; interface=%s\n", path, DIVERSITY_DBUS_INTERFACE".viewport"); proxy = dbus_g_proxy_new_for_name(frontend.connection, DIVERSITY_DBUS_SERVICE, path, DIVERSITY_DBUS_INTERFACE".viewport"); g_free(path); dbus_g_object_register_marshaller( g_cclosure_marshal_VOID__STRING, G_TYPE_NONE, DBUS_TYPE_G_OBJECT_PATH, G_TYPE_INVALID); dbus_g_proxy_add_signal(proxy, "ObjectAdded", DBUS_TYPE_G_OBJECT_PATH, G_TYPE_INVALID); dbus_g_proxy_connect_signal(proxy, "ObjectAdded", G_CALLBACK(on_viewport_object_added), NULL, NULL); dbus_g_proxy_add_signal(proxy, "ObjectRemoved", DBUS_TYPE_G_OBJECT_PATH, G_TYPE_INVALID); dbus_g_proxy_connect_signal(proxy, "ObjectRemoved", G_CALLBACK(on_viewport_object_removed), NULL, NULL); return proxy; } static void bail(void) { if (frontend.view) g_object_unref(frontend.view); if (frontend.self) g_object_unref(frontend.self); if (frontend.world) g_object_unref(frontend.world); if (frontend.control) g_object_unref(frontend.control); if (frontend.connection) dbus_g_connection_unref(frontend.connection); } int main(int argc, char **argv) { GError *error = NULL; g_type_init(); if (!g_thread_supported()) { g_thread_init(NULL); dbus_g_thread_init(); } frontend.connection = dbus_g_bus_get(DIVERSITY_DBUS_BUS, &error); if (!frontend.connection) { g_print("failed to get bus: %s\n", error->message); g_error_free(error); return 1; } frontend.control = proxy_new_for_control(); if (!frontend.control) { g_print("failed to create control proxy\n"); bail(); return 1; } frontend.world = proxy_new_for_world(); if (!frontend.world) { g_print("failed to create world proxy\n"); bail(); return 1; } frontend.self = proxy_new_for_self(); if (!frontend.self) { g_print("failed to create self proxy\n"); bail(); return 1; } frontend.view = proxy_new_for_view(); if (!frontend.view) { g_print("failed to create view proxy\n"); bail(); return 1; } frontend.view_geom = dbus_g_proxy_new_from_proxy(frontend.view, DIVERSITY_DBUS_INTERFACE".object", NULL); { gint i, j, steps; gdouble lon, lat, w, h; lon = 121.45; lat = 24.95; steps = 50; w = 0.2 / steps; h = 0.2 / steps; for (i = 0; i < steps; i++) { for (j = 0; j < steps; j++) { if (!org_openmoko_Diversity_World_tag_add( frontend.world, lon + w * i, lat + h * j, NULL, NULL, &error)) { g_print("failed to add tag(%d, %d): %s\n", i, j, error->message); g_error_free(error); error = NULL; } } } } mainloop = g_main_loop_new(NULL, FALSE); g_main_loop_run(mainloop); g_main_loop_unref(mainloop); bail(); return 0; }