#ifndef POWERSAVE_DAEMONLIB_H
#define POWERSAVE_DAEMONLIB_H

#ifdef __cplusplus
extern "C" {
#endif
	
#include <unistd.h>
#include <stdio.h>
#include <stdint.h>
#include "powerlib.h"

	/* These functions obtain powersave related data
	   from the powersave daemon through a local socket connection 


	   such as rem. battery lifetime and percent, 
	   ac online/offline,
	   current CPU freq policy, 
	   Available powersave policy schemes
	   Whether suspend/standby is allowed
	   
	*/

	
#define POWERSAVED_SOCKET_PATH "/var/run/powersave_socket"
#define POWERSAVED_CLIENTSOCKET_PATH "/var/run/powersave_clientsocket"
#define MAX_SCHEME_NAME_LENGTH 255
#define MAX_PACKET_SIZE  4095

        typedef struct SchemeN SchemeNode;
	struct SchemeN{
		char name[MAX_SCHEME_NAME_LENGTH];
		int disable_display_settings;
		int disable_DPMS;
		int disable_screen_saver;
		int dpms_standby_timeout;
		int dpms_suspend_timeout;
		int dpms_off_timeout;
		SchemeNode *next;
	};
	typedef struct SchemeL SchemeList;
	struct SchemeL{
		SchemeNode* beginning;
		SchemeNode* current_scheme;
		SchemeNode* ac_scheme;
		SchemeNode* battery_scheme;
	};


	typedef struct PowerS PowerStruct;
	struct PowerS{
		/* enum ISACPI{NOT_SUPPORTED=-1, APM, ACPI}; -> powerlib.h */
		int isACPI;
		/* enum AC_ADAPTER_STATE{AC_UNKNOWN, AC_ONLINE, AC_OFFLINE}; -> powerlib.h */
		int AC_online;
		BatteryGeneral *bg;
		
		/* zero or one */
		int suspend_to_disk_disabled;
		int suspend_to_ram_disabled;
		int standby_disabled;
		int suspend_to_disk_available;
		int suspend_to_ram_available;
		int standby_available;

		/* POWERSAVED_ACTION_CPUFREQ_PERFORMANCE 
		   || POWERSAVED_ACTION_CPUFREQ_POWERSAVE
		   || POWERSAVED_ACTION_CPUFREQ_DYNAMIC */
		int cpufreq_policy;
		/* POWERSAVED_BATTERY_STATE_NORMAL
		   || POWERSAVED_BATTERY_STATE_WARNING
		   || POWERSAVED_BATTERY_STATE_LOW
		   || POWERSAVED_BATTERY_STATE_CRITICAL
		   || POWERSAVED_BATTERY_STATE_NO_BAT */
		int battery_state;
	};
		


        /* overwrites the given SchemeList *s with the current schemes.
	   Evertime you call this funktion without previously invocing
	   destroySchemeList(...), new memory will be allocated */
	int send_Action_get_all_Schemes(SchemeList* s);
	int send_Action_ActivateScheme(const char *schemeName);

	/* you need to invoke this function after invoking get_all_Schemes
	   to free memory of the list and addtitionally free the struct 
	   itself afterwards !*/
	void destroySchemeList(SchemeList *s);

	/* This one is to avoid multiple invokations of send_Action(int)
	   Each send_Action opens a socket connection (which may result in 
	   addititional query to the resmgr to check permissions)
	   
	   The data is placed bit by bit into the int64_t returned.
	   To evaluate the data pass the int64_t with the define used before
	   by send_Action and you receive the same result value as before 
	   by send_Action. Here is an example how to use this 
	   (see user_binary/powersave.c for more examples):

	   int64_t states = send_Action_get_States();
	   // maybe you want to check whether something changed since last call
	   if (states != old_states){
	         int current_CPUfreq_policy = evaluate_States(states, POWERSAVED_REQUEST_CPUFREQ_STATE_INFO);
		 switch (current_CPUfreq_policy){
		 case POWERSAVED_CPUFREQ_STATE_PERFORMANCE:
		       printf ("Currently CPU frequency policy is set to performance");
		 case ...:
		 }
		 int current_battery_state = evaluate_States(states, POWERSAVED_REQUEST_BATTERY_STATE_INFO);
		 switch (current_battery_state){
		 case POWERSAVED_BATTERY_STATE_WARNING:
		       printf ("Be carful, current battery state: Warning");
		       ...
		 }
	   }
	*/

	/* define only used by lib and daemon */
#define POWERSAVED_REQUEST_SYSTEM_STATE        0x000002FF

	int64_t send_Action_get_States(void);

	int evaluate_States(int64_t states, int data_to_evaluate);

	int send_Action(int action);

	int getPowerStruct(PowerStruct *power);

	

	/* higher protocols, will send back more than an integer 
	   you do not need these defines them in client apps, 
	   only daemon needs to know about the magic code.
	*/
#define POWERSAVED_REQUEST_ALL_SCHEMES              0x000000FF
#define POWERSAVED_SET_SCHEME                       0x000001FF

	/* These still have to be passed through send_Action
	   (as it would not make much sense to pack them into
	   one bitfield */
	
	/* will be ignored, just for seeing if the server is running*/
#define POWERSAVED_ACTION_PING                      0x00000001
	/* request machine to go into standby*/
#define POWERSAVED_ACTION_SUSPEND_TO_RAM            0x00000002
	/* request machine to go into standby*/
#define POWERSAVED_ACTION_SUSPEND_TO_DISK           0x00000003
 	/* request machine to go into suspend_to_disk*/
	/* be careful this was defined as 0x00000002 in older versions*/
#define POWERSAVED_ACTION_STNDBY                    0x00000010
	/* this concerns cpufrequency scaling*/
	/* set to daemon to performance (alway max frequency)*/
#define POWERSAVED_ACTION_CPUFREQ_PERFORMANCE       0x00000004
	/* set to kernel powersave (always min frequency*/
#define POWERSAVED_ACTION_CPUFREQ_POWERSAVE         0x00000005
	/* this enables dynamic management of the powersave daemon*/
#define POWERSAVED_ACTION_CPUFREQ_DYNAMIC           0x00000006
	/* specifies that an event has returned, successfully or no.
	   Only used internally by the daemon */
#define POWERSAVED_EVENT_RETURN                     0x00000011
	/* deprecated */
#define POWERSAVED_ACTION_SUSPEND                   0x00000003
#define POWERSAVED_ACTION_STANDBY                   0x00000002
	/* deprecated */


	/* These defines you should now use by calling first
	   int64_t system_states = send_Action_get_States();
	   and then evaluate the result with the function:

	   int result = evaluate_States(states, define_from_below);
	   Do not use the send_Action function in combination with
	   these defines any more as this functionality may vanish sooner 
	   or later
	*/
	/* request current cpufreq mode*/
#define POWERSAVED_REQUEST_CPUFREQ_STATE_INFO        0x00000007
	/* request current battery state*/
#define POWERSAVED_REQUEST_BATTERY_STATE_INFO        0x00000008
	/* are we on AC or battery power? */
#define POWERSAVED_REQUEST_ON_POWER_INFO             0x00000009
#define POWERSAVED_REQUEST_REM_PERCENT_BATTERY       0x0000000a
#define POWERSAVED_REQUEST_REM_CHARG_MINUTES_BATTERY 0x00000010
#define POWERSAVED_REQUEST_REM_MINUTES_BATTERY       0x0000000b
#define POWERSAVED_REQUEST_BAT_CHARGING_STATE        0x0000000c
#define POWERSAVED_REQUEST_SUSPEND_TO_RAM_ALLOWED    0x0000000d
#define POWERSAVED_REQUEST_SUSPEND_TO_DISK_ALLOWED   0x0000000e
#define POWERSAVED_REQUEST_STNDBY_ALLOWED            0x0000000f

	/* DEPRECATED */
#define POWERSAVED_REQUEST_STANDBY_ALLOWED           0x0000000d
#define POWERSAVED_REQUEST_SUSPEND_ALLOWED           0x0000000e
	/* DEPRECATED */

	/*****************************************************************/
	
	/***  Defined Error/Return codes returned by the server ************/

	/* always test on all three errors (-3, -2, -1 definitions) when using 
	   the functions:
	        int send_Action_get_all_Schemes(SchemeList* s);
		int send_Action_ActivateScheme(const char *schemeName);
		int64_t int send_Action_get_States(void);
		int send_Action(int action);
	*/

	/* This one normally indicates that no daemon is running.
	   Or possibly that an internal socket communication
	   error occured, in this case it is a bug (should not happen)*/

	/* GENERAL RETURN VALUES 
	   Always check for these, does not matter which function you use !!!
	*/
	/* probably daemon is not running*/
#define POWERSAVED_ERROR_SOCKET_ERROR          -0x00000003
	/* User has not enough rights to access daemon
	   (see -a parameter when invoking powersaved) */
#define POWERSAVED_NO_RIGHTS                   -0x00000002
	/* Indicates a general error */
#define POWERSAVED_ERROR                       -0x00000001
	/* Indicates success */
#define POWERSAVED_ERROR_NOERROR                0x00000001
	/* GENERAL RETURN VALUES */

	/* SPECIFIC RETURN VALUES */
	/*
	for: 
	send_Action(POWERSAVED_ACTION_CPUFREQ_PERFORMANCE); */ 
#define POWERSAVED_ERROR_CPUFREQ_NOT_SUPPORTED  0x00000003
#define POWERSAVED_CPUFREQ_MODE_ALREADY_SET     0x00000004
	
	/* return codes for 
	evaluate_States(X, POWERSAVED_ACTION_CPUFREQ_XXXpolicy); and 
 	send_Action(POWERSAVED_ACTION_CPUFREQ_XXXpolicy); (deprecated) 
	+ POWERSAVED_ERROR_CPUFREQ_NOT_SUPPORTED (above)
	*/ 
#define POWERSAVED_CPUFREQ_STATE_PERFORMANCE    0x00000005
#define POWERSAVED_CPUFREQ_STATE_POWERSAVE      0x00000006
#define POWERSAVED_CPUFREQ_STATE_DYNAMIC        0x00000007
	
	/* return codes for POWERSAVED_BATTERY_STATE_INFO*/
#define POWERSAVED_BATTERY_STATE_NORMAL         0x00000008
#define POWERSAVED_BATTERY_STATE_WARNING        0x00000009
#define POWERSAVED_BATTERY_STATE_LOW            0x0000000a
#define POWERSAVED_BATTERY_STATE_CRITICAL       0x0000000b
#define POWERSAVED_BATTERY_STATE_NO_BAT         0x00000015

	/* return codes for POWERSAVED_REQUEST_ON_POWER_INFO */
#define POWERSAVED_ON_AC_POWER                  0x0000000c
#define POWERSAVED_ON_BATTERY_POWER             0x0000000d
	/* return codes for POWERSAVED_REQUEST_BAT_CHARGIN_STATE */
#define POWERSAVED_BATTERY_CHARGING             0x0000000e
#define POWERSAVED_BATTERY_DISCHARGING          0x00000010
#define POWERSAVED_BATTERY_CHARG_DISCHARG       0x00000011
#define POWERSAVED_BATTERY_CHARG_UNKNOWN        0x00000012
	/* return codes for POWERSAVED_ACTION_STANDBY, 
	   POWERSAVED_ACTION_SUSPEND, POWERSAVED_REQUEST_STANDBY_ALLOWED and
	   POWERSAVED_REQUEST_SUSPEND_ALLOWED if disabled by configs*/
#define POWERSAVED_ERROR_SUSPEND_TO_DISK_DISABLED 0x00000013
#define POWERSAVED_ERROR_SUSPEND_TO_RAM_DISABLED  0x00000014
#define POWERSAVED_ERROR_STNDBY_DISABLED          0x00000018

	/* depecated */
#define POWERSAVED_SUSPEND_DISABLED               0x00000013
#define POWERSAVED_STANDBY_DISABLED               0x00000014
	/* depecated */

	/* additional return code for:
	   int send_Action_ActivateScheme(const char *schemeName); 
	   function
	*/
#define POWERSAVED_SCHEME_ALREADY_SET           0x00000016
#define POWERSAVED_SCHEME_NOT_SET               0x00000017
	/* SPECIFIC RETURN VALUES */
	/*****************************************************************/



#ifdef __cplusplus
}
#endif

#endif
