sgl

view src/wsys.c @ 26:e4c79d2c7f78

finishing win32 module
author John Tsiombikas <nuclear@member.fsf.org>
date Mon, 27 Jun 2011 12:19:14 +0300
parents 1b6c5dadb460
children 124195562f7e
line source
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include "wsys.h"
6 static struct wsys_module *merge(struct wsys_module *list1, struct wsys_module *list2);
7 static struct wsys_module *msort(struct wsys_module *list, int count);
10 struct wsys_module *wslist, *sel;
11 int wscount;
13 int sgl_register_module(struct wsys_module *ws)
14 {
15 ws->next = wslist;
16 wslist = ws;
17 wscount++;
18 return 0;
19 }
21 void sgl_sort_modules(void)
22 {
23 wslist = msort(wslist, wscount);
24 }
26 void sgl_print_modules(void)
27 {
28 struct wsys_module *ws = wslist;
30 printf("window system modules:\n");
31 while(ws) {
32 printf("- %s\n", ws->name);
33 ws = ws->next;
34 }
35 }
37 struct wsys_module *sgl_wsys_module(void)
38 {
39 if(!sel) {
40 char *modname;
41 if((modname = getenv("SGL_MODULE"))) {
42 struct wsys_module *ws = wslist;
43 while(ws) {
44 if(strcmp(ws->name, modname) == 0) {
45 sel = ws;
46 break;
47 }
48 ws = ws->next;
49 }
50 if(!sel) {
51 sel = wslist;
52 }
53 } else {
54 sel = wslist;
55 }
56 }
57 return sel;
58 }
60 #define APPEND(node) \
61 do { \
62 if(!res) { \
63 res = tail = node; \
64 } else { \
65 tail->next = (node); \
66 tail = (node); \
67 } \
68 (node) = (node)->next; \
69 tail->next = 0; \
70 } while(0)
72 static struct wsys_module *merge(struct wsys_module *list1, struct wsys_module *list2)
73 {
74 struct wsys_module *res, *tail;
76 res = tail = 0;
78 while(list1 || list2) {
79 if(list1 && list2) {
80 if(list1->prio <= list2->prio) {
81 APPEND(list1);
82 } else {
83 APPEND(list2);
84 }
85 } else if(list1) {
86 APPEND(list1);
87 } else {
88 APPEND(list2);
89 }
90 }
91 return res;
92 }
94 static struct wsys_module *msort(struct wsys_module *list, int count)
95 {
96 int i, mid = count / 2;
97 struct wsys_module *prev, *list2 = list;
99 if(!list->next) {
100 return list;
101 }
102 for(i=0; i<mid; i++) {
103 prev = list2;
104 list2 = list2->next;
105 }
106 prev->next = 0;
108 list = msort(list, mid);
109 list2 = msort(list2, count - mid);
110 return merge(list, list2);
111 }