debug_extension.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. #!/usr/bin/env python3
  2. """debug_extension.py <out dir> [extension.js] - writes <out dir>/extension.js, the GNOME Shell extension with LWE-DBG
  3. journal lines added: every wallpaper window's adopt, unmanaging, unmanaged, wm map/destroy, restacked, lower, the pruning of
  4. windows that vanished and the hiding of orphan actors, plus a line whenever the wallpaper actors or the children of the
  5. Shell's window group change. Read them with `journalctl -b -o short-monotonic | grep LWE-DBG` (ghost_run.sh does)."""
  6. import os
  7. import sys
  8. HERE = os.path.dirname(os.path.abspath(__file__))
  9. DEFAULT = os.path.join(HERE, '..', '..', 'gnome-shell-extension',
  10. 'linux-wallpaperengine@wallpaperenginelover.github.io', 'extension.js')
  11. def replace(source, old, new):
  12. if source.count(old) != 1:
  13. sys.exit(f"anchor not found exactly once in the extension: {old.strip()[:60]!r}")
  14. return source.replace(old, new)
  15. HELPERS = '''function dbg(message) {
  16. console.log(`LWE-DBG ${message}`);
  17. }
  18. function wdesc(window) {
  19. try {
  20. return `#${window.get_stable_sequence()} "${window.get_title()}" type=${window.get_window_type()} layer=${window.get_layer()} mon=${window.get_monitor()} actor=${window.get_compositor_private() ? 1 : 0}`;
  21. } catch (e) {
  22. return `(window gone: ${e.message})`;
  23. }
  24. }
  25. function appIdOf(window) {'''
  26. DUMP = ''' for (const actor of global.get_window_actors())
  27. this._watch(actor.meta_window);
  28. this._dbgLast = '';
  29. this._dbgTimer = GLib.timeout_add_seconds(GLib.PRIORITY_DEFAULT, 2, () => {
  30. const live = new Set(global.display.list_all_windows());
  31. const lines = global.get_window_actors().map(actor => {
  32. const w = actor.meta_window;
  33. return `${wdesc(w)} vis=${actor.visible ? 1 : 0} mapped=${actor.mapped ? 1 : 0} inlist=${live.has(w) ? 1 : 0} ours=${this._wallpapers.has(w) ? 1 : 0} shown=${this._shown.has(w) ? 1 : 0}`;
  34. }).filter(line => line.includes('wallpaper') || line.includes('linux-wallpaperengine'));
  35. const text = lines.join(' | ') || '(no wallpaper actors)';
  36. if (text !== this._dbgLast) {
  37. this._dbgLast = text;
  38. dbg(`ACTORS ${text}`);
  39. }
  40. const group = global.window_group.get_children().map(c => {
  41. let title = '';
  42. try { title = c.meta_window ? c.meta_window.get_title() : '-'; } catch (e) { title = '?'; }
  43. return `${c.constructor.name}[${title}|v${c.visible ? 1 : 0}|m${c.mapped ? 1 : 0}|${Math.round(c.width)}x${Math.round(c.height)}|o${c.opacity}]`;
  44. }).join(' ');
  45. if (group !== this._dbgGroup) {
  46. this._dbgGroup = group;
  47. dbg(`GROUP ${group}`);
  48. }
  49. return GLib.SOURCE_CONTINUE;
  50. });
  51. this._dbgWm = [
  52. global.window_manager.connect('map', (_wm, actor) => dbg(`wm map ${wdesc(actor.meta_window)}`)),
  53. global.window_manager.connect('destroy', (_wm, actor) => dbg(`wm destroy ${wdesc(actor.meta_window)}`)),
  54. ];
  55. dbg('enabled');
  56. this._queueState();
  57. }
  58. '''
  59. PLAIN_DUMP = ''' for (const actor of global.get_window_actors())
  60. this._watch(actor.meta_window);
  61. this._queueState();
  62. }
  63. '''
  64. def main():
  65. out = sys.argv[1] if len(sys.argv) > 1 else sys.exit(__doc__)
  66. path = sys.argv[2] if len(sys.argv) > 2 else DEFAULT
  67. source = open(path).read()
  68. source = replace(source, 'function appIdOf(window) {', HELPERS)
  69. source = replace(source, PLAIN_DUMP, DUMP)
  70. source = replace(source, ' disable() {\n', ''' disable() {
  71. dbg('disable');
  72. if (this._dbgTimer)
  73. GLib.source_remove(this._dbgTimer);
  74. for (const id of this._dbgWm ?? [])
  75. global.window_manager.disconnect(id);
  76. ''')
  77. source = replace(source, " window.connect('unmanaged', () => {\n", ''' window.connect('unmanaging', () => dbg(`unmanaging ${wdesc(window)} ours=${this._wallpapers.has(window) ? 1 : 0}`)),
  78. window.connect('unmanaged', () => {
  79. dbg(`unmanaged ours=${this._wallpapers.has(window) ? 1 : 0}`);
  80. ''')
  81. source = replace(source, ' _adopt(window) {\n', ' _adopt(window) {\n dbg(`adopt ${wdesc(window)}`);\n')
  82. source = replace(source, ' wallpaper.lower();\n', ''' {
  83. dbg(`lower ${wdesc(wallpaper)} index=${index} of ${stack.length}`);
  84. wallpaper.lower();
  85. }
  86. ''')
  87. source = replace(source, ''' for (const id of this._watched.get(window) ?? [])
  88. window.disconnect(id);
  89. this._watched.delete(window);
  90. this._wallpapers.delete(window);
  91. this._shown.delete(window);
  92. this._raisedIds.delete(window);
  93. this._syncOverview();''', ''' dbg(`PRUNE drops ${wdesc(window)} (not in list_all_windows, shown)`);
  94. for (const id of this._watched.get(window) ?? [])
  95. window.disconnect(id);
  96. this._watched.delete(window);
  97. this._wallpapers.delete(window);
  98. this._shown.delete(window);
  99. this._raisedIds.delete(window);
  100. this._syncOverview();''')
  101. source = replace(source, " global.display.connect('restacked', () => {\n", ''' global.display.connect('restacked', () => {
  102. dbg(`restacked wallpapers=${[...this._wallpapers.keys()].map(w => '#' + w.get_stable_sequence()).join(',')}`);
  103. ''')
  104. if ' actor.hide();\n' in source:
  105. source = replace(source, ' actor.hide();\n', ' dbg(`hiding orphan ${wdesc(window)}`);\n actor.hide();\n')
  106. os.makedirs(out, exist_ok=True)
  107. with open(os.path.join(out, 'extension.js'), 'w') as f:
  108. f.write(source)
  109. main()