/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-â
âvi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8                                :viâ
ââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ¡
â Copyright 2021 Justine Alexandra Roberts Tunney                              â
â                                                                              â
â Permission to use, copy, modify, and/or distribute this software for         â
â any purpose with or without fee is hereby granted, provided that the         â
â above copyright notice and this permission notice appear in all copies.      â
â                                                                              â
â THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL                â
â WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED                â
â WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE             â
â AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL         â
â DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR        â
â PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER               â
â TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR             â
â PERFORMANCE OF THIS SOFTWARE.                                                â
ââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ*/
#include "libc/calls/calls.h"
#include "libc/calls/struct/framebuffervirtualscreeninfo.h"
#include "libc/dce.h"
#include "libc/nt/enum/version.h"
#include "libc/nt/system.h"
#include "libc/runtime/runtime.h"
#include "libc/sysv/errfuns.h"

#define kNtShutdownForceOthers   1
#define kNtShutdownForceSelf     2
#define kNtShutdownGraceOverride 0x20
#define kNtShutdownHybrid        0x200

int32_t sys_reboot_linux(int32_t, int32_t, int32_t) asm("sys_reboot");
int32_t sys_reboot_bsd(int32_t, const char *) asm("sys_reboot");

/**
 * Reboots system.
 *
 * The `howto` argument may be one of the following:
 *
 * - `RB_AUTOBOOT`
 * - `RB_POWER_OFF`
 * - `RB_HALT_SYSTEM`
 * - `RB_SW_SUSPEND` (linux and windows only)
 * - `RB_KEXEC` (linux only)
 * - `RB_ENABLE_CAD` (linux only)
 * - `RB_DISABLE_CAD` (linux only)
 *
 * There's an implicit `sync()` operation before the reboot happens.
 * This can be prevented by or'ing `howto` with `RB_NOSYNC`. Setting
 * this option will also prevent apps on Windows from having time to
 * close themselves.
 */
int reboot(int howto) {
  bool ok, immediately;
  if (howto != -1) {
    if (!(howto & 0x20000000)) {
      sync();
      immediately = false;
    } else {
      howto &= ~0x20000000;
      immediately = true;
    }
    if (!IsWindows()) {
      if (IsLinux()) {
        return sys_reboot_linux(0xFEE1DEAD, 0x28121969, howto);
      } else {
        return sys_reboot_bsd(howto, 0);
      }
    } else {
      if (howto == 0xD000FCE2u) {
        ok = !!SetSuspendState(0, 0, 0);
      } else {
        howto |= kNtShutdownForceSelf;
        howto |= kNtShutdownForceOthers;
        if (NtGetVersion() >= kNtVersionWindows8) {
          howto |= kNtShutdownHybrid;
        }
        if (immediately) {
          ok = !!InitiateShutdown(0, 0, 0, howto | kNtShutdownGraceOverride, 0);
        } else {
          ok = !!InitiateShutdown(0, 0, 20, howto, 0);
        }
      }
      if (ok) {
        return 0;
      } else {
        return -1;
      }
    }
  } else {
    return einval();
  }
}
