Skip to content

Commit

Permalink
wsudo: 5.0.1 some behavioral corrections
Browse files Browse the repository at this point in the history
  • Loading branch information
fcharlie committed Jul 23, 2022
1 parent 7be840e commit 8de6f60
Show file tree
Hide file tree
Showing 17 changed files with 1,088 additions and 5,541 deletions.
2 changes: 1 addition & 1 deletion AppExec/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

add_executable(
AppExec WIN32
appsettings.cc
settings.cc
capabilities.cc
app.cc
appx.cc
Expand Down
4 changes: 2 additions & 2 deletions AppExec/app.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@
#include <string_view>
#include <vector>
#include <unordered_map>
#include <bela/color.hpp>

namespace priv {

using wid_t = WELL_KNOWN_SID_TYPE;
static constexpr const auto whitecolor = RGB(255, 255, 255);
static constexpr const auto blackcolor = RGB(0, 0, 0);

struct AppSettings {
COLORREF bk{whitecolor};
COLORREF bk{RGB(243, 243, 243)};
COLORREF textcolor{blackcolor};
};
bool AppInitializeSettings(AppSettings &as);
Expand Down
2 changes: 1 addition & 1 deletion AppExec/appsettings.cc → AppExec/settings.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
#include <bela/color.hpp>
#include <vfsenv.hpp>
#include <filesystem>
#include <graphics.hpp>
#include "app.hpp"

namespace priv {

bool AppInitializeSettings(AppSettings &as) {
auto file = PathSearcher::Instance().JoinAppData(L"Privexec\\AppExec.json");
FD fd;
Expand Down
2 changes: 1 addition & 1 deletion Privexec/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

add_executable(
Privexec WIN32
appalias.cc
alias.cc
capabilities.cc
app.cc
main.cc
Expand Down
File renamed without changes.
22 changes: 15 additions & 7 deletions Privexec/app.cc
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,11 @@ bool App::SelChanged() {
if (box.IsMatch((int)wsudo::exec::privilege_t::appcontainer)) {
appx.Update(L"");
appx.Visible(TRUE);
appcas.Visible(TRUE);
capabilities.Visible(TRUE);
} else {
appx.Update(L"AppxManifest.xml or Package.appxmanifest");
appx.Visible(FALSE);
appcas.Visible(FALSE);
capabilities.Visible(FALSE);
}
return true;
}
Expand Down Expand Up @@ -161,9 +161,9 @@ bool App::AppExecute() {
bela::mbs_t::FATAL);
return false;
}
cmd.caps = appcas.Capabilities();
cmd.caps = capabilities.Capabilities();
cmd.appmanifest = bela::WindowsExpandEnv(appx.Content());
cmd.islpac = appcas.IsLowPrivilegeAppContainer();
cmd.islpac = capabilities.IsLowPrivilegeAppContainer();
cmd.cwd = ResolveCWD(true);
if (!cmd.initialize(ec)) {
bela::BelaMessageBox(hWnd, L"Privexec AppContainer init", ec.message.data(), PRIVEXEC_APPLINKE,
Expand Down Expand Up @@ -251,14 +251,22 @@ bool App::DropFiles(WPARAM wParam, LPARAM lParam) {

INT_PTR App::MessageHandler(UINT message, WPARAM wParam, LPARAM lParam) {
switch (message) {
case WM_CTLCOLORDLG:
case WM_CTLCOLORDLG: {
if (hbrBkgnd == nullptr) {
hbrBkgnd = CreateSolidBrush(RGB(243, 243, 243));
}
return (INT_PTR)hbrBkgnd;
}
case WM_CTLCOLORSTATIC: {
HDC hdc = (HDC)wParam;
SetTextColor(hdc, RGB(0, 0, 0));
SetBkColor(hdc, RGB(243, 243, 243));
SetBkMode(hdc, TRANSPARENT);
if (hbrBkgnd == nullptr) {
hbrBkgnd = CreateSolidBrush(RGB(255, 255, 255));
hbrBkgnd = CreateSolidBrush(RGB(243, 243, 243));
}
return (INT_PTR)hbrBkgnd;
}

case WM_DROPFILES:
DropFiles(wParam, lParam);
return TRUE;
Expand Down
2 changes: 1 addition & 1 deletion Privexec/app.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ class App {
Element cmd;
Element cwd;
Element appx;
AppCapabilities appcas;
AppCapabilities capabilities;
alias_t alias;
HBRUSH hbrBkgnd{nullptr};
};
Expand Down
12 changes: 6 additions & 6 deletions Privexec/capabilities.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ bool IsKnownCapabilityNames(std::wstring_view name) {
}

bool App::InitializeCapabilities() {
appcas.hlview = GetDlgItem(hWnd, IDL_APPCONTAINER_LISTVIEW);
appcas.hlpacbox = GetDlgItem(hWnd, IDC_LPACMODE);
ListView_SetExtendedListViewStyleEx(appcas.hlview, LVS_EX_CHECKBOXES, LVS_EX_CHECKBOXES);
capabilities.hlview = GetDlgItem(hWnd, IDL_APPCONTAINER_LISTVIEW);
capabilities.hlpacbox = GetDlgItem(hWnd, IDC_LPACMODE);
ListView_SetExtendedListViewStyleEx(capabilities.hlview, LVS_EX_CHECKBOXES, LVS_EX_CHECKBOXES);
LVCOLUMNW lvw;
memset(&lvw, 0, sizeof(lvw));
lvw.cx = 200;
ListView_InsertColumn(appcas.hlview, 0, &lvw);
ListView_InsertColumn(capabilities.hlview, 0, &lvw);
constexpr std::size_t wnlen = sizeof(wncas) / sizeof(CapabilityName);
for (std::size_t n = wnlen; n > 0; n--) {
const auto &w = wncas[n - 1];
Expand All @@ -35,9 +35,9 @@ bool App::InitializeCapabilities() {
item.mask = LVIF_TEXT | LVIF_PARAM;
item.pszText = const_cast<LPWSTR>(w.name);
item.lParam = (LPARAM)w.value;
ListView_InsertItem(appcas.hlview, &item);
ListView_InsertItem(capabilities.hlview, &item);
}
ListView_SetColumnWidth(appcas.hlview, 0, LVSCW_AUTOSIZE_USEHEADER);
ListView_SetColumnWidth(capabilities.hlview, 0, LVSCW_AUTOSIZE_USEHEADER);
return true;
}
} // namespace priv
2 changes: 1 addition & 1 deletion docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@

## 2022-07-22

1. wsudo -U will create a process without privilege escalation. If it is currently running under the System account, it will fail to start the Store App with reduced privileges.(wsudo -U 会创建未提权的进程,如果当前运行在 System 账户下,降权启动 Store App 会失败。)
1. wsudo -U will create a process without privilege escalation. If it is currently running under the System account, wsudo -U cannot start the Store App directly, but it can start the Store App in the launched shell.(在系统权限下,wsudo -U 无法直接启动 Store App,但是可以在启动的 shell 中启动 Store App。)
2. wsudo add -B --basic as default (permission inheritance mode)(wsudo 添加 `-B` `--basic` 作为默认执行模式,即继承父进程权限,而 -U 则总是启动未提权的进程。)
197 changes: 197 additions & 0 deletions include/graphics.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,197 @@
#ifndef BAULK_GRAPHICS_HPP
#define BAULK_GRAPHICS_HPP
#include <bela/base.hpp>
#include <bela/color.hpp>
#include <bela/win32.hpp>
#include <dwmapi.h>

namespace baulk::windows {
constexpr bool mica_materials_enabled(const bela::windows_version &ver) { return ver >= bela::windows::win11_21h2; }
constexpr bool mica_system_backdrop_enabled(const bela::windows_version &ver) {
constexpr bela::windows_version win11_22523(10, 0, 22523);
return ver >= win11_22523;
}
constexpr bool title_bar_customization_enabled(const bela::windows_version &ver) {
return ver >= bela::windows::win10_21h1;
}

namespace dwm {
// clang-format off
// Window attributes
enum window11_attributes {
DWMWA_USE_HOSTBACKDROPBRUSH = 17, // [set] BOOL, Allows the use of host backdrop brushes for the window.
DWMWA_USE_IMMERSIVE_DARK_MODE = 20, // [set] BOOL, Allows a window to either use the accent color, or dark, according to the user Color Mode preferences.
DWMWA_WINDOW_CORNER_PREFERENCE = 33, // [set] WINDOW_CORNER_PREFERENCE, Controls the policy that rounds top-level window corners
DWMWA_BORDER_COLOR = 34, // [set] COLORREF, The color of the thin border around a top-level window
DWMWA_CAPTION_COLOR = 35, // [set] COLORREF, The color of the caption
DWMWA_TEXT_COLOR = 36, // [set] COLORREF, The color of the caption text
DWMWA_VISIBLE_FRAME_BORDER_THICKNESS = 37, // [get] UINT, width of the visible border around a thick frame window
DWMWA_SYSTEMBACKDROP_TYPE = 38, // [get, set] SYSTEMBACKDROP_TYPE, Controls the system-drawn backdrop material of a window, including behind the non-client area.
DWMWA_MICA_EFFECT = 1029, // [set] BOOL
};
enum window_corner_preference {
/*
* Let the system decide whether or not to round window corners
*/
DWMWCP_DEFAULT = 0,

/*
* Never round window corners
*/
DWMWCP_DONOTROUND = 1,

/*
* Round the corners if appropriate
*/
DWMWCP_ROUND = 2,

/*
* Round the corners if appropriate, with a small radius
*/
DWMWCP_ROUNDSMALL = 3,

/*
* Round the corners of the window in the same style as system popup menus
*/
DWMWCP_MENU = 4

};

// Types used with DWMWA_SYSTEMBACKDROP_TYPE
enum DWM_SYSTEMBACKDROP_TYPE
{
DWMSBT_AUTO, // [Default] Let DWM automatically decide the system-drawn backdrop for this window.
DWMSBT_NONE, // Do not draw any system backdrop.
DWMSBT_MAINWINDOW, // Draw the backdrop material effect corresponding to a long-lived window.
DWMSBT_TRANSIENTWINDOW, // Draw the backdrop material effect corresponding to a transient window.
DWMSBT_TABBEDWINDOW, // Draw the backdrop material effect corresponding to a window with a tabbed title bar.
};

// clang-format on

// Use this constant to reset any window part colors to the system default behavior
constexpr auto dwmwa_color_default = 0xFFFFFFFFu;

// Use this constant to specify that a window part should not be rendered
constexpr auto dwmwa_color_none = 0xFFFFFFFEu;

enum DWM_BOOL { DWMWCP_FALSE = 0, DWMWCP_TRUE = 1 };

} // namespace dwm

// PersonalizeMode
struct PersonalizeMode {
bela::color background;
bela::color text;
bela::color border;
bela::color disable;
};
constexpr auto LightMode = PersonalizeMode{
.background = bela::color(243, 243, 243),
.text = bela::color(0, 0, 0),
.border = bela::color(245, 245, 245),
.disable = bela::color(77, 77, 77),
};
constexpr auto DarkMode = PersonalizeMode{
.background = bela::color(26, 31, 52),
.text = bela::color(255, 255, 255),
.border = bela::color(51, 52, 54),
.disable = bela::color(204, 204, 204),
};

constexpr auto CustomMode = PersonalizeMode{
.background = bela::color(204, 204, 204),
.text = bela::color(0, 0, 0),
.border = bela::color(245, 245, 245),
.disable = bela::color(77, 77, 77),
};

// HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Themes\Personalize
// SystemUsesLightTheme DWORD 0 or 1 // Changes the taskbar and system tray color
// AppsUseLightTheme DWORD 0 or 1 // Changes app colors

struct PersonalizeThemes {
PersonalizeMode Mode{LightMode};
bool SystemUsesLightTheme{true};
bool AppsUseLightTheme{true};
bool Load(bela::error_code &ec);
};

inline bool PersonalizeThemes::Load(bela::error_code &ec) {
HKEY hkey = nullptr;
auto closer = bela::finally([&] {
if (hkey != nullptr) {
RegCloseKey(hkey);
}
});
if (RegOpenKeyW(HKEY_CURRENT_USER, LR"(Software\Microsoft\Windows\CurrentVersion\Themes\Personalize)", &hkey) != 0) {
ec = bela::make_system_error_code(L"RegOpenKeyW: ");
return false;
}
DWORD regType = 0;
DWORD value = 0;
DWORD bufsize = sizeof(DWORD);
if (RegQueryValueExW(hkey, L"SystemUsesLightTheme", nullptr, &regType, reinterpret_cast<PBYTE>(&value), &bufsize) !=
0) {
ec = bela::make_system_error_code(L"RegQueryValueExW: ");
return false;
}
if (regType != REG_DWORD) {
ec = bela::make_error_code(bela::ErrGeneral, L"SystemUsesLightTheme not REG_DWORD: ", regType);
return false;
}
SystemUsesLightTheme = (value == 1);
bufsize = sizeof(DWORD);
if (RegQueryValueExW(hkey, L"AppsUseLightTheme", nullptr, &regType, reinterpret_cast<PBYTE>(&value), &bufsize) != 0) {
ec = bela::make_system_error_code(L"AppsUseLightTheme: ");
return false;
}
if (regType != REG_DWORD) {
ec = bela::make_error_code(bela::ErrGeneral, L"AppsUseLightTheme not REG_DWORD: ", regType);
return false;
}
AppsUseLightTheme = (value == 1);
if (!AppsUseLightTheme) {
Mode = DarkMode;
}
return true;
}

inline bool PersonalizeWindowUI(HWND hWnd, const PersonalizeThemes &themes) {
COLORREF captionColor = themes.Mode.background;
DwmSetWindowAttribute(hWnd, dwm::DWMWA_CAPTION_COLOR, &captionColor, sizeof(captionColor));
COLORREF titleColor = themes.Mode.text;
DwmSetWindowAttribute(hWnd, dwm::DWMWA_TEXT_COLOR, &titleColor, sizeof(titleColor));
COLORREF borderColor = themes.Mode.border;
DwmSetWindowAttribute(hWnd, dwm::DWMWA_BORDER_COLOR, &borderColor, sizeof(borderColor));
enum DWM_BOOL { DWMWCP_FALSE = 0, DWMWCP_TRUE = 1 };
DWM_BOOL darkPreference = themes.AppsUseLightTheme ? DWMWCP_FALSE : DWMWCP_FALSE;
DwmSetWindowAttribute(hWnd, dwm::DWMWA_USE_IMMERSIVE_DARK_MODE, &darkPreference, sizeof(darkPreference));
return true;
}

inline bool EnableMicaMaterials(HWND hWnd) {
// DWM_SYSTEMBACKDROP_TYPE
//
auto flag = dwm::DWMSBT_MAINWINDOW;
DwmSetWindowAttribute(hWnd, dwm::DWMWA_SYSTEMBACKDROP_TYPE, &flag, sizeof(flag));
return true;
}

inline bool EnableRoundWindow(HWND hWnd) {
auto preference = dwm::DWMWCP_ROUND;
return DwmSetWindowAttribute(hWnd, dwm::DWMWA_WINDOW_CORNER_PREFERENCE, &preference, sizeof(preference)) == S_OK;
}

inline bool DisableRoundWindow(HWND hWnd) {
auto preference = dwm::DWMWCP_DONOTROUND;
return DwmSetWindowAttribute(hWnd, dwm::DWMWA_WINDOW_CORNER_PREFERENCE, &preference, sizeof(preference)) == S_OK;
}

// DPI scale
constexpr int scale_cast(int value, UINT dpi) { return static_cast<int>(static_cast<float>(value) * dpi / 96); }
constexpr auto rect_height(const RECT &rc) { return rc.bottom - rc.top; }
constexpr auto rect_width(const RECT &rc) { return rc.right - rc.left; }
} // namespace baulk::windows

#endif
Loading

0 comments on commit 8de6f60

Please sign in to comment.