Start tracking changes
This commit is contained in:
308
i3bar/src/ipc.c
Normal file
308
i3bar/src/ipc.c
Normal file
@ -0,0 +1,308 @@
|
||||
#include <stdio.h>
|
||||
#include <ev.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/un.h>
|
||||
#include <string.h>
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <i3/ipc.h>
|
||||
|
||||
#include "common.h"
|
||||
#include "ipc.h"
|
||||
|
||||
struct callback_t {
|
||||
void (*callback)(char*, void*);
|
||||
void* params;
|
||||
struct callback_t* next;
|
||||
};
|
||||
|
||||
struct callback_t* outputs_cb_queue;
|
||||
struct callback_t* workspaces_cb_queue;
|
||||
|
||||
int get_ipc_fd(const char* socket_path) {
|
||||
int sockfd = socket(AF_LOCAL, SOCK_STREAM, 0);
|
||||
if (sockfd == -1) {
|
||||
printf("ERROR: Could not create Socket!\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
struct sockaddr_un addr;
|
||||
memset(&addr, 0, sizeof(struct sockaddr_un));
|
||||
addr.sun_family = AF_LOCAL;
|
||||
strcpy(addr.sun_path, socket_path);
|
||||
if (connect(sockfd, (const struct sockaddr*) &addr, sizeof(struct sockaddr_un)) < 0) {
|
||||
printf("ERROR: Could not connct to i3\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
return sockfd;
|
||||
}
|
||||
|
||||
void get_outputs_cb(struct ev_loop* loop, ev_io *watcher, int revents) {
|
||||
|
||||
}
|
||||
|
||||
void init_i3(const char* socket_path) {
|
||||
int sockfd = get_ipc_fd(socket_path);
|
||||
|
||||
struct get_outputs_callback* cb = malloc(sizeof(struct get_outputs_callback));
|
||||
cb->callback = callback;
|
||||
cb->params = params;
|
||||
|
||||
ev_io* get_outputs_write = malloc(sizeof(ev_io));
|
||||
|
||||
ev_io_init(get_outputs_write, &get_outputs_write_cb, sockfd, EV_WRITE);
|
||||
get_outputs_write->data = (void*) cb;
|
||||
ev_io_start(main_loop, get_outputs_write);
|
||||
|
||||
ev_io* get_outputs_read = malloc(sizeof(ev_io));
|
||||
ev_io_init(get_outputs_read, &get_outputs_read_cb, sockfd, EV_READ);
|
||||
get_outputs_read->data = (void*) cb;
|
||||
ev_io_start(main_loop, get_outputs_read);
|
||||
}
|
||||
|
||||
|
||||
void get_outputs_write_cb(struct ev_loop* loop, ev_io *watcher, int revents) {
|
||||
ev_io_stop(loop, watcher);
|
||||
|
||||
int buffer_size = strlen(I3_IPC_MAGIC) + sizeof(uint32_t) + sizeof(uint32_t);
|
||||
char msg[buffer_size];
|
||||
char *walk = msg;
|
||||
uint32_t msg_size = 0;
|
||||
uint32_t msg_type = I3_IPC_MESSAGE_TYPE_GET_OUTPUTS;
|
||||
int sockfd = watcher->fd;
|
||||
|
||||
strcpy(walk, I3_IPC_MAGIC);
|
||||
walk += strlen(I3_IPC_MAGIC);
|
||||
memcpy(walk, &msg_size, sizeof(uint32_t));
|
||||
walk += sizeof(uint32_t);
|
||||
memcpy(walk, &msg_type, sizeof(uint32_t));
|
||||
|
||||
int sent_bytes = 0;
|
||||
int bytes_to_go = buffer_size;
|
||||
while (sent_bytes < bytes_to_go) {
|
||||
int n = write(sockfd, msg + sent_bytes, bytes_to_go);
|
||||
if (n == -1) {
|
||||
printf("ERROR: write() failed!\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
sent_bytes += n;
|
||||
bytes_to_go -= n;
|
||||
}
|
||||
FREE(watcher);
|
||||
}
|
||||
|
||||
void get_outputs_read_cb(struct ev_loop* loop, ev_io *watcher, int revents) {
|
||||
ev_io_stop(loop, watcher);
|
||||
|
||||
int to_read = strlen(I3_IPC_MAGIC) + sizeof(uint32_t) + sizeof(uint32_t);
|
||||
char msg[to_read];
|
||||
char *walk = msg;
|
||||
int sockfd = watcher->fd;
|
||||
uint8_t *reply;
|
||||
struct get_outputs_callback* cb = watcher->data;
|
||||
|
||||
uint32_t reply_length;
|
||||
|
||||
uint32_t read_bytes = 0;
|
||||
while (read_bytes < to_read) {
|
||||
int n = read(sockfd, msg + read_bytes, to_read);
|
||||
if (n == -1) {
|
||||
printf("ERROR: read() failed!\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
if (n == 0) {
|
||||
printf("ERROR: No reply!\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
read_bytes += n;
|
||||
to_read -= n;
|
||||
}
|
||||
|
||||
if (memcmp(walk, I3_IPC_MAGIC, strlen(I3_IPC_MAGIC)) != 0) {
|
||||
printf("ERROR: Wrong magic!\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
walk += strlen(I3_IPC_MAGIC);
|
||||
reply_length = *((uint32_t*) walk);
|
||||
walk += sizeof(uint32_t);
|
||||
if (*((uint32_t*) walk) != I3_IPC_MESSAGE_TYPE_GET_OUTPUTS) {
|
||||
printf("ERROR: Wrong reply type (%d) expected %d!\n",
|
||||
*((uint32_t*) walk),
|
||||
I3_IPC_MESSAGE_TYPE_GET_OUTPUTS);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
walk += sizeof(uint32_t);
|
||||
|
||||
reply = malloc(reply_length);
|
||||
if (reply == NULL) {
|
||||
printf("ERROR: malloc() failed!\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
to_read = reply_length;
|
||||
read_bytes = 0;
|
||||
while (read_bytes < to_read) {
|
||||
int n = read(sockfd, reply + read_bytes, to_read);
|
||||
if (n == -1) {
|
||||
printf("ERROR: read() failed!\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
read_bytes += n;
|
||||
to_read -= n;
|
||||
}
|
||||
|
||||
cb->callback((char*) reply, cb->params);
|
||||
FREE(cb);
|
||||
FREE(watcher);
|
||||
}
|
||||
|
||||
void get_outputs_json(void (*callback)(char*, void*), void* params) {
|
||||
}
|
||||
|
||||
|
||||
|
||||
struct get_workspaces_callback {
|
||||
void (*callback)(char*, void*);
|
||||
void* params;
|
||||
};
|
||||
|
||||
void get_workspaces_write_cb(struct ev_loop* loop, ev_io *watcher, int revents) {
|
||||
ev_io_stop(loop, watcher);
|
||||
//FREE(watcher);
|
||||
|
||||
int buffer_size = strlen(I3_IPC_MAGIC) + sizeof(uint32_t) + sizeof(uint32_t);
|
||||
char msg[buffer_size];
|
||||
char *walk = msg;
|
||||
uint32_t msg_size = 0;
|
||||
uint32_t msg_type = I3_IPC_MESSAGE_TYPE_GET_WORKSPACES;
|
||||
int sockfd = watcher->fd;
|
||||
|
||||
strcpy(walk, I3_IPC_MAGIC);
|
||||
walk += strlen(I3_IPC_MAGIC);
|
||||
memcpy(walk, &msg_size, sizeof(uint32_t));
|
||||
walk += sizeof(uint32_t);
|
||||
memcpy(walk, &msg_type, sizeof(uint32_t));
|
||||
|
||||
int sent_bytes = 0;
|
||||
int bytes_to_go = buffer_size;
|
||||
while (sent_bytes < bytes_to_go) {
|
||||
int n = write(sockfd, msg + sent_bytes, bytes_to_go);
|
||||
if (n == -1) {
|
||||
printf("ERROR: write() failed!\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
sent_bytes += n;
|
||||
bytes_to_go -= n;
|
||||
}
|
||||
FREE(watcher);
|
||||
}
|
||||
|
||||
void get_workspaces_read_cb(struct ev_loop* loop, ev_io *watcher, int revents) {
|
||||
ev_io_stop(loop, watcher);
|
||||
//FREE(watcher);
|
||||
|
||||
int to_read = strlen(I3_IPC_MAGIC) + sizeof(uint32_t) + sizeof(uint32_t);
|
||||
char msg[to_read];
|
||||
char *walk = msg;
|
||||
int sockfd = watcher->fd;
|
||||
uint8_t *reply;
|
||||
struct get_workspaces_callback* cb = watcher->data;
|
||||
|
||||
uint32_t reply_length;
|
||||
|
||||
uint32_t read_bytes = 0;
|
||||
while (read_bytes < to_read) {
|
||||
int n = read(sockfd, msg + read_bytes, to_read);
|
||||
if (n == -1) {
|
||||
printf("ERROR: read() failed!\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
if (n == 0) {
|
||||
printf("ERROR: No reply!\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
read_bytes += n;
|
||||
to_read -= n;
|
||||
}
|
||||
|
||||
if (memcmp(walk, I3_IPC_MAGIC, strlen(I3_IPC_MAGIC)) != 0) {
|
||||
printf("ERROR: Wrong magic!\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
walk += strlen(I3_IPC_MAGIC);
|
||||
reply_length = *((uint32_t*) walk);
|
||||
walk += sizeof(uint32_t);
|
||||
if (*((uint32_t*) walk) != I3_IPC_MESSAGE_TYPE_GET_WORKSPACES) {
|
||||
printf("ERROR: Wrong reply type (%d) expected %d!\n",
|
||||
*((uint32_t*) walk),
|
||||
I3_IPC_MESSAGE_TYPE_GET_WORKSPACES);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
walk += sizeof(uint32_t);
|
||||
|
||||
reply = malloc(reply_length);
|
||||
if (reply == NULL) {
|
||||
printf("ERROR: malloc() failed!\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
to_read = reply_length;
|
||||
read_bytes = 0;
|
||||
while (read_bytes < to_read) {
|
||||
int n = read(sockfd, reply + read_bytes, to_read);
|
||||
if (n == -1) {
|
||||
printf("ERROR: read() failed!\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
read_bytes += n;
|
||||
to_read -= n;
|
||||
}
|
||||
|
||||
cb->callback((char*) reply, cb->params);
|
||||
FREE(cb);
|
||||
|
||||
FREE(watcher);
|
||||
}
|
||||
|
||||
void get_workspaces_json(void (*callback)(char*, void*), void* params) {
|
||||
socket_path = "/home/mero/.i3/ipc.sock";
|
||||
|
||||
int sockfd = socket(AF_LOCAL, SOCK_STREAM, 0);
|
||||
if (sockfd == -1) {
|
||||
printf("ERROR: Could not create Socket!\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
struct sockaddr_un addr;
|
||||
memset(&addr, 0, sizeof(struct sockaddr_un));
|
||||
addr.sun_family = AF_LOCAL;
|
||||
strcpy(addr.sun_path, socket_path);
|
||||
if (connect(sockfd, (const struct sockaddr*) &addr, sizeof(struct sockaddr_un)) < 0) {
|
||||
printf("ERROR: Could not connct to i3\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
struct get_workspaces_callback* cb = malloc(sizeof(struct get_workspaces_callback));
|
||||
cb->callback = callback;
|
||||
cb->params = params;
|
||||
|
||||
ev_io* get_workspaces_write = malloc(sizeof(ev_io));
|
||||
|
||||
ev_io_init(get_workspaces_write, &get_workspaces_write_cb, sockfd, EV_WRITE);
|
||||
get_workspaces_write->data = (void*) cb;
|
||||
ev_io_start(main_loop, get_workspaces_write);
|
||||
|
||||
ev_io* get_workspaces_read = malloc(sizeof(ev_io));
|
||||
ev_io_init(get_workspaces_read, &get_workspaces_read_cb, sockfd, EV_READ);
|
||||
get_workspaces_read->data = (void*) cb;
|
||||
ev_io_start(main_loop, get_workspaces_read);
|
||||
}
|
29
i3bar/src/main.c
Normal file
29
i3bar/src/main.c
Normal file
@ -0,0 +1,29 @@
|
||||
#include "ipc.h"
|
||||
#include "outputs.h"
|
||||
#include "workspaces.h"
|
||||
#include "common.h"
|
||||
#include "xcb.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <ev.h>
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
main_loop = ev_default_loop(0);
|
||||
|
||||
init_xcb();
|
||||
|
||||
refresh_outputs(&create_windows, NULL);
|
||||
refresh_workspaces(NULL, NULL);
|
||||
|
||||
ev_loop(main_loop, 0);
|
||||
|
||||
ev_default_destroy();
|
||||
clean_xcb();
|
||||
free_outputs();
|
||||
free_workspaces();
|
||||
|
||||
//sleep(5);
|
||||
return 0;
|
||||
}
|
225
i3bar/src/outputs.c
Normal file
225
i3bar/src/outputs.c
Normal file
@ -0,0 +1,225 @@
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include <yajl/yajl_parse.h>
|
||||
|
||||
#include "common.h"
|
||||
#include "outputs.h"
|
||||
#include "ipc.h"
|
||||
|
||||
struct outputs_json_params {
|
||||
i3_output* outputs;
|
||||
i3_output* outputs_walk;
|
||||
char* cur_key;
|
||||
char* json;
|
||||
void (*callback)(void*);
|
||||
void* cb_params;
|
||||
};
|
||||
|
||||
static int outputs_null_cb(void* params_) {
|
||||
struct outputs_json_params* params = (struct outputs_json_params*) params_;
|
||||
|
||||
if (strcmp(params->cur_key, "current_workspace")) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
FREE(params->cur_key);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int outputs_boolean_cb(void* params_, bool val) {
|
||||
struct outputs_json_params* params = (struct outputs_json_params*) params_;
|
||||
|
||||
if (strcmp(params->cur_key, "active")) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
params->outputs_walk->active = val;
|
||||
|
||||
FREE(params->cur_key);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int outputs_integer_cb(void* params_, long val) {
|
||||
struct outputs_json_params* params = (struct outputs_json_params*) params_;
|
||||
|
||||
if (!strcmp(params->cur_key, "current_workspace")) {
|
||||
params->outputs_walk->ws = (int) val;
|
||||
FREE(params->cur_key);
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (!strcmp(params->cur_key, "x")) {
|
||||
params->outputs_walk->rect.x = (int) val;
|
||||
FREE(params->cur_key);
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (!strcmp(params->cur_key, "y")) {
|
||||
params->outputs_walk->rect.y = (int) val;
|
||||
FREE(params->cur_key);
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (!strcmp(params->cur_key, "width")) {
|
||||
params->outputs_walk->rect.w = (int) val;
|
||||
FREE(params->cur_key);
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (!strcmp(params->cur_key, "height")) {
|
||||
params->outputs_walk->rect.h = (int) val;
|
||||
FREE(params->cur_key);
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int outputs_string_cb(void* params_, const unsigned char* val, unsigned int len) {
|
||||
struct outputs_json_params* params = (struct outputs_json_params*) params_;
|
||||
|
||||
if (strcmp(params->cur_key, "name")) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
params->outputs_walk->name = malloc(sizeof(const unsigned char) * (len + 1));
|
||||
strncpy(params->outputs_walk->name, (const char*) val, len);
|
||||
params->outputs_walk->name[len] = '\0';
|
||||
|
||||
FREE(params->cur_key);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int outputs_start_map_cb(void* params_) {
|
||||
struct outputs_json_params* params = (struct outputs_json_params*) params_;
|
||||
i3_output* new_output = NULL;
|
||||
|
||||
if (params->cur_key == NULL) {
|
||||
new_output = malloc(sizeof(i3_output));
|
||||
new_output->name = NULL;
|
||||
new_output->ws = 0,
|
||||
memset(&new_output->rect, 0, sizeof(rect));
|
||||
new_output->next = NULL;
|
||||
|
||||
if (params->outputs == NULL) {
|
||||
params->outputs = new_output;
|
||||
} else {
|
||||
params->outputs_walk->next = new_output;
|
||||
}
|
||||
|
||||
params->outputs_walk = new_output;
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int outputs_map_key_cb(void* params_, const unsigned char* keyVal, unsigned int keyLen) {
|
||||
struct outputs_json_params* params = (struct outputs_json_params*) params_;
|
||||
FREE(params->cur_key);
|
||||
|
||||
params->cur_key = malloc(sizeof(unsigned char) * (keyLen + 1));
|
||||
strncpy(params->cur_key, (const char*) keyVal, keyLen);
|
||||
params->cur_key[keyLen] = '\0';
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
yajl_callbacks outputs_callbacks = {
|
||||
&outputs_null_cb,
|
||||
&outputs_boolean_cb,
|
||||
&outputs_integer_cb,
|
||||
NULL,
|
||||
NULL,
|
||||
&outputs_string_cb,
|
||||
&outputs_start_map_cb,
|
||||
&outputs_map_key_cb,
|
||||
NULL,
|
||||
NULL,
|
||||
NULL
|
||||
};
|
||||
|
||||
void got_outputs_json_cb(char* json, void* params_) {
|
||||
/* FIXME: Fasciliate stream-processing, i.e. allow starting to interpret
|
||||
* JSON in chunks */
|
||||
struct outputs_json_params* params = (struct outputs_json_params*) params_;
|
||||
|
||||
yajl_handle handle;
|
||||
yajl_parser_config parse_conf = { 0, 0 };
|
||||
yajl_status state;
|
||||
|
||||
params->json = json;
|
||||
|
||||
handle = yajl_alloc(&outputs_callbacks, &parse_conf, NULL, (void*) params);
|
||||
|
||||
state = yajl_parse(handle, (const unsigned char*) json, strlen(json));
|
||||
|
||||
/* FIXME: Propper errorhandling for JSON-parsing */
|
||||
switch (state) {
|
||||
case yajl_status_ok:
|
||||
break;
|
||||
case yajl_status_client_canceled:
|
||||
case yajl_status_insufficient_data:
|
||||
case yajl_status_error:
|
||||
printf("ERROR: Could not parse outputs-reply!\n");
|
||||
exit(EXIT_FAILURE);
|
||||
break;
|
||||
}
|
||||
|
||||
yajl_free(handle);
|
||||
|
||||
free_outputs();
|
||||
outputs = params->outputs;
|
||||
|
||||
if (params->callback != NULL) {
|
||||
params->callback(params->cb_params);
|
||||
}
|
||||
|
||||
FREE(params->json);
|
||||
FREE(params);
|
||||
}
|
||||
|
||||
void refresh_outputs(void (*callback)(void*), void* cb_params) {
|
||||
struct outputs_json_params* params = malloc(sizeof(struct outputs_json_params));
|
||||
|
||||
params->outputs = NULL;
|
||||
params->outputs_walk = NULL;
|
||||
params->cur_key = NULL;
|
||||
params->json = NULL;
|
||||
params->callback = callback;
|
||||
params->cb_params = cb_params;
|
||||
|
||||
get_outputs_json(&got_outputs_json_cb, params);
|
||||
}
|
||||
|
||||
void free_outputs() {
|
||||
i3_output* tmp;
|
||||
while (outputs != NULL) {
|
||||
tmp = outputs;
|
||||
outputs = outputs->next;
|
||||
FREE(tmp->name);
|
||||
FREE(tmp);
|
||||
}
|
||||
}
|
||||
|
||||
i3_output* get_output_by_name(char* name) {
|
||||
if (outputs == NULL) {
|
||||
refresh_outputs(NULL, NULL);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
i3_output* walk;
|
||||
|
||||
for (walk = outputs; walk != NULL; walk = walk->next) {
|
||||
if (strcmp(walk->name, name)) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return walk;
|
||||
}
|
229
i3bar/src/workspaces.c
Normal file
229
i3bar/src/workspaces.c
Normal file
@ -0,0 +1,229 @@
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include <yajl/yajl_parse.h>
|
||||
|
||||
#include "common.h"
|
||||
#include "workspaces.h"
|
||||
#include "ipc.h"
|
||||
|
||||
struct workspaces_json_params {
|
||||
i3_ws* workspaces;
|
||||
i3_ws* workspaces_walk;
|
||||
char* cur_key;
|
||||
char* json;
|
||||
};
|
||||
|
||||
static int workspaces_null_cb(void* params_) {
|
||||
struct workspaces_json_params* params = (struct workspaces_json_params*) params_;
|
||||
|
||||
if (strcmp(params->cur_key, "current_workspace")) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
FREE(params->cur_key);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int workspaces_boolean_cb(void* params_, bool val) {
|
||||
struct workspaces_json_params* params = (struct workspaces_json_params*) params_;
|
||||
|
||||
if (!strcmp(params->cur_key, "visible")) {
|
||||
params->workspaces_walk->visible = val;
|
||||
FREE(params->cur_key);
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (!strcmp(params->cur_key, "focused")) {
|
||||
params->workspaces_walk->focused = val;
|
||||
FREE(params->cur_key);
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (!strcmp(params->cur_key, "urgent")) {
|
||||
params->workspaces_walk->focused = val;
|
||||
FREE(params->cur_key);
|
||||
return 1;
|
||||
}
|
||||
|
||||
FREE(params->cur_key);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int workspaces_integer_cb(void* params_, long val) {
|
||||
struct workspaces_json_params* params = (struct workspaces_json_params*) params_;
|
||||
|
||||
if (!strcmp(params->cur_key, "num")) {
|
||||
params->workspaces_walk->num = (int) val;
|
||||
FREE(params->cur_key);
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (!strcmp(params->cur_key, "x")) {
|
||||
params->workspaces_walk->rect.x = (int) val;
|
||||
FREE(params->cur_key);
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (!strcmp(params->cur_key, "y")) {
|
||||
params->workspaces_walk->rect.y = (int) val;
|
||||
FREE(params->cur_key);
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (!strcmp(params->cur_key, "width")) {
|
||||
params->workspaces_walk->rect.w = (int) val;
|
||||
FREE(params->cur_key);
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (!strcmp(params->cur_key, "height")) {
|
||||
params->workspaces_walk->rect.h = (int) val;
|
||||
FREE(params->cur_key);
|
||||
return 1;
|
||||
}
|
||||
|
||||
FREE(params->cur_key);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int workspaces_string_cb(void* params_, const unsigned char* val, unsigned int len) {
|
||||
struct workspaces_json_params* params = (struct workspaces_json_params*) params_;
|
||||
|
||||
char* output_name;
|
||||
|
||||
if (!strcmp(params->cur_key, "name")) {
|
||||
params->workspaces_walk->name = malloc(sizeof(const unsigned char) * (len + 1));
|
||||
strncpy(params->workspaces_walk->name, (const char*) val, len);
|
||||
params->workspaces_walk->name[len] = '\0';
|
||||
|
||||
FREE(params->cur_key);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (!strcmp(params->cur_key, "output")) {
|
||||
output_name = malloc(sizeof(const unsigned char) * (len + 1));
|
||||
strncpy(output_name, (const char*) val, len);
|
||||
output_name[len] = '\0';
|
||||
params->workspaces_walk->output = get_output_by_name(output_name);
|
||||
free(output_name);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int workspaces_start_map_cb(void* params_) {
|
||||
struct workspaces_json_params* params = (struct workspaces_json_params*) params_;
|
||||
i3_ws* new_workspace = NULL;
|
||||
|
||||
if (params->cur_key == NULL) {
|
||||
new_workspace = malloc(sizeof(i3_ws));
|
||||
new_workspace->num = -1;
|
||||
new_workspace->name = NULL;
|
||||
new_workspace->visible = 0;
|
||||
new_workspace->focused = 0;
|
||||
new_workspace->urgent = 0;
|
||||
memset(&new_workspace->rect, 0, sizeof(rect));
|
||||
new_workspace->output = NULL;
|
||||
new_workspace->next = NULL;
|
||||
|
||||
if (params->workspaces == NULL) {
|
||||
params->workspaces = new_workspace;
|
||||
} else {
|
||||
params->workspaces_walk->next = new_workspace;
|
||||
}
|
||||
|
||||
params->workspaces_walk = new_workspace;
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int workspaces_map_key_cb(void* params_, const unsigned char* keyVal, unsigned int keyLen) {
|
||||
struct workspaces_json_params* params = (struct workspaces_json_params*) params_;
|
||||
FREE(params->cur_key);
|
||||
|
||||
params->cur_key = malloc(sizeof(unsigned char) * (keyLen + 1));
|
||||
strncpy(params->cur_key, (const char*) keyVal, keyLen);
|
||||
params->cur_key[keyLen] = '\0';
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
yajl_callbacks workspaces_callbacks = {
|
||||
&workspaces_null_cb,
|
||||
&workspaces_boolean_cb,
|
||||
&workspaces_integer_cb,
|
||||
NULL,
|
||||
NULL,
|
||||
&workspaces_string_cb,
|
||||
&workspaces_start_map_cb,
|
||||
&workspaces_map_key_cb,
|
||||
NULL,
|
||||
NULL,
|
||||
NULL
|
||||
};
|
||||
|
||||
void got_workspaces_json_cb(char* json, void* params_) {
|
||||
/* FIXME: Fasciliate stream-processing, i.e. allow starting to interpret
|
||||
* JSON in chunks */
|
||||
struct workspaces_json_params* params = (struct workspaces_json_params*) params_;
|
||||
|
||||
yajl_handle handle;
|
||||
yajl_parser_config parse_conf = { 0, 0 };
|
||||
yajl_status state;
|
||||
|
||||
params->json = json;
|
||||
|
||||
handle = yajl_alloc(&workspaces_callbacks, &parse_conf, NULL, (void*) params);
|
||||
|
||||
state = yajl_parse(handle, (const unsigned char*) json, strlen(json));
|
||||
|
||||
/* FIXME: Propper errorhandling for JSON-parsing */
|
||||
switch (state) {
|
||||
case yajl_status_ok:
|
||||
break;
|
||||
case yajl_status_client_canceled:
|
||||
case yajl_status_insufficient_data:
|
||||
case yajl_status_error:
|
||||
printf("ERROR: Could not parse workspaces-reply!\n");
|
||||
exit(EXIT_FAILURE);
|
||||
break;
|
||||
}
|
||||
|
||||
yajl_free(handle);
|
||||
|
||||
free_workspaces();
|
||||
workspaces = params->workspaces;
|
||||
|
||||
FREE(params->json);
|
||||
FREE(params);
|
||||
}
|
||||
|
||||
void refresh_workspaces() {
|
||||
struct workspaces_json_params* params = malloc(sizeof(struct workspaces_json_params));
|
||||
|
||||
params->workspaces = NULL;
|
||||
params->workspaces_walk = NULL;
|
||||
params->cur_key = NULL;
|
||||
params->json = NULL;
|
||||
|
||||
get_workspaces_json(&got_workspaces_json_cb, params);
|
||||
}
|
||||
|
||||
void free_workspaces() {
|
||||
i3_ws* tmp;
|
||||
while (workspaces != NULL) {
|
||||
tmp = workspaces;
|
||||
workspaces = workspaces->next;
|
||||
FREE(tmp->name);
|
||||
FREE(tmp);
|
||||
}
|
||||
}
|
125
i3bar/src/xcb.c
Normal file
125
i3bar/src/xcb.c
Normal file
@ -0,0 +1,125 @@
|
||||
#include <xcb/xcb.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "xcb.h"
|
||||
#include "outputs.h"
|
||||
|
||||
xcb_intern_atom_cookie_t atom_cookies[NUM_ATOMS];
|
||||
|
||||
void init_xcb() {
|
||||
/* LEAK: xcb_connect leaks Memory */
|
||||
xcb_connection = xcb_connect(NULL, NULL);
|
||||
if (xcb_connection_has_error(xcb_connection)) {
|
||||
printf("Cannot open display\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
printf("Connected to xcb\n");
|
||||
|
||||
/* We have to request the atoms we need */
|
||||
#define ATOM_DO(name) atom_cookies[name] = xcb_intern_atom(xcb_connection, 0, strlen(#name), #name);
|
||||
#include "xcb_atoms.def"
|
||||
|
||||
xcb_screens = xcb_setup_roots_iterator(xcb_get_setup(xcb_connection)).data;
|
||||
xcb_root = xcb_screens->root;
|
||||
|
||||
/* FIXME: Maybe we can push that further backwards */
|
||||
get_atoms();
|
||||
}
|
||||
|
||||
void clean_xcb() {
|
||||
xcb_disconnect(xcb_connection);
|
||||
}
|
||||
|
||||
void get_atoms() {
|
||||
xcb_intern_atom_reply_t* reply;
|
||||
#define ATOM_DO(name) reply = xcb_intern_atom_reply(xcb_connection, atom_cookies[name], NULL); \
|
||||
atoms[name] = reply->atom; \
|
||||
free(reply);
|
||||
|
||||
#include "xcb_atoms.def"
|
||||
printf("Got Atoms\n");
|
||||
}
|
||||
|
||||
void create_windows() {
|
||||
uint32_t mask;
|
||||
uint32_t values[2];
|
||||
|
||||
i3_output* walk = outputs;
|
||||
while (walk != NULL) {
|
||||
if (!walk->active) {
|
||||
walk = walk->next;
|
||||
continue;
|
||||
}
|
||||
printf("Creating Window for output %s\n", walk->name);
|
||||
|
||||
walk->win = xcb_generate_id(xcb_connection);
|
||||
mask = XCB_CW_BACK_PIXEL;
|
||||
values[0] = xcb_screens->black_pixel;
|
||||
xcb_create_window(xcb_connection,
|
||||
xcb_screens->root_depth,
|
||||
walk->win,
|
||||
xcb_root,
|
||||
walk->rect.x, walk->rect.y,
|
||||
walk->rect.w, 20,
|
||||
1,
|
||||
XCB_WINDOW_CLASS_INPUT_OUTPUT,
|
||||
xcb_screens->root_visual,
|
||||
mask,
|
||||
values);
|
||||
|
||||
xcb_change_property(xcb_connection,
|
||||
XCB_PROP_MODE_REPLACE,
|
||||
walk->win,
|
||||
atoms[_NET_WM_WINDOW_TYPE],
|
||||
atoms[ATOM],
|
||||
32,
|
||||
1,
|
||||
(unsigned char*) &atoms[_NET_WM_WINDOW_TYPE_DOCK]);
|
||||
|
||||
xcb_map_window(xcb_connection, walk->win);
|
||||
walk = walk->next;
|
||||
}
|
||||
xcb_flush(xcb_connection);
|
||||
}
|
||||
|
||||
#if 0
|
||||
xcb_screen_t* screens = xcb_setup_roots_iterator(xcb_get_setup(xcb_connection)).data;
|
||||
|
||||
xcb_gcontext_t ctx = xcb_generate_id(xcb_connection);
|
||||
|
||||
xcb_window_t win = screens->root;
|
||||
uint32_t mask = XCB_GC_FOREGROUND | XCB_GC_GRAPHICS_EXPOSURES;
|
||||
uint32_t values[2];
|
||||
values[0] = screens->black_pixel;
|
||||
values[1] = 0;
|
||||
xcb_create_gc(xcb_connection, ctx, win, mask, values);
|
||||
|
||||
request_atoms();
|
||||
|
||||
/* Fenster erzeugen */
|
||||
win = xcb_generate_id(xcb_connection);
|
||||
mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK;
|
||||
values[0] = screens->white_pixel;
|
||||
values[1] = XCB_EVENT_MASK_EXPOSURE | XCB_EVENT_MASK_KEY_PRESS;
|
||||
xcb_create_window(xcb_connection, screens->root_depth, win, screens->root,
|
||||
10, 10, 20, 20, 1,
|
||||
XCB_WINDOW_CLASS_INPUT_OUTPUT, screens->root_visual,
|
||||
mask, values);
|
||||
|
||||
get_atoms();
|
||||
|
||||
xcb_change_property(xcb_connection,
|
||||
XCB_PROP_MODE_REPLACE,
|
||||
win,
|
||||
atoms[_NET_WM_WINDOW_TYPE],
|
||||
atoms[ATOM],
|
||||
32,
|
||||
1,
|
||||
(unsigned char *) &atoms[_NET_WM_WINDOW_TYPE_DOCK]);
|
||||
|
||||
xcb_map_window(xcb_connection, win);
|
||||
|
||||
xcb_flush(xcb_connection);
|
||||
#endif
|
Reference in New Issue
Block a user