forked from vivien/i3blocks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
click.c
65 lines (56 loc) · 1.83 KB
/
click.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/*
* click.c - read and parse of a click
* Copyright (C) 2014 Vivien Didelot
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "json.h"
#include "log.h"
/*
* Parse a click, previous read from stdin.
*
* A click looks like this ("name" and "instance" can be absent):
*
* ',{"name":"foo","instance":"bar","button":1,"x":1186,"y":13}\n'
*
* Note that this function is non-idempotent. We need to parse from right to
* left. It's ok since the JSON layout is known and fixed.
*/
void
click_parse(char *json, struct click *click)
{
int nst, nlen;
int ist, ilen;
int bst, blen;
int xst, xlen;
int yst, ylen;
json_parse(json, "y", &yst, &ylen);
json_parse(json, "x", &xst, &xlen);
json_parse(json, "button", &bst, &blen);
json_parse(json, "instance", &ist, &ilen);
json_parse(json, "name", &nst, &nlen);
click->name = json + nst;
*(click->name + nlen) = '\0';
click->instance = json + ist;
*(click->instance + ilen) = '\0';
click->button = json + bst;
*(click->button + blen) = '\0';
click->y = json + yst;
*(click->y + ylen) = '\0';
click->x = json + xst;
*(click->x + xlen) = '\0';
debug("parsed click: name=%s instance=%s button=%s x=%s y=%s",
click->name, click->instance,
click->button, click->x, click->y);
}