var Iconv = require('iconv').Iconv;
var xsettings = require('x11-xsettings');

exports.decode = function(type, data) {
	var i;
	var result;
    var init = 0;
	switch (type) {
		case 'STRING':
            result = [];
            var converter = new Iconv('ISO-8859-1', 'UTF-8');
            for (i = 0; i < data.length; ++i) {
                if (data[i] === 0) {
                    result.push(converter.convert(data.slice(init, i)));
                    init = i + 1;
                }
            }

            if (init < data.length) {
                result.push(converter.convert(data.slice(init)));
            }
		break;

        case 'UTF8_STRING':
            result = [];
            for (i = 0; i < data.length; ++i) {
                if (data[i] === 0) {
                    result.push(data.toString('utf8', init, i));
                    init = i + 1;
                }
            }

            if (init < data.length) {
                result.push(data.toString('utf8', init));
            }
        break;

		case 'ATOM':
		case 'INTEGER':
		case 'CARDINAL':
        case 'WINDOW':
            result = [];
            for (i = 0; i < data.length; i += 4) {
               result.push(data.readUInt32LE(i));
            }

		break;

        case 'WM_STATE':
            if (data.length !== 8) {
                result = new Error('Invalid WM_STATE data. Length: ' + data.length);
            } else {
                result = {
                    state : data.readUInt32LE(0),
                    icon : data.readUInt32LE(4)
                };
            }
        break;

        case '_XSETTINGS_SETTINGS':
            result = xsettings.decode(data);
        break;

        default:
            result = new Error('Unsupported type: ' + type);
	}

    return result;
};

@@ -116,17 +116,23 @@
 ) %7B%0A
-%09
+    
 var i;%0A
-%09
+    
 var 
@@ -157,17 +157,20 @@
 it = 0;%0A
-%09
+    
 switch (
@@ -177,18 +177,24 @@
 type) %7B%0A
-%09%09
+        
 case 'ST
@@ -630,18 +630,24 @@
       %7D%0A
-%09%09
+        
 break;%0A%0A
@@ -1042,18 +1042,24 @@
 break;%0A%0A
-%09%09
+        
 case 'AT
@@ -1063,18 +1063,24 @@
 'ATOM':%0A
-%09%09
+        
 case 'IN
@@ -1087,17 +1087,18 @@
 TEGER':%0A
-%09
+  
 %09case 'C
@@ -1275,10 +1275,16 @@
  %7D%0A%0A
-%09%09
+        
 brea

fix decoder format