int key_pressed(KEYBOARD_REC *keyboard, const char *key) { KEY_REC *rec; char *combo; int first_key, consumed; g_return_val_if_fail(keyboard != NULL, FALSE); g_return_val_if_fail(key != NULL && *key != '\0', FALSE); if (keyboard->timer_tag > 0) { g_source_remove(keyboard->timer_tag); keyboard->timer_tag = 0; } if (keyboard->key_state == NULL && key[1] == '\0' && !used_keys[(int) (unsigned char) key[0]]) { /* fast check - key not used */ return -1; } first_key = keyboard->key_state == NULL; combo = keyboard->key_state == NULL ? g_strdup(key) : g_strconcat(keyboard->key_state, "-", key, NULL); g_free_and_null(keyboard->key_state); rec = g_tree_search(key_states, (GCompareFunc) key_states_search, combo); if (rec == NULL) { /* unknown key combo, eat the invalid key unless it was the first key pressed */ g_free(combo); return first_key ? -1 : 1; } if (g_tree_lookup(key_states, combo) != rec) { /* key combo continues.. */ keyboard->key_state = combo; /* respect the timeout if specified by the user */ if (key_timeout > 0) { keyboard->timer_tag = g_timeout_add(key_timeout, (GSourceFunc) key_timeout_expired, keyboard); } return 0; } /* finished key combo, execute */ g_free(combo); consumed = key_emit_signal(keyboard, rec); /* never consume non-control characters */ return consumed ? 1 : -1; }
int main() { printf("Create a new GTree using g_tree_new_full().\n"); GTree *tree = g_tree_new_full((GCompareDataFunc)g_ascii_strcasecmp, NULL, (GDestroyNotify)destroy_key, (GDestroyNotify)destroy_val); printf("Add items to the tree.\n"); g_tree_insert(tree, "c", "Captain America"); g_tree_insert(tree, "i", "Iron Man"); g_tree_insert(tree, "h", "Hawk Eye"); g_tree_insert(tree, "s", "Spider Man"); printf("Tree height from g_tree_height(): %d\n", g_tree_height(tree)); printf("Number of nodes from g_tree_nnodes(): %d\n", g_tree_nnodes(tree)); printf("List the tree using g_tree_foreach() and a GTraverseFunc.\n"); printf("Tree Contents:\n"); g_tree_foreach(tree, (GTraverseFunc)print_iterator, NULL); printf("\n"); printf("Find items using g_tree_lookup().\n"); printf("The data at 'i' is '%s'\n", g_tree_lookup(tree, "i")); printf("%s\n\n", g_tree_lookup(tree, "d") ? "Unexpected value!!" : "No data found for 'd'"); printf("Find items using g_tree_search() and a GCompareFunc.\n"); gpointer val = g_tree_search(tree, (GCompareFunc)finder, NULL); printf("The value is %s\n\n", val); printf("Remove an item using g_tree_remove().\n"); g_tree_remove(tree, "h"); printf("Number of nodes is now: %d\n", g_tree_nnodes(tree)); printf("Tree Contents:\n"); g_tree_foreach(tree, (GTraverseFunc)print_iterator, NULL); printf("\n"); printf("Replacing 's', destroy functions will be called.\n"); g_tree_replace(tree, "s", "Storm"); printf("Number of nodes is now: %d\n", g_tree_nnodes(tree)); printf("Tree Contents:\n"); g_tree_foreach(tree, (GTraverseFunc)print_iterator, NULL); printf("\n"); printf("Stealing 'c' with g_tree_steal(), no drstroy functions now.\n"); g_tree_steal(tree, "c"); printf("Number of nodes is now: %d\n", g_tree_nnodes(tree)); printf("Tree Contents:\n"); g_tree_foreach(tree, (GTraverseFunc)print_iterator, NULL); printf("\n"); printf("Destroying the tree using g_tree_destroy().\n"); g_tree_destroy(tree); }
/* Returns TRUE if key press was consumed. Control characters should be sent as "^@" .. "^_" instead of #0..#31 chars, #127 should be sent as ^? */ int key_pressed(KEYBOARD_REC *keyboard, const char *key) { KEY_REC *rec; char *combo; int first_key, consumed; g_return_val_if_fail(keyboard != NULL, FALSE); g_return_val_if_fail(key != NULL && *key != '\0', FALSE); if (keyboard->key_state == NULL && key[1] == '\0' && !used_keys[(int) (unsigned char) key[0]]) { /* fast check - key not used */ return FALSE; } first_key = keyboard->key_state == NULL; combo = keyboard->key_state == NULL ? g_strdup(key) : g_strconcat(keyboard->key_state, "-", key, NULL); g_free_and_null(keyboard->key_state); #if GLIB_MAJOR_VERSION == 2 # define GSearchFunc GCompareFunc #endif rec = g_tree_search(key_states, (GSearchFunc) key_states_search, combo); if (rec == NULL) { /* unknown key combo, eat the invalid key unless it was the first key pressed */ g_free(combo); return !first_key; } if (g_tree_lookup(key_states, combo) != rec) { /* key combo continues.. */ keyboard->key_state = combo; return TRUE; } /* finished key combo, execute */ g_free(combo); consumed = key_emit_signal(keyboard, rec); /* never consume non-control characters */ return consumed; }
static GstIndexEntry * gst_mem_index_get_assoc_entry (GstIndex * index, gint id, GstIndexLookupMethod method, GstIndexAssociationFlags flags, GstFormat format, gint64 value, GCompareDataFunc func, gpointer user_data) { GstMemIndex *memindex = GST_MEM_INDEX (index); GstMemIndexId *id_index; GstMemIndexFormatIndex *format_index; GstIndexEntry *entry; GstMemIndexSearchData data; id_index = g_hash_table_lookup (memindex->id_index, &id); if (!id_index) return NULL; format_index = g_hash_table_lookup (id_index->format_index, &format); if (!format_index) return NULL; data.value = value; data.index = format_index; data.exact = (method == GST_INDEX_LOOKUP_EXACT); /* setup data for low/high checks if we are not looking * for an exact match */ if (!data.exact) { data.low_diff = G_MININT64; data.lower = NULL; data.high_diff = G_MAXINT64; data.higher = NULL; } entry = g_tree_search (format_index->tree, mem_index_search, &data); /* get the low/high values if we're not exact */ if (entry == NULL && !data.exact) { if (method == GST_INDEX_LOOKUP_BEFORE) entry = data.lower; else if (method == GST_INDEX_LOOKUP_AFTER) { entry = data.higher; } } if (entry && ((GST_INDEX_ASSOC_FLAGS (entry) & flags) != flags)) { if (method != GST_INDEX_LOOKUP_EXACT) { GList *l_entry = g_list_find (memindex->associations, entry); entry = NULL; while (l_entry) { entry = (GstIndexEntry *) l_entry->data; if (entry->id == id && (GST_INDEX_ASSOC_FLAGS (entry) & flags) == flags) break; if (method == GST_INDEX_LOOKUP_BEFORE) l_entry = g_list_next (l_entry); else if (method == GST_INDEX_LOOKUP_AFTER) { l_entry = g_list_previous (l_entry); } } } else { entry = NULL; } } return entry; }
int main (int argc, char *argv[]) { gint i; GTree *tree; gboolean removed; char c, d; char *p; tree = g_tree_new (my_compare); for (i = 0; chars[i]; i++) g_tree_insert (tree, &chars[i], &chars[i]); g_tree_foreach (tree, my_traverse, NULL); g_assert (g_tree_nnodes (tree) == strlen (chars)); g_assert (g_tree_height (tree) == 6); p = chars; g_tree_foreach (tree, check_order, &p); for (i = 0; i < 26; i++) { removed = g_tree_remove (tree, &chars[i + 10]); g_assert (removed); } c = '\0'; removed = g_tree_remove (tree, &c); g_assert (removed == FALSE); g_tree_foreach (tree, my_traverse, NULL); g_assert (g_tree_nnodes (tree) == strlen (chars2)); g_assert (g_tree_height (tree) == 6); p = chars2; g_tree_foreach (tree, check_order, &p); for (i = 25; i >= 0; i--) g_tree_insert (tree, &chars[i + 10], &chars[i + 10]); p = chars; g_tree_foreach (tree, check_order, &p); c = '0'; p = g_tree_lookup (tree, &c); g_assert (p && *p == c); c = 'A'; p = g_tree_lookup (tree, &c); g_assert (p && *p == c); c = 'a'; p = g_tree_lookup (tree, &c); g_assert (p && *p == c); c = 'z'; p = g_tree_lookup (tree, &c); g_assert (p && *p == c); c = '!'; p = g_tree_lookup (tree, &c); g_assert (p == NULL); c = '='; p = g_tree_lookup (tree, &c); g_assert (p == NULL); c = '|'; p = g_tree_lookup (tree, &c); g_assert (p == NULL); c = '0'; p = g_tree_search (tree, my_search, &c); g_assert (p && *p == c); c = 'A'; p = g_tree_search (tree, my_search, &c); g_assert (p && *p == c); c = 'a'; p = g_tree_search (tree, my_search, &c); g_assert (p &&*p == c); c = 'z'; p = g_tree_search (tree, my_search, &c); g_assert (p && *p == c); c = '!'; p = g_tree_search (tree, my_search, &c); g_assert (p == NULL); c = '='; p = g_tree_search (tree, my_search, &c); g_assert (p == NULL); c = '|'; p = g_tree_search (tree, my_search, &c); g_assert (p == NULL); g_tree_destroy (tree); tree = g_tree_new_full ((GCompareDataFunc)my_compare, NULL, my_key_destroy, my_value_destroy); for (i = 0; chars[i]; i++) g_tree_insert (tree, &chars[i], &chars[i]); c = '0'; g_tree_insert (tree, &c, &c); g_assert (destroyed_key == &c); g_assert (destroyed_value == &chars[0]); destroyed_key = NULL; destroyed_value = NULL; d = '1'; g_tree_replace (tree, &d, &d); g_assert (destroyed_key == &chars[1]); g_assert (destroyed_value == &chars[1]); destroyed_key = NULL; destroyed_value = NULL; c = '2'; removed = g_tree_remove (tree, &c); g_assert (removed); g_assert (destroyed_key == &chars[2]); g_assert (destroyed_value == &chars[2]); destroyed_key = NULL; destroyed_value = NULL; c = '3'; removed = g_tree_steal (tree, &c); g_assert (removed); g_assert (destroyed_key == NULL); g_assert (destroyed_value == NULL); return 0; }
/** * eva_g_tree_max: * @tree: tree to examine. * * Find the maximum key in the tree. * * returns: largest key in the tree. */ gpointer eva_g_tree_max (GTree *tree) { gpointer rv = NULL; g_tree_search (tree, max_search_func, &rv); return rv; }