From 02df973564f67edd4a91a0ff8bc74df057860b5b Mon Sep 17 00:00:00 2001 From: Axel Wagner Date: Thu, 22 Jul 2010 01:15:18 +0200 Subject: [PATCH] Start tracking changes --- i3bar/.gitignore | 1 + i3bar/Makefile | 18 ++ i3bar/common.mk | 9 + i3bar/include/common.h | 19 ++ i3bar/include/ipc.h | 14 + i3bar/include/outputs.h | 27 ++ i3bar/include/queue.h | 527 ++++++++++++++++++++++++++++++++++++ i3bar/include/util.h | 22 ++ i3bar/include/workspaces.h | 26 ++ i3bar/include/xcb.h | 24 ++ i3bar/include/xcb_atoms.def | 4 + i3bar/src/ipc.c | 308 +++++++++++++++++++++ i3bar/src/main.c | 29 ++ i3bar/src/outputs.c | 225 +++++++++++++++ i3bar/src/workspaces.c | 229 ++++++++++++++++ i3bar/src/xcb.c | 125 +++++++++ 16 files changed, 1607 insertions(+) create mode 100644 i3bar/.gitignore create mode 100644 i3bar/Makefile create mode 100644 i3bar/common.mk create mode 100644 i3bar/include/common.h create mode 100644 i3bar/include/ipc.h create mode 100644 i3bar/include/outputs.h create mode 100644 i3bar/include/queue.h create mode 100644 i3bar/include/util.h create mode 100644 i3bar/include/workspaces.h create mode 100644 i3bar/include/xcb.h create mode 100644 i3bar/include/xcb_atoms.def create mode 100644 i3bar/src/ipc.c create mode 100644 i3bar/src/main.c create mode 100644 i3bar/src/outputs.c create mode 100644 i3bar/src/workspaces.c create mode 100644 i3bar/src/xcb.c diff --git a/i3bar/.gitignore b/i3bar/.gitignore new file mode 100644 index 00000000..a890aa1d --- /dev/null +++ b/i3bar/.gitignore @@ -0,0 +1 @@ +i3bar diff --git a/i3bar/Makefile b/i3bar/Makefile new file mode 100644 index 00000000..172a442d --- /dev/null +++ b/i3bar/Makefile @@ -0,0 +1,18 @@ +TOPDIR=$(shell pwd) + +include $(TOPDIR)/common.mk + +FILES:=$(wildcard src/*.c) +FILES:=$(FILES:.c=.o) +HEADERS:=$(wildcard include/*.h) + +all: ${FILES} + echo "LINK" + $(CC) -o i3bar ${FILES} ${LDFLAGS} + +src/%.o: src/%.c ${HEADERS} + echo "CC $<" + $(CC) $(CFLAGS) -c -o $@ $< + +clean: + rm src/*.o diff --git a/i3bar/common.mk b/i3bar/common.mk new file mode 100644 index 00000000..e2be3aab --- /dev/null +++ b/i3bar/common.mk @@ -0,0 +1,9 @@ +CFLAGS += -Wall +CFLAGS += -pipe +CFLAGS += -Iinclude +CFLAGS += -g +LDFLAGS += -lev +LDFLAGS += -lyajl +LDFLAGS += -lxcb +LDFLAGS += -lxcb-atom +.SILENT: diff --git a/i3bar/include/common.h b/i3bar/include/common.h new file mode 100644 index 00000000..431c4694 --- /dev/null +++ b/i3bar/include/common.h @@ -0,0 +1,19 @@ +#ifndef COMMON_H_ +#define COMMON_H_ + +#include "util.h" + +typedef int bool; + +typedef struct rect_t rect; + +struct rect_t { + int x; + int y; + int w; + int h; +}; + +struct ev_loop* main_loop; + +#endif diff --git a/i3bar/include/ipc.h b/i3bar/include/ipc.h new file mode 100644 index 00000000..45565618 --- /dev/null +++ b/i3bar/include/ipc.h @@ -0,0 +1,14 @@ +#ifndef IPC_H_ +#define IPC_H_ + +#include + +ev_io* i3_events; +ev_io* outputs_watcher; +ev_io* workspaces_watcher; + +void init_i3(const char* socket_path); +void get_outputs_json(void (*callback)(char*, void*), void* params); +void get_workspaces_json(void (*callback)(char*, void*), void* params); + +#endif diff --git a/i3bar/include/outputs.h b/i3bar/include/outputs.h new file mode 100644 index 00000000..83364c2f --- /dev/null +++ b/i3bar/include/outputs.h @@ -0,0 +1,27 @@ +#ifndef OUTPUTS_H_ +#define OUTPUTS_H_ + +#include "common.h" +#include + +typedef struct i3_output_t i3_output; + +i3_output* outputs; + +void refresh_outputs(); +void free_outputs(); +i3_output* get_output_by_name(char* name); + +struct i3_output_t { + char* name; + bool active; + int ws; + rect rect; + + xcb_window_t win; + xcb_gcontext_t gctx; + + i3_output* next; +}; + +#endif diff --git a/i3bar/include/queue.h b/i3bar/include/queue.h new file mode 100644 index 00000000..75bb957a --- /dev/null +++ b/i3bar/include/queue.h @@ -0,0 +1,527 @@ +/* $OpenBSD: queue.h,v 1.1 2007/10/26 03:14:08 niallo Exp $ */ +/* $NetBSD: queue.h,v 1.11 1996/05/16 05:17:14 mycroft Exp $ */ + +/* + * Copyright (c) 1991, 1993 + * The Regents of the University of California. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * @(#)queue.h 8.5 (Berkeley) 8/20/94 + */ + +#ifndef _SYS_QUEUE_H_ +#define _SYS_QUEUE_H_ + +/* + * This file defines five types of data structures: singly-linked lists, + * lists, simple queues, tail queues, and circular queues. + * + * + * A singly-linked list is headed by a single forward pointer. The elements + * are singly linked for minimum space and pointer manipulation overhead at + * the expense of O(n) removal for arbitrary elements. New elements can be + * added to the list after an existing element or at the head of the list. + * Elements being removed from the head of the list should use the explicit + * macro for this purpose for optimum efficiency. A singly-linked list may + * only be traversed in the forward direction. Singly-linked lists are ideal + * for applications with large datasets and few or no removals or for + * implementing a LIFO queue. + * + * A list is headed by a single forward pointer (or an array of forward + * pointers for a hash table header). The elements are doubly linked + * so that an arbitrary element can be removed without a need to + * traverse the list. New elements can be added to the list before + * or after an existing element or at the head of the list. A list + * may only be traversed in the forward direction. + * + * A simple queue is headed by a pair of pointers, one the head of the + * list and the other to the tail of the list. The elements are singly + * linked to save space, so elements can only be removed from the + * head of the list. New elements can be added to the list before or after + * an existing element, at the head of the list, or at the end of the + * list. A simple queue may only be traversed in the forward direction. + * + * A tail queue is headed by a pair of pointers, one to the head of the + * list and the other to the tail of the list. The elements are doubly + * linked so that an arbitrary element can be removed without a need to + * traverse the list. New elements can be added to the list before or + * after an existing element, at the head of the list, or at the end of + * the list. A tail queue may be traversed in either direction. + * + * A circle queue is headed by a pair of pointers, one to the head of the + * list and the other to the tail of the list. The elements are doubly + * linked so that an arbitrary element can be removed without a need to + * traverse the list. New elements can be added to the list before or after + * an existing element, at the head of the list, or at the end of the list. + * A circle queue may be traversed in either direction, but has a more + * complex end of list detection. + * + * For details on the use of these macros, see the queue(3) manual page. + */ + +#if defined(QUEUE_MACRO_DEBUG) || (defined(_KERNEL) && defined(DIAGNOSTIC)) +#define _Q_INVALIDATE(a) (a) = ((void *)-1) +#else +#define _Q_INVALIDATE(a) +#endif + +/* + * Singly-linked List definitions. + */ +#define SLIST_HEAD(name, type) \ +struct name { \ + struct type *slh_first; /* first element */ \ +} + +#define SLIST_HEAD_INITIALIZER(head) \ + { NULL } + +#define SLIST_ENTRY(type) \ +struct { \ + struct type *sle_next; /* next element */ \ +} + +/* + * Singly-linked List access methods. + */ +#define SLIST_FIRST(head) ((head)->slh_first) +#define SLIST_END(head) NULL +#define SLIST_EMPTY(head) (SLIST_FIRST(head) == SLIST_END(head)) +#define SLIST_NEXT(elm, field) ((elm)->field.sle_next) + +#define SLIST_FOREACH(var, head, field) \ + for((var) = SLIST_FIRST(head); \ + (var) != SLIST_END(head); \ + (var) = SLIST_NEXT(var, field)) + +#define SLIST_FOREACH_PREVPTR(var, varp, head, field) \ + for ((varp) = &SLIST_FIRST((head)); \ + ((var) = *(varp)) != SLIST_END(head); \ + (varp) = &SLIST_NEXT((var), field)) + +/* + * Singly-linked List functions. + */ +#define SLIST_INIT(head) { \ + SLIST_FIRST(head) = SLIST_END(head); \ +} + +#define SLIST_INSERT_AFTER(slistelm, elm, field) do { \ + (elm)->field.sle_next = (slistelm)->field.sle_next; \ + (slistelm)->field.sle_next = (elm); \ +} while (0) + +#define SLIST_INSERT_HEAD(head, elm, field) do { \ + (elm)->field.sle_next = (head)->slh_first; \ + (head)->slh_first = (elm); \ +} while (0) + +#define SLIST_REMOVE_NEXT(head, elm, field) do { \ + (elm)->field.sle_next = (elm)->field.sle_next->field.sle_next; \ +} while (0) + +#define SLIST_REMOVE_HEAD(head, field) do { \ + (head)->slh_first = (head)->slh_first->field.sle_next; \ +} while (0) + +#define SLIST_REMOVE(head, elm, type, field) do { \ + if ((head)->slh_first == (elm)) { \ + SLIST_REMOVE_HEAD((head), field); \ + } else { \ + struct type *curelm = (head)->slh_first; \ + \ + while (curelm->field.sle_next != (elm)) \ + curelm = curelm->field.sle_next; \ + curelm->field.sle_next = \ + curelm->field.sle_next->field.sle_next; \ + _Q_INVALIDATE((elm)->field.sle_next); \ + } \ +} while (0) + +/* + * List definitions. + */ +#define LIST_HEAD(name, type) \ +struct name { \ + struct type *lh_first; /* first element */ \ +} + +#define LIST_HEAD_INITIALIZER(head) \ + { NULL } + +#define LIST_ENTRY(type) \ +struct { \ + struct type *le_next; /* next element */ \ + struct type **le_prev; /* address of previous next element */ \ +} + +/* + * List access methods + */ +#define LIST_FIRST(head) ((head)->lh_first) +#define LIST_END(head) NULL +#define LIST_EMPTY(head) (LIST_FIRST(head) == LIST_END(head)) +#define LIST_NEXT(elm, field) ((elm)->field.le_next) + +#define LIST_FOREACH(var, head, field) \ + for((var) = LIST_FIRST(head); \ + (var)!= LIST_END(head); \ + (var) = LIST_NEXT(var, field)) + +/* + * List functions. + */ +#define LIST_INIT(head) do { \ + LIST_FIRST(head) = LIST_END(head); \ +} while (0) + +#define LIST_INSERT_AFTER(listelm, elm, field) do { \ + if (((elm)->field.le_next = (listelm)->field.le_next) != NULL) \ + (listelm)->field.le_next->field.le_prev = \ + &(elm)->field.le_next; \ + (listelm)->field.le_next = (elm); \ + (elm)->field.le_prev = &(listelm)->field.le_next; \ +} while (0) + +#define LIST_INSERT_BEFORE(listelm, elm, field) do { \ + (elm)->field.le_prev = (listelm)->field.le_prev; \ + (elm)->field.le_next = (listelm); \ + *(listelm)->field.le_prev = (elm); \ + (listelm)->field.le_prev = &(elm)->field.le_next; \ +} while (0) + +#define LIST_INSERT_HEAD(head, elm, field) do { \ + if (((elm)->field.le_next = (head)->lh_first) != NULL) \ + (head)->lh_first->field.le_prev = &(elm)->field.le_next;\ + (head)->lh_first = (elm); \ + (elm)->field.le_prev = &(head)->lh_first; \ +} while (0) + +#define LIST_REMOVE(elm, field) do { \ + if ((elm)->field.le_next != NULL) \ + (elm)->field.le_next->field.le_prev = \ + (elm)->field.le_prev; \ + *(elm)->field.le_prev = (elm)->field.le_next; \ + _Q_INVALIDATE((elm)->field.le_prev); \ + _Q_INVALIDATE((elm)->field.le_next); \ +} while (0) + +#define LIST_REPLACE(elm, elm2, field) do { \ + if (((elm2)->field.le_next = (elm)->field.le_next) != NULL) \ + (elm2)->field.le_next->field.le_prev = \ + &(elm2)->field.le_next; \ + (elm2)->field.le_prev = (elm)->field.le_prev; \ + *(elm2)->field.le_prev = (elm2); \ + _Q_INVALIDATE((elm)->field.le_prev); \ + _Q_INVALIDATE((elm)->field.le_next); \ +} while (0) + +/* + * Simple queue definitions. + */ +#define SIMPLEQ_HEAD(name, type) \ +struct name { \ + struct type *sqh_first; /* first element */ \ + struct type **sqh_last; /* addr of last next element */ \ +} + +#define SIMPLEQ_HEAD_INITIALIZER(head) \ + { NULL, &(head).sqh_first } + +#define SIMPLEQ_ENTRY(type) \ +struct { \ + struct type *sqe_next; /* next element */ \ +} + +/* + * Simple queue access methods. + */ +#define SIMPLEQ_FIRST(head) ((head)->sqh_first) +#define SIMPLEQ_END(head) NULL +#define SIMPLEQ_EMPTY(head) (SIMPLEQ_FIRST(head) == SIMPLEQ_END(head)) +#define SIMPLEQ_NEXT(elm, field) ((elm)->field.sqe_next) + +#define SIMPLEQ_FOREACH(var, head, field) \ + for((var) = SIMPLEQ_FIRST(head); \ + (var) != SIMPLEQ_END(head); \ + (var) = SIMPLEQ_NEXT(var, field)) + +/* + * Simple queue functions. + */ +#define SIMPLEQ_INIT(head) do { \ + (head)->sqh_first = NULL; \ + (head)->sqh_last = &(head)->sqh_first; \ +} while (0) + +#define SIMPLEQ_INSERT_HEAD(head, elm, field) do { \ + if (((elm)->field.sqe_next = (head)->sqh_first) == NULL) \ + (head)->sqh_last = &(elm)->field.sqe_next; \ + (head)->sqh_first = (elm); \ +} while (0) + +#define SIMPLEQ_INSERT_TAIL(head, elm, field) do { \ + (elm)->field.sqe_next = NULL; \ + *(head)->sqh_last = (elm); \ + (head)->sqh_last = &(elm)->field.sqe_next; \ +} while (0) + +#define SIMPLEQ_INSERT_AFTER(head, listelm, elm, field) do { \ + if (((elm)->field.sqe_next = (listelm)->field.sqe_next) == NULL)\ + (head)->sqh_last = &(elm)->field.sqe_next; \ + (listelm)->field.sqe_next = (elm); \ +} while (0) + +#define SIMPLEQ_REMOVE_HEAD(head, field) do { \ + if (((head)->sqh_first = (head)->sqh_first->field.sqe_next) == NULL) \ + (head)->sqh_last = &(head)->sqh_first; \ +} while (0) + +/* + * Tail queue definitions. + */ +#define TAILQ_HEAD(name, type) \ +struct name { \ + struct type *tqh_first; /* first element */ \ + struct type **tqh_last; /* addr of last next element */ \ +} + +#define TAILQ_HEAD_INITIALIZER(head) \ + { NULL, &(head).tqh_first } + +#define TAILQ_ENTRY(type) \ +struct { \ + struct type *tqe_next; /* next element */ \ + struct type **tqe_prev; /* address of previous next element */ \ +} + +/* + * tail queue access methods + */ +#define TAILQ_FIRST(head) ((head)->tqh_first) +#define TAILQ_END(head) NULL +#define TAILQ_NEXT(elm, field) ((elm)->field.tqe_next) +#define TAILQ_LAST(head, headname) \ + (*(((struct headname *)((head)->tqh_last))->tqh_last)) +/* XXX */ +#define TAILQ_PREV(elm, headname, field) \ + (*(((struct headname *)((elm)->field.tqe_prev))->tqh_last)) +#define TAILQ_EMPTY(head) \ + (TAILQ_FIRST(head) == TAILQ_END(head)) + +#define TAILQ_FOREACH(var, head, field) \ + for((var) = TAILQ_FIRST(head); \ + (var) != TAILQ_END(head); \ + (var) = TAILQ_NEXT(var, field)) + +#define TAILQ_FOREACH_REVERSE(var, head, headname, field) \ + for((var) = TAILQ_LAST(head, headname); \ + (var) != TAILQ_END(head); \ + (var) = TAILQ_PREV(var, headname, field)) + +/* + * Tail queue functions. + */ +#define TAILQ_INIT(head) do { \ + (head)->tqh_first = NULL; \ + (head)->tqh_last = &(head)->tqh_first; \ +} while (0) + +#define TAILQ_INSERT_HEAD(head, elm, field) do { \ + if (((elm)->field.tqe_next = (head)->tqh_first) != NULL) \ + (head)->tqh_first->field.tqe_prev = \ + &(elm)->field.tqe_next; \ + else \ + (head)->tqh_last = &(elm)->field.tqe_next; \ + (head)->tqh_first = (elm); \ + (elm)->field.tqe_prev = &(head)->tqh_first; \ +} while (0) + +#define TAILQ_INSERT_TAIL(head, elm, field) do { \ + (elm)->field.tqe_next = NULL; \ + (elm)->field.tqe_prev = (head)->tqh_last; \ + *(head)->tqh_last = (elm); \ + (head)->tqh_last = &(elm)->field.tqe_next; \ +} while (0) + +#define TAILQ_INSERT_AFTER(head, listelm, elm, field) do { \ + if (((elm)->field.tqe_next = (listelm)->field.tqe_next) != NULL)\ + (elm)->field.tqe_next->field.tqe_prev = \ + &(elm)->field.tqe_next; \ + else \ + (head)->tqh_last = &(elm)->field.tqe_next; \ + (listelm)->field.tqe_next = (elm); \ + (elm)->field.tqe_prev = &(listelm)->field.tqe_next; \ +} while (0) + +#define TAILQ_INSERT_BEFORE(listelm, elm, field) do { \ + (elm)->field.tqe_prev = (listelm)->field.tqe_prev; \ + (elm)->field.tqe_next = (listelm); \ + *(listelm)->field.tqe_prev = (elm); \ + (listelm)->field.tqe_prev = &(elm)->field.tqe_next; \ +} while (0) + +#define TAILQ_REMOVE(head, elm, field) do { \ + if (((elm)->field.tqe_next) != NULL) \ + (elm)->field.tqe_next->field.tqe_prev = \ + (elm)->field.tqe_prev; \ + else \ + (head)->tqh_last = (elm)->field.tqe_prev; \ + *(elm)->field.tqe_prev = (elm)->field.tqe_next; \ + _Q_INVALIDATE((elm)->field.tqe_prev); \ + _Q_INVALIDATE((elm)->field.tqe_next); \ +} while (0) + +#define TAILQ_REPLACE(head, elm, elm2, field) do { \ + if (((elm2)->field.tqe_next = (elm)->field.tqe_next) != NULL) \ + (elm2)->field.tqe_next->field.tqe_prev = \ + &(elm2)->field.tqe_next; \ + else \ + (head)->tqh_last = &(elm2)->field.tqe_next; \ + (elm2)->field.tqe_prev = (elm)->field.tqe_prev; \ + *(elm2)->field.tqe_prev = (elm2); \ + _Q_INVALIDATE((elm)->field.tqe_prev); \ + _Q_INVALIDATE((elm)->field.tqe_next); \ +} while (0) + +/* + * Circular queue definitions. + */ +#define CIRCLEQ_HEAD(name, type) \ +struct name { \ + struct type *cqh_first; /* first element */ \ + struct type *cqh_last; /* last element */ \ +} + +#define CIRCLEQ_HEAD_INITIALIZER(head) \ + { CIRCLEQ_END(&head), CIRCLEQ_END(&head) } + +#define CIRCLEQ_ENTRY(type) \ +struct { \ + struct type *cqe_next; /* next element */ \ + struct type *cqe_prev; /* previous element */ \ +} + +/* + * Circular queue access methods + */ +#define CIRCLEQ_FIRST(head) ((head)->cqh_first) +#define CIRCLEQ_LAST(head) ((head)->cqh_last) +#define CIRCLEQ_END(head) ((void *)(head)) +#define CIRCLEQ_NEXT(elm, field) ((elm)->field.cqe_next) +#define CIRCLEQ_PREV(elm, field) ((elm)->field.cqe_prev) +#define CIRCLEQ_EMPTY(head) \ + (CIRCLEQ_FIRST(head) == CIRCLEQ_END(head)) + +#define CIRCLEQ_FOREACH(var, head, field) \ + for((var) = CIRCLEQ_FIRST(head); \ + (var) != CIRCLEQ_END(head); \ + (var) = CIRCLEQ_NEXT(var, field)) + +#define CIRCLEQ_FOREACH_REVERSE(var, head, field) \ + for((var) = CIRCLEQ_LAST(head); \ + (var) != CIRCLEQ_END(head); \ + (var) = CIRCLEQ_PREV(var, field)) + +/* + * Circular queue functions. + */ +#define CIRCLEQ_INIT(head) do { \ + (head)->cqh_first = CIRCLEQ_END(head); \ + (head)->cqh_last = CIRCLEQ_END(head); \ +} while (0) + +#define CIRCLEQ_INSERT_AFTER(head, listelm, elm, field) do { \ + (elm)->field.cqe_next = (listelm)->field.cqe_next; \ + (elm)->field.cqe_prev = (listelm); \ + if ((listelm)->field.cqe_next == CIRCLEQ_END(head)) \ + (head)->cqh_last = (elm); \ + else \ + (listelm)->field.cqe_next->field.cqe_prev = (elm); \ + (listelm)->field.cqe_next = (elm); \ +} while (0) + +#define CIRCLEQ_INSERT_BEFORE(head, listelm, elm, field) do { \ + (elm)->field.cqe_next = (listelm); \ + (elm)->field.cqe_prev = (listelm)->field.cqe_prev; \ + if ((listelm)->field.cqe_prev == CIRCLEQ_END(head)) \ + (head)->cqh_first = (elm); \ + else \ + (listelm)->field.cqe_prev->field.cqe_next = (elm); \ + (listelm)->field.cqe_prev = (elm); \ +} while (0) + +#define CIRCLEQ_INSERT_HEAD(head, elm, field) do { \ + (elm)->field.cqe_next = (head)->cqh_first; \ + (elm)->field.cqe_prev = CIRCLEQ_END(head); \ + if ((head)->cqh_last == CIRCLEQ_END(head)) \ + (head)->cqh_last = (elm); \ + else \ + (head)->cqh_first->field.cqe_prev = (elm); \ + (head)->cqh_first = (elm); \ +} while (0) + +#define CIRCLEQ_INSERT_TAIL(head, elm, field) do { \ + (elm)->field.cqe_next = CIRCLEQ_END(head); \ + (elm)->field.cqe_prev = (head)->cqh_last; \ + if ((head)->cqh_first == CIRCLEQ_END(head)) \ + (head)->cqh_first = (elm); \ + else \ + (head)->cqh_last->field.cqe_next = (elm); \ + (head)->cqh_last = (elm); \ +} while (0) + +#define CIRCLEQ_REMOVE(head, elm, field) do { \ + if ((elm)->field.cqe_next == CIRCLEQ_END(head)) \ + (head)->cqh_last = (elm)->field.cqe_prev; \ + else \ + (elm)->field.cqe_next->field.cqe_prev = \ + (elm)->field.cqe_prev; \ + if ((elm)->field.cqe_prev == CIRCLEQ_END(head)) \ + (head)->cqh_first = (elm)->field.cqe_next; \ + else \ + (elm)->field.cqe_prev->field.cqe_next = \ + (elm)->field.cqe_next; \ + _Q_INVALIDATE((elm)->field.cqe_prev); \ + _Q_INVALIDATE((elm)->field.cqe_next); \ +} while (0) + +#define CIRCLEQ_REPLACE(head, elm, elm2, field) do { \ + if (((elm2)->field.cqe_next = (elm)->field.cqe_next) == \ + CIRCLEQ_END(head)) \ + (head)->cqh_last = (elm2); \ + else \ + (elm2)->field.cqe_next->field.cqe_prev = (elm2); \ + if (((elm2)->field.cqe_prev = (elm)->field.cqe_prev) == \ + CIRCLEQ_END(head)) \ + (head)->cqh_first = (elm2); \ + else \ + (elm2)->field.cqe_prev->field.cqe_next = (elm2); \ + _Q_INVALIDATE((elm)->field.cqe_prev); \ + _Q_INVALIDATE((elm)->field.cqe_next); \ +} while (0) + +#endif /* !_SYS_QUEUE_H_ */ diff --git a/i3bar/include/util.h b/i3bar/include/util.h new file mode 100644 index 00000000..2e55e114 --- /dev/null +++ b/i3bar/include/util.h @@ -0,0 +1,22 @@ +#ifndef UTIL_H_ +#define UTIL_H_ + +/* Securely free p */ +#define FREE(p) do { \ + if (p != NULL) { \ + free(p); \ + p = NULL; \ + } \ +} while (0) + +/* Securely fee single-linked list */ +#define FREE_LIST(l, type) do { \ + type* FREE_LIST_TMP; \ + while (l != NULL) { \ + FREE_LIST_TMP = l; \ + free(l); \ + l = l->next; \ + } \ +} while (0) + +#endif diff --git a/i3bar/include/workspaces.h b/i3bar/include/workspaces.h new file mode 100644 index 00000000..ad6ac787 --- /dev/null +++ b/i3bar/include/workspaces.h @@ -0,0 +1,26 @@ +#ifndef WORKSPACES_H_ +#define WORKSPACES_H_ + +#include "common.h" +#include "outputs.h" + +typedef struct i3_ws_t i3_ws; + +i3_ws* workspaces; + +void refresh_workspaces(); +void free_workspaces(); + +struct i3_ws_t { + int num; + char* name; + bool visible; + bool focused; + bool urgent; + rect rect; + i3_output* output; + + i3_ws* next; +}; + +#endif diff --git a/i3bar/include/xcb.h b/i3bar/include/xcb.h new file mode 100644 index 00000000..69c53b83 --- /dev/null +++ b/i3bar/include/xcb.h @@ -0,0 +1,24 @@ +#ifndef XCB_H_ +#define XCB_H_ + +#include + +#define NUM_ATOMS 3 + +enum { + #define ATOM_DO(name) name, + #include "xcb_atoms.def" +}; + +xcb_atom_t atoms[NUM_ATOMS]; + +xcb_connection_t* xcb_connection; +xcb_screen_t* xcb_screens; +xcb_window_t xcb_root; + +void init_xcb(); +void clean_xcb(); +void get_atoms(); +void create_windows(); + +#endif diff --git a/i3bar/include/xcb_atoms.def b/i3bar/include/xcb_atoms.def new file mode 100644 index 00000000..5084231c --- /dev/null +++ b/i3bar/include/xcb_atoms.def @@ -0,0 +1,4 @@ +ATOM_DO(ATOM) +ATOM_DO(_NET_WM_WINDOW_TYPE) +ATOM_DO(_NET_WM_WINDOW_TYPE_DOCK) +#undef ATOM_DO diff --git a/i3bar/src/ipc.c b/i3bar/src/ipc.c new file mode 100644 index 00000000..35d3d5be --- /dev/null +++ b/i3bar/src/ipc.c @@ -0,0 +1,308 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#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); +} diff --git a/i3bar/src/main.c b/i3bar/src/main.c new file mode 100644 index 00000000..90a48ee2 --- /dev/null +++ b/i3bar/src/main.c @@ -0,0 +1,29 @@ +#include "ipc.h" +#include "outputs.h" +#include "workspaces.h" +#include "common.h" +#include "xcb.h" + +#include +#include +#include +#include + +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; +} diff --git a/i3bar/src/outputs.c b/i3bar/src/outputs.c new file mode 100644 index 00000000..2b2600b5 --- /dev/null +++ b/i3bar/src/outputs.c @@ -0,0 +1,225 @@ +#include +#include +#include + +#include + +#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; +} diff --git a/i3bar/src/workspaces.c b/i3bar/src/workspaces.c new file mode 100644 index 00000000..15927631 --- /dev/null +++ b/i3bar/src/workspaces.c @@ -0,0 +1,229 @@ +#include +#include +#include + +#include + +#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); + } +} diff --git a/i3bar/src/xcb.c b/i3bar/src/xcb.c new file mode 100644 index 00000000..d23d114b --- /dev/null +++ b/i3bar/src/xcb.c @@ -0,0 +1,125 @@ +#include +#include +#include +#include + +#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