libthinkpad  2.6
https://github.com/libthinkpad
libthinkpad.h
1 /*
2  * Copyright (c) 2017 Ognjen Galić
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice,
9  * this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
15  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
18  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24  * POSSIBILITY OF SUCH DAMAGE.
25 */
26 
27 #ifndef LIBTHINKDOCK_LIBRARY_H
28 #define LIBTHINKDOCK_LIBRARY_H
29 
30 #define LIBTHINKPAD_MAJOR 2
31 #define LIBTHINKPAD_MINOR 6
32 
33 #include <string>
34 #include <vector>
35 #include <cstdio>
36 
37 #define IBM_DOCK "/sys/devices/platform/dock.2"
38 #define IBM_DOCK_DOCKED "/sys/devices/platform/dock.2/docked"
39 #define IBM_DOCK_MODALIAS "/sys/devices/platform/dock.2/modalias"
40 #define IBM_DOCK_ID "acpi:IBM0079:PNP0C15:LNXDOCK:\n"
41 #define ERR_INVALID (-1)
42 
43 // #define DRYRUN
44 
45 #define ACPI_POWERBUTTON "button/power PBTN"
46 #define ACPI_LID_OPEN "button/lid LID open"
47 #define ACPI_LID_CLOSE "button/lid LID close"
48 
49 #define ACPI_DOCK_EVENT "ibm/hotkey LEN0068:00 00000080 00004010"
50 #define ACPI_DOCK_EVENT2 "ibm/hotkey LEN0268:00 00000080 00004010"
51 #define ACPI_UNDOCK_EVENT "ibm/hotkey LEN0068:00 00000080 00004011"
52 #define ACPI_UNDOCK_EVENT2 "ibm/hotkey LEN0268:00 00000080 00004011"
53 
54 #define ACPI_BUTTON_BRIGHTNESS_UP "video/brightnessup BRTUP"
55 #define ACPI_BUTTON_BRIGHTNESS_DOWN "video/brightnessdown BRTDN"
56 #define ACPI_BUTTON_VOLUME_UP "button/volumeup VOLUP"
57 #define ACPI_BUTTON_VOLUME_DOWN "button/volumedown VOLDN"
58 #define ACPI_BUTTON_MICMUTE "button/f20 F20"
59 #define ACPI_BUTTON_MUTE "button/mute MUTE"
60 #define ACPI_BUTTON_THINKVANTAGE "button/prog1 PROG1"
61 #define ACPI_BUTTON_FNF2_LOCK "button/screenlock SCRNLCK"
62 #define ACPI_BUTTON_FNF3_BATTERY "button/battery BAT"
63 #define ACPI_BUTTON_FNF4_SLEEP "button/sleep SBTN"
64 #define ACPI_BUTTON_FNF5_WLAN "button/wlan WLAN"
65 #define ACPI_BUTTON_FNF7_PROJECTOR "video/switchmode VMOD"
66 #define ACPI_BUTTON_FNF12_HIBERNATE "button/suspend SUSP"
67 
68 #define ACPID_SOCK "/var/run/acpid.socket"
69 
70 #define SYSFS_THINKLIGHT "/sys/class/leds/tpacpi::thinklight/brightness"
71 #define SYSFS_MACHINECHECK "/sys/devices/system/machinecheck/machinecheck"
72 
73 #define SYSFS_BACKLIGHT_NVIDIA "/sys/class/backlight/nv_backlight"
74 #define SYSFS_BACKLIGHT_INTEL "/sys/class/backlight/intel_backlight"
75 
76 #define SYSFS_BATTERY_PRIMARY "/sys/class/power_supply/BAT0"
77 #define SYSFS_BATTERY_SECONDARY "/sys/class/power_supply/BAT1"
78 
79 #define BUFSIZE 128
80 #define INBUFSZ 1
81 
82 using std::string;
83 using std::vector;
84 
85 typedef int SUSPEND_REASON;
86 typedef int STATUS;
87 
91 namespace ThinkPad {
92 
96  namespace Hardware {
97 
102  class Dock {
103 
104  public:
105 
111  bool isDocked();
112 
118  bool probe();
119 
120  };
121 
122 
127  class ThinkLight {
128  public:
133  bool isOn();
134 
139  bool probe();
140 
141  };
142 
147  class Backlight {
148  private:
149 
150  enum System {
151  NVIDIA, INTEL
152  };
153 
154  int getMaxBrightness(System system);
155  int getCurrentBrightness(System system);
156  void setBrightness(System system, int value);
157 
158  public:
159 
164  void setBacklightLevel(float factor);
165 
170  float getBacklightLevel();
171  };
172 #if 0
173 
174  /* Disabled the BM API, the patch is not in mainline yet.
175  * https://patchwork.kernel.org/patch/10142173/
176  */
177 
182  class BatteryManager {
183  public:
184 
189  enum BatteryID {
190  BATTERY_PRIMARY,
191  BATTERY_SECONDARY
192  };
193 
199  int getChargeStartThreshold(BatteryID battery);
200 
206  int getChargeStopThreshold(BatteryID battery);
207 
213  int setChargeStopThreshold(BatteryID battery, int value);
214 
220  int setChargeStartThreshold(BatteryID battery, int value);
221 
227  bool probe(BatteryID battery);
228 
229  const char *getFRU(BatteryID battery);
230  const char *getOEM(BatteryID battery);
231 
232  };
233 #endif
234 
235  }
236 
237 
238 
243  namespace PowerManagement {
244 
245  class PowerStateManager;
246  class ACPIEventHandler;
247  class ACPI;
248 
252  enum ACPIEvent {
253 
258 
263 
268 
273 
278 
283 
288 
293 
298 
303 
308 
313 
318 
323 
328 
333 
338 
343 
348 
349  /*
350  * The brightness decrease button on the ThinkPad has been pressed
351  */
352  BUTTON_BRIGHTNESS_DOWN,
353 
354  /*
355  * The brightness increase button on the ThinkPad has been pressed
356  */
357  BUTTON_BRIGHTNESS_UP
358  };
359 
364  LID,
365  BUTTON
366  };
367 
371  struct _ACPIEventMetadata {
372  ACPIEvent event;
373  ACPIEventHandler *handler;
374  };
375 
376  typedef struct _ACPIEventMetadata ACPIEventMetadata;
377 
386 
387  private:
388 
394  static bool suspend();
395 
396  public:
404  static bool requestSuspend(SuspendReason reason);
405 
406  };
407 
415  class ACPI {
416  private:
417 
418  static void *handle_acpid(void*);
419  static void *handle_udev(void*);
420 
421  static pthread_t acpid_listener;
422  static pthread_t udev_listener;
423 
424  vector<ACPIEventHandler*> *ACPIhandlers;
425 
426  bool udev_running = true;
427 
428  public:
429 
430  ACPI();
431  ~ACPI();
432 
438  void addEventHandler(ACPIEventHandler *handler);
439 
444  void wait();
445 
449  void start();
450  };
451 
452 
464  public:
468  static void* _handleEvent(void* _this);
469 
476  virtual void handleEvent(ACPIEvent event) = 0;
477  };
478 
479  }
480 
481 
486  namespace Utilities {
487 
491  namespace Ini {
492 
497  {
498  public:
499 
500  char key[128];
501  char value[128];
502 
508  IniKeypair(const char *key, const char *value);
509  IniKeypair();
510 
511  };
512 
513 
515  {
516  public:
517 
518  ~IniSection();
519 
523  IniSection();
524 
530  IniSection(const char *name);
531 
532  char name[128];
533  vector<IniKeypair*> *keypairs = nullptr;
534 
540  const char *getString(const char *key) const;
541 
547  const void setString(const char *key, const char *value);
548 
557  const int getInt(const char *key) const;
558 
564  const void setInt(const char *key, const int value);
565 
571  const vector<int> getIntArray(const char *key) const;
572 
578  const void setIntArray(const char *key, vector<int> *values);
579 
585  const void setStringArray(const char *key, const vector<const char*> *strings);
586 
592  const vector<const char*> getStringArray(const char *key);
593  };
594 
595 
602  class Ini
603  {
604  vector<IniSection*> *sections = new vector<IniSection*>;
605 
606  public:
607  ~Ini();
608 
614  vector<IniSection*>* readIni(string path);
615 
621  bool writeIni(string path);
622 
623 
629  vector<IniSection*> getSections(const char *section);
630 
636  IniSection* getSection(const char *section);
637 
642  void addSection(IniSection* section);
643  };
644 
645  }
646 
651  class Versioning {
652  public:
653 
659  static int getMajorVersion() {
660  return LIBTHINKPAD_MAJOR;
661  }
662 
667  static int getMinorVersion() {
668  return LIBTHINKPAD_MINOR;
669  }
670 
671  };
672 
673  class CommonUtils {
674  public:
675  static const char *fileRead(const char *path);
676  static const int intRead(const char *path);
677  static int intWrite(const char *path, int value);
678  };
679 
680  }
681 
682 }
683 
684 
685 #endif
Definition: libthinkpad.h:257
Definition: libthinkpad.h:332
Definition: libthinkpad.h:262
Definition: libthinkpad.h:673
Definition: libthinkpad.h:282
static int getMinorVersion()
The minor version of the library.
Definition: libthinkpad.h:667
The main libthinkpad interface. This contains all the libthinkpad features.
Definition: libthinkpad.cpp:56
The Dock class is used to probe for the dock validity and probe for basic information about the dock...
Definition: libthinkpad.h:102
The ThinkLight class is ued to probe for the ThinkLight state and validity.
Definition: libthinkpad.h:127
bool probe()
Probes the dock if it is an IBM dock and if the dock is sane and ready for detection/state changes...
Definition: libthinkpad.cpp:76
SuspendReason
this defines the reason why a system suspend was requested
Definition: libthinkpad.h:363
static int getMajorVersion()
The major version of the library. If this changes, the ABI compat is broken.
Definition: libthinkpad.h:659
This handles the system power state and ACPI event dispatches.
Definition: libthinkpad.h:415
Definition: libthinkpad.h:272
Definition: libthinkpad.h:347
bool isDocked()
Check if the ThinkPad is physically docked into the UltraDock or the UltraBase.
Definition: libthinkpad.cpp:60
This class represents a .ini/.conf/.desktop file parser based on the Windows INI standard.
Definition: libthinkpad.h:602
Definition: libthinkpad.h:307
ACPIEvent
Varoius ACPI events that can occur on the ThinkPad.
Definition: libthinkpad.h:252
Class used for programs to access the library version.
Definition: libthinkpad.h:651
This is the abstract ACPI event handler class.
Definition: libthinkpad.h:463
Definition: libthinkpad.h:514
Definition: libthinkpad.h:267
Class for hardware power management.
Definition: libthinkpad.h:385
The backlight class is used to control the backlight level on the integrated laptop screen...
Definition: libthinkpad.h:147
Defines a keypair in a .ini file.
Definition: libthinkpad.h:496
Definition: libthinkpad.h:302
Definition: libthinkpad.h:292
Definition: libthinkpad.h:277
Definition: libthinkpad.h:287
Definition: libthinkpad.h:317