core: add the splashy2fbsplash theme converter
[fbsplash.git] / core / scripts / splashy2fbsplash.py.in
blob81f50ee66db6fb36c27656764cc0c6e316ce7945
1 #!/usr/bin/python
3 # A simple converter to convert splashy themes to fbsplash format.
5 # Copyright (C) 2008, Michal Januszewski <spock@gentoo.org>
7 # This file is a part of the splashutils package.
9 # This file is subject to the terms and conditions of the GNU General Public
10 # License v2. See the file COPYING in the main directory of this archive for
11 # more details.
13 import os, sys, shutil
14 import xml.etree.ElementTree as et
15 from PIL import Image
17 def usage():
18 print 'splashy2fbsplash/splashutils'
19 print 'Usage: %s <path/theme.xml> <fbsplash_name>' % sys.argv[0]
20 print
21 print 'A splashy to fbsplash theme converter.'
23 if len(sys.argv) < 3:
24 usage();
25 sys.exit(0)
27 try:
28 f = open(sys.argv[1], 'r');
29 d = et.parse(f)
30 except:
31 print >>sys.stderr, 'Failed to open or parse %s' % sys.argv[1]
32 sys.exit(1)
34 try:
35 os.mkdir('@themedir@/%s' % (sys.argv[2]))
36 os.mkdir('@themedir@/%s/images' % (sys.argv[2]))
37 except:
38 print >>sys.stderr, 'Failed to create @themedir@/%s' % sys.argv[2]
39 sys.exit(1)
41 def getColor(el):
42 return (int(el.find('color/red').text) * 0x1000000 +
43 int(el.find('color/green').text) * 0x10000 +
44 int(el.find('color/blue').text) * 0x100 +
45 int(el.find('color/alpha').text))
47 def processBorder(el, x, y, w, h):
48 if el.find('border/enable').text == 'yes':
49 print >>out, '# Border'
50 brdcol = getColor(prg.find('border'))
51 print >>out, 'box silent %d %d %d %d #%08x' % (x - 1, y - 1, x + w + 1, y - 1, brdcol)
52 print >>out, 'box silent %d %d %d %d #%08x' % (x - 1, y + h + 1, x + w + 1, y + h + 1, brdcol)
53 print >>out, 'box silent %d %d %d %d #%08x' % (x - 1, y, x - 1, y + h, brdcol)
54 print >>out, 'box silent %d %d %d %d #%08x' % (x + w + 1, y, x + w + 1, y + h, brdcol)
55 print >>out
57 if int(d.find('background/dimension/width').text) > 0 and int(d.find('background/dimension/height').text) > 0:
58 pixelcoords = True
59 else:
60 pixelcoords = False
62 def getSize(size, type):
63 if pixelcoords:
64 return size
65 else:
66 return size * im.size[type] / 100
68 ## Process the background picture.
69 ## -------------------------------
70 images = {}
72 def appendType(list, type):
73 if type == 'boot':
74 list.append('bootup')
75 list.append('other')
76 else:
77 list.append(type)
78 if type == 'shutdown':
79 list.append('reboot')
82 def processImg(type):
83 i = d.find('background/%s' % type).text
84 if not images.has_key(i):
85 images[i] = []
86 appendType(images[i], type);
88 processImg('boot')
89 processImg('shutdown')
90 processImg('suspend')
91 processImg('resume')
93 im = Image.open('%s/%s' % (os.path.dirname(sys.argv[1]), d.find('background/boot').text))
94 res = '%dx%d' % (im.size[0], im.size[1])
96 out = open('@themedir@/%s/%s.cfg' % (sys.argv[2], res), 'w')
98 print >>out, '# Background images'
99 for k, v in images.iteritems():
100 print >>out, '<type %s>' % ' '.join(v)
101 print >>out, ' silentpic = images/%s' % k
102 print >>out, '</type>'
103 shutil.copy('%s/%s' % (os.path.dirname(sys.argv[1]), k),
104 '@themedir@/%s/images/%s' % (sys.argv[2], k))
105 print >>out, ''
107 ## Process the progress bar.
108 ## -------------------------
109 print >>out, '## Progress bar'
110 prg = d.find('progressbar')
112 forward = []
113 backward = []
115 for type in ['boot', 'shutdown', 'resume', 'suspend']:
116 if prg.find('visibility/' + type).text == 'yes':
117 if prg.find('direction/' + type).text == 'forward':
118 appendType(forward, type)
119 else:
120 appendType(backward, type)
122 dim = prg.find('dimension')
123 x = getSize(int(dim.find('x').text), 0)
124 y = getSize(int(dim.find('y').text), 1)
125 w = getSize(int(dim.find('width').text), 0)
126 h = getSize(int(dim.find('height').text), 1)
128 processBorder(prg, x, y, w, h)
130 print >>out, '# Background'
131 print >>out, 'box silent %d %d %d %d #%08x' % (x, y, x + w, y + h, getColor(prg.find('background')))
132 print >>out, ''
133 print >>out, '# Progress bar(s)'
135 if forward:
136 print >>out, '<type %s>' % (' '.join(forward))
137 print >>out, ' box silent inter %d %d %d %d #%08x' % (x, y, x, y, getColor(prg))
138 print >>out, ' box silent %d %d %d %d #%08x' % (x, y, x + w, y + h, getColor(prg))
139 print >>out, '</type>'
141 if backward:
142 print >>out, '<type %s>' % (' '.join(backward))
143 print >>out, ' box silent inter %d %d %d %d #%8x' % (x, y, x + w, y + h, getColor(prg))
144 print >>out, ' box silent %d %d %d %d #%8x' % (x, y, x, y, getColor(prg))
145 print >>out, '</type>'
147 print >>out, ''
149 ## Process the textbox.
150 ## --------------------
151 print >>out, '## Textbox'
152 txt = d.find('textbox')
153 dim = txt.find('dimension')
155 x = getSize(int(dim.find('x').text), 0)
156 y = getSize(int(dim.find('y').text), 1)
157 w = getSize(int(dim.find('width').text), 0)
158 h = getSize(int(dim.find('height').text), 1)
160 processBorder(txt, x, y, w, h)
162 print >>out, '# Background'
163 print >>out, 'box silent %d %d %d %d #%08x' % (x, y, x + w, y + h, getColor(txt))
164 print >>out, ''
165 print >>out, '# Text area'
166 print >>out, 'text %s %s %d %d #%08x msglog' % (txt.find('text/font/file').text, txt.find('text/font/height').text,
167 x, y, getColor(txt.find('text')))
168 print >>out, ''
169 print >>out, 'log_lines = %d' % (h / (int(txt.find('text/font/height').text) * 1.3))
171 shutil.copy('%s/%s' % (os.path.dirname(sys.argv[1]), txt.find('text/font/file').text),
172 '@themedir@/%s/%s' % (sys.argv[2], txt.find('text/font/file').text))
174 out.close()
175 f.close()