summaryrefslogtreecommitdiff
blob: aac02864b0e41bc25265422256818ea9a3cae136 (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
From 2040a15d2a23ee4d16af8f2294e1593fa267e8d9 Mon Sep 17 00:00:00 2001
From: Aleksey Chernov <valexlin@gmail.com>
Date: Thu, 9 Sep 2021 10:41:38 +0400
Subject: [PATCH] Fixed wxWidgets build failure: compilation error due to API
 change. Introduced in c47cab44799475a48230312b8e97cd7a71edddb0.

---
 cr3wx/src/view.cpp | 82 ++++++++++++++++++++++++++++++++++------------
 1 file changed, 61 insertions(+), 21 deletions(-)

diff --git a/cr3wx/src/view.cpp b/cr3wx/src/view.cpp
index df2a264f7f..2ee30f9421 100644
--- a/cr3wx/src/view.cpp
+++ b/cr3wx/src/view.cpp
@@ -307,33 +307,73 @@ void cr3view::OnTimer(wxTimerEvent& event)
     }
 }
 
-void cr3view::Paint()
+static bool getBatteryState(int& state, int& chargingConn, int& level)
 {
-    //printf("cr3view::Paint() \n");
-    int battery_state = -1;
 #ifdef _WIN32
+    // update battery state
     SYSTEM_POWER_STATUS bstatus;
     BOOL pow = GetSystemPowerStatus(&bstatus);
-    if (bstatus.BatteryFlag & 128)
-        pow = FALSE;
-    if (bstatus.ACLineStatus!=0 || bstatus.BatteryLifePercent==255)
-        pow = FALSE;
-    if ( pow )
-        battery_state = bstatus.BatteryLifePercent;
+    if (pow) {
+        state = CR_BATTERY_STATE_DISCHARGING;
+        if (bstatus.BatteryFlag & 128)
+            state = CR_BATTERY_STATE_NO_BATTERY;  // no system battery
+        else if (bstatus.BatteryFlag & 8)
+            state = CR_BATTERY_STATE_CHARGING;    // charging
+        chargingConn = CR_BATTERY_CHARGER_NO;
+        if (bstatus.ACLineStatus==1)
+            chargingConn = CR_BATTERY_CHARGER_AC; // AC power charging connected
+        if (bstatus.BatteryLifePercent>=0 && bstatus.BatteryLifePercent<=100)
+            level = bstatus.BatteryLifePercent;
+        return true;
+    }
+    return false;
 #else
-    if ( ::wxGetPowerType() == wxPOWER_BATTERY ) {
-        int n = ::wxGetBatteryState();
-        if ( n == wxBATTERY_NORMAL_STATE )
-            battery_state = 100;
-        else if ( n == wxBATTERY_LOW_STATE )
-            battery_state = 50;
-        else if ( n == wxBATTERY_CRITICAL_STATE )
-            battery_state = 0;
-        else if ( n == wxBATTERY_SHUTDOWN_STATE )
-            battery_state = 0;
-    };
+    wxPowerType wxpwrtype = wxGetPowerType();
+    switch (wxpwrtype) {
+        case wxPOWER_SOCKET:
+            state = CR_BATTERY_STATE_CHARGING;
+            chargingConn = CR_BATTERY_CHARGER_AC;
+            break;
+        case wxPOWER_BATTERY:
+            state = CR_BATTERY_STATE_DISCHARGING;
+            chargingConn = CR_BATTERY_CHARGER_NO;
+            break;
+        default:
+            state = CR_BATTERY_STATE_NO_BATTERY;
+            chargingConn = CR_BATTERY_CHARGER_NO;
+            break;
+    }
+    wxBatteryState wxbatstate = wxGetBatteryState();
+    switch (wxbatstate) {
+        case wxBATTERY_NORMAL_STATE:
+            level = 100;
+            break;
+        case wxBATTERY_LOW_STATE:
+            level = 50;
+            break;
+        case wxBATTERY_CRITICAL_STATE:
+            level = 5;
+            break;
+        case wxBATTERY_SHUTDOWN_STATE:
+            level = 0;
+            break;
+        default:
+            level = 0;
+            break;
+    }
+    return true;
 #endif
-    getDocView()->setBatteryState( battery_state );
+}
+
+void cr3view::Paint()
+{
+    //printf("cr3view::Paint() \n");
+    int battery_state;
+    int charging_conn;
+    int charge_level;
+    if (getBatteryState(battery_state, charging_conn, charge_level)) {
+        getDocView()->setBatteryState( battery_state, charging_conn, charge_level );
+    }
     //_docview->Draw();
     UpdateScrollBar();
     Refresh( FALSE );