forked from ArthurkaX/cds-text-sync
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProject_Daemon.py
More file actions
579 lines (476 loc) · 20.8 KB
/
Project_Daemon.py
File metadata and controls
579 lines (476 loc) · 20.8 KB
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
# -*- coding: utf-8 -*-
"""
Project_Daemon.py - Background Hotkey Listener & Quick Actions for CODESYS Sync
- Runs in background (via hidden form polling).
- Listens for global ALT+Q keypress.
- Shows a non-blocking Quick Action Dashboard.
- Silent execution of Export/Import tasks.
Usage: Run once to Start. Run again to Stop.
"""
import sys
import os
import clr
import time
clr.AddReference("System.Windows.Forms")
clr.AddReference("System.Drawing")
from System.Windows.Forms import (
Application, Form, Label, FormBorderStyle, FormStartPosition,
Keys, Timer, Screen
)
from System.Drawing import Size, Point, Color, Font, FontStyle, SystemIcons
# --- Global State Management ---
if not hasattr(sys, "_codesys_daemon"):
sys._codesys_daemon = {
"timer": None,
"running": False,
"projects": None # Will be set by Start_Daemon.py wrapper
}
# Try to capture CODESYS global objects at runtime
# This function is called lazily when actions are executed
def try_capture_projects():
"""Attempt to capture the projects object using multiple strategies"""
# Strategy 1: Check if already captured by wrapper
if sys._codesys_daemon.get("projects"):
return sys._codesys_daemon["projects"]
# Strategy 2: Check __main__ module's globals
try:
import __main__
if hasattr(__main__, 'projects'):
return __main__.projects
except:
pass
# Strategy 3: Search sys.modules for a module with 'projects'
try:
for module_name, module in sys.modules.items():
if hasattr(module, 'projects'):
proj = getattr(module, 'projects')
# Verify it's the right type (has .primary attribute)
if hasattr(proj, 'primary'):
return proj
except:
pass
return None
# --- Quick Action Dashboard Form ---
class QuickActionForm(Form):
def __init__(self):
self.Text = "CODESYS Quick Actions"
self.FormBorderStyle = FormBorderStyle.None
self.StartPosition = FormStartPosition.CenterScreen
self.Size = Size(350, 280)
self.BackColor = Color.FromArgb(30, 30, 30) # Dark Theme
self.ForeColor = Color.White
self.TopMost = True
self.KeyPreview = True
self.Opacity = 0.95
# Title
lbl = Label()
lbl.Text = "CODESYS Sync Actions"
lbl.Location = Point(20, 15)
lbl.AutoSize = True
lbl.Font = Font("Segoe UI", 12, FontStyle.Bold)
lbl.ForeColor = Color.FromArgb(0, 122, 204) # Blue accent
self.Controls.Add(lbl)
# Instructions
help_text = "Press key to execute:\n\n" \
" [E] Export Source (Silent)\n" \
" [I] Import Source (Silent)\n" \
" [X] Export All (with XML)\n" \
" [B] Build Project (Compile)\n" \
" [C] Compare IDE vs Disk\n" \
" [P] Backup Project (.project)\n" \
"\n" \
" [D] Deactivate Daemon\n" \
" [Esc] Cancel"
lbl_help = Label()
lbl_help.Text = help_text
lbl_help.Location = Point(30, 50)
lbl_help.AutoSize = True
lbl_help.Font = Font("Consolas", 10, FontStyle.Regular)
self.Controls.Add(lbl_help)
# Footnote
lbl_foot = Label()
lbl_foot.Text = "Waiting for input..."
lbl_foot.Location = Point(20, 250)
lbl_foot.AutoSize = True
lbl_foot.ForeColor = Color.Gray
self.Controls.Add(lbl_foot)
# Event Handlers
self.KeyDown += self.on_key_down
self.Deactivate += self.on_deactivate
def on_deactivate(self, sender, args):
self.Close()
def on_key_down(self, sender, args):
key = args.KeyCode
# ESC -> Close Menu (Cancel)
if key == Keys.Escape:
self.Close()
return
# D -> Deactivate Daemon (Stop Script)
if key == Keys.D:
self.Close()
stop_daemon()
return
action = None
if key == Keys.E:
action = "EXPORT_SRC"
elif key == Keys.I:
action = "IMPORT_SRC"
elif key == Keys.X:
action = "EXPORT_ALL"
elif key == Keys.P:
action = "BACKUP_PROJ"
elif key == Keys.B:
action = "BUILD_PROJ"
elif key == Keys.C:
action = "COMPARE_PROJ"
if action:
self.Hide() # Hide immediately to show we are working
# Force UI update so hide happens
Application.DoEvents()
try:
self.execute_action(action)
except Exception as e:
# Show error if script failed
print("Error executing action: " + str(e))
# Re-show form if error? Or just toast?
try:
from codesys_ui import show_toast
show_toast("Error", "Script failed: " + str(e))
except:
pass
finally:
self.Close()
def execute_action(self, action):
from codesys_utils import load_base_dir
base_dir, error = load_base_dir()
if error:
print("Base dir error: " + error)
return
print("Executing " + action + "...")
# Get script directory
script_dir = os.path.dirname(os.path.abspath(__file__))
# Try to resolve projects/system objects for the script namespace
projects_obj = sys._codesys_daemon.get("projects")
if not projects_obj:
projects_obj = try_capture_projects()
if projects_obj:
sys._codesys_daemon["projects"] = projects_obj
system_obj = globals().get("system")
if action == "EXPORT_SRC":
# Force Source Only - Temp disable XML and Binary Backup
from codesys_utils import set_project_prop, get_project_prop
old_xml = get_project_prop("cds-sync-export-xml", False)
old_backup = get_project_prop("cds-sync-backup-binary", False)
set_project_prop("cds-sync-export-xml", False)
set_project_prop("cds-sync-backup-binary", False)
# Execute Project_export.py in current namespace to preserve CODESYS globals
export_script = os.path.join(script_dir, "Project_export.py")
try:
# Read the script
with open(export_script, "r") as f:
script_code = f.read()
# Create a namespace with the necessary globals
script_globals = globals().copy()
script_globals["__name__"] = "__main__"
script_globals["__file__"] = export_script
script_globals["SILENT"] = True # Force silent mode
# Explicitly inject CODESYS objects if we found them
if projects_obj:
script_globals["projects"] = projects_obj
if system_obj:
script_globals["system"] = system_obj
# Execute in that namespace
exec(script_code, script_globals)
except Exception as e:
print("Export error: " + str(e))
try:
from codesys_ui import show_toast
show_toast("Export Failed", str(e))
except:
pass
finally:
# Restore original settings
set_project_prop("cds-sync-export-xml", old_xml)
set_project_prop("cds-sync-backup-binary", old_backup)
elif action == "IMPORT_SRC":
# Execute Project_import.py in current namespace
import_script = os.path.join(script_dir, "Project_import.py")
try:
with open(import_script, "r") as f:
script_code = f.read()
script_globals = globals().copy()
script_globals["__name__"] = "__main__"
script_globals["__file__"] = import_script
script_globals["SILENT"] = True # Force silent mode
# Explicitly inject CODESYS objects if we found them
if projects_obj:
script_globals["projects"] = projects_obj
if system_obj:
script_globals["system"] = system_obj
exec(script_code, script_globals)
except Exception as e:
print("Import error: " + str(e))
try:
from codesys_ui import show_toast
show_toast("Import Failed", str(e))
except:
pass
elif action == "EXPORT_ALL":
# Force XML ON for this run
from codesys_utils import set_project_prop, get_project_prop
old_xml = get_project_prop("cds-sync-export-xml", False)
set_project_prop("cds-sync-export-xml", True)
try:
export_script = os.path.join(script_dir, "Project_export.py")
with open(export_script, "r") as f:
script_code = f.read()
script_globals = globals().copy()
script_globals["__name__"] = "__main__"
script_globals["__file__"] = export_script
script_globals["SILENT"] = True # Force silent mode
# Explicitly inject CODESYS objects if we found them
if projects_obj:
script_globals["projects"] = projects_obj
if system_obj:
script_globals["system"] = system_obj
exec(script_code, script_globals)
finally:
set_project_prop("cds-sync-export-xml", old_xml)
elif action == "BACKUP_PROJ":
from codesys_utils import backup_project_binary
print("Backing up .project file...")
# Get projects from globals (should be available in Daemon's namespace)
projects_obj = globals().get("projects")
if not projects_obj:
print("Error: 'projects' not available")
return
backup_project_binary(base_dir, projects_obj)
try:
from codesys_ui import show_toast
show_toast("Backup Complete", "Project binary saved to /project folder")
except: pass
elif action == "BUILD_PROJ":
# Execute Project_Build.py in current namespace
build_script = os.path.join(script_dir, "Project_Build.py")
try:
with open(build_script, "r") as f:
script_code = f.read()
script_globals = globals().copy()
script_globals["__name__"] = "__main__"
script_globals["__file__"] = build_script
script_globals["SILENT"] = True
if projects_obj:
script_globals["projects"] = projects_obj
if system_obj:
script_globals["system"] = system_obj
exec(script_code, script_globals)
except Exception as e:
print("Build error: " + str(e))
try:
from codesys_ui import show_toast
show_toast("Build Failed", str(e))
except:
pass
elif action == "COMPARE_PROJ":
# Execute Project_compare.py in current namespace
compare_script = os.path.join(script_dir, "Project_compare.py")
try:
with open(compare_script, "r") as f:
script_code = f.read()
script_globals = globals().copy()
script_globals["__name__"] = "__main__"
script_globals["__file__"] = compare_script
script_globals["SILENT"] = True
if projects_obj:
script_globals["projects"] = projects_obj
if system_obj:
script_globals["system"] = system_obj
exec(script_code, script_globals)
except Exception as e:
print("Compare error: " + str(e))
try:
from codesys_ui import show_toast
show_toast("Compare Failed", str(e))
except:
pass
# --- Hotkey Polling Logic ---
# Since we cannot easily specificy P/Invoke in pure IronPython script without compiling C# class,
# We rely on System.Windows.Forms.Control.ModifierKeys check in a Timer loop.
# This works ONLY if the script or CODESYS has focus? No, ModifierKeys is static but often Application bound.
# BUT: GetAsyncKeyState IS available via ctypes if python standard lib is present.
# IronPython often does NOT have ctypes standard lib.
#
# Alternative: Use a hidden form that uses RegisterHotKey via C# snippet compilation.
# This is the most robust way.
def create_hotkey_listener():
# Implementation of a polling listener using C# snippet for GetAsyncKeyState
# This avoids ctypes dependency issues
import clr
try:
clr.AddReference("System.Runtime.InteropServices")
pass
except:
pass
# We can try to compile a small class to call user32
# If that fails, we are stuck.
# Let's assume we can use the Timer + Control.ModifierKeys trick MIGHT work
# but usually only when app is focused.
#
# Actually, the user asked for "background even if changed desktop".
# P/Invoke is required.
# Simple Polling with ctypes (if available)
try:
import ctypes
GetAsyncKeyState = ctypes.windll.user32.GetAsyncKeyState
def check_hotkey():
# Check ALT (VK_MENU=0x12) and Q (0x51)
# GetAsyncKeyState returns short. MSB set if down.
# 0x8000 is the mask for "Currently Down"
alt_down = (GetAsyncKeyState(0x12) & 0x8000) != 0
q_down = (GetAsyncKeyState(0x51) & 0x8000) != 0
return alt_down and q_down
except:
# Fallback: We might not be able to listen globally easily.
# Let's try to load a C# class that does P/Invoke.
# This works in CODESYS usually.
src = """
using System;
using System.Runtime.InteropServices;
public class Win32 {
[DllImport("user32.dll")]
public static extern short GetAsyncKeyState(int vKey);
[DllImport("user32.dll")]
public static extern bool SetForegroundWindow(IntPtr hWnd);
[DllImport("user32.dll")]
public static extern bool BringWindowToTop(IntPtr hWnd);
[DllImport("user32.dll")]
public static extern IntPtr GetForegroundWindow();
}
"""
import clr
try:
# CodeDom provider usage
from Microsoft.CSharp import CSharpCodeProvider
from System.CodeDom.Compiler import CompilerParameters
params = CompilerParameters()
params.GenerateInMemory = True
provider = CSharpCodeProvider()
res = provider.CompileAssemblyFromSource(params, [src])
if res.Errors.Count > 0:
print("C# Compile Error: " + str(res.Errors[0]))
return None
assembly = res.CompiledAssembly
win32_type = assembly.GetType("Win32")
# Store win32 helper globally in the daemon state
sys._codesys_daemon["win32_helper"] = win32_type
def check_hotkey():
# VK_MENU=0x12, Q=0x51
# Use reflection to call static methods
state_alt = win32_type.GetMethod("GetAsyncKeyState").Invoke(None, [0x12])
state_q = win32_type.GetMethod("GetAsyncKeyState").Invoke(None, [0x51])
alt_down = (int(state_alt) & 0x8000) != 0
q_down = (int(state_q) & 0x8000) != 0
return alt_down and q_down
except Exception as e:
print("Failed to compile Win32 helper: " + str(e))
return None
return check_hotkey
# --- Main Logic ---
def on_tick(sender, args):
# Check hotkey
try:
# Safety check: if daemon was stopped, timer might be None
timer = sys._codesys_daemon.get("timer")
if not timer:
return
check_func = sys._codesys_daemon.get("check_func")
if check_func and check_func():
# Hotkey detected!
timer.Stop()
try:
# Capture previous window to restore focus later
prev_hwnd = None
try:
win32_helper = sys._codesys_daemon.get("win32_helper")
if win32_helper:
prev_hwnd = win32_helper.GetMethod("GetForegroundWindow").Invoke(None, None)
except: pass
# Show Dashboard
form = QuickActionForm()
# Setup focus trick on SHOWN (Load is too early)
def on_form_shown(s, e):
try:
form.Activate()
win32_helper = sys._codesys_daemon.get("win32_helper")
if win32_helper:
hwnd = form.Handle
# Call Win32 APIs for aggressive focus
win32_helper.GetMethod("BringWindowToTop").Invoke(None, [hwnd])
win32_helper.GetMethod("SetForegroundWindow").Invoke(None, [hwnd])
except Exception as ex:
print("Focus error: " + str(ex))
form.Shown += on_form_shown
# ShowDialog blocks until closed
form.ShowDialog()
# Restore focus to previous window
if prev_hwnd and win32_helper:
try:
# Small delay to let Windows process the closing animation?
# time.sleep(0.1)
win32_helper.GetMethod("SetForegroundWindow").Invoke(None, [prev_hwnd])
except: pass
except Exception as e:
print("Dashboard Error: " + str(e))
# Wait for keys to be released to prevent immediate re-trigger
time.sleep(0.5)
# ALWAYS attempt to resume if we haven't been stopped globally
if sys._codesys_daemon.get("timer") and sys._codesys_daemon.get("running"):
sys._codesys_daemon["timer"].Start()
except Exception as e:
print("Daemon Tick Error: " + str(e))
# Don't stop the timer here unless it's a fatal error
# Instead, just try to make sure it's running for the next tick
try:
t = sys._codesys_daemon.get("timer")
if t and sys._codesys_daemon.get("running"):
t.Start()
except: pass
def start_daemon():
print("Starting Daemon...")
check_func = create_hotkey_listener()
if not check_func:
system.ui.error("Could not initialize global key listener.\n(C# Compiler or P/Invoke failed).")
return
timer = Timer()
timer.Interval = 100 # 10Hz check
timer.Tick += on_tick
timer.Start()
sys._codesys_daemon["timer"] = timer
sys._codesys_daemon["check_func"] = check_func
sys._codesys_daemon["running"] = True
print("Daemon Started. Press ALT+Q to open dashboard.")
# Notify user
try:
from codesys_ui import show_toast
show_toast("Daemon Started", "Listening for ALT+Q...")
except:
pass
def stop_daemon():
if sys._codesys_daemon["timer"]:
sys._codesys_daemon["timer"].Stop()
sys._codesys_daemon["timer"].Dispose()
sys._codesys_daemon["timer"] = None
sys._codesys_daemon["running"] = False
print("Daemon Stopped.")
try:
from codesys_ui import show_toast
show_toast("Daemon Stopped", "Background listener deactivated.")
except:
pass
def main():
if sys._codesys_daemon["running"]:
stop_daemon()
else:
start_daemon()
if __name__ == "__main__":
main()