aboutsummaryrefslogtreecommitdiffstats
path: root/src/w32fns.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/w32fns.c')
-rw-r--r--src/w32fns.c142
1 files changed, 142 insertions, 0 deletions
diff --git a/src/w32fns.c b/src/w32fns.c
index 7fd7ce2eea8..1c780739bec 100644
--- a/src/w32fns.c
+++ b/src/w32fns.c
@@ -11337,6 +11337,135 @@ if the selected frame is not (yet) associated with a window handle */)
11337#endif /* WINDOWSNT */ 11337#endif /* WINDOWSNT */
11338 11338
11339/*********************************************************************** 11339/***********************************************************************
11340 System Sleep Support
11341 ***********************************************************************/
11342
11343typedef ULONG (WINAPI * SetThreadExecutionState_Proc) (IN ULONG);
11344static SetThreadExecutionState_Proc SetThreadExecutionState_fn = NULL;
11345
11346static unsigned int sleep_block_id = 0;
11347static unsigned int sleep_block_count = 0;
11348
11349DEFUN ("w32-block-system-sleep",
11350 Fw32_block_system_sleep,
11351 Sw32_block_system_sleep,
11352 1, 1, 0,
11353 doc: /* Block system idle sleep.
11354If ALLOW-DISPLAY-SLEEP is non-nil, block the screen from sleeping.
11355Return a token to unblock this block using `w32-unblock-system-sleep',
11356or nil if the block fails. */)
11357 (Lisp_Object allow_display_sleep)
11358{
11359 if (SetThreadExecutionState_fn == NULL)
11360 return Qnil;
11361
11362 /* ES_CONTINUOUS keeps the state until cleared. */
11363 EXECUTION_STATE new_state = ES_SYSTEM_REQUIRED | ES_CONTINUOUS;
11364 if (NILP (allow_display_sleep))
11365 new_state |= ES_DISPLAY_REQUIRED;
11366
11367 if (SetThreadExecutionState (new_state) == 0)
11368 return Qnil;
11369 else
11370 {
11371 /* One more block and next id. */
11372 ++sleep_block_count;
11373 ++sleep_block_id;
11374
11375 /* Synthesize a token. */
11376 return make_fixnum (sleep_block_id);
11377 }
11378}
11379
11380DEFUN ("w32-unblock-system-sleep",
11381 Fw32_unblock_system_sleep,
11382 Sw32_unblock_system_sleep,
11383 0, 0, 0,
11384 doc: /* Unblock system idle sleep.
11385Return non-nil if the TOKEN block was unblocked. */)
11386 (void)
11387{
11388 if (SetThreadExecutionState_fn == NULL)
11389 return Qnil;
11390
11391 /* No blocks to unblock. */
11392 if (sleep_block_count == 0)
11393 return Qnil;
11394
11395 /* One fewer block. */
11396 if (--sleep_block_count == 0
11397 && SetThreadExecutionState (ES_CONTINUOUS) == 0)
11398 return Qnil;
11399 else
11400 return Qt;
11401}
11402
11403DEFUN ("w32-system-sleep-block-count",
11404 Fw32_system_sleep_block_count,
11405 Sw32_system_sleep_block_count,
11406 0, 0, 0,
11407 doc: /* Return the w32 sleep block count. */)
11408 (void)
11409{
11410 return make_fixnum (sleep_block_count);
11411}
11412
11413typedef ULONG (CALLBACK *PMY_DEVICE_NOTIFY_CALLBACK_ROUTINE)
11414 (PVOID Context, ULONG Type, PVOID Setting);
11415
11416static ULONG CALLBACK ALIGN_STACK
11417sleep_notification_callback (PVOID _Context, ULONG Type, PVOID _Setting)
11418{
11419 struct input_event ie;
11420 EVENT_INIT (ie);
11421 ie.kind = SLEEP_EVENT;
11422
11423 switch (Type)
11424 {
11425 case PBT_APMRESUMEAUTOMATIC:
11426 /* Ignore this event. No user is present. */
11427 break;
11428 case PBT_APMSUSPEND:
11429 ie.arg = list1 (Qpre_sleep);
11430 kbd_buffer_store_event (&ie);
11431 break;
11432 case PBT_APMRESUMESUSPEND:
11433 ie.arg = list1 (Qpost_wake);
11434 kbd_buffer_store_event (&ie);
11435 break;
11436 }
11437 return 0;
11438}
11439
11440typedef HPOWERNOTIFY (WINAPI * RegisterSuspendResumeNotification_Proc)
11441 (IN HANDLE, IN DWORD);
11442static RegisterSuspendResumeNotification_Proc RegisterSuspendResumeNotification_fn = NULL;
11443
11444static HPOWERNOTIFY sleep_notification_handle = 0;
11445
11446typedef struct _MY_DEVICE_NOTIFY_SUBSCRIBE_PARAMETERS {
11447 PMY_DEVICE_NOTIFY_CALLBACK_ROUTINE Callback;
11448 PVOID Context;
11449} MY_DEVICE_NOTIFY_SUBSCRIBE_PARAMETERS, *PMY_DEVICE_NOTIFY_SUBSCRIBE_PARAMETERS;
11450
11451void
11452w32_register_for_sleep_notifications (void)
11453{
11454 /* PowerRegisterSuspendResumeNotification is not a user-space call so
11455 we use RegisterSuspendResumeNotification. */
11456 if (RegisterSuspendResumeNotification_fn)
11457 {
11458 MY_DEVICE_NOTIFY_SUBSCRIBE_PARAMETERS params;
11459 params.Callback = sleep_notification_callback;
11460 params.Context = NULL;
11461
11462 /* DEVICE_NOTIFY_CALLBACK = 2 */
11463 sleep_notification_handle =
11464 RegisterSuspendResumeNotification_fn (&params, 2);
11465 }
11466}
11467
11468/***********************************************************************
11340 Initialization 11469 Initialization
11341 ***********************************************************************/ 11470 ***********************************************************************/
11342 11471
@@ -11845,6 +11974,10 @@ keys when IME input is received. */);
11845 defsubr (&Sw32_request_user_attention); 11974 defsubr (&Sw32_request_user_attention);
11846 DEFSYM (Qinformational, "informational"); 11975 DEFSYM (Qinformational, "informational");
11847 DEFSYM (Qcritical, "critical"); 11976 DEFSYM (Qcritical, "critical");
11977 /* System sleep support. */
11978 defsubr (&Sw32_unblock_system_sleep);
11979 defsubr (&Sw32_block_system_sleep);
11980 defsubr (&Sw32_system_sleep_block_count);
11848#endif 11981#endif
11849} 11982}
11850 11983
@@ -12105,6 +12238,7 @@ void
12105globals_of_w32fns (void) 12238globals_of_w32fns (void)
12106{ 12239{
12107 HMODULE user32_lib = GetModuleHandle ("user32.dll"); 12240 HMODULE user32_lib = GetModuleHandle ("user32.dll");
12241 HMODULE kernel32_lib = GetModuleHandle ("kernel32.dll");
12108 /* 12242 /*
12109 TrackMouseEvent not available in all versions of Windows, so must load 12243 TrackMouseEvent not available in all versions of Windows, so must load
12110 it dynamically. Do it once, here, instead of every time it is used. 12244 it dynamically. Do it once, here, instead of every time it is used.
@@ -12131,6 +12265,14 @@ globals_of_w32fns (void)
12131 RegisterTouchWindow_fn 12265 RegisterTouchWindow_fn
12132 = (RegisterTouchWindow_proc) get_proc_addr (user32_lib, 12266 = (RegisterTouchWindow_proc) get_proc_addr (user32_lib,
12133 "RegisterTouchWindow"); 12267 "RegisterTouchWindow");
12268 /* For system sleep support. */
12269 SetThreadExecutionState_fn
12270 = (SetThreadExecutionState_Proc)
12271 get_proc_addr (kernel32_lib, "SetThreadExecutionState");
12272 RegisterSuspendResumeNotification_fn
12273 = (RegisterSuspendResumeNotification_Proc)
12274 get_proc_addr (user32_lib, "RegisterSuspendResumeNotification");
12275
12134 SetGestureConfig_fn 12276 SetGestureConfig_fn
12135 = (SetGestureConfig_proc) get_proc_addr (user32_lib, 12277 = (SetGestureConfig_proc) get_proc_addr (user32_lib,
12136 "SetGestureConfig"); 12278 "SetGestureConfig");