/* test-tree.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 #include static GMainLoop *mainloop; enum { OBJECT_ADD, OBJECT_RESIZE, OBJECT_REMOVE, VIEWPORT_ADD, VIEWPORT_RESIZE, VIEWPORT_REMOVE, VIEWPORT_SET_FILTER, N_ACTIONS }; static const char *object_describe(DiversityObject *obj) { static char desc[256]; gdouble x, y, w, h; diversity_object_geometry_get(obj, &x, &y, &w, &h); g_snprintf(desc, sizeof(desc), "%s %p (%.2f, %.2f, %.2f, %.2f)", (G_TYPE_FROM_INSTANCE(obj) == DIVERSITY_TYPE_OBJECT) ? "obj" : "view", obj, x, y, w, h); return desc; } static gboolean mangle(gpointer data) { static DiversityObject *obj = NULL; static DiversityViewport *view = NULL; static gint num_objs, num_views; WorldTree *tree; gint act; gdouble lat1, lon1, lat2, lon2; tree = data; act = g_random_int_range(0, N_ACTIONS); if (act == VIEWPORT_ADD && num_views > 5) act = VIEWPORT_RESIZE; lat1 = g_random_double_range(-1.0, 2.0); lon1 = g_random_double_range(-1.0, 2.0); lat2 = g_random_double_range(lat1, 2.0); lon2 = g_random_double_range(lon1, 2.0); switch (act) { case OBJECT_ADD: { obj = g_object_new(DIVERSITY_TYPE_OBJECT, NULL); diversity_object_geometry_set(obj, lon1, lat1, lon2 - lon1, lat2 - lat1); if (!world_tree_add(tree, obj)) g_print("world_tree_add failed\n"); g_object_unref(obj); g_print("ADD: %s\n", object_describe(obj)); num_objs++; } break; case OBJECT_RESIZE: if (obj) { g_print("RESIZE: %s", object_describe(obj)); diversity_object_geometry_set(obj, lon1, lat1, lon2 - lon1, lat2 - lat1); g_print(" -> %s\n", object_describe(obj)); } break; case OBJECT_REMOVE: if (obj) { g_print("REMOVE: %s\n", object_describe(obj)); if (!world_tree_remove(tree, obj)) g_print("world_tree_remove failed\n"); obj = NULL; num_objs--; } break; case VIEWPORT_ADD: { view = diversity_viewport_new(lon1, lat1, lon2, lat2); diversity_viewport_set_enabled(view, TRUE); world_tree_add_viewport(tree, view); g_object_unref(view); g_print("ADD: %s\n", object_describe((DiversityObject *) view)); num_views++; } break; case VIEWPORT_RESIZE: if (view) { g_print("RESIZE: %s", object_describe((DiversityObject *) view)); diversity_object_geometry_set((DiversityObject *) view, lon1, lat1, lon2 - lon1, lat2 - lat1); g_print(" -> %s\n", object_describe((DiversityObject *) view)); } break; case VIEWPORT_REMOVE: if (view) { g_print("REMOVE: %s\n", object_describe((DiversityObject *) view)); world_tree_remove_viewport(tree, view); view = NULL; num_views--; } break; case VIEWPORT_SET_FILTER: if (view) { gint type = g_random_int_range(0, NUM_DIVERSITY_OBJECT_TYPES); diversity_viewport_set_rule(view, type, 0, 0); g_print("SET_FILTER: %d\n", type); } break; default: break; } return TRUE; } int main(int argc, char **argv) { WorldTree *tree; g_type_init(); tree = world_tree_new(3, 3); g_timeout_add(100, mangle, tree); mainloop = g_main_loop_new(NULL, FALSE); g_main_loop_run(mainloop); g_main_loop_unref(mainloop); world_tree_free(tree); return 0; }