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
//! Activity monitoring and daemon management command.
//!
//! Handles the core functionality of kasl - monitoring user activity to automatically detect work sessions, breaks, and workday boundaries.
//!
//! ## Features
//!
//! - **Background Monitoring**: Runs as daemon to track activity automatically
//! - **Real-time Detection**: Immediate response to keyboard and mouse activity
//! - **Workday Management**: Automatic start/end detection for work sessions
//! - **Pause Tracking**: Records breaks and inactive periods
//! - **Foreground Debugging**: Debug mode with enhanced logging
//!
//! ## Usage
//!
//! ```bash
//! # Start background monitoring
//! kasl watch
//!
//! # Run in foreground for debugging
//! kasl watch --foreground
//!
//! # Stop background monitoring
//! kasl watch --stop
//! ```
use crate;
use cratemsg_print;
use Result;
use Args;
use instrument;
/// Command-line arguments for the watch command.
///
/// The watch command provides different operational modes to suit various use cases,
/// from daily background monitoring to debugging and development.
/// Main entry point for the watch command.
///
/// Acts as a dispatcher that routes to the appropriate operation based on the
/// provided command-line arguments, handling the three main operational modes.
///
/// # Arguments
///
/// * `args` - Parsed command-line arguments specifying the operation mode
///
/// # Returns
///
/// Returns `Ok(())` on successful operation completion, or an error if
/// the requested operation fails.
pub async
/// Core monitoring logic that initializes and runs the activity monitor.
///
/// This function is called either directly for foreground mode or by the daemon
/// process for background operation. It performs the following steps:
///
/// 1. **Configuration Loading**: Reads monitor settings from config file
/// 2. **Monitor Initialization**: Sets up input device listeners and database connections
/// 3. **Main Loop Execution**: Runs the continuous activity monitoring loop
///
/// ## Monitor Configuration
///
/// The monitor behavior is controlled by configuration settings:
/// - `pause_threshold`: Seconds of inactivity before recording a pause
/// - `poll_interval`: Milliseconds between activity checks
/// - `activity_threshold`: Seconds of activity needed to start a workday
/// - `min_pause_duration`: Minimum pause length to record (filters noise)
///
/// ## Activity Detection
///
/// The monitor tracks these input events:
/// - Keyboard presses and releases
/// - Mouse button clicks
/// - Mouse movement
/// - Mouse wheel scrolling
///
/// ## Database Operations
///
/// During monitoring, the system automatically:
/// - Creates workday records when sustained activity is detected
/// - Records pause start times when inactivity threshold is exceeded
/// - Records pause end times when activity resumes
/// - Updates workday end times when monitoring stops
///
/// # Returns
///
/// Returns `Ok(())` when monitoring completes normally, or an error if
/// initialization fails or a critical error occurs during monitoring.
///
/// # Error Scenarios
///
/// - Database connection failures
/// - Input device access denied
/// - Invalid configuration values
/// - System resource exhaustion
async
/// Entry point for daemon mode execution.
///
/// This function is called when the application is started with the `--daemon-run`
/// flag, which happens when the main process spawns a background daemon. It sets
/// up proper signal handling for graceful shutdown and runs the monitoring loop.
///
/// ## Signal Handling
///
/// The daemon process responds to these signals:
/// - **SIGTERM**: Graceful shutdown (Unix)
/// - **SIGINT**: Interrupt signal (Unix)
/// - **Ctrl+C**: Console interrupt (Windows)
///
/// ## Process Management
///
/// The daemon:
/// - Detaches from the parent process
/// - Creates a PID file for process tracking
/// - Handles cleanup on shutdown
/// - Logs operations for debugging
///
/// # Returns
///
/// Returns `Ok(())` when the daemon shuts down normally, or an error if
/// startup fails or a critical error occurs.
///
/// # Usage
///
/// This function is called internally by the application and should not be
/// called directly. It's triggered by the `--daemon-run` argument which is
/// used when spawning the background process.
pub async