Skip to content

Commit

Permalink
added more logging, fixed some bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
Nimaoth committed Aug 21, 2021
1 parent b72a1b7 commit 301078a
Show file tree
Hide file tree
Showing 8 changed files with 233 additions and 195 deletions.
5 changes: 3 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
zig-cache
/window_data.json
/dist
window_data.json
dist
.vs
log.txt
15 changes: 7 additions & 8 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,23 @@ pub fn build(b: *std.build.Builder) void {
// between Debug, ReleaseSafe
const release = b.option(bool, "release", "Optimizations on and safety on, log to file") orelse false;

const mode = if (release) std.builtin.Mode.ReleaseSafe else std.builtin.Mode.Debug;
var mode = if (release) std.builtin.Mode.ReleaseSafe else std.builtin.Mode.Debug;
mode = .Debug;
b.is_release = mode != .Debug;
b.release_mode = mode;

const shipping = mode != .Debug;

const exe = b.addExecutable(if (shipping) "zwtwm" else "zwtwm_debug", "src/main.zig");
exe.addBuildOption(bool, "RUN_IN_CONSOLE", !shipping);
if (shipping) {
const exe = b.addExecutable(if (release) "zwtwm" else "zwtwm_debug", "src/main.zig");
exe.addBuildOption(bool, "RUN_IN_CONSOLE", !release);
if (release) {
exe.addBuildOption([]const u8, "TRAY_GUID", "99b74174-d3a4-48ba-a886-9af100149755");
} else {
exe.addBuildOption([]const u8, "TRAY_GUID", "b3e926bb-e7ee-4f2a-b513-0080167ec220");
}
exe.subsystem = if (shipping) .Windows else .Console;
exe.subsystem = if (release) .Windows else .Console;
exe.addPackage(.{ .name = "zigwin32", .path = "./deps/custom_zigwin32/win32.zig" });

exe.setTarget(target);
exe.setBuildMode(if (shipping) .ReleaseSafe else mode);
exe.setBuildMode(mode);
exe.install();

// Add config to install.
Expand Down
4 changes: 3 additions & 1 deletion config.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@
"Main HighGUI class",
"WindowsForms10.Window.8.app.0.3e799b_r6_ad1",
"TSC_POPUP_PARENT_WNDCLASS",
"BBarWindowClass"
"BBarWindowClass",
"TmForever",
"#32770"
],

"ignoredTitles": [
Expand Down
16 changes: 16 additions & 0 deletions src/layer.zig
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ pub const Layer = struct {
const Self = @This();

windows: std.ArrayList(Window),
currentWindow: usize = 0,
fullscreen: bool = false,
options: Options = .{},

Expand Down Expand Up @@ -126,6 +127,13 @@ pub const Layer = struct {
}
}

pub fn getCurrentWindow(self: *Self) ?*Window {
if (self.currentWindow >= self.windows.items.len) {
return null;
}
return &self.windows.items[self.currentWindow];
}

pub fn getWindowAt(self: *Self, index: usize) ?*Window {
if (index >= self.windows.items.len) {
return null;
Expand Down Expand Up @@ -159,6 +167,14 @@ pub const Layer = struct {
std.sort.sort(Window, self.windows.items, self, Self.compareWindowIndex);
}

pub fn clampCurrentWindowIndex(self: *Self) void {
if (self.windows.items.len == 0) {
self.currentWindow = 0;
} else if (self.currentWindow >= self.windows.items.len) {
self.currentWindow = self.windows.items.len - 1;
}
}

fn compareWindowIndex(context: *Self, a: Window, b: Window) bool {
return a.index < b.index;
}
Expand Down
42 changes: 38 additions & 4 deletions src/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ pub const ONLY_USE_HALF_MONITOR = false;
pub const TRAY_GUID = Guid.initString(BuildOptions.TRAY_GUID);

const LOG_TO_FILE = !BuildOptions.RUN_IN_CONSOLE;
const LOG_FILE_PATH = "log.txt";
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
pub var gWindowManager: WindowManager = undefined;
pub var gWindowStringArena: *std.mem.Allocator = undefined;
Expand All @@ -22,21 +21,56 @@ var gLogFile: ?std.fs.File = undefined;

pub const log_level: std.log.Level = switch (std.builtin.mode) {
.Debug => .debug,
.ReleaseSafe => .info,
.ReleaseSafe => .debug,
.ReleaseFast => .err,
.ReleaseSmall => .err,
};

pub fn main() anyerror!void {
defer _ = gpa.deinit();

if (LOG_TO_FILE) {
gLogFile = try std.fs.cwd().createFile(LOG_FILE_PATH, .{});
const time = @divFloor(std.time.milliTimestamp(), std.time.ms_per_s);
var fileNameBuffer = std.ArrayList(u8).init(&gpa.allocator);
if (std.fmt.format(fileNameBuffer.writer(), "log-{}.txt", .{time})) {
gLogFile = try std.fs.cwd().createFile(fileNameBuffer.items, .{});
} else |err| {
gLogFile = try std.fs.cwd().createFile("log.txt", .{});
}
}
defer if (gLogFile) |logFile| {
logFile.close();
};

defer _ = gpa.deinit();
zwtwmMain() catch |err| {
std.log.crit("{}", .{err});

if (gLogFile) |logFile| {
if (@errorReturnTrace()) |trace| {
writeStackTraceToLogFile(logFile.writer(), trace.*);
}
} else {
return err;
}
};
std.log.notice("Terminating zwtwm", .{});
}

fn writeStackTraceToLogFile(writer: anytype, trace: std.builtin.StackTrace) void {
nosuspend {
if (!std.builtin.strip_debug_info) {
const debug_info = std.debug.getSelfDebugInfo() catch |err| {
writer.print("Unable to dump stack trace: Unable to open debug info: {s}\n", .{@errorName(err)}) catch return;
return;
};
std.debug.writeStackTrace(trace, writer, &gpa.allocator, debug_info, std.debug.detectTTYConfig()) catch |err| {
writer.print("Unable to dump stack trace: {s}\n", .{@errorName(err)}) catch return;
return;
};
}
}
}
pub fn zwtwmMain() anyerror!void {
var windowStringArena = std.heap.ArenaAllocator.init(&gpa.allocator);
defer windowStringArena.deinit();
gWindowStringArena = &windowStringArena.allocator;
Expand Down
2 changes: 1 addition & 1 deletion src/misc.zig
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ pub fn setWindowVisibility(hwnd: HWND, shouldBeVisible: bool) void {
}

if (shouldBeVisible) {
_ = ShowWindow(hwnd, SW_RESTORE);
_ = ShowWindow(hwnd, SW_SHOW);
} else {
_ = ShowWindow(hwnd, SW_HIDE);
}
Expand Down
Loading

0 comments on commit 301078a

Please sign in to comment.