[NTOSKRNL]
[reactos.git] / reactos / ntoskrnl / po / events.c
index 51450fa..efbcc10 100644 (file)
@@ -1,18 +1,27 @@
 /*
- * COPYRIGHT:       See COPYING in the top level directory
- * PROJECT:         ReactOS kernel
+ * PROJECT:         ReactOS Kernel
+ * LICENSE:         GPL - See COPYING in the top level directory
  * FILE:            ntoskrnl/po/events.c
  * PURPOSE:         Power Manager
- *
  * PROGRAMMERS:     HervĂ© Poussineau (hpoussin@reactos.org)
  */
 
+/* INCLUDES ******************************************************************/
+
 #include <ntoskrnl.h>
 #define NDEBUG
-#include <internal/debug.h>
+#include <debug.h>
 
-PKWIN32_POWEREVENT_CALLOUT PopEventCallout;
-extern PCALLBACK_OBJECT SetSystemTimeCallback;
+/* GLOBALS *******************************************************************/
+
+typedef struct _SYS_BUTTON_CONTEXT
+{
+       PDEVICE_OBJECT DeviceObject;
+       PIO_WORKITEM WorkItem;
+       KEVENT Event;
+       IO_STATUS_BLOCK IoStatusBlock;
+       ULONG SysButton;
+} SYS_BUTTON_CONTEXT, *PSYS_BUTTON_CONTEXT;
 
 static VOID
 NTAPI
@@ -20,6 +29,11 @@ PopGetSysButton(
        IN PDEVICE_OBJECT DeviceObject,
        IN PVOID Context);
 
+PKWIN32_POWEREVENT_CALLOUT PopEventCallout;
+extern PCALLBACK_OBJECT SetSystemTimeCallback;
+
+/* FUNCTIONS *****************************************************************/
+
 VOID
 NTAPI
 PoNotifySystemTimeSet(VOID)
@@ -40,15 +54,6 @@ PoNotifySystemTimeSet(VOID)
     }
 }
 
-typedef struct _SYS_BUTTON_CONTEXT
-{
-       PDEVICE_OBJECT DeviceObject;
-       PIO_WORKITEM WorkItem;
-       KEVENT Event;
-       IO_STATUS_BLOCK IoStatusBlock;
-       ULONG SysButton;
-} SYS_BUTTON_CONTEXT, *PSYS_BUTTON_CONTEXT;
-
 static NTSTATUS
 NTAPI
 PopGetSysButtonCompletion(
@@ -74,6 +79,15 @@ PopGetSysButtonCompletion(
                if (SysButton & SYS_BUTTON_LID) DbgPrint(" LID");
                if (SysButton == 0) DbgPrint(" WAKE");
                DbgPrint(" )\n");
+        
+        if (SysButton & SYS_BUTTON_POWER)
+        {
+            /* FIXME: Read registry for the action we should perform here */
+            DPRINT1("Initiating shutdown after power button event\n");
+            
+            ZwShutdownSystem(ShutdownNoReboot);
+        }
+            
        }
 
        /* Allocate a new workitem to send the next IOCTL_GET_SYS_BUTTON_EVENT */
@@ -137,9 +151,8 @@ PopGetSysButton(
 
 NTSTATUS
 NTAPI
-PopAddRemoveSysCapsCallback(
-       IN PVOID NotificationStructure,
-       IN PVOID Context)
+PopAddRemoveSysCapsCallback(IN PVOID NotificationStructure,
+                            IN PVOID Context)
 {
        PDEVICE_INTERFACE_CHANGE_NOTIFICATION Notification;
        PSYS_BUTTON_CONTEXT SysButtonContext;
@@ -153,6 +166,8 @@ PopAddRemoveSysCapsCallback(
        BOOLEAN Arrival;
        ULONG Caps;
        NTSTATUS Status;
+       UNICODE_STRING DeviceName;
+       UNICODE_STRING DeviceNamePrefix = RTL_CONSTANT_STRING(L"\\??\\");
 
        DPRINT("PopAddRemoveSysCapsCallback(%p %p)\n",
                NotificationStructure, Context);
@@ -173,10 +188,20 @@ PopAddRemoveSysCapsCallback(
        {
                DPRINT("Arrival of %wZ\n", Notification->SymbolicLinkName);
 
+               DeviceName.Length = 0;
+               DeviceName.MaximumLength = Notification->SymbolicLinkName->MaximumLength + DeviceNamePrefix.MaximumLength;
+               DeviceName.Buffer = ExAllocatePool(PagedPool, DeviceName.MaximumLength);
+               if (!DeviceName.Buffer) return STATUS_INSUFFICIENT_RESOURCES;
+
+               RtlCopyUnicodeString(&DeviceName, &DeviceNamePrefix);
+               RtlAppendUnicodeStringToString(&DeviceName, Notification->SymbolicLinkName);
+
+               DPRINT("Opening handle to %wZ\n", &DeviceName);
+
                /* Open the device */
                InitializeObjectAttributes(
                        &ObjectAttributes,
-                       Notification->SymbolicLinkName,
+                       &DeviceName,
                        OBJ_KERNEL_HANDLE,
                        NULL,
                        NULL);