sgl

view 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
line source
1 #include <stdio.h>
2 #include "wsys.h"
4 static struct wsys_module *merge(struct wsys_module *list1, struct wsys_module *list2);
5 static struct wsys_module *msort(struct wsys_module *list, int count);
8 struct wsys_module *wslist;
9 int wscount;
11 int sgl_register_module(struct wsys_module *ws)
12 {
13 ws->next = wslist;
14 wslist = ws;
15 wscount++;
16 return 0;
17 }
19 void sgl_sort_modules(void)
20 {
21 wslist = msort(wslist, wscount);
22 }
24 struct wsys_module *sgl_wsys_module(void)
25 {
26 return wslist;
27 }
29 #define APPEND(node) \
30 do { \
31 if(!res) { \
32 res = tail = node; \
33 } else { \
34 tail->next = (node); \
35 tail = (node); \
36 } \
37 (node) = (node)->next; \
38 tail->next = 0; \
39 } while(0)
41 static struct wsys_module *merge(struct wsys_module *list1, struct wsys_module *list2)
42 {
43 struct wsys_module *res, *tail;
45 res = tail = 0;
47 while(list1 || list2) {
48 if(list1 && list2) {
49 if(list1->prio <= list2->prio) {
50 APPEND(list1);
51 } else {
52 APPEND(list2);
53 }
54 } else if(list1) {
55 APPEND(list1);
56 } else {
57 APPEND(list2);
58 }
59 }
60 return res;
61 }
63 static struct wsys_module *msort(struct wsys_module *list, int count)
64 {
65 int i, mid = count / 2;
66 struct wsys_module *prev, *list2 = list;
68 if(!list->next) {
69 return list;
70 }
71 for(i=0; i<mid; i++) {
72 prev = list2;
73 list2 = list2->next;
74 }
75 prev->next = 0;
77 list = msort(list, mid);
78 list2 = msort(list2, count - mid);
79 return merge(list, list2);
80 }