sgl

annotate src/wsys.c @ 38:a7b96de14be8

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