diff options
author | Thorsten Wißmann <edu@thorsten-wissmann.de> | 2013-01-05 17:48:53 +0100 |
---|---|---|
committer | Thorsten Wißmann <edu@thorsten-wissmann.de> | 2013-02-17 12:23:58 +0100 |
commit | 9a0ffdfc85d47ca31f60d89d5117bab65554377a (patch) | |
tree | d67df755eb2eb3fd5b19dbc57c30379c0287fb55 | |
parent | cc31ab3f9178ed74d50eb4801865db5a89ed459f (diff) |
Handle an empty object tree
-rw-r--r-- | src/command.c | 1 | ||||
-rw-r--r-- | src/main.c | 3 | ||||
-rw-r--r-- | src/object.c | 96 | ||||
-rw-r--r-- | src/object.h | 35 |
4 files changed, 135 insertions, 0 deletions
diff --git a/src/command.c b/src/command.c index b66f0a52..53bcdd46 100644 --- a/src/command.c +++ b/src/command.c @@ -12,6 +12,7 @@ #include "clientlist.h" #include "monitor.h" #include "rules.h" +#include "object.h" #include <glib.h> #include <string.h> @@ -18,6 +18,7 @@ #include "rules.h" #include "ewmh.h" #include "stack.h" +#include "object.h" // standard #include <string.h> #include <stdio.h> @@ -162,6 +163,7 @@ CommandBinding g_commands[] = { CMD_BIND( "chain", command_chain_command), CMD_BIND( "and", command_chain_command), CMD_BIND( "or", command_chain_command), + CMD_BIND( "ls", list_objects_command), CMD_BIND( "!", negate_command), CMD_BIND( "getenv", getenv_command), CMD_BIND( "setenv", setenv_command), @@ -699,6 +701,7 @@ static struct { void (*destroy)(); } g_modules[] = { { ipc_init, ipc_destroy }, + { object_tree_init, object_tree_destroy }, { key_init, key_destroy }, { settings_init, settings_destroy }, { stacklist_init, stacklist_destroy }, diff --git a/src/object.c b/src/object.c new file mode 100644 index 00000000..f24024a4 --- /dev/null +++ b/src/object.c @@ -0,0 +1,96 @@ +/** Copyright 2011-2013 Thorsten Wißmann. All rights reserved. + * + * This software is licensed under the "Simplified BSD License". + * See LICENSE for details */ + +#include "object.h" +#include "command.h" +#include "utils.h" +#include "ipc-protocol.h" + +#include <string.h> + +typedef struct { + char* name; + HSObject* child; +} HSObjectChild; + +static HSObject g_root_object; + +void object_tree_init() { + hsobject_init(&g_root_object); +} + +void object_tree_destroy() { + hsobject_free(&g_root_object); +} + +HSObject* hsobject_root() { + return &g_root_object; +} + +bool hsobject_init(HSObject* obj) { + obj->children = NULL; + return true; +} + +void hsobject_free(HSObject* obj) { +} + +void hsobject_link(HSObject* parent, HSObject* child, char* name) { +} + +void hsobject_unlink(HSObject* parent, HSObject* child) { +} + +struct HSObjectComplChild { + char* needle; + GString* output; +}; + +static void completion_helper(HSObjectChild* child, struct HSObjectComplChild* data) { + try_complete(data->needle, child->name, data->output); +} + +void hsobject_complete_children(HSObject* obj, char* needle, GString* output) { + struct HSObjectComplChild data = { needle, output }; + g_list_foreach(obj->children, (GFunc) completion_helper, &data); +} + +static int child_check_name(HSObjectChild* child, char* name) { + return strcmp(child->name, name); +} + +HSObject* hsobject_find_child(HSObject* obj, char* name) { + GList* elem = g_list_find_custom(obj->children, name, + (GCompareFunc)child_check_name); + if (elem) { + return ((HSObjectChild*)(elem->data))->child; + } else { + return NULL; + } +} + +static void print_child_name(HSObjectChild* child, GString* output) { + g_string_append_printf(output, "%s\n", child->name); +} + +int list_objects_command(int argc, char* argv[], GString* output) { + char* cmdname = argv[0]; + HSObject* obj = hsobject_root(); + while (SHIFT(argc,argv)) { + obj = hsobject_find_child(obj, argv[0]); + if (!obj) { + g_string_append_printf(output, "%s: Can not find object \"%s\"\n", + cmdname, argv[0]); + return HERBST_INVALID_ARGUMENT; + } + } + // list object contents + // TODO: list attributes + // list children + g_string_append_printf(output, "children:\n"); + g_list_foreach(obj->children, (GFunc) print_child_name, output); + return 0; +} + diff --git a/src/object.h b/src/object.h new file mode 100644 index 00000000..93d2d081 --- /dev/null +++ b/src/object.h @@ -0,0 +1,35 @@ +/** Copyright 2011-2013 Thorsten Wißmann. All rights reserved. + * + * This software is licensed under the "Simplified BSD License". + * See LICENSE for details */ + +#ifndef __HS_OBJECT_H_ +#define __HS_OBJECT_H_ + +#include <stdbool.h> +#include <glib.h> + +typedef struct { + //struct HSAttribute* attributes; + //int attribute_count; + GList* children; // list of HSObjectChild +} HSObject; + +void object_tree_init(); +void object_tree_destroy(); + +HSObject* hsobject_root(); + +bool hsobject_init(HSObject* obj); +void hsobject_free(HSObject* obj); +void hsobject_link(HSObject* parent, HSObject* child, char* name); +void hsobject_unlink(HSObject* parent, HSObject* child); + +HSObject* hsobject_find_child(HSObject* obj, char* name); + +int list_objects_command(int argc, char* argv[], GString* output); + +void hsobject_complete_children(HSObject* obj, char* needle, GString* output); + +#endif + |