Migrate to queue.h

This commit is contained in:
Axel Wagner 2010-07-30 03:11:54 +02:00
parent 654b51fef1
commit 43c057f19a
8 changed files with 96 additions and 98 deletions

View File

@ -1,18 +1,21 @@
#ifndef OUTPUTS_H_ #ifndef OUTPUTS_H_
#define OUTPUTS_H_ #define OUTPUTS_H_
#include "common.h"
#include <xcb/xcb.h> #include <xcb/xcb.h>
typedef struct i3_output_t i3_output; #include "common.h"
#include "workspaces.h"
i3_output* outputs; typedef struct i3_output i3_output;
SLIST_HEAD(outputs_head, i3_output);
struct outputs_head *outputs;
void parse_outputs_json(char* json); void parse_outputs_json(char* json);
void free_outputs(); void free_outputs();
i3_output* get_output_by_name(char* name); i3_output* get_output_by_name(char* name);
struct i3_output_t { struct i3_output {
char* name; char* name;
bool active; bool active;
int ws; int ws;
@ -21,7 +24,9 @@ struct i3_output_t {
xcb_window_t bar; xcb_window_t bar;
xcb_gcontext_t bargc; xcb_gcontext_t bargc;
i3_output* next; struct ws_head *workspaces;
SLIST_ENTRY(i3_output) slist;
}; };
#endif #endif

View File

@ -1,6 +1,8 @@
#ifndef UTIL_H_ #ifndef UTIL_H_
#define UTIL_H_ #define UTIL_H_
#include "queue.h"
/* Securely free p */ /* Securely free p */
#define FREE(p) do { \ #define FREE(p) do { \
if (p != NULL) { \ if (p != NULL) { \
@ -10,13 +12,23 @@
} while (0) } while (0)
/* Securely fee single-linked list */ /* Securely fee single-linked list */
#define FREE_LIST(l, type) do { \ #define FREE_SLIST(l, type) do { \
type* FREE_LIST_TMP; \ type *walk = SLIST_FIRST(l); \
while (l != NULL) { \ while (!SLIST_EMPTY(l)) { \
FREE_LIST_TMP = l; \ SLIST_REMOVE_HEAD(l, slist); \
free(l); \ FREE(walk); \
l = l->next; \ walk = SLIST_FIRST(l); \
} \ } \
} while (0) } while (0)
#endif #endif
/* Securely fee tail-queues */
#define FREE_TAILQ(l, type) do { \
type *walk = TAILQ_FIRST(l); \
while (!TAILQ_EMPTY(l)) { \
TAILQ_REMOVE(l, TAILQ_FIRST(l), tailq); \
FREE(walk); \
walk = TAILQ_FIRST(l); \
} \
} while (0)

View File

@ -4,14 +4,14 @@
#include "common.h" #include "common.h"
#include "outputs.h" #include "outputs.h"
typedef struct i3_ws_t i3_ws; typedef struct i3_ws i3_ws;
i3_ws* workspaces; TAILQ_HEAD(ws_head, i3_ws);
void parse_workspaces_json(); void parse_workspaces_json();
void free_workspaces(); void free_workspaces();
struct i3_ws_t { struct i3_ws {
int num; int num;
char *name; char *name;
int name_width; int name_width;
@ -19,9 +19,9 @@ struct i3_ws_t {
bool focused; bool focused;
bool urgent; bool urgent;
rect rect; rect rect;
i3_output* output; struct i3_output *output;
i3_ws* next; TAILQ_ENTRY(i3_ws) tailq;
}; };
#endif #endif

View File

@ -25,5 +25,6 @@ void destroy_windows();
void create_windows(); void create_windows();
void draw_buttons(); void draw_buttons();
int get_string_width(char *string); int get_string_width(char *string);
void handle_xcb_event(xcb_generic_event_t *event);
#endif #endif

View File

@ -59,8 +59,9 @@ int main(int argc, char **argv) {
ev_default_destroy(); ev_default_destroy();
clean_xcb(); clean_xcb();
free_outputs();
free_workspaces(); free_workspaces();
FREE_SLIST(outputs, i3_output);
return 0; return 0;
} }

View File

@ -10,7 +10,7 @@
#include "ipc.h" #include "ipc.h"
struct outputs_json_params { struct outputs_json_params {
i3_output* outputs; struct outputs_head *outputs;
i3_output *outputs_walk; i3_output *outputs_walk;
char* cur_key; char* cur_key;
char* json; char* json;
@ -103,16 +103,15 @@ static int outputs_start_map_cb(void* params_) {
new_output->name = NULL; new_output->name = NULL;
new_output->ws = 0, new_output->ws = 0,
memset(&new_output->rect, 0, sizeof(rect)); memset(&new_output->rect, 0, sizeof(rect));
new_output->next = NULL;
new_output->bar = XCB_NONE; new_output->bar = XCB_NONE;
if (params->outputs == NULL) { new_output->workspaces = malloc(sizeof(struct ws_head));
params->outputs = new_output; TAILQ_INIT(new_output->workspaces);
} else {
params->outputs_walk->next = new_output; SLIST_INSERT_HEAD(params->outputs, new_output, slist);
}
params->outputs_walk = SLIST_FIRST(params->outputs);
params->outputs_walk = new_output;
return 1; return 1;
} }
@ -148,7 +147,10 @@ void parse_outputs_json(char* json) {
/* FIXME: Fasciliate stream-processing, i.e. allow starting to interpret /* FIXME: Fasciliate stream-processing, i.e. allow starting to interpret
* JSON in chunks */ * JSON in chunks */
struct outputs_json_params params; struct outputs_json_params params;
params.outputs = NULL; printf(json);
params.outputs = malloc(sizeof(struct outputs_head));
SLIST_INIT(params.outputs);
params.outputs_walk = NULL; params.outputs_walk = NULL;
params.cur_key = NULL; params.cur_key = NULL;
params.json = json; params.json = json;
@ -175,29 +177,16 @@ void parse_outputs_json(char* json) {
yajl_free(handle); yajl_free(handle);
free_outputs(); if (outputs != NULL) {
FREE_SLIST(outputs, i3_output);
}
outputs = params.outputs; outputs = params.outputs;
} }
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) { i3_output* get_output_by_name(char* name) {
if (outputs == NULL) {
i3_send_msg(I3_IPC_MESSAGE_TYPE_GET_OUTPUTS, NULL);
return NULL;
}
i3_output *walk; i3_output *walk;
SLIST_FOREACH(walk, outputs, slist) {
for (walk = outputs; walk != NULL; walk = walk->next) {
if (!strcmp(walk->name, name)) { if (!strcmp(walk->name, name)) {
break; break;
} }

View File

@ -10,7 +10,7 @@
#include "ipc.h" #include "ipc.h"
struct workspaces_json_params { struct workspaces_json_params {
i3_ws* workspaces; struct ws_head *workspaces;
i3_ws *workspaces_walk; i3_ws *workspaces_walk;
char *cur_key; char *cur_key;
char *json; char *json;
@ -117,8 +117,12 @@ static int workspaces_string_cb(void* params_, const unsigned char* val, unsigne
strncpy(output_name, (const char*) val, len); strncpy(output_name, (const char*) val, len);
output_name[len] = '\0'; output_name[len] = '\0';
params->workspaces_walk->output = get_output_by_name(output_name); params->workspaces_walk->output = get_output_by_name(output_name);
free(output_name);
TAILQ_INSERT_TAIL(params->workspaces_walk->output->workspaces,
params->workspaces_walk,
tailq);
free(output_name);
return 1; return 1;
} }
@ -127,6 +131,7 @@ static int workspaces_string_cb(void* params_, const unsigned char* val, unsigne
static int workspaces_start_map_cb(void* params_) { static int workspaces_start_map_cb(void* params_) {
struct workspaces_json_params* params = (struct workspaces_json_params*) params_; struct workspaces_json_params* params = (struct workspaces_json_params*) params_;
i3_ws *new_workspace = NULL; i3_ws *new_workspace = NULL;
if (params->cur_key == NULL) { if (params->cur_key == NULL) {
@ -138,13 +143,6 @@ static int workspaces_start_map_cb(void* params_) {
new_workspace->urgent = 0; new_workspace->urgent = 0;
memset(&new_workspace->rect, 0, sizeof(rect)); memset(&new_workspace->rect, 0, sizeof(rect));
new_workspace->output = NULL; 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; params->workspaces_walk = new_workspace;
return 1; return 1;
@ -186,7 +184,9 @@ void parse_workspaces_json(char* json) {
/* FIXME: Fasciliate stream-processing, i.e. allow starting to interpret /* FIXME: Fasciliate stream-processing, i.e. allow starting to interpret
* JSON in chunks */ * JSON in chunks */
struct workspaces_json_params params; struct workspaces_json_params params;
params.workspaces = NULL;
free_workspaces();
params.workspaces_walk = NULL; params.workspaces_walk = NULL;
params.cur_key = NULL; params.cur_key = NULL;
params.json = json; params.json = json;
@ -213,18 +213,14 @@ void parse_workspaces_json(char* json) {
yajl_free(handle); yajl_free(handle);
free_workspaces();
workspaces = params.workspaces;
FREE(params.cur_key); FREE(params.cur_key);
} }
void free_workspaces() { void free_workspaces() {
i3_ws* tmp; i3_output *outputs_walk;
while (workspaces != NULL) { SLIST_FOREACH(outputs_walk, outputs, slist) {
tmp = workspaces; if (outputs_walk->workspaces != NULL && !TAILQ_EMPTY(outputs_walk->workspaces)) {
workspaces = workspaces->next; FREE_TAILQ(outputs_walk->workspaces, i3_ws);
FREE(tmp->name); }
FREE(tmp);
} }
} }

View File

@ -19,7 +19,7 @@ uint32_t get_colorpixel(const char *s) {
return (r << 16 | g << 8 | b); return (r << 16 | g << 8 | b);
} }
void handle_xcb_event(xcb_generic_event_t ev) { void handle_xcb_event(xcb_generic_event_t *event) {
switch (event->response_type & ~0x80) { switch (event->response_type & ~0x80) {
case XCB_EXPOSE: case XCB_EXPOSE:
draw_buttons(); draw_buttons();
@ -99,8 +99,11 @@ void get_atoms() {
} }
void destroy_windows() { void destroy_windows() {
i3_output *walk = outputs; i3_output *walk;
while(walk != NULL) { if (outputs == NULL) {
return;
}
SLIST_FOREACH(walk, outputs, slist) {
if (walk->bar == XCB_NONE) { if (walk->bar == XCB_NONE) {
continue; continue;
} }
@ -113,10 +116,9 @@ void create_windows() {
uint32_t mask; uint32_t mask;
uint32_t values[2]; uint32_t values[2];
i3_output* walk = outputs; i3_output *walk;
while (walk != NULL) { SLIST_FOREACH(walk, outputs, slist) {
if (!walk->active) { if (!walk->active) {
walk = walk->next;
continue; continue;
} }
printf("Creating Window for output %s\n", walk->name); printf("Creating Window for output %s\n", walk->name);
@ -156,19 +158,17 @@ void create_windows() {
values); values);
xcb_map_window(xcb_connection, walk->bar); xcb_map_window(xcb_connection, walk->bar);
walk = walk->next;
} }
xcb_flush(xcb_connection); xcb_flush(xcb_connection);
} }
void draw_buttons() { void draw_buttons() {
printf("Drawing Buttons...\n"); printf("Drawing Buttons...\n");
i3_output *outputs_walk = outputs;
int i = 0; int i = 0;
while (outputs_walk != NULL) { i3_output *outputs_walk;
SLIST_FOREACH(outputs_walk, outputs, slist) {
if (!outputs_walk->active) { if (!outputs_walk->active) {
printf("Output %s inactive, skipping...\n", outputs_walk->name); printf("Output %s inactive, skipping...\n", outputs_walk->name);
outputs_walk = outputs_walk->next;
continue; continue;
} }
if (outputs_walk->bar == XCB_NONE) { if (outputs_walk->bar == XCB_NONE) {
@ -185,19 +185,15 @@ void draw_buttons() {
outputs_walk->bargc, outputs_walk->bargc,
1, 1,
&rect); &rect);
i3_ws *ws_walk = workspaces; i3_ws *ws_walk;
while (ws_walk != NULL) { TAILQ_FOREACH(ws_walk, outputs_walk->workspaces, tailq) {
if (ws_walk->output != outputs_walk) {
printf("WS %s on wrong output, skipping...\n", ws_walk->name);
ws_walk = ws_walk->next;
continue;
}
printf("Drawing Button for WS %s...\n", ws_walk->name); printf("Drawing Button for WS %s...\n", ws_walk->name);
uint32_t color = get_colorpixel("240000"); uint32_t color = get_colorpixel("240000");
if (ws_walk->visible) { if (ws_walk->visible) {
color = get_colorpixel("480000"); color = get_colorpixel("480000");
} }
if (ws_walk->urgent) { if (ws_walk->urgent) {
printf("WS %s is urgent!\n", ws_walk->name);
color = get_colorpixel("002400"); color = get_colorpixel("002400");
} }
xcb_change_gc(xcb_connection, xcb_change_gc(xcb_connection,
@ -226,9 +222,7 @@ void draw_buttons() {
i + 5, font_height + 1, i + 5, font_height + 1,
ws_walk->name); ws_walk->name);
i += 10 + ws_walk->name_width; i += 10 + ws_walk->name_width;
ws_walk = ws_walk->next;
} }
outputs_walk = outputs_walk->next;
i = 0; i = 0;
} }
xcb_flush(xcb_connection); xcb_flush(xcb_connection);