]> git.itanic.dy.fi Git - maemo-mapper/blob - src/dbus-ifc.c
Administrative changes in preparation for release of Maemo Mapper v2.6.2.
[maemo-mapper] / src / dbus-ifc.c
1 /*
2  * Copyright (C) 2006, 2007 John Costigan.
3  *
4  * POI and GPS-Info code originally written by Cezary Jackiewicz.
5  *
6  * Default map data provided by http://www.openstreetmap.org/
7  *
8  * This file is part of Maemo Mapper.
9  *
10  * Maemo Mapper is free software: you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation, either version 3 of the License, or
13  * (at your option) any later version.
14  *
15  * Maemo Mapper is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Maemo Mapper.  If not, see <http://www.gnu.org/licenses/>.
22  */
23
24 #define _GNU_SOURCE
25
26 #include <math.h>
27 #include <stdio.h>
28 #include <string.h>
29 #include <libosso.h>
30 #include <glib.h>
31 #include "dbus/dbus.h"
32
33 #define DBUS_API_SUBJECT_TO_CHANGE
34
35 #include "types.h"
36 #include "data.h"
37 #include "defines.h"
38
39 #include "display.h"
40 #include "dbus-ifc.h"
41
42 static DBusConnection *_bus = NULL;
43
44 /***********************
45  * BELOW: DBUS METHODS *
46  ***********************/
47
48 static gint
49 dbus_ifc_cb_default(const gchar *interface, const gchar *method,
50         GArray *args, gpointer data, osso_rpc_t *retval)
51 {
52     printf("%s()\n", __PRETTY_FUNCTION__);
53
54     if(!strcmp(method, "top_application"))
55     {
56         g_idle_add((GSourceFunc)window_present, NULL);
57     }
58
59     retval->type = DBUS_TYPE_INVALID;
60
61     vprintf("%s(): return\n", __PRETTY_FUNCTION__);
62     return OSSO_OK;
63 }
64
65 static gboolean
66 dbus_ifc_set_view_position_idle(SetViewPositionArgs *args)
67 {
68     Point center;
69     printf("%s(%f, %f, %d, %f)\n", __PRETTY_FUNCTION__, args->new_lat,
70             args->new_lon, args->new_zoom, args->new_viewing_angle);
71
72     if(!_mouse_is_down)
73     {
74         if(_center_mode > 0)
75             gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(
76                         _menu_view_ac_none_item), TRUE);
77
78         latlon2unit(args->new_lat, args->new_lon, center.unitx, center.unity);
79
80         map_center_unit_full(center, args->new_zoom, args->new_viewing_angle);
81     }
82
83     g_free(args);
84
85     vprintf("%s(): return FALSE\n", __PRETTY_FUNCTION__);
86     return FALSE;
87 }
88
89 static gint
90 dbus_ifc_handle_set_view_center(GArray *args, osso_rpc_t *retval)
91 {
92     printf("%s()\n", __PRETTY_FUNCTION__);
93     SetViewPositionArgs *svca = g_new(SetViewPositionArgs, 1);
94
95     /* Argument 3: int: zoom.  Get this first because we might need it to
96      * calculate the next latitude and/or longitude. */
97     if(args->len >= 3
98             && g_array_index(args, osso_rpc_t, 2).type == DBUS_TYPE_INT32)
99     {
100         svca->new_zoom = CLAMP(g_array_index(args, osso_rpc_t, 2).value.i,
101                 MIN_ZOOM, MAX_ZOOM);
102     }
103     else
104         svca->new_zoom = _next_zoom;
105
106     if(args->len < 2)
107     {
108         /* Latitude and/or Longitude are not defined.  Calculate next. */
109         Point new_center = map_calc_new_center(svca->new_zoom);
110         unit2latlon(new_center.unitx, new_center.unity,
111                 svca->new_lat, svca->new_lon);
112     }
113
114     /* Argument 1: double: latitude. */
115     if(args->len >= 1
116         && g_array_index(args, osso_rpc_t, 0).type == DBUS_TYPE_DOUBLE)
117     {
118         svca->new_lat = CLAMP(g_array_index(args, osso_rpc_t, 0).value.d,
119                 -90.0, 90.0);
120     }
121
122     /* Argument 2: double: longitude. */
123     if(args->len >= 2
124             && g_array_index(args, osso_rpc_t, 1).type == DBUS_TYPE_DOUBLE)
125     {
126         svca->new_lon = CLAMP(g_array_index(args, osso_rpc_t, 1).value.d,
127                 -180.0, 180.0);
128     }
129
130     /* Argument 4: double: viewing angle. */
131     if(args->len >= 4
132             && g_array_index(args, osso_rpc_t, 3).type == DBUS_TYPE_DOUBLE)
133     {
134         svca->new_viewing_angle
135             = (gint)(g_array_index(args, osso_rpc_t, 2).value.d + 0.5) % 360;
136     }
137     else
138         svca->new_viewing_angle = _center_mode > 0 && _center_rotate
139             ? _gps.heading : _next_map_rotate_angle;
140
141     g_idle_add((GSourceFunc)dbus_ifc_set_view_position_idle, svca);
142     retval->type = DBUS_TYPE_INVALID;
143
144     vprintf("%s(): return OSSO_OK\n", __PRETTY_FUNCTION__);
145     return OSSO_OK;
146 }
147
148 static gint
149 dbus_ifc_controller(const gchar *interface, const gchar *method,
150         GArray *args, gpointer data, osso_rpc_t *retval)
151 {
152     printf("%s(%s)\n", __PRETTY_FUNCTION__, method);
153
154     /* Method: set_view_center */
155     if(!strcmp(method, MM_DBUS_METHOD_SET_VIEW_POSITION))
156         return dbus_ifc_handle_set_view_center(args, retval);
157
158     vprintf("%s(): return OSSO_ERROR\n", __PRETTY_FUNCTION__);
159     return OSSO_ERROR;
160 }
161
162 /***********************
163  * ABOVE: DBUS METHODS *
164  ***********************/
165
166
167 /***********************
168  * BELOW: DBUS SIGNALS *
169  ***********************/
170
171 void
172 dbus_ifc_fire_view_position_changed(
173         Point new_center, gint new_zoom, gdouble new_viewing_angle)
174 {
175     DBusMessage *message = NULL;
176     gdouble new_lat, new_lon;
177     printf("%s(%d, %d, %d, %f)\n", __PRETTY_FUNCTION__, new_center.unitx,
178             new_center.unity, new_zoom, new_viewing_angle);
179
180     unit2latlon(new_center.unitx, new_center.unity, new_lat, new_lon);
181
182     if(NULL == (message = dbus_message_new_signal(MM_DBUS_PATH,
183                     MM_DBUS_INTERFACE, MM_DBUS_SIGNAL_VIEW_POSITION_CHANGED))
184             || !dbus_message_append_args(message,
185                 DBUS_TYPE_DOUBLE, &new_lat,
186                 DBUS_TYPE_DOUBLE, &new_lon,
187                 DBUS_TYPE_INT32, &new_zoom,
188                 DBUS_TYPE_DOUBLE, &new_viewing_angle,
189                 DBUS_TYPE_INVALID)
190             || !dbus_connection_send(_bus, message, NULL))
191     {
192         g_printerr("Error sending view_position_changed signal.\n");
193     }
194
195     if(message)
196         dbus_message_unref(message);
197
198     vprintf("%s(): return\n", __PRETTY_FUNCTION__);
199 }
200
201 void
202 dbus_ifc_fire_view_dimensions_changed(
203         gint new_view_width_pixels, gint new_view_height_pixels)
204 {
205     DBusMessage *message = NULL;
206     printf("%s(%d, %d)\n", __PRETTY_FUNCTION__,
207             new_view_width_pixels, new_view_height_pixels);
208
209     if(NULL == (message = dbus_message_new_signal(MM_DBUS_PATH,
210                     MM_DBUS_INTERFACE, MM_DBUS_SIGNAL_VIEW_DIMENSIONS_CHANGED))
211             || !dbus_message_append_args(message,
212                 DBUS_TYPE_INT32, &new_view_width_pixels,
213                 DBUS_TYPE_INT32, &new_view_height_pixels,
214                 DBUS_TYPE_INVALID)
215             || !dbus_connection_send(_bus, message, NULL))
216     {
217         g_printerr("Error sending view_dimensions_changed signal.\n");
218     }
219
220     if(message)
221         dbus_message_unref(message);
222
223     vprintf("%s(): return\n", __PRETTY_FUNCTION__);
224 }
225
226 /***********************
227  * ABOVE: DBUS SIGNALS *
228  ***********************/
229
230 void
231 dbus_ifc_init()
232 {
233     DBusError error;
234     printf("%s()\n", __PRETTY_FUNCTION__);
235
236     if(OSSO_OK != osso_rpc_set_default_cb_f(_osso, dbus_ifc_cb_default, NULL))
237         g_printerr("osso_rpc_set_default_cb_f failed.\n");
238
239     if(OSSO_OK != osso_rpc_set_cb_f(_osso, MM_DBUS_SERVICE,
240                 MM_DBUS_PATH, MM_DBUS_INTERFACE, dbus_ifc_controller, NULL))
241         g_printerr("osso_rpc_set_cb_f failed.\n");
242
243     dbus_error_init(&error);
244     if(NULL == (_bus = dbus_bus_get(DBUS_BUS_SESSION, &error)))
245     {
246         g_printerr("Error getting session bus: %s: %s\n",
247                 (dbus_error_is_set(&error)
248                  ? error.name : "<no error message>"),
249                 (dbus_error_is_set(&error)
250                  ? error.message : ""));
251     }
252
253     vprintf("%s(): return\n", __PRETTY_FUNCTION__);
254 }