sgl

annotate src/wsys.c @ 12:bf34fa677960

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