aboutsummarylogtreecommitdiffstats
path: root/display-profiles
blob: 6992a503018f3167c78b0c8a3371fb6c4cbc755c (plain)
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
#!/usr/bin/env python3

import os
import sys
import json
import argparse
import subprocess
import time
import threading
from pathlib import Path
from typing import Dict, List, Optional, Tuple
import hashlib

try:
    import curses
    CURSES_AVAILABLE = True
except ImportError:
    CURSES_AVAILABLE = False

class DisplayManager:
    def __init__(self):
        self.config_dir = Path.home() / '.config' / 'display-profiles'
        self.config_file = self.config_dir / 'profiles.json'
        self.config_dir.mkdir(parents=True, exist_ok=True)
        self.profiles = self.load_profiles()
        
    def load_profiles(self) -> Dict:
        if self.config_file.exists():
            try:
                with open(self.config_file, 'r') as f:
                    return json.load(f)
            except (json.JSONDecodeError, IOError):
                return {}
        return {}
    
    def save_profiles(self):
        try:
            with open(self.config_file, 'w') as f:
                json.dump(self.profiles, f, indent=2)
        except IOError as e:
            print(f"Error saving profiles: {e}")

    def run_command(self, cmd: List[str]) -> Tuple[bool, str]:
        try:
            result = subprocess.run(cmd, capture_output=True, text=True, check=True)
            return True, result.stdout
        except subprocess.CalledProcessError as e:
            return False, e.stderr
        except FileNotFoundError:
            return False, f"Command not found: {cmd[0]}"
    
    def get_xrandr_info(self) -> Tuple[bool, str]:
        return self.run_command(['xrandr', '--query'])
    
    def get_kscreen_info(self) -> Tuple[bool, str]:
        return self.run_command(['kscreen-doctor', '-o'])
    
    def detect_displays(self) -> Dict[str, List[str]]:
        displays = {'xrandr': [], 'kscreen': []}
        
        success, output = self.get_xrandr_info()
        if success:
            for line in output.split('\n'):
                if ' connected' in line:
                    display_name = line.split()[0]
                    displays['xrandr'].append(display_name)
        
        success, output = self.get_kscreen_info()
        if success:
            for line in output.split('\n'):
                if 'Output:' in line:
                    parts = line.split()
                    if len(parts) >= 2:
                        displays['kscreen'].append(parts[1])
        
        return displays
    
    def get_display_signature(self) -> str:
        displays = self.detect_displays()
        display_list = sorted(displays['xrandr'] + displays['kscreen'])
        signature = hashlib.md5('|'.join(display_list).encode()).hexdigest()[:8]
        return signature
    
    def save_current_profile(self, name: str, backend: str = 'auto') -> bool:
        if backend == 'auto':
            xrandr_success, xrandr_config = self.get_xrandr_info()
            kscreen_success, kscreen_config = self.get_kscreen_info()
            
            if xrandr_success:
                backend = 'xrandr'
                config = xrandr_config
            elif kscreen_success:
                backend = 'kscreen'
                config = kscreen_config
            else:
                return False
        elif backend == 'xrandr':
            success, config = self.get_xrandr_info()
            if not success:
                return False
        elif backend == 'kscreen':
            success, config = self.get_kscreen_info()
            if not success:
                return False
        else:
            return False
        
        signature = self.get_display_signature()
        
        self.profiles[name] = {
            'backend': backend,
            'config': config,
            'signature': signature,
            'displays': self.detect_displays()
        }
        
        self.save_profiles()
        return True
    
    def apply_profile(self, name: str) -> bool:
        if name not in self.profiles:
            return False
        
        profile = self.profiles[name]
        backend = profile['backend']
        
        if backend == 'xrandr':
            return self.apply_xrandr_profile(profile)
        elif backend == 'kscreen':
            return self.apply_kscreen_profile(profile)
        
        return False
    
    def apply_xrandr_profile(self, profile: Dict) -> bool:
        config_lines = profile['config'].split('\n')
        cmd = ['xrandr']
        
        for line in config_lines:
            if ' connected' in line:
                parts = line.split()
                display_name = parts[0]
                
                if 'primary' in line:
                    cmd.extend(['--output', display_name, '--primary'])
                else:
                    cmd.extend(['--output', display_name])

                if len(parts) > 2 and 'x' in parts[2]:
                    resolution_info = parts[2]
                    if '+' in resolution_info:
                        res_parts = resolution_info.split('+')
                        resolution = res_parts[0]
                        if 'x' in resolution:
                            cmd.extend(['--mode', resolution])
                        if len(res_parts) >= 3:
                            cmd.extend(['--pos', f"{res_parts[1]}x{res_parts[2]}"])
        
        success, output = self.run_command(cmd)
        return success
    
    def apply_kscreen_profile(self, profile: Dict) -> bool:
        success, _ = self.run_command(['kscreen-doctor'])
        return success
    
    def list_profiles(self) -> List[str]:
        return list(self.profiles.keys())
    
    def delete_profile(self, name: str) -> bool:
        if name in self.profiles:
            del self.profiles[name]
            self.save_profiles()
            return True
        return False
    
    def auto_switch_profile(self) -> Optional[str]:
        current_signature = self.get_display_signature()
        
        for name, profile in self.profiles.items():
            if profile.get('signature') == current_signature:
                if self.apply_profile(name):
                    return name
        return None

class DisplayProfilesCLI:
    def __init__(self):
        self.manager = DisplayManager()

    def cmd_list(self, args):
        profiles = self.manager.list_profiles()
        if not profiles:
            print("No profiles saved.")
            return
        
        print("Saved profiles:")
        for name in profiles:
            profile = self.manager.profiles[name]
            backend = profile.get('backend', 'unknown')
            signature = profile.get('signature', 'none')
            print(f"  {name} ({backend}) [{signature}]")

    def cmd_save(self, args):
        if self.manager.save_current_profile(args.name, args.backend):
            print(f"Profile '{args.name}' saved successfully.")
        else:
            print(f"Failed to save profile '{args.name}'.")

    def cmd_apply(self, args):
        if self.manager.apply_profile(args.name):
            print(f"Profile '{args.name}' applied successfully.")
        else:
            print(f"Failed to apply profile '{args.name}'.")

    def cmd_delete(self, args):
        if self.manager.delete_profile(args.name):
            print(f"Profile '{args.name}' deleted.")
        else:
            print(f"Profile '{args.name}' not found.")

    def cmd_detect(self, args):
        displays = self.manager.detect_displays()
        signature = self.manager.get_display_signature()
        
        print(f"Display signature: {signature}")
        print("Connected displays:")
        
        if displays['xrandr']:
            print("  xrandr:", ', '.join(displays['xrandr']))
        
        if displays['kscreen']:
            print("  kscreen:", ', '.join(displays['kscreen']))

    def cmd_auto(self, args):
        if args.daemon:
            self.run_auto_daemon()
        else:
            profile = self.manager.auto_switch_profile()
            if profile:
                print(f"Auto-switched to profile: {profile}")
            else:
                print("No matching profile found for current display configuration.")

    def run_auto_daemon(self):
        print("Starting auto-switch daemon...")
        last_signature = self.manager.get_display_signature()
        
        try:
            while True:
                time.sleep(2)  
                current_signature = self.manager.get_display_signature()
                
                if current_signature != last_signature:
                    print(f"Display change detected: {last_signature} -> {current_signature}")
                    profile = self.manager.auto_switch_profile()
                    if profile:
                        print(f"Auto-switched to profile: {profile}")
                    last_signature = current_signature
                    
        except KeyboardInterrupt:
            print("\nDaemon stopped.")

class DisplayProfilesTUI:
    def __init__(self):
        self.manager = DisplayManager()
    
    def run(self, stdscr):
        curses.curs_set(0) 
        stdscr.keypad(True)
        
        current_selection = 0
        profiles = []
        
        while True:
            stdscr.clear()
            profiles = self.manager.list_profiles()
            
            stdscr.addstr(0, 0, "Display Profiles Manager", curses.A_BOLD)
            stdscr.addstr(1, 0, "=" * 40)
            
            options = [
                "List Profiles",
                "Save Current Profile",
                "Apply Profile",
                "Delete Profile", 
                "Detect Displays",
                "Auto Switch",
                "Exit"
            ]
            
            for i, option in enumerate(options):
                if i == current_selection:
                    stdscr.addstr(3 + i, 2, f"> {option}", curses.A_REVERSE)
                else:
                    stdscr.addstr(3 + i, 2, f"  {option}")

            if profiles:
                stdscr.addstr(12, 0, "Saved Profiles:", curses.A_BOLD)
                for i, name in enumerate(profiles):
                    profile = self.manager.profiles[name]
                    backend = profile.get('backend', 'unknown')
                    stdscr.addstr(13 + i, 2, f"{name} ({backend})")
            
            height, width = stdscr.getmaxyx()
            if height > 1:
                stdscr.addstr(height - 1, 0, "Use arrow keys to navigate, Enter to select, 'q' to quit")
            stdscr.refresh()
            
            key = stdscr.getch()
            
            if key == ord('q'):
                break
            elif key == curses.KEY_UP:
                current_selection = max(0, current_selection - 1)
            elif key == curses.KEY_DOWN:
                current_selection = min(len(options) - 1, current_selection + 1)
            elif key == curses.KEY_ENTER or key == 10:
                self.handle_selection(stdscr, current_selection, options)
    
    def handle_selection(self, stdscr, selection, options):
        if selection == 0:  
            profiles = self.manager.list_profiles()
            if not profiles:
                self.show_message(stdscr, "No profiles saved.")
            else:
                lines = []
                for name in profiles:
                    profile = self.manager.profiles[name]
                    backend = profile.get('backend', 'unknown')
                    signature = profile.get('signature', 'none')
                    lines.append(f"{name} ({backend}) [{signature}]")
                self.show_message(stdscr, "\n".join(lines))
        elif selection == 1:  
            name = self.get_input(stdscr, "Enter profile name: ")
            if name:
                if self.manager.save_current_profile(name):
                    self.show_message(stdscr, f"Profile '{name}' saved!")
                else:
                    self.show_message(stdscr, "Failed to save profile")
        elif selection == 2:  
            profiles = self.manager.list_profiles()
            if profiles:
                profile = self.select_from_list(stdscr, "Select profile to apply:", profiles)
                if profile:
                    if self.manager.apply_profile(profile):
                        self.show_message(stdscr, f"Profile '{profile}' applied!")
                    else:
                        self.show_message(stdscr, "Failed to apply profile")
            else:
                self.show_message(stdscr, "No profiles available")
        elif selection == 3: 
            profiles = self.manager.list_profiles()
            if profiles:
                profile = self.select_from_list(stdscr, "Select profile to delete:", profiles)
                if profile:
                    if self.manager.delete_profile(profile):
                        self.show_message(stdscr, f"Profile '{profile}' deleted!")
                    else:
                        self.show_message(stdscr, "Failed to delete profile")
            else:
                self.show_message(stdscr, "No profiles available")
        elif selection == 4:  
            displays = self.manager.detect_displays()
            signature = self.manager.get_display_signature()
            msg = f"Signature: {signature}\\n"
            if displays['xrandr']:
                msg += f"xrandr: {', '.join(displays['xrandr'])}\\n"
            if displays['kscreen']:
                msg += f"kscreen: {', '.join(displays['kscreen'])}"
            self.show_message(stdscr, msg)
        elif selection == 5: 
            profile = self.manager.auto_switch_profile()
            if profile:
                self.show_message(stdscr, f"Auto-switched to: {profile}")
            else:
                self.show_message(stdscr, "No matching profile found")
        elif selection == 6: 
            return
    
    def get_input(self, stdscr, prompt):
        curses.echo()
        stdscr.addstr(22, 0, prompt)
        stdscr.refresh()
        text = stdscr.getstr(22, len(prompt)).decode('utf-8')
        curses.noecho()
        return text
    
    def select_from_list(self, stdscr, prompt, items):
        selection = 0
        while True:
            stdscr.clear()
            stdscr.addstr(0, 0, prompt, curses.A_BOLD)
            
            for i, item in enumerate(items):
                if i == selection:
                    stdscr.addstr(2 + i, 2, f"> {item}", curses.A_REVERSE)
                else:
                    stdscr.addstr(2 + i, 2, f"  {item}")
            
            stdscr.addstr(len(items) + 4, 0, "Enter to select, 'q' to cancel")
            stdscr.refresh()
            
            key = stdscr.getch()
            if key == ord('q'):
                return None
            elif key == curses.KEY_UP:
                selection = max(0, selection - 1)
            elif key == curses.KEY_DOWN:
                selection = min(len(items) - 1, selection + 1)
            elif key == curses.KEY_ENTER or key == 10:
                return items[selection]
    
    def show_message(self, stdscr, message):
        stdscr.clear()
        lines = message.split('\\n')
        for i, line in enumerate(lines):
            stdscr.addstr(2 + i, 2, line)
        stdscr.addstr(len(lines) + 4, 2, "Press any key to continue...")
        stdscr.refresh()
        stdscr.getch()

def main():
    parser = argparse.ArgumentParser(description='Display Profiles Manager')
    parser.add_argument('--tui', action='store_true', help='Run in TUI mode')
    
    subparsers = parser.add_subparsers(dest='command', help='Available commands')
    
    subparsers.add_parser('list', help='List saved profiles')
    
    save_parser = subparsers.add_parser('save', help='Save current display configuration')
    save_parser.add_argument('name', help='Profile name')
    save_parser.add_argument('--backend', choices=['auto', 'xrandr', 'kscreen'], 
                           default='auto', help='Backend to use')
    
    apply_parser = subparsers.add_parser('apply', help='Apply saved profile')
    apply_parser.add_argument('name', help='Profile name')
    
    delete_parser = subparsers.add_parser('delete', help='Delete saved profile')
    delete_parser.add_argument('name', help='Profile name')
    
    subparsers.add_parser('detect', help='Detect connected displays')
    
    auto_parser = subparsers.add_parser('auto', help='Auto-switch profile')
    auto_parser.add_argument('--daemon', action='store_true', 
                           help='Run as daemon, monitoring for changes')
    
    args = parser.parse_args()
    
    if args.tui:
        if not CURSES_AVAILABLE:
            print("Error: curses not available. Install python3-curses package.")
            sys.exit(1)
        
        tui = DisplayProfilesTUI()
        curses.wrapper(tui.run)
    elif args.command:
        cli = DisplayProfilesCLI()
        cmd_method = getattr(cli, f'cmd_{args.command}', None)
        if cmd_method:
            cmd_method(args)
        else:
            parser.print_help()
    else:
        parser.print_help()

if __name__ == '__main__':
    main()