-
Notifications
You must be signed in to change notification settings - Fork 14
/
Battery.Mode.pas
691 lines (570 loc) · 23.7 KB
/
Battery.Mode.pas
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
unit Battery.Mode;
interface
uses
Winapi.Windows, Winapi.Messages,
System.Classes, System.SysUtils,
System.Generics.Collections, System.Generics.Defaults,
Core.Language, Core.Startup.Tasks,
Power,
Power.Schemes.Providers.Default, Power.Schemes.Providers.Legacy,
Power.WinApi.PowrProf,
Versions.Helpers,
Helpers.Services;
type
TBatteryState = record
Hint: string;
Percentage: DWORD;
PowerScheme: IPowerScheme;
PowerCondition: TSystemPowerCondition;
BatterySaver: Boolean;
EnergySaverBrightness: DWORD;
Mobile: Boolean;
BatteryPresent: Boolean;
LidSwitchOpen: Boolean;
end;
TEventBatteryStateChange = procedure(Sender: TObject; const State: TBatteryState) of object;
TEventUpdatePowerScheme = procedure(Sender: TObject; const PowerSchemeList: TPowerSchemeList) of object;
TEventPowerChange = procedure(Sender: TObject; Sleep: Boolean) of object;
TBatteryMode = class
strict private const
TimerSystemPowerStatusCheck = 1;
private
class var FPowerSchemeProvider: IPowerSchemeProvider;
class var FOnStateChange: TEventBatteryStateChange;
class var FOnUpdatePowerScheme: TEventUpdatePowerScheme;
class var FOnGlobalPowerSchemeChange: TEventBatteryStateChange;
class var FOnLocalPowerSchemeChanges: TEventBatteryStateChange;
class var FOnLocalPowerSchemeChanged: TEventBatteryStateChange;
class var FOnPowerChange: TEventPowerChange;
class var HBatteryMsgForm: HWND;
class var FBatteryState: TBatteryState;
class var NotifyBatteryPercentageRemaining : HPOWERNOTIFY;
class var NotifyAcdcPowerSource : HPOWERNOTIFY;
class var NotifyLidSwitchStateChange : HPOWERNOTIFY;
class var FTimerSystemPowerStatus: TSystemPowerStatus;
class var FBrightness: Byte;
class var FNotSetBrightness: Byte;
class var FBrightnessForAllScheme: Boolean;
class var FFirstChangeBrightnessFix: Boolean;
class procedure BatteryMsgFormHandle(var Msg: TMessage);
class function FormatBatteryMessage(Msg: string; const Status: TSystemPowerStatus): string;
class function SystemPowerStatysToIndex(const Status: TSystemPowerStatus; const State: TBatteryState): Integer;
class function SystemPowerStatusToHint(const Status: TSystemPowerStatus; const State: TBatteryState): string;
class procedure PowerSchemeProviderActivate(Sender: TObject; const PowerScheme: IPowerScheme);
class procedure PowerSchemeProviderUpdate(Sender: TObject; const PowerSchemes: TPowerSchemeList);
class procedure PowerSchemeProviderInternalActivating(Sender: TObject; const PowerScheme: IPowerScheme);
class procedure PowerSchemeProviderInternalActivated(Sender: TObject; const PowerScheme: IPowerScheme);
class procedure PowerSchemeProviderEnegrySaverSwitched(Sender: TObject; const Value: Boolean);
class procedure PowerSchemeProviderEnegrySaverBrightnessWeightChange(Sender: TObject; const Value: DWORD);
class function ACLineStatusToSystemPowerCondition(ACLineStatus : Byte): TSystemPowerCondition;
class procedure UpdateSystemPowerStatus(const Status: TSystemPowerStatus);
class procedure UpdateLidSwitchState(LidSwitchOpen: BOOL);
class function ApplyBrightness: Boolean;
class function GetIsBatteriesPresent: Boolean; static;
class function GetIsMobilePlatform: Boolean; static;
class procedure SetBrightness(const Value: Byte); static;
class procedure SetBrightnessForAllScheme(const Value: Boolean); static;
class function GetBatterySaver: Boolean; static;
class procedure SetBatterySaver(const Value: Boolean); static;
class procedure SetOnStateChange(const Value: TEventBatteryStateChange); static;
class procedure SetOnUpdatePowerScheme(const Value: TEventUpdatePowerScheme); static;
class procedure Init;
class procedure Done;
public
class procedure UpdateSchemes;
class procedure UpdateState;
class function NextScheme: Boolean;
class function CustomScheme(UniqueString: string): Boolean;
class function MaxPowerSavings: Boolean;
class function TypicalPowerSavings: Boolean;
class function MinPowerSavings: Boolean;
class function UltimatePowerSavings: Boolean;
class property PowerSchemes: IPowerSchemeProvider read FPowerSchemeProvider;
class property IsBatteriesPresent: Boolean read GetIsBatteriesPresent;
class property IsMobilePlatform: Boolean read GetIsMobilePlatform;
class property State: TBatteryState read FBatteryState;
class property Brightness: Byte read FBrightness write SetBrightness;
class property BrightnessForAllScheme: Boolean read FBrightnessForAllScheme write SetBrightnessForAllScheme;
class property BatterySaver: Boolean read GetBatterySaver write SetBatterySaver;
class property OnStateChange: TEventBatteryStateChange read FOnStateChange write SetOnStateChange;
class property OnUpdatePowerScheme: TEventUpdatePowerScheme read FOnUpdatePowerScheme write SetOnUpdatePowerScheme;
class property OnLocalPowerSchemeChanges: TEventBatteryStateChange read FOnLocalPowerSchemeChanges write FOnLocalPowerSchemeChanges;
class property OnLocalPowerSchemeChanged: TEventBatteryStateChange read FOnLocalPowerSchemeChanged write FOnLocalPowerSchemeChanged;
class property OnGlobalPowerSchemeChange: TEventBatteryStateChange read FOnGlobalPowerSchemeChange write FOnGlobalPowerSchemeChange;
class property OnPowerChange: TEventPowerChange read FOnPowerChange write FOnPowerChange;
end;
implementation
{ TBatteryMode }
{$REGION 'SETTERS and GETTERS'}
class procedure TBatteryMode.SetBrightness(const Value: Byte);
begin
FBrightness := Value;
ApplyBrightness;
end;
class procedure TBatteryMode.SetBrightnessForAllScheme(const Value: Boolean);
begin
if FBrightnessForAllScheme = Value then Exit;
FBrightnessForAllScheme := Value;
if FBrightnessForAllScheme then ApplyBrightness;
end;
class function TBatteryMode.GetBatterySaver: Boolean;
begin
Result := FBatteryState.BatterySaver;
end;
class procedure TBatteryMode.SetBatterySaver(const Value: Boolean);
begin
//
end;
class procedure TBatteryMode.SetOnStateChange(const Value: TEventBatteryStateChange);
begin
FOnStateChange := Value;
if Assigned(FOnStateChange) then FOnStateChange(nil, FBatteryState);
end;
class procedure TBatteryMode.SetOnUpdatePowerScheme(
const Value: TEventUpdatePowerScheme);
begin
FOnUpdatePowerScheme := Value;
if Assigned(FOnUpdatePowerScheme) then
FOnUpdatePowerScheme(nil, FPowerSchemeProvider.Schemes);
end;
{$ENDREGION}
{$REGION 'Formating Hint Message'}
class function TBatteryMode.FormatBatteryMessage(Msg: string;
const Status: TSystemPowerStatus): string;
var
Hour, Min, Sec, MSec, HourFull, MinFull, SecFull, MSecFull: Word;
begin
DecodeTime(Status.BatteryLifeTime/SecsPerDay, Hour, Min, Sec, MSec);
DecodeTime(Status.BatteryFullLifeTime/SecsPerDay, HourFull, MinFull, SecFull, MSecFull);
Result:= Format(Msg, [Hour, Min, Status.BatteryLifePercent, HourFull, MinFull]);
end;
class function TBatteryMode.SystemPowerStatysToIndex(
const Status: TSystemPowerStatus; const State: TBatteryState): Integer;
var
Hour, Min, Sec, MSec: Word;
function IsFlag(b: Byte; Flag: Byte): Boolean; inline;
begin
Result:= b and Flag = Flag;
end;
begin
DecodeTime(Status.BatteryLifeTime/SecsPerDay, Hour, Min, Sec, MSec);
with Status do begin
// Настольный компьютер
if not State.BatteryPresent and (State.PowerCondition <> PoHot) then
Exit(600);
// Батарея не обнаружена
if (State.PowerCondition = PoAc) and IsFlag(BatteryFlag, BATTERY_FLAG_NO_BATTERY) then
Exit(627);
// Подключена и заряжается
if (ACLineStatus = AC_LINE_ONLINE) and IsFlag(BatteryFlag, BATTERY_FLAG_CHARGING) and
(BatteryLifePercent = 255) then
Exit(621);
// %0:u ч. %1:.2u мин. до полной зарядки
if (ACLineStatus = AC_LINE_ONLINE) and IsFlag(BatteryFlag, BATTERY_FLAG_CHARGING) and
(BatteryLifeTime <> DWORD(-1)) and (Hour > 0) then
Exit(622);
// %1:u мин. до полной зарядки
if (ACLineStatus = AC_LINE_ONLINE) and IsFlag(BatteryFlag, BATTERY_FLAG_CHARGING) and
(BatteryLifeTime <> DWORD(-1)) and (Hour = 0) then
Exit(623);
// %2:u%% доступно (подключена, заряжается)
if (ACLineStatus = AC_LINE_ONLINE) and
IsFlag(BatteryFlag, BATTERY_FLAG_CHARGING) and
(BatteryLifePercent < 100) then
Exit(624);
// %2:u%% доступно (подключена, не заряжается)
if (ACLineStatus = AC_LINE_ONLINE) and
not IsFlag(BatteryFlag, BATTERY_FLAG_CHARGING) and
(BatteryLifePercent < 100) then
Exit(625);
// Полностью заряжена (100%%)
if (BatteryLifePercent = 100) then
Exit(626);
// Батарея не обнаружена
if IsFlag(BatteryFlag, BATTERY_FLAG_NO_BATTERY) then
Exit(627);
// Подключена и не заряжается
if (ACLineStatus = AC_LINE_ONLINE) and not IsFlag(BatteryFlag, BATTERY_FLAG_CHARGING) and
(BatteryLifePercent = 255) then
Exit(628);
// Осталось %0:u ч. %1:.2u мин. (%2:u%%)
if (ACLineStatus = AC_LINE_OFFLINE) and (BatteryLifePercent < 100) and
(BatteryLifeTime <> DWORD(-1)) and (Hour > 0) then
Exit(650);
// Осталось %1:u мин. (%2:u%%)
if (ACLineStatus = AC_LINE_OFFLINE) and (BatteryLifePercent < 100) and
(BatteryLifeTime <> DWORD(-1)) and (Hour = 0) then
Exit(651);
// Осталось %2:u%%
if (ACLineStatus = AC_LINE_OFFLINE) and (BatteryLifePercent < 100) and
(BatteryLifeTime = DWORD(-1)) then
Exit(654);
// Нет данных
if (ACLineStatus = AC_LINE_UNKNOWN) or
(IsFlag(BatteryFlag, BATTERY_FLAG_UNKNOWN)) then
Exit(655);
Result := 655;
end;
end;
class function TBatteryMode.SystemPowerStatusToHint(
const Status: TSystemPowerStatus; const State: TBatteryState): string;
var
RawMsg: string;
begin
RawMsg:= TLang[SystemPowerStatysToIndex(Status, State)];
Result:= FormatBatteryMessage(RawMsg, Status);
end;
{$ENDREGION}
class function TBatteryMode.GetIsBatteriesPresent: Boolean;
var
PowerCapabilities: SYSTEM_POWER_CAPABILITIES;
begin
Result := False;
if GetPwrCapabilities(PowerCapabilities) then
Result := PowerCapabilities.SystemBatteriesPresent;
end;
class function TBatteryMode.GetIsMobilePlatform: Boolean;
var
PlatformRole: POWER_PLATFORM_ROLE;
begin
if IsWindows8OrGreater then
begin
PlatformRole := PowerDeterminePlatformRoleEx(POWER_PLATFORM_ROLE_V2);
Result := (PlatformRole = PlatformRoleMobile) or (PlatformRole = PlatformRoleSlate);
end
else if IsWindowsVistaOrGreater then
Result := PowerDeterminePlatformRole = PlatformRoleMobile
else
Result := IsBatteriesPresent;
end;
class procedure TBatteryMode.UpdateSchemes;
begin
FPowerSchemeProvider.CheckForUpdates;
end;
class procedure TBatteryMode.UpdateState;
var
SystemPowerStatus: TSystemPowerStatus;
begin
GetSystemPowerStatus(SystemPowerStatus);
UpdateSystemPowerStatus(SystemPowerStatus);
end;
class function TBatteryMode.NextScheme: Boolean;
var
Index: Integer;
begin
if not Assigned(FPowerSchemeProvider) then Exit(False);
if FPowerSchemeProvider.Schemes.Count = 0 then Exit(False);
Index := FPowerSchemeProvider.Schemes.IndexOf(FPowerSchemeProvider.Active);
if Index < 0 then
FPowerSchemeProvider.CheckForUpdates;
if Index = FPowerSchemeProvider.Schemes.Count - 1 then
Result := FPowerSchemeProvider.Schemes.First.Activate
else if Index >= 0 then
Result := FPowerSchemeProvider.Schemes[Index + 1].Activate
else
Result := FPowerSchemeProvider.Schemes.First.Activate;
end;
class function TBatteryMode.CustomScheme(UniqueString: string): Boolean;
var
PowerScheme: IPowerScheme;
begin
if not Assigned(FPowerSchemeProvider) then Exit(False);
PowerScheme := FPowerSchemeProvider.Schemes.Find(UniqueString);
if PowerScheme <> nil then
Exit(PowerScheme.Activate);
Result := False;
end;
class function TBatteryMode.MaxPowerSavings: Boolean;
var
PowerScheme: IPowerScheme;
begin
if not Assigned(FPowerSchemeProvider) then Exit(False);
Result := False;
for PowerScheme in FPowerSchemeProvider.Schemes do
if PowerScheme.PowerSchemeType = pstMaxPowerSavings then begin
PowerScheme.Activate;
Exit(True);
end;
end;
class function TBatteryMode.TypicalPowerSavings: Boolean;
var
PowerScheme: IPowerScheme;
begin
if not Assigned(FPowerSchemeProvider) then Exit(False);
Result := False;
for PowerScheme in FPowerSchemeProvider.Schemes do
if PowerScheme.PowerSchemeType = pstTypicalPowerSavings then begin
PowerScheme.Activate;
Exit(True);
end;
end;
class function TBatteryMode.MinPowerSavings: Boolean;
var
PowerScheme: IPowerScheme;
begin
if not Assigned(FPowerSchemeProvider) then Exit(False);
Result := False;
for PowerScheme in FPowerSchemeProvider.Schemes do
if PowerScheme.PowerSchemeType = pstMinPowerSavings then begin
PowerScheme.Activate;
Exit(True);
end;
end;
class function TBatteryMode.UltimatePowerSavings: Boolean;
var
PowerScheme: IPowerScheme;
begin
if not Assigned(FPowerSchemeProvider) then Exit(False);
Result := False;
for PowerScheme in FPowerSchemeProvider.Schemes do
if PowerScheme.PowerSchemeType = pstUltimatePowerSavings then begin
PowerScheme.Activate;
Exit(True);
end;
end;
class procedure TBatteryMode.BatteryMsgFormHandle(var Msg: TMessage);
var
PowerBroadcastSetting: TPowerBroadcastSetting;
SystemPowerStatus: TSystemPowerStatus;
begin
Msg.Result := DefWindowProc(HBatteryMsgForm, Msg.Msg, Msg.WParam, Msg.LParam);
if Msg.Msg = WM_TIMER then
begin
case Msg.WParam of
TimerSystemPowerStatusCheck:
begin
GetSystemPowerStatus(SystemPowerStatus);
if not CompareMem(@FTimerSystemPowerStatus, @SystemPowerStatus, SizeOf(TSystemPowerStatus)) then
begin
UpdateSystemPowerStatus(SystemPowerStatus);
FTimerSystemPowerStatus := SystemPowerStatus;
end;
end;
end;
Exit;
end;
if Msg.Msg = WM_POWERBROADCAST then
begin
case Msg.WParam of
PBT_POWERSETTINGCHANGE:
begin
PowerBroadcastSetting:= PPowerBroadcastSetting(Msg.LParam)^;
if PowerBroadcastSetting.PowerSetting = GUID_BATTERY_PERCENTAGE_REMAINING then
begin
// Изменился заряд батареи
GetSystemPowerStatus(SystemPowerStatus);
UpdateSystemPowerStatus(SystemPowerStatus);
Exit;
end;
if PowerBroadcastSetting.PowerSetting = GUID_ACDC_POWER_SOURCE then
begin
// Изменился источник питания
GetSystemPowerStatus(SystemPowerStatus);
UpdateSystemPowerStatus(SystemPowerStatus);
Exit;
end;
if PowerBroadcastSetting.PowerSetting = GUID_LIDSWITCH_STATE_CHANGE then
begin
// Изменилось состояние крышки ноутбука
UpdateLidSwitchState(PBOOL(@PowerBroadcastSetting.Data)^);
Exit;
end;
end;
PBT_APMPOWERSTATUSCHANGE:
begin
GetSystemPowerStatus(SystemPowerStatus);
UpdateSystemPowerStatus(SystemPowerStatus);
end;
PBT_APMSUSPEND:
if Assigned(FOnPowerChange) then FOnPowerChange(nil, True);
PBT_APMRESUMEAUTOMATIC:
if Assigned(FOnPowerChange) then FOnPowerChange(nil, False);
end;
Exit;
end;
end;
class procedure TBatteryMode.PowerSchemeProviderActivate(Sender: TObject;
const PowerScheme: IPowerScheme);
begin
FNotSetBrightness := UnknownBrightness;
FFirstChangeBrightnessFix := False;
FBatteryState.PowerScheme := PowerScheme;
if Assigned(FOnGlobalPowerSchemeChange) then
FOnGlobalPowerSchemeChange(nil, FBatteryState);
if Assigned(FOnStateChange) then FOnStateChange(nil, FBatteryState);
end;
class procedure TBatteryMode.PowerSchemeProviderUpdate(Sender: TObject;
const PowerSchemes: TPowerSchemeList);
begin
if Assigned(FOnUpdatePowerScheme) then FOnUpdatePowerScheme(nil, FPowerSchemeProvider.Schemes);
end;
class procedure TBatteryMode.PowerSchemeProviderInternalActivating(
Sender: TObject; const PowerScheme: IPowerScheme);
begin
FBatteryState.PowerScheme := PowerScheme;
if Assigned(FOnLocalPowerSchemeChanges) then
FOnLocalPowerSchemeChanges(nil, FBatteryState);
end;
class procedure TBatteryMode.PowerSchemeProviderInternalActivated(
Sender: TObject; const PowerScheme: IPowerScheme);
begin
if Assigned(FOnLocalPowerSchemeChanged) then
FOnLocalPowerSchemeChanged(nil, FBatteryState);
end;
class procedure TBatteryMode.PowerSchemeProviderEnegrySaverSwitched(
Sender: TObject; const Value: Boolean);
begin
if FBatteryState.BatterySaver = Value then Exit;
FFirstChangeBrightnessFix := False;
FBatteryState.BatterySaver := Value;
if Assigned(FOnStateChange) then FOnStateChange(nil, FBatteryState);
end;
class procedure TBatteryMode.PowerSchemeProviderEnegrySaverBrightnessWeightChange(
Sender: TObject; const Value: DWORD);
begin
if FBatteryState.EnergySaverBrightness = Value then Exit;
FBatteryState.EnergySaverBrightness := Value;
if Assigned(FOnStateChange) then FOnStateChange(nil, FBatteryState);
end;
class function TBatteryMode.ACLineStatusToSystemPowerCondition(
ACLineStatus: Byte): TSystemPowerCondition;
begin
case ACLineStatus of
AC_LINE_OFFLINE : Exit(PoDc);
AC_LINE_ONLINE : Exit(PoAc);
AC_LINE_BACKUP_POWER : Exit(PoHot);
else Exit(PoConditionMaximum);
end;
end;
class procedure TBatteryMode.UpdateSystemPowerStatus(const Status: TSystemPowerStatus);
var
PowerCondition: TSystemPowerCondition;
begin
FBatteryState.Percentage := Status.BatteryLifePercent;
FBatteryState.BatteryPresent := IsBatteriesPresent;
PowerCondition := ACLineStatusToSystemPowerCondition(Status.ACLineStatus);
if PowerCondition <> FBatteryState.PowerCondition then
begin
// Изменился источник питания
FFirstChangeBrightnessFix := True;
FBatteryState.PowerCondition := PowerCondition;
FBatteryState.Mobile := IsMobilePlatform;
end;
FBatteryState.Hint := SystemPowerStatusToHint(Status, FBatteryState);
if Assigned(FOnStateChange) then FOnStateChange(nil, FBatteryState);
end;
class procedure TBatteryMode.UpdateLidSwitchState(LidSwitchOpen: BOOL);
begin
if FBatteryState.LidSwitchOpen = LidSwitchOpen then Exit;
FBatteryState.LidSwitchOpen := LidSwitchOpen;
if Assigned(FOnStateChange) then FOnStateChange(nil, FBatteryState);
end;
class function TBatteryMode.ApplyBrightness: Boolean;
var
PreparedBrightness: DWORD;
begin
Result := True;
try
if FBrightness = UnknownBrightness then Exit(True);
if FBrightnessForAllScheme then begin
if FFirstChangeBrightnessFix then
FBrightness := FNotSetBrightness;
if FBrightness = UnknownBrightness then Exit(True);
if FBatteryState.BatterySaver then
PreparedBrightness := FBrightness * 100 div FBatteryState.EnergySaverBrightness
else
PreparedBrightness := FBrightness;
try
FPowerSchemeProvider.BrightnessForAllScheme[PoAc] := PreparedBrightness;
FPowerSchemeProvider.BrightnessForAllScheme[PoDc] := PreparedBrightness;
except
Result := False;
end;
if FFirstChangeBrightnessFix then
FPowerSchemeProvider.ReActivate;
FNotSetBrightness := FBrightness;
end else begin
if FBatteryState.BatterySaver then
PreparedBrightness := FBrightness * 100 div FBatteryState.EnergySaverBrightness
else
PreparedBrightness := FBrightness;
try
FPowerSchemeProvider.Active.Brightness[FBatteryState.PowerCondition] := PreparedBrightness;
except
Result := False;
end;
end;
finally
FFirstChangeBrightnessFix := False;
end;
end;
class procedure TBatteryMode.Init;
var
SystemPowerStatus: TSystemPowerStatus;
begin
if IsWindowsVistaOrGreater then
begin
if not IsServiceRunning(nil, 'Power') then
begin
MessageBox(0, LPCTSTR(TLang[3000]), LPCTSTR(TLang[1]), MB_OK or MB_ICONERROR);
ExitProcess(TStartupTasks.ERROR_PowerService);
end;
FPowerSchemeProvider := TPowerSchemeProvider.Create;
end
else
FPowerSchemeProvider := TPowerSchemeProviderLegacy.Create;
FPowerSchemeProvider.OnUpdate := PowerSchemeProviderUpdate;
FPowerSchemeProvider.OnActivate := PowerSchemeProviderActivate;
FPowerSchemeProvider.OnInternalActivating := PowerSchemeProviderInternalActivating;
FPowerSchemeProvider.OnInternalActivated := PowerSchemeProviderInternalActivated;
FPowerSchemeProvider.OnEnegrySaverSwitched := PowerSchemeProviderEnegrySaverSwitched;
FPowerSchemeProvider.OnEnegrySaverBrightnessWeightChange := PowerSchemeProviderEnegrySaverBrightnessWeightChange;
FBrightness := UnknownBrightness;
FNotSetBrightness := UnknownBrightness;
FBrightnessForAllScheme := False;
FFirstChangeBrightnessFix := False;
GetSystemPowerStatus(SystemPowerStatus);
FBatteryState.Percentage := SystemPowerStatus.BatteryLifePercent;
FBatteryState.PowerScheme := FPowerSchemeProvider.Active;
FBatteryState.PowerCondition := ACLineStatusToSystemPowerCondition(SystemPowerStatus.ACLineStatus);
FBatteryState.BatterySaver := FPowerSchemeProvider.EnegrySaver;
FBatteryState.EnergySaverBrightness := FPowerSchemeProvider.Active.EnegrySaverBrightnessWeight[FBatteryState.PowerCondition];
FBatteryState.BatteryPresent := IsBatteriesPresent;
FBatteryState.Mobile := IsMobilePlatform;
FBatteryState.Hint := SystemPowerStatusToHint(SystemPowerStatus, FBatteryState);
FBatteryState.LidSwitchOpen := True;
HBatteryMsgForm := AllocateHWnd(BatteryMsgFormHandle);
if IsWindowsVistaOrGreater then
begin
// NotifyLidSwitchStateChange должен быть зарегистрирован первым
// из за невозможности получить значение без события
NotifyLidSwitchStateChange := RegisterPowerSettingNotification(
HBatteryMsgForm, GUID_LIDSWITCH_STATE_CHANGE, DEVICE_NOTIFY_WINDOW_HANDLE);
NotifyBatteryPercentageRemaining := RegisterPowerSettingNotification(
HBatteryMsgForm, GUID_BATTERY_PERCENTAGE_REMAINING, DEVICE_NOTIFY_WINDOW_HANDLE);
NotifyAcdcPowerSource := RegisterPowerSettingNotification(
HBatteryMsgForm, GUID_ACDC_POWER_SOURCE, DEVICE_NOTIFY_WINDOW_HANDLE);
end
else
begin
FTimerSystemPowerStatus := SystemPowerStatus;
if SetTimer(HBatteryMsgForm, TimerSystemPowerStatusCheck, 1000, nil) = 0 then
RaiseLastOSError;
end;
end;
class procedure TBatteryMode.Done;
begin
KillTimer(HBatteryMsgForm, TimerSystemPowerStatusCheck);
if NotifyAcdcPowerSource <> nil then
UnregisterPowerSettingNotification(NotifyAcdcPowerSource);
if NotifyBatteryPercentageRemaining <> nil then
UnregisterPowerSettingNotification(NotifyBatteryPercentageRemaining);
if NotifyLidSwitchStateChange <> nil then
UnregisterPowerSettingNotification(NotifyLidSwitchStateChange);
DeallocateHWnd(HBatteryMsgForm);
end;
initialization
TBatteryMode.Init;
finalization
TBatteryMode.Done;
end.