Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…3-fe4c-0410-82e9-b9c6d0c95a22
  • Loading branch information
jordansissel committed Dec 22, 2007
1 parent 53991da commit 87d267e
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 41 deletions.
8 changes: 7 additions & 1 deletion wmlib/Makefile
@@ -1,14 +1,20 @@
CFLAGS=`pkg-config --cflags x11 2> /dev/null || echo -I/usr/X11R6/include -I/usr/local/include`
LDFLAGS=`pkg-config --libs x11 2> /dev/null || echo -L/usr/X11R6/lib -L/usr/local/lib -lX11`

LDFLAGS+=-ldb
CFLAGS+=-I/usr/local/include/db45
LDFLAGS+=-L/usr/local/lib/db45 -ldb
CFLAGS+=-fPIC
all: wmlib.so

clean:
rm *.o || true

wmlib.c: wmlib.h
event_list.c: event_list.h

wmlib.a: wmlib.o event_list.o
gcc $(LDFLAGS) wmlib.o event_list.o -o wmlib.a


%.o: %.c
gcc $(CFLAGS) -c -o $@ $<
Expand Down
27 changes: 27 additions & 0 deletions wmlib/event_list.c
@@ -0,0 +1,27 @@
#include "event_list.h"

/* Note: item_t is the wm_event_handler */

list_t *list_new() {
list_t *list;
list = malloc(sizeof(list_t));

list->max_items = 10;
list->nitems = 0;
list->items = malloc(list->max_items * sizeof(item_t));
}

void list_free(list_t *list) {
free(list->items);
free(list);
}

void list_append(list_t *list, item_t item) {
list->items[list->nitems] = item;

list->nitems++;
if (list->nitems == list->max_items) {
list->max_items *= 2;
list->items = (item_t *)realloc(list->items, list->max_items * sizeof(item_t));
}
}
16 changes: 16 additions & 0 deletions wmlib/event_list.h
@@ -0,0 +1,16 @@
#ifndef EVENT_LIST_H
#define EVENT_LIST_H

typedef void* item_t;

typedef struct list {
item_t *items;
int nitems;
int max_items;
} list_t;

list_t* list_new();
void list_free(list_t *list);
void list_append(list_t *list, item_t item);

#endif /* EVENT_LIST_H */
53 changes: 18 additions & 35 deletions wmlib/wmlib.c
Expand Up @@ -95,9 +95,9 @@ void wm_x_init_handlers(wm_t *wm) {
wm->x_event_handlers[UnmapNotify] = wm_event_unmapnotify;
wm->x_event_handlers[DestroyNotify] = wm_event_destroynotify;

db_create(&wm->evdb, NULL, 0);
wm->evdb->set_flags(wm->evdb, DB_DUP);
wm->evdb->open(wm->evdb, NULL, NULL, NULL, DB_HASH, DB_CREATE, 0);
wm->listeners = list_new();
for (i = WM_EVENT_MIN; i < WM_EVENT_MAX; i++)
list_append(wm->listeners, list_new());
}

void wm_x_init_windows(wm_t *wm) {
Expand Down Expand Up @@ -214,7 +214,7 @@ void wm_event_maprequest(wm_t *wm, XEvent *ev) {

{ // Call handlers
wm_event_t wmev;
wmev.event_name = WM_EVENT_MAPREQUEST;
wmev.event_id = WM_EVENT_MAPREQUEST;
wmev.window = mrev.window;
wm_listener_call(wm, &wmev);
}
Expand Down Expand Up @@ -259,43 +259,26 @@ void wm_event_unknown(wm_t *wm, XEvent *ev) {
wm_log(wm, LOG_INFO, "%s: Unknown event type '%d'", __func__, ev->type);
}

void wm_listener_add(wm_t *wm, char *event_name, wm_event_handler *callback) {
DBT key, value;
memset(&key, 0, sizeof(DBT));
memset(&value, 0, sizeof(DBT));
void wm_listener_add(wm_t *wm, wm_event_id event, wm_event_handler callback) {
wm_event_handler callback_copy;

wm_log(wm, LOG_INFO, "Adding listener for event '%s': %016tx",
event_name, callback);
wm_log(wm, LOG_INFO, "Adding listener for event %d: %016tx",
event, callback);

key.data = event_name;
key.size = strlen(event_name) + 1;
value.data = callback;
value.size = sizeof(wm_event_handler*);

wm->evdb->put(wm->evdb, NULL, &key, &value, 0);
list_append(wm->listeners, callback);
}

void wm_listener_call(wm_t *wm, wm_event_t *event) {
DBT key, value;
DBC *cursor;
int flags;
int ret;
memset(&key, 0, sizeof(DBT));
memset(&value, 0, sizeof(DBT));

wm_log(wm, LOG_INFO, "Calling all listeners for event '%s'", event->event_name);

key.data = event->event_name;
key.size = strlen(event->event_name) + 1;
wm->evdb->cursor(wm->evdb, NULL, &cursor, 0);
//flags = DB_SET;
flags = DB_FIRST;
while ((ret = cursor->c_get(cursor, &key, &value, flags)) == 0) {
wm_event_handler *callback = NULL;
callback = *(wm_event_handler**)value.data;
wm_log(wm, LOG_INFO, "Calling func %016tx", value.data);
int i = 0;

wm_log(wm, LOG_INFO, "Calling all listeners for event %d", event->event_id);

list_t *callbacks = wm->listeners->items[event->event_id];

for (i = 0; i < callbacks->nitems; i++) {
wm_event_handler callback = callbacks->items[i];
wm_log(wm, LOG_INFO, "Calling func %016tx", callback);
//callback(wm, event);
flags = DB_NEXT; //_DUP;
}
}

Expand Down
19 changes: 14 additions & 5 deletions wmlib/wmlib.h
@@ -1,10 +1,16 @@

#ifndef WMLIB_H
#define WMLIB_H

#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/keysym.h>
#include <X11/extensions/shape.h>

#include <db.h>

#include "event_list.h"

#define LOG_FATAL 0
#define LOG_ERROR 1
#define LOG_WARN 2
Expand All @@ -20,22 +26,24 @@ struct wm {
int log_level;
Screen **screens;
x_event_handler *x_event_handlers;
DB *evdb;
list_t *listeners;
};

typedef unsigned int wm_event_id;
typedef struct wm_event {
char *event_name;
wm_event_id event_id;
Window window;
int x;
int y;
int width;
int height;
} wm_event_t;

typedef Bool (wm_event_handler)(wm_t *wm, wm_event_t *event);
typedef Bool (*wm_event_handler)(wm_t *wm, wm_event_t *event);

#define WM_EVENT_MAPREQUEST "maprequest"
#define WM_EVENT_MIN 2U
#define WM_EVENT_MAPREQUEST 1U
#define WM_EVENT_MAX 2U

/* XXX: Check if we have __FUNCTION__ */
#define __func__ __FUNCTION__
Expand Down Expand Up @@ -70,6 +78,7 @@ void wm_event_destroynotify(wm_t *wm, XEvent *ev);
void wm_event_unknown(wm_t *wm, XEvent *ev);

#define EVENT_WINDOW_ADD 1
void wm_listener_add(wm_t *wm, char *event_name, wm_event_handler callback);
void wm_listener_add(wm_t *wm, wm_event_id event, wm_event_handler callback);
void wm_listener_call(wm_t *wm, wm_event_t *event);

#endif /* WMLIB_H */

0 comments on commit 87d267e

Please sign in to comment.