sgl

view src/wsys.c @ 34:9841c90ec769

added some comments in the header file
author John Tsiombikas <nuclear@mutantstargoat.com>
date Mon, 25 Jun 2012 00:39:46 +0300
parents edbfc96fe80d
children
line source
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include "wsys.h"
5 #include "log.h"
7 static struct wsys_module *merge(struct wsys_module *list1, struct wsys_module *list2);
8 static struct wsys_module *msort(struct wsys_module *list, int count);
11 struct wsys_module *wslist, *sel;
12 int wscount;
14 int sgl_register_module(struct wsys_module *ws)
15 {
16 ws->next = wslist;
17 wslist = ws;
18 wscount++;
19 return 0;
20 }
22 void sgl_sort_modules(void)
23 {
24 wslist = msort(wslist, wscount);
25 }
27 void sgl_print_modules(void)
28 {
29 struct wsys_module *ws = wslist;
31 sgl_log("window system modules:\n");
32 while(ws) {
33 sgl_log("- %s\n", ws->name);
34 ws = ws->next;
35 }
36 }
38 struct wsys_module *sgl_wsys_module(void)
39 {
40 if(!sel) {
41 char *modname;
42 if((modname = getenv("SGL_MODULE"))) {
43 struct wsys_module *ws = wslist;
44 while(ws) {
45 if(strcmp(ws->name, modname) == 0) {
46 sel = ws;
47 break;
48 }
49 ws = ws->next;
50 }
51 if(!sel) {
52 sel = wslist;
53 }
54 } else {
55 sel = wslist;
56 }
57 }
58 return sel;
59 }
61 #define APPEND(node) \
62 do { \
63 if(!res) { \
64 res = tail = node; \
65 } else { \
66 tail->next = (node); \
67 tail = (node); \
68 } \
69 (node) = (node)->next; \
70 tail->next = 0; \
71 } while(0)
73 static struct wsys_module *merge(struct wsys_module *list1, struct wsys_module *list2)
74 {
75 struct wsys_module *res, *tail;
77 res = tail = 0;
79 while(list1 || list2) {
80 if(list1 && list2) {
81 if(list1->prio <= list2->prio) {
82 APPEND(list1);
83 } else {
84 APPEND(list2);
85 }
86 } else if(list1) {
87 APPEND(list1);
88 } else {
89 APPEND(list2);
90 }
91 }
92 return res;
93 }
95 static struct wsys_module *msort(struct wsys_module *list, int count)
96 {
97 int i, mid = count / 2;
98 struct wsys_module *prev, *list2 = list;
100 if(!list->next) {
101 return list;
102 }
103 for(i=0; i<mid; i++) {
104 prev = list2;
105 list2 = list2->next;
106 }
107 prev->next = 0;
109 list = msort(list, mid);
110 list2 = msort(list2, count - mid);
111 return merge(list, list2);
112 }