- if (m == NULL || d == NULL)
- continue;
- node_t *birth = make_node();
- if (level == 2) {
- empty_desktop(d);
- d->root = birth;
- } else if (n != NULL) {
- if (level > last_level) {
- n->first_child = birth;
- } else {
- do {
- n = n->parent;
- } while (n != NULL && n->second_child != NULL);
- if (n == NULL)
- continue;
- n->second_child = birth;
- }
- birth->parent = n;
- }
- n = birth;
- char birth_rotation;
- if (isupper(line[level])) {
- char split_type;
- sscanf(line + level, "%c %c %lf", &split_type, &birth_rotation, &n->split_ratio);
- if (split_type == 'H') {
- n->split_type = TYPE_HORIZONTAL;
- } else if (split_type == 'V') {
- n->split_type = TYPE_VERTICAL;
- }
- } else {
- client_t *c = make_client(XCB_NONE, d->border_width);
- num_clients++;
- char urgent, locked, sticky, private, split_dir, split_mode, state, layer, end = 0;
- sscanf(line + level, "%c %s %s %X %u %hux%hu%hi%hi %c%c %c%c %c%c%c%c %c", &birth_rotation,
- c->class_name, c->instance_name, &c->window, &c->border_width,
- &c->floating_rectangle.width, &c->floating_rectangle.height,
- &c->floating_rectangle.x, &c->floating_rectangle.y,
- &split_dir, &split_mode, &state, &layer,
- &urgent, &locked, &sticky, &private, &end);
- n->split_mode = (split_mode == '-' ? MODE_AUTOMATIC : MODE_MANUAL);
- if (split_dir == 'U') {
- n->split_dir = DIR_UP;
- } else if (split_dir == 'R') {
- n->split_dir = DIR_RIGHT;
- } else if (split_dir == 'D') {
- n->split_dir = DIR_DOWN;
- } else if (split_dir == 'L') {
- n->split_dir = DIR_LEFT;
- }
- if (state == 'f') {
- c->state = STATE_FLOATING;
- } else if (state == 'F') {
- c->state = STATE_FULLSCREEN;
- } else if (state == 'p') {
- c->state = STATE_PSEUDO_TILED;
- }
- if (layer == 'b') {
- c->layer = LAYER_BELOW;
- } else if (layer == 'a') {
- c->layer = LAYER_ABOVE;
- }
- c->urgent = (urgent == '-' ? false : true);
- c->locked = (locked == '-' ? false : true);
- c->sticky = (sticky == '-' ? false : true);
- c->private = (private == '-' ? false : true);
- n->client = c;
- if (end != 0) {
- d->focus = n;
- }
- if (c->sticky) {
- m->num_sticky++;
+ tokens = rtokens;
+ }
+ }
+
+ if (ret < 0) {
+ warn("Restore tree: jsmn_parse: ");
+ switch (ret) {
+ case JSMN_ERROR_NOMEM:
+ warn("not enough memory.\n");
+ break;
+ case JSMN_ERROR_INVAL:
+ warn("found invalid character inside JSON string.\n");
+ break;
+ case JSMN_ERROR_PART:
+ warn("not a full JSON packet.\n");
+ break;
+ default:
+ warn("unknown error.\n");
+ break;
+ }
+
+ free(tokens);
+ free(json);
+
+ return false;
+ }
+
+ int num = tokens[0].size;
+
+ if (num < 1) {
+ free(tokens);
+ free(json);
+
+ return false;
+ }
+
+ mon = NULL;
+ while (mon_head != NULL) {
+ remove_monitor(mon_head);
+ }
+
+ jsmntok_t *t = tokens + 1;
+ char *focusedMonitorName = NULL, *primaryMonitorName = NULL;
+
+ for (int i = 0; i < num; i++) {
+ if (keyeq("focusedMonitorName", t, json)) {
+ free(focusedMonitorName);
+ t++;
+ focusedMonitorName = copy_string(json + t->start, t->end - t->start);
+ } else if (keyeq("primaryMonitorName", t, json)) {
+ free(primaryMonitorName);
+ t++;
+ primaryMonitorName = copy_string(json + t->start, t->end - t->start);
+ } else if (keyeq("clientsCount", t, json)) {
+ t++;
+ sscanf(json + t->start, "%u", &clients_count);
+ } else if (keyeq("monitors", t, json)) {
+ t++;
+ int s = t->size;
+ t++;
+ for (int j = 0; j < s; j++) {
+ monitor_t *m = restore_monitor(&t, json);
+ if (m->desk == NULL) {
+ add_desktop(m, make_desktop(NULL, XCB_NONE));