refactored asunder and added build scripts
[stereo.git] / Asunder / src / webserver / dacp.js
blob344ef1439b3ac3fead942ffa8793da5674ec4d7a
1 function Node(text) {
2         
3         var parseNum = function (index, len) {
4                 if (len > 4) return parseBigNum(index, len);
5                 
6                 var num = text.charCodeAt(index) & 255;
7                 for (var i = 1; i < len; i++) {
8                         num = num * 256;
9                         num += text.charCodeAt(index + i) & 255;
10                 }
11                 return num;
12         };
13         
14         var parseBigNum = function (index, len) {
15                 var num = new BigNumber(text.charCodeAt(index) & 255);
16                 for (var i = 1; i < len; i++) {
17                         num = num.multiply(256);
18                         num = num.add(text.charCodeAt(index + i) & 255);
19                 }
20                 return num;
21         };
22         
23         this.name = function (index) {
24                 return text.substring(index, index+4);
25         };
26         this.length = function (index) {
27                 return parseNum(index+4, 4);
28         };
29         this.byteVal = function (index) {
30                 return parseNum(index+8, 1);
31         };
32         this.shortVal = function (index) {
33                 return parseNum(index+8, 2);
34         };
35         this.intVal = function (index) {
36                 return parseNum(index+8, 4);
37         };
38         this.longVal = function (index) {
39                 return parseNum(index+8, 8);
40         };
41         this.longlongVal = function (index) {
42                 return {
43                         0:      parseNum(index+8, 4),
44                         1:      parseNum(index+12, 4),
45                         2:      parseNum(index+16, 4),
46                         3:      parseNum(index+20, 4)
47                 };
48         };
49         this.stringVal = function (index) {
50                 var length = this.length(index);
51                 var string = "";
52                 for ( var i = 8; i < 8+length;) {
53                         var c = text.charCodeAt(index + i) & 255;
54                         var c2,c3;
55                         if (c < 128) {
56                                 string += String.fromCharCode(c);
57                                 i++;
58                         } else if (c > 191 && c < 224) {
59                                 c2 = text.charCodeAt(index + i + 1) & 255;
60                                 string += String.fromCharCode(((c & 31)) << 6 | (c2 & 63));
61                                 i += 2;
62                         } else {
63                                 c2 = text.charCodeAt(index + i + 1) & 255;
64                                 c3 = text.charCodeAt(index + i + 2) & 255;
65                                 string += String.fromCharCode(((c & 15)) << 12 | (c2 & 63) << 6
66                                                 | (c3 & 63));
67                                 i += 3;
68                         }
69                 }
70                 return string;
71         };
72         this.dateVal = function (index) {
73                 return parseNum(index+8, 8);
74         };
75         this.versionVal = function (index) {
76                 var version = text.charCodeAt(index+8) & 255;
77                 for (i in 1, 2, 3) {
78                         version += ".";
79                         version += text.charCodeAt(index+8 + i) & 255;
80                 }
81                 return version;
82         };
83         this.children = function (index) {
84                 var kids = new Array();
85                 var len = index + 8 + this.length(index);
86                 var i = index+8;
87                 while (i < len) {
88                         kids.push(i);
89                         i += 8 + this.length(i);
90                 }
91                 return kids;
92         };
95 function PlayStatusUpdate(tree, index) {
97         var kids = tree.children(index);
99         this.album = "";
100         this.title = "";
101         this.artist = "";
102         this.genre = "";
103         this.id = 0;
104         this.status = 0;
105         this.revision = 0;
106         this.container = 0;
107         
108         for (child in kids) {
109                 var node = kids[child];
110                 switch (tree.name(node)) {
111                 case "caps":
112                         this.status = tree.byteVal(node);
113                         break;
114                 case "cmsr":
115                         this.revision = tree.intVal(node);
116                         break;
117                 case "cann":
118                         this.title = tree.stringVal(node);
119                         break;
120                 case "cana":
121                         this.artist = tree.stringVal(node);
122                         break;
123                 case "canl":
124                         this.album = tree.stringVal(node);
125                         break;
126                 case "cang":
127                         this.genre = tree.stringVal(node);
128                         break;
129                 case "canp":
130                         this.container = tree.longlongVal(node)[1];
131                         this.id = tree.longlongVal(node)[3];
132                         break;
133                 }
134         }
137 function findNode(tree, index, tag) {
138         var kids = tree.children(index);
139         for (child in kids) {
140                 if (tree.name(kids[child]) == tag) {
141                         return kids[child];
142                 }
143         }
144         return 0;
147 function getItems(tree, index, handler) {
148         
149         var items = new Array();
150         
151         var kids = tree.children(index);
152         for (child in kids) {
153                 items.push(handler(tree, kids[child]));
154         }
155         
156         return items;
159 function Container(tree, index) {
160         
161         this.id = 0;
162         this.persistent = 0;
163         this.name = "";
164         this.base = false;
165         this.parent = 0;
166         this.items = 0;
168         var kids = tree.children(index);
169         for (child in kids) {
170                 var node = kids[child];
171                 switch (tree.name(node)) {
172                 case "miid":
173                         this.id = tree.intVal(node);
174                         break;
175                 case "mper":
176                         this.persistent = tree.longVal(node);
177                         break;
178                 case "minm":
179                         this.name = tree.stringVal(node);
180                         break;
181                 case "abpl":
182                         this.base = tree.byteVal(node);
183                         break;
184                 case "mpco":
185                         this.parent = tree.intVal(node);
186                         break;
187                 case "mimc":
188                         this.items = tree.intVal(node);
189                         break;
190                 }
191         }
194 function Playlist(tree, index) {
196         this.tracks = new Array();
198         var kids = tree.children(index);
199         var list = 0;
200         for (child in kids) {
201                 if (tree.name(kids[child]) == "mlcl") {
202                         list = kids[child];
203                         break;
204                 }
205         }
207         if (!list) return;
208         
209         kids = tree.children(list);
210         for (child in kids) {
211                 this.tracks.push(new Track(tree, kids[child]));
212         }
215 function Album(tree, index) {
217         this.id = 0;
218         this.name = "";
219         this.artist = 0;
220         this.persistantId = 0;
222         var kids = tree.children(index);
223         for (child in kids) {
224                 var node = kids[child];
225                 switch (tree.name(node)) {
226                 case "miid":
227                         this.id = tree.intVal(node);
228                         break;
229                 case "minm":
230                         this.name = tree.stringVal(node);
231                         break;
232                 case "asaa":
233                         this.artist = tree.stringVal(node);
234                         break;
235                 case "mper":
236                         this.persistantId = tree.longVal(node);
237                         break;
238                 }
239         }
242 function Track(tree, index) {
244         this.album = "";
245         this.title = "";
246         this.artist = "";
247         this.genre = "";
248         this.id = 0;
249         this.persistent = 0;
250         
251         var kids = tree.children(index);
252         for (child in kids) {
253                 var node = kids[child];
254                 switch (tree.name(node)) {
255                 case "asal":
256                         this.album = tree.stringVal(node);
257                         break;
258                 case "minm":
259                         this.title = tree.stringVal(node);
260                         break;
261                 case "asar":
262                         this.artist = tree.stringVal(node);
263                         break;
264                 case "asag":
265                         this.genre = tree.stringVal(node);
266                         break;
267                 case "miid":
268                         this.id = tree.intVal(node);
269                         break;
270                 case "mper":
271                         this.persistent = tree.longVal(node);
272                         break;
273                 }
274         }
277 function sysout(text) {
278         var p = document.createElement("PRE");
279         p.className = "debug";
280         p.appendChild(document.createTextNode(text));
281         document.body.appendChild(p);