forked from Podshot/MCEdit-Unified
-
Notifications
You must be signed in to change notification settings - Fork 1
/
mcplatform.py
1290 lines (1141 loc) · 49.4 KB
/
mcplatform.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""Copyright (c) 2010-2012 David Rio Vierra
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE."""
# -# Modified by D.C.-G. for translation purpose
# !# Tests for file chooser
"""
mcplatform.py
Platform-specific functions, folder paths, and the whole fixed/portable nonsense.
"""
import logging
log = logging.getLogger(__name__)
import directories
import os
from os.path import dirname, exists, join
import sys
import platform
enc = sys.getfilesystemencoding()
hasXlibDisplay = False
if sys.platform == "win32":
if platform.architecture()[0] == "32bit":
plat = "win32"
if platform.architecture()[0] == "64bit":
plat = "win-amd64"
sys.path.append(join(directories.getDataDir(), "pymclevel", "build", "lib." + plat + "-2.6").encode(enc))
elif sys.platform in ['linux2', 'darwin']:
try:
import Xlib.display
import Xlib.X
import Xlib.protocol
hasXlibDisplay = True
except ImportError:
hasXlibDisplay = None
os.environ["YAML_ROOT"] = join(directories.getDataDir(), "pymclevel").encode(enc)
from pygame import display
from albow import request_new_filename, request_old_filename
from albow.translate import _
from pymclevel import minecraftSaveFileDir, getMinecraftProfileDirectory, getSelectedProfile
from datetime import datetime
import re
import subprocess
try:
import pygtk
pygtk.require('2.0')
import gtk
if gtk.pygtk_version < (2, 3, 90):
raise ImportError
hasGtk = True
except ImportError:
hasGtk = False # Using old method as fallback
texturePacksDir = os.path.join(getMinecraftProfileDirectory(getSelectedProfile()), "texturepacks")
# Compatibility layer for filters:
filtersDir = directories.filtersDir
schematicsDir = directories.schematicsDir
# !# Disabling platform specific file chooser:
# !# Please, don't touch these two lines and the 'platChooser' stuff. -- D.C.-G.
# platChooser = sys.platform in ('linux2', 'darwin')
platChooser = sys.platform == 'darwin'
def dynamic_arguments(func_to_replace, askFile_func):
def wrapper(initialDir, displayName, fileFormat):
if isinstance(fileFormat, tuple):
return func_to_replace(initialDir, displayName, fileFormat)
else:
def old_askSaveSchematic(initialDir, displayName, fileFormat):
dt = datetime.now().strftime("%Y-%m-%d--%H-%M-%S")
return askFile_func(initialDir,
title=_('Save this schematic...'),
defaultName=displayName + "_" + dt + "." + fileFormat,
filetype=_('Minecraft Schematics (*.{0})\0*.{0}\0\0').format(fileFormat),
suffix=fileFormat,
)
return old_askSaveSchematic(initialDir, displayName, fileFormat)
return wrapper
def getTexturePacks():
try:
return os.listdir(texturePacksDir)
except:
return []
# for k,v in os.environ.iteritems():
# try:
# os.environ[k] = v.decode(sys.getfilesystemencoding())
# except:
# continue
if sys.platform == "win32":
try:
from win32 import win32gui
from win32 import win32api
from win32.lib import win32con
except ImportError:
import win32gui
import win32api
import win32con
try:
import win32com.client
from win32com.shell import shell, shellcon # @UnresolvedImport
except:
pass
try:
import pywintypes
except:
pass
if sys.platform == 'darwin':
cmd_name = "Cmd"
option_name = "Opt"
else:
cmd_name = "Ctrl"
option_name = "Alt"
def OSXVersionChecker(name, compare):
"""Rediculously complicated function to compare current System version to inputted version."""
if compare != 'gt' and compare != 'lt' and compare != 'eq' and compare != 'gteq' and compare != 'lteq':
print "Invalid version check {}".format(compare)
return False
if sys.platform == 'darwin':
try:
systemVersion = platform.mac_ver()[0].split('.')
if len(systemVersion) == 2:
systemVersion.append('0')
major, minor, patch = 10, 0, 0
if name.lower() == 'cheetah':
minor = 0
patch = 4
elif name.lower() == 'puma':
minor = 1
patch = 5
elif name.lower() == 'jaguar':
minor = 2
patch = 8
elif name.lower() == 'panther':
minor = 3
patch = 9
elif name.lower() == 'tiger':
minor = 4
patch = 11
elif name.lower() == 'snow_leopard':
minor = 5
patch = 8
elif name.lower() == 'snow_leopard':
minor = 6
patch = 8
elif name.lower() == 'lion':
minor = 7
patch = 5
elif name.lower() == 'mountain_lion':
minor = 8
patch = 5
elif name.lower() == 'mavericks':
minor = 9
patch = 5
elif name.lower() == 'yosemite':
minor = 10
patch = 0
else:
major = 0
if int(systemVersion[0]) > int(major):
ret_val = 1
elif int(systemVersion[0]) < int(major):
ret_val = -1
else:
if int(systemVersion[1]) > int(minor):
ret_val = 1
elif int(systemVersion[1]) < int(minor):
ret_val = -1
else:
if int(systemVersion[2]) > int(patch):
ret_val = 1
elif int(systemVersion[2]) < int(patch):
ret_val = -1
else:
ret_val = 0
if ret_val == 0 and (compare == 'eq' or compare == 'gteq' or compare == 'lteq'):
return True
elif ret_val == -1 and (compare == 'lt' or compare == 'lteq'):
return True
elif ret_val == 1 and (compare == 'gt' or compare == 'gteq'):
return True
except:
print "An error occured determining the system version"
return False
else:
return False
lastSchematicsDir = None
lastSaveDir = None
def buildFileTypes(filetypes):
result = ""
for key in filetypes[0]:
ftypes = []
result += key + " ("
for ftype in filetypes[0][key]:
ftypes.append("*." + ftype)
result += ",".join(ftypes) + ")\0"
result += ";".join(ftypes) + "\0"
return result + "\0"
def askOpenFile(title='Select a Minecraft level....', schematics=False, suffixes=None):
title = _(title)
global lastSchematicsDir, lastSaveDir
if not suffixes:
suffixes = ["mclevel", "dat", "mine", "mine.gz"]
suffixesChanged = False
else:
suffixesChanged = True
initialDir = lastSaveDir or minecraftSaveFileDir
if schematics:
initialDir = lastSchematicsDir or directories.schematicsDir
def _askOpen(_suffixes):
if schematics:
_suffixes.append("schematic")
_suffixes.append("schematic.gz")
_suffixes.append("zip")
_suffixes.append("inv")
_suffixes.append("nbt")
# BO support
_suffixes.append("bo2")
_suffixes.append("bo3")
_suffixes.sort()
if sys.platform == "win32": # !#
if suffixesChanged:
sendSuffixes = _suffixes
else:
sendSuffixes = None
return askOpenFileWin32(title, schematics, initialDir, sendSuffixes)
elif hasGtk and not platChooser: # !# #Linux (When GTK 2.4 or newer is installed)
return askOpenFileGtk(title, _suffixes, initialDir)
else:
log.debug("Calling internal file chooser.")
log.debug("'initialDir' is %s (%s)" % (repr(initialDir), type(initialDir)))
try:
iDir = initialDir.encode(enc)
except Exception, e:
iDir = initialDir
log.debug("Could not encode 'initialDir' %s" % repr(initialDir))
log.debug("Encode function returned: %s" % e)
return request_old_filename(suffixes=_suffixes, directory=iDir)
filename = _askOpen(suffixes)
if filename:
if schematics:
lastSchematicsDir = dirname(filename)
else:
lastSaveDir = dirname(filename)
return filename
def askOpenFileWin32(title, schematics, initialDir, suffixes=None):
try:
# if schematics:
if not suffixes:
f = (_('Levels and Schematics') + '\0*.mclevel;*.dat;*.mine;*.mine.gz;*.schematic;*.zip;*.schematic.gz;*.inv;*.nbt\0' +
'*.*\0*.*\0\0')
else:
f = "All\0"
for suffix in suffixes:
f += "*." + suffix + ";"
f += "\0*.*\0\0"
# else:
# f = ('Levels (*.mclevel, *.dat;*.mine;*.mine.gz;)\0' +
# '*.mclevel;*.dat;*.mine;*.mine.gz;*.zip;*.lvl\0' +
# '*.*\0*.*\0\0')
(filename, customfilter, flags) = win32gui.GetOpenFileNameW(
hwndOwner=display.get_wm_info()['window'],
InitialDir=initialDir,
Flags=(win32con.OFN_EXPLORER
| win32con.OFN_NOCHANGEDIR
| win32con.OFN_FILEMUSTEXIST
| win32con.OFN_LONGNAMES
# |win32con.OFN_EXTENSIONDIFFERENT
),
Title=title,
Filter=f,
)
except Exception:
# print "Open File: ", e
pass
else:
return filename
def askOpenFileGtk(title, suffixes, initialDir):
fls = []
def run_dlg():
chooser = gtk.FileChooserDialog(title,
None, gtk.FILE_CHOOSER_ACTION_SAVE,
(gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL,
gtk.STOCK_OPEN, gtk.RESPONSE_OK))
chooser.set_default_response(gtk.RESPONSE_OK)
chooser.set_current_folder(initialDir)
chooser.set_current_name("world") # For some reason the Windows isn't closing if this line ins missing or the parameter is ""
# Add custom Filter
file_filter = gtk.FileFilter()
file_filter.set_name(_("Levels and Schematics"))
for suffix in suffixes:
file_filter.add_pattern("*." + suffix)
chooser.add_filter(file_filter)
# Add "All files" Filter
file_filter = gtk.FileFilter()
file_filter.set_name("All files")
file_filter.add_pattern("*")
chooser.add_filter(file_filter)
response = chooser.run()
if response == gtk.RESPONSE_OK:
fls.append(chooser.get_filename())
else:
fls.append(None)
chooser.destroy()
gtk.main_quit()
gtk.idle_add(run_dlg)
gtk.main()
return fls[0]
def askSaveSchematic(initialDir, displayName, fileFormats):
fileFormat = buildFileTypes(fileFormats)
dt = datetime.now().strftime("%Y-%m-%d--%H-%M-%S")
return askSaveFile(initialDir,
title=_('Save this schematic...'),
defaultName=displayName + "_" + dt + "." + fileFormats[0][fileFormats[0].keys()[0]][0],
filetype=fileFormat,
suffix=fileFormat,
)
def askCreateWorld(initialDir):
defaultName = name = _("Untitled World")
i = 0
while exists(join(initialDir, name)):
i += 1
name = defaultName + " " + str(i)
return askSaveFile(initialDir,
title=_('Name this new world.'),
defaultName=name,
filetype=_('Minecraft World\0*.*\0\0'),
suffix="",
)
def askSaveFile(initialDir, title, defaultName, filetype, suffix):
if sys.platform == "win32": # !#
try:
(filename, customfilter, flags) = win32gui.GetSaveFileNameW(
hwndOwner=display.get_wm_info()['window'],
InitialDir=initialDir,
Flags=win32con.OFN_EXPLORER | win32con.OFN_NOCHANGEDIR | win32con.OFN_OVERWRITEPROMPT,
File=defaultName,
DefExt=suffix,
Title=title,
Filter=filetype,
)
except Exception, e:
print "Error getting file name: ", e
return
try:
filename = filename[:filename.index('\0')]
filename = filename.decode(sys.getfilesystemencoding())
except:
pass
else:
# Reformat the Windows stuff
if "\0" in suffix and suffix.count("*.") > 1:
_suffix = suffix.split("\0")[:-2]
else:
_suffix = suffix
if "\0" in filetype and filetype.count("*.") > 1:
_filetype = filetype.split("\0")[:-2]
else:
_filetype = filetype
if hasGtk and not platChooser: # !# #Linux (When GTK 2.4 or newer is installed)
fls = []
def run_dlg():
chooser = gtk.FileChooserDialog(title,
None, gtk.FILE_CHOOSER_ACTION_SAVE,
(gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL,
gtk.STOCK_SAVE, gtk.RESPONSE_OK))
chooser.set_default_response(gtk.RESPONSE_OK)
chooser.set_current_folder(initialDir)
chooser.set_current_name(defaultName)
# Add custom Filter
if type(_filetype) in (list, tuple):
for i, filet in enumerate(_filetype):
if i % 2 == 0:
file_filter = gtk.FileFilter()
file_filter.set_name(filet)
if ";" in _suffix[i + 1]:
for _suff in _suffix.split(";"):
if _suff:
file_filter.add_pattern(_suff)
else:
file_filter.add_pattern(_suffix[i + 1])
chooser.add_filter(file_filter)
else:
file_filter = gtk.FileFilter()
file_filter.set_name(filetype[:filetype.index("\0")])
if "\0" in suffix and suffix.count("*.") > 1:
__suffix = suffix.split("\0")[:-2]
else:
__suffix = suffix
if type(__suffix) in (list, tuple):
for suff in __suffix:
file_filter.add_pattern("*." + suff)
else:
file_filter.add_pattern("*." + __suffix)
chooser.add_filter(file_filter)
# Add "All files" Filter
file_filter = gtk.FileFilter()
file_filter.set_name("All files")
file_filter.add_pattern("*")
chooser.add_filter(file_filter)
response = chooser.run()
if response == gtk.RESPONSE_OK:
fls.append(chooser.get_filename())
else:
fls.append(None)
chooser.destroy()
gtk.main_quit()
gtk.idle_add(run_dlg)
gtk.main()
filename = fls[0]
else: # Fallback
log.debug("Calling internal file chooser.")
log.debug("'initialDir' is %s (%s)" % (repr(initialDir), type(initialDir)))
log.debug("'defaultName' is %s (%s)" % (repr(defaultName), type(defaultName)))
try:
iDir = initialDir.encode(enc)
except:
iDir = initialDir
log.debug("Could not encode 'initialDir' %s" % repr(initialDir))
log.debug("Encode function returned: %s" % e)
try:
dName = defaultName.encode(enc)
except:
dName = defaultName
log.debug("Could not encode 'defaultName' %s" % repr(defaultName))
log.debug("Encode function returned: %s" % e)
if type(_suffix) in (list, tuple):
sffxs = [a[1:] for a in _suffix[1::2]]
sffx = sffxs.pop(0)
sffxs.append('.*')
else:
sffx = _suffix
sffxs = []
filename = request_new_filename(prompt=title,
suffix=sffx,
extra_suffixes=sffxs,
directory=iDir,
filename=dName,
pathname=None)
return filename
askSaveSchematic = dynamic_arguments(askSaveSchematic, askSaveFile)
# Start Open Folder Dialogs
# TODO: Possibly get an OS X dialog
def askOpenFolderWin32(title, initialDir):
try:
desktop_pidl = shell.SHGetFolderLocation(0, shellcon.CSIDL_DESKTOP, 0, 0)
pidl, display_name, image_list = shell.SHBrowseForFolder (
win32gui.GetDesktopWindow(),
desktop_pidl,
"Choose a folder",
0,
None,
None
)
return shell.SHGetPathFromIDList(pidl)
except pywintypes.com_error as e:
if e.args[0] == -2147467259:
print "Invalid folder selected"
pass
def askOpenFolderGtk(title, initialDir):
if hasGtk:
fls = []
def run_dlg():
chooser = gtk.FileChooserDialog(title,
None, gtk.FILE_CHOOSER_ACTION_SAVE,
(gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL,
gtk.STOCK_OPEN, gtk.RESPONSE_OK))
chooser.set_default_response(gtk.RESPONSE_OK)
chooser.set_current_folder(initialDir)
chooser.set_current_name("world")
chooser.set_action(gtk.FILE_CHOOSER_ACTION_SELECT_FOLDER)
response = chooser.run()
if response == gtk.RESPONSE_OK:
fls.append(chooser.get_filename()) # Returns the folder path if gtk.FILE_CHOOSER_ACTION_SELECT_FOLDER is the action
else:
fls.append(None)
chooser.destroy()
gtk.main_quit()
gtk.idle_add(run_dlg)
gtk.main()
return fls[0]
else:
print "You currently need gtk to use an Open Folder Dialog!"
# End Open Folder Dialogs
# if sys.platform == "win32":
# try:
#
# (filename, customfilter, flags) = win32gui.GetSaveFileNameW(
# hwndOwner = display.get_wm_info()['window'],
# # InitialDir=minecraftSaveFileDir,
# Flags=win32con.OFN_EXPLORER | win32con.OFN_NOCHANGEDIR | win32con.OFN_OVERWRITEPROMPT,
# File=initialDir + os.sep + displayName,
# DefExt=fileFormat,
# Title=,
# Filter=,
# )
# except Exception, e:
# print "Error getting filename: {0!r}".format(e)
# return
#
# elif sys.platform == "darwin" and AppKit is not None:
# sp = AppKit.NSSavePanel.savePanel()
# sp.setDirectory_(initialDir)
# sp.setAllowedFileTypes_([fileFormat])
# # sp.setFilename_(self.editor.level.displayName)
#
# if sp.runModal() == 0:
# return; # pressed cancel
#
# filename = sp.filename()
# AppKit.NSApp.mainWindow().makeKeyWindow()
#
# else:
#
# filename = request_new_filename(prompt = "Save this schematic...",
# suffix = ".{0}".format(fileFormat),
# directory = initialDir,
# filename = displayName,
# pathname = None)
#
# return filename
def platform_open(path):
try:
if sys.platform == "win32":
os.startfile(path)
# os.system('start ' + path + '\'')
elif sys.platform == "darwin":
# os.startfile(path)
os.system('open "' + path + '"')
else:
os.system('xdg-open "' + path + '"')
except Exception, e:
print "platform_open failed on {0}: {1}".format(sys.platform, e)
win32_window_size = True
#=============================================================================
#=============================================================================
# DESKTOP ENVIRONMENTS AND OS SIDE WINDOW MANAGEMENT.
#
# The idea is to have a single object to deal with underling OS specific window management interface.
# This will help to save/restore MCEdit window sate, position and size.
#
# TODO:
# * Test on the actually unsupported Linux DEs.
# * Review WWindowHandler class for Windows.
# * Create a DWindowHandler class for Darwin (OSX).
# Window states
MINIMIZED = 0
NORMAL = 1
MAXIMIZED = 2
FULLSCREEN = 3
#=============================================================================
# Linux desktop environment detection.
#
# source: http://stackoverflow.com/questions/2035657/what-is-my-current-desktop-environment (http://stackoverflow.com/a/21213358)
#
# Tweaked, of course ;)
#
def get_desktop_environment():
# From http://stackoverflow.com/questions/2035657/what-is-my-current-desktop-environment
# and http://ubuntuforums.org/showthread.php?t=652320
# and http://ubuntuforums.org/showthread.php?t=652320
# and http://ubuntuforums.org/showthread.php?t=1139057
if sys.platform in ["win32", "cygwin"]:
return "windows"
elif sys.platform == "darwin":
return "mac"
else: # Most likely either a POSIX system or something not much common
ds = os.environ.get("DESKTOP_SESSION", None)
if ds in ('default', None):
ds = os.environ.get("XDG_CURRENT_DESKTOP", None)
if ds is not None: # easier to match if we doesn't have to deal with caracter cases
desktop_session = ds.lower()
found = re.findall(r"gnome|unity|cinnamon|mate|xfce4|lxde|fluxbox|blackbox|openbox|icewm|jwm|afterstep|trinity|kde", desktop_session, re.I)
if len(found) == 1:
return found[0]
elif len(found) > 1:
print "Houston? We have a problem...\n\nThe desktop environment can't be found: '%s' has been detected to be %s alltogeteher." % (ds, " and ".join((", ".join(found[:-1]), found[-1])))
return 'unknown'
# # Special cases ##
# Canonical sets $DESKTOP_SESSION to Lubuntu rather than LXDE if using LXDE.
# There is no guarantee that they will not do the same with the other desktop environments.
elif "xfce" in desktop_session or desktop_session.startswith("xubuntu"):
return "xfce4"
elif desktop_session.startswith("ubuntu"):
return "unity"
elif desktop_session.startswith("lubuntu"):
return "lxde"
elif desktop_session.startswith("kubuntu"):
return "kde"
elif desktop_session.startswith("razor"): # e.g. razorkwin
return "razor-qt"
elif desktop_session.startswith("wmaker"): # e.g. wmaker-common
return "windowmaker"
if os.environ.get('KDE_FULL_SESSION', None) == 'true':
return "kde"
elif os.environ.get('GNOME_DESKTOP_SESSION_ID', None):
if not "deprecated" in os.environ.get('GNOME_DESKTOP_SESSION_ID', None):
return "gnome2"
# From http://ubuntuforums.org/showthread.php?t=652320
elif is_running("xfce-mcs-manage"):
return "xfce4"
elif is_running("ksmserver"):
return "kde"
return "unknown"
def is_running(process):
# From http://www.bloggerpolis.com/2011/05/how-to-check-if-a-process-is-running-using-python/
# and http://richarddingwall.name/2009/06/18/windows-equivalents-of-ps-and-kill-commands/
try: # Linux/Unix
s = subprocess.Popen(["ps", "axw"], stdout=subprocess.PIPE)
except: # Windows
s = subprocess.Popen(["tasklist", "/v"], stdout=subprocess.PIPE)
for x in s.stdout:
if re.search(process, x):
return True
return False
#=============================================================================
# Window handling.
desktop_environment = get_desktop_environment()
DEBUG_WM = False
USE_WM = True
# Desktops settings
# Each entry in the platform sub-dictionaries represent which object is used to get/set the window metrics.
#
# For Linux:
# Valid entries are: position_gap, position_getter, position_setter, size_getter, size_setter and state.
# Entries can be omitted; default values will be used.
# For position_gap the default is (0, 0, False, False)
# For the other ones, the default is the Pygame window object.
#
# position_gap is used on some environment to restore the windows at the coords it was formerly.
# The two first values of the tuple are the amount of pixels to add to the window x and y coords.
# The two last ones tell whether these pixels shall be added only once (at program startup) or always.
#
desktops = {'linux2': {
'cinnamon': { # Actually, there's a bug when resizing on XCinnamon.
'position_setter': 'parent',
'position_getter': 'parent.parent',
'position_gap': (9, 8, True, True),
'state': 'parent'
},
'gnome': {
'position_setter': 'parent',
'position_getter': 'parent.parent',
'size_setter': 'parent',
'size_getter': 'parent',
'state': 'parent'
},
'kde': {
'position_setter': 'parent',
'position_getter': 'parent.parent.parent',
'state': 'parent'
},
'unity': {
'position_setter': 'parent',
'position_getter': 'parent.parent.parent',
'position_gap': (10, 10, False, False),
'size_setter': 'parent',
'state': 'parent'
}
},
# 'win32': {},
# 'darwin': {}
}
# The environments in the next definition need to be tested.
linux_unsuported = ('afterstep',
'blackbox',
'fluxbox',
'gnome2',
'icewm',
'jwm',
'lxde',
'mate',
'openbox',
'razor-qt',
'trinity',
'windowmaker',
'xfce4')
# Window handlers classes
class BaseWindowHandler:
"""Abstract class for the platform specific window handlers.
If initialized, this class casts a NotImplementedError."""
desk_env = desktop_environment
def __init__(self, *args, **kwargs):
"""..."""
if not len(kwargs):
raise NotImplementedError, "Abstract class."
self.mode = kwargs['mode']
def set_mode(self, size, mode):
"""Wrapper for pygame.display.set_mode()."""
display.set_mode(size, mode)
def get_root_rect(self):
"""..."""
raise NotImplementedError, "Abstract method."
def get_size(self):
"""..."""
raise NotImplementedError, "Abstract method."
def set_size(self, size, update=True):
"""..."""
raise NotImplementedError, "Abstract method."
def get_position(self):
"""..."""
raise NotImplementedError, "Abstract method."
def set_position(self, pos, update=True):
"""..."""
raise NotImplementedError, "Abstract method."
def get_state(self):
"""..."""
raise NotImplementedError, "Abstract method."
def set_state(self, state=NORMAL, size=(-1, -1), pos=(-1, -1), update=True):
"""..."""
raise NotImplementedError, "Abstract method."
def flush(self):
"""Just does nothing..."""
return
def sync(self):
"""Just does nothing..."""
return
class XWindowHandler(BaseWindowHandler):
"""Object to deal with XWindow managers (Linux)."""
desk_env = desktop_environment
def __init__(self, pos=(0, 0), size=(0, 0), mode=None):
"""Set up the internal handlers."""
BaseWindowHandler.__init__(self, pos=pos, size=size, mode=mode)
self.mode = mode
# setup the internal data, especially the Xlib object we need.
# Tests
if DEBUG_WM:
print "#" * 72
print "XWindowHandler.__init__"
print "Desktop environment:", desktop_environment
dis = self.display = Xlib.display.Display()
pygame_win = dis.create_resource_object('window', display.get_wm_info()['window'])
pygame_win_id = pygame_win.id
if DEBUG_WM:
root = dis.screen().root
active_wid_id = root.get_full_property(dis.intern_atom('_NET_ACTIVE_WINDOW'), Xlib.X.AnyPropertyType).value[0]
active_win = dis.create_resource_object('window', active_wid_id)
# Print pygame_win and active_win styff
for (win, name) in ((pygame_win, 'pygame_win'), (active_win, 'active_win')):
print "=" * 72
print "%s guts" % name, "(ID %s)" % win.id
print "-" * 72
print "* State"
prop = win.get_full_property(dis.intern_atom("_NET_WM_STATE"), 4)
print " ", prop
if prop:
print dir(prop)
print "* Geometry"
print " ", win.get_geometry()
parent = win.query_tree().parent
p = '%s.parent' % name
while parent.id != root.id:
print "-" * 72
print p, "ID", parent.id
print "* State"
prop = parent.get_full_property(dis.intern_atom("_NET_WM_STATE"), 4)
print " ", prop
if prop:
print dir(prop)
print "* Geometry"
print " ", parent.get_geometry()
parent = parent.query_tree().parent
p += ".parent"
# Size handlers
self.base_handler = pygame_win
self.base_handler_id = pygame_win.id
size = desktops['linux2'][self.desk_env].get('size_getter', None)
if size:
if DEBUG_WM:
print "size_getter.split('.')", size.split('.')
handler = pygame_win
for item in size.split('.'):
handler = getattr(handler.query_tree(), item)
self.sizeGetter = handler
else:
self.sizeGetter = pygame_win
size = desktops['linux2'][self.desk_env].get('size_setter', None)
if size:
if DEBUG_WM:
print "size_setter.split('.')", size.split('.')
handler = pygame_win
for item in size.split('.'):
handler = getattr(handler.query_tree(), item)
self.sizeSetter = handler
else:
self.sizeSetter = pygame_win
# Position handlers
pos = desktops['linux2'][self.desk_env].get('position_getter', None)
if pos:
if DEBUG_WM:
print "pos_getter.split('.')", pos.split('.')
handler = pygame_win
for item in pos.split('.'):
handler = getattr(handler.query_tree(), item)
self.positionGetter = handler
else:
self.positionGetter = pygame_win
pos = desktops['linux2'][self.desk_env].get('position_setter', None)
if pos:
if DEBUG_WM:
print "pos_setter.split('.')", pos.split('.')
handler = pygame_win
for item in pos.split('.'):
handler = getattr(handler.query_tree(), item)
self.positionSetter = handler
else:
self.positionSetter = pygame_win
# Position gap. Used to correct wrong positions on some environments.
self.position_gap = desktops['linux2'][self.desk_env].get('position_gap', (0, 0, False, False))
self.starting = True
self.gx, self.gy = 0, 0
# State handler
state = desktops['linux2'][self.desk_env].get('state', None)
if state:
if DEBUG_WM:
print "state.split('.')", state.split('.')
handler = pygame_win
for item in state.split('.'):
handler = getattr(handler.query_tree(), item)
self.stateHandler = handler
else:
self.stateHandler = pygame_win
if DEBUG_WM:
print "self.positionGetter:", self.positionGetter, 'ID:', self.positionGetter.id
print "self.positionSetter:", self.positionSetter, 'ID:', self.positionSetter.id
print "self.sizeGetter:", self.sizeGetter, 'ID:', self.sizeGetter.id
print "self.sizeSetter:", self.sizeSetter, 'ID:', self.sizeSetter.id
print "self.stateHandler:", self.stateHandler, 'ID:', self.stateHandler.id
print self.stateHandler.get_wm_state()
def get_root_rect(self):
"""Return a four values tuple containing the position and size of the very first OS window object."""
geom = self.display.screen().root.get_geometry()
return (geom.x, geom.y, geom.width, geom.height)
def get_size(self):
"""Return the window actual size as a tuple (width, height)."""
geom = self.sizeGetter.get_geometry()
if DEBUG_WM:
print "Actual size is", geom.width, geom.height
return (geom.width, geom.height)
def set_size(self, size, update=True):
"""Set the window size.
:size: list or tuple: the new size.
Raises a TypeError if something else than a list or a tuple is sent."""
if type(size) in (list, tuple):
# Call the Xlib object handling the size to update it.
if DEBUG_WM:
print "Setting size to", size
print "actual size", self.get_size()
self.sizeSetter.configure(width=size[0], height=size[1])
if update:
self.sync()
else:
# Raise a Type error.
raise TypeError, "%s is not a list or a tuple." % size
def get_position(self):
"""Return the window actual position as a tuple."""
geom = self.positionGetter.get_geometry()
x, y = geom.x, geom.y
# if DEBUG_WM:
# print "Actual position is", x, y
return (x, y)
def set_position(self, pos, update=True):
"""Set the window position.
:pos: list or tuple: the new position (x, y).
:update: bool: wheteher to call the internal sync method."""
if DEBUG_WM:
print "Setting position to", pos
if type(pos) in (list, tuple):
gx, gy = 0 or self.gx, 0 or self.gy
if self.starting:
gx, gy = self.position_gap[:2]
if self.position_gap[2]:
self.gx = gx
if self.position_gap[3]:
self.gy = gy
self.starting = False
# Call the Xlib object handling the position to update it.
self.positionSetter.configure(x=pos[0] + gx, y=pos[1] + gy)
if update:
self.sync()
else:
# Raise a Type error.