sgl

annotate src/wsys.c @ 3:1b6c5dadb460

started hacking the X11 module
author John Tsiombikas <nuclear@siggraph.org>
date Wed, 11 May 2011 09:09:43 +0300
parents 0c13a30be2c1
children edbfc96fe80d
rev   line source
nuclear@1 1 #include <stdio.h>
nuclear@1 2 #include "wsys.h"
nuclear@1 3
nuclear@3 4 static struct wsys_module *merge(struct wsys_module *list1, struct wsys_module *list2);
nuclear@3 5 static struct wsys_module *msort(struct wsys_module *list, int count);
nuclear@3 6
nuclear@3 7
nuclear@1 8 struct wsys_module *wslist;
nuclear@1 9 int wscount;
nuclear@1 10
nuclear@1 11 int sgl_register_module(struct wsys_module *ws)
nuclear@1 12 {
nuclear@1 13 ws->next = wslist;
nuclear@1 14 wslist = ws;
nuclear@1 15 wscount++;
nuclear@1 16 return 0;
nuclear@1 17 }
nuclear@1 18
nuclear@3 19 void sgl_sort_modules(void)
nuclear@3 20 {
nuclear@3 21 wslist = msort(wslist, wscount);
nuclear@3 22 }
nuclear@3 23
nuclear@3 24 struct wsys_module *sgl_wsys_module(void)
nuclear@3 25 {
nuclear@3 26 return wslist;
nuclear@3 27 }
nuclear@3 28
nuclear@1 29 #define APPEND(node) \
nuclear@1 30 do { \
nuclear@1 31 if(!res) { \
nuclear@1 32 res = tail = node; \
nuclear@1 33 } else { \
nuclear@1 34 tail->next = (node); \
nuclear@1 35 tail = (node); \
nuclear@1 36 } \
nuclear@1 37 (node) = (node)->next; \
nuclear@1 38 tail->next = 0; \
nuclear@1 39 } while(0)
nuclear@1 40
nuclear@1 41 static struct wsys_module *merge(struct wsys_module *list1, struct wsys_module *list2)
nuclear@1 42 {
nuclear@1 43 struct wsys_module *res, *tail;
nuclear@1 44
nuclear@1 45 res = tail = 0;
nuclear@1 46
nuclear@1 47 while(list1 || list2) {
nuclear@1 48 if(list1 && list2) {
nuclear@1 49 if(list1->prio <= list2->prio) {
nuclear@1 50 APPEND(list1);
nuclear@1 51 } else {
nuclear@1 52 APPEND(list2);
nuclear@1 53 }
nuclear@1 54 } else if(list1) {
nuclear@1 55 APPEND(list1);
nuclear@1 56 } else {
nuclear@1 57 APPEND(list2);
nuclear@1 58 }
nuclear@1 59 }
nuclear@1 60 return res;
nuclear@1 61 }
nuclear@1 62
nuclear@1 63 static struct wsys_module *msort(struct wsys_module *list, int count)
nuclear@1 64 {
nuclear@1 65 int i, mid = count / 2;
nuclear@1 66 struct wsys_module *prev, *list2 = list;
nuclear@1 67
nuclear@1 68 if(!list->next) {
nuclear@1 69 return list;
nuclear@1 70 }
nuclear@1 71 for(i=0; i<mid; i++) {
nuclear@1 72 prev = list2;
nuclear@1 73 list2 = list2->next;
nuclear@1 74 }
nuclear@1 75 prev->next = 0;
nuclear@1 76
nuclear@1 77 list = msort(list, mid);
nuclear@1 78 list2 = msort(list2, count - mid);
nuclear@1 79 return merge(list, list2);
nuclear@1 80 }