foo-wm

(Archived) Experimental Zooming IPC-based WM
git clone http://milesalan.com/git/foo-wm
Log | Files | Refs | Mirror | README | LICENSE

responses.c (2390B)


      1 #include <string.h>
      2 #include <stdlib.h>
      3 #include <stdio.h>
      4 #include "foo-wm.h"
      5 #include "util.h"
      6 #include "tree.h"
      7 #include "responses.h"
      8 
      9 
     10 char * jsonMarks(void) {
     11   char * marksResponse = malloc(1024);
     12   sprintf(marksResponse, "{\"marks\":[");
     13   Mark *n = NULL;
     14   for(n = markTail; n; n = n -> previous) {
     15     sprintf(marksResponse, 
     16         "%s%s\"%s\"",
     17         marksResponse,
     18         n != markTail ? "," : "",
     19         n -> name 
     20         );
     21   }
     22 
     23   sprintf(marksResponse, "%s]}", marksResponse);
     24   realloc(marksResponse, bytesUntilNull(marksResponse));
     25   return marksResponse;
     26 }
     27 
     28 
     29 char * jsonTree(Node * node, int level) {
     30   if (!node) return "Foo";
     31   char 
     32     *buffer = malloc(5120),
     33     *label = "", 
     34     *nestString = "";
     35 
     36   /* Recursivly Crawl Children */
     37   if (!isClient(node)) {
     38     nestString = malloc(5120);
     39     Node *n = NULL; int c = 0;
     40 
     41     int last = -1;
     42     for (n = node -> child; n; n = n -> next, last++) { }
     43     n = NULL;
     44 
     45     for (n = node -> child; n; n = n -> next, c++) {
     46       char *crawlResult = malloc(5120);
     47       crawlResult = jsonTree(n, level + 1);
     48       sprintf(nestString, "%s%s%s", 
     49           c == 0 ? "" : nestString, crawlResult, c != last  ? "," : "");
     50       free(crawlResult);
     51     }
     52   }
     53 
     54 
     55   /* Determine Label for sprintf */
     56   if (!isClient(node)) {
     57     switch (node -> layout) {
     58       case VERTICAL   : label = ",\"layout\":\"vertical\""; break;
     59       case HORIZONTAL : label = ",\"layout\":\"horizontal\""; break;
     60       case GRID       : label = ",\"layout\":\"grid\""; break;
     61       case MAX        : label = ",\"layout\":\"max\""; break;
     62       case FLOAT      : label = ",\"layout\":\"float\""; break;
     63     }
     64   }
     65 
     66   /* Print to the buffer */
     67   sprintf(buffer,
     68       /* Type (Pointer) (?Layout) (?R/V/F) (?ChildFocus) */
     69       "{ \"node\":\"%p\",\"id\":\"%p\",\"type\":\"%s\"%s%s%s%s,\"focus\":\"%p\",\"children\":[%s]}",
     70       node,
     71       node -> window,
     72       isClient(node) ? "client" : "container",
     73       label,
     74       node == rootNode    ? ",\"root\":\"true\""    : "",
     75       node == focusedNode ? ",\"focus\":\"true\""    : "",
     76       node == viewNode    ? ",\"view\":\"true\""    : "",
     77       node -> focus ? node -> focus : "",
     78       nestString
     79       );
     80 
     81   /* If it was a container, we malloc'd room for nestString */
     82   if (sizeof(nestString) > sizeof(char*))
     83     free(nestString);
     84 
     85   realloc(buffer, bytesUntilNull(buffer));
     86   return buffer;
     87 }