+
+/*
+ * Create a string representing the subtree under con.
+ *
+ */
+char *con_get_tree_representation(Con *con) {
+ /* this code works as follows:
+ * 1) create a string with the layout type (D/V/H/T/S) and an opening bracket
+ * 2) append the tree representation of the children to the string
+ * 3) add closing bracket
+ *
+ * The recursion ends when we hit a leaf, in which case we return the
+ * class_instance of the contained window.
+ */
+
+ /* end of recursion */
+ if (con_is_leaf(con)) {
+ if (!con->window)
+ return sstrdup("nowin");
+
+ if (!con->window->class_instance)
+ return sstrdup("noinstance");
+
+ return sstrdup(con->window->class_instance);
+ }
+
+ char *buf;
+ /* 1) add the Layout type to buf */
+ if (con->layout == L_DEFAULT)
+ buf = sstrdup("D[");
+ else if (con->layout == L_SPLITV)
+ buf = sstrdup("V[");
+ else if (con->layout == L_SPLITH)
+ buf = sstrdup("H[");
+ else if (con->layout == L_TABBED)
+ buf = sstrdup("T[");
+ else if (con->layout == L_STACKED)
+ buf = sstrdup("S[");
+
+ /* 2) append representation of children */
+ Con *child;
+ TAILQ_FOREACH(child, &(con->nodes_head), nodes) {
+ char *child_txt = con_get_tree_representation(child);
+
+ char *tmp_buf;
+ sasprintf(&tmp_buf, "%s%s%s", buf,
+ (TAILQ_FIRST(&(con->nodes_head)) == child ? "" : " "), child_txt);
+ free(buf);
+ buf = tmp_buf;
+ }
+
+ /* 3) close the brackets */
+ char *complete_buf;
+ sasprintf(&complete_buf, "%s]", buf);
+ free(buf);
+
+ return complete_buf;
+}