Updated main.cpp

Updated existing functions which took a pointer for structs, now take a
reference instead for ease of access and avoiding the use of parameter
as optional.

Added fullscreen functionality - togglable.
 - feature works by directly resizing the currently focused window.
 - terminal window can be toggled between fullscreen and original size.
 - if multiple windows exist, then exiting fullscreen will cause all
   windows to be organised, essentially placing the focused fullscreen
   window back into its original tiling placement.
This commit is contained in:
Ethan Smith-Coss 2022-07-23 18:24:44 +01:00
parent 6fafed8993
commit 571e595c3b
Signed by: TheOnePath
GPG Key ID: 4E7D436CE1A0BAF1

116
main.cpp
View File

@ -144,6 +144,7 @@ TODO:
#include <cstring>
#include <regex>
#include <iostream>
#include <windows.h>
#include <stdio.h>
@ -198,9 +199,12 @@ struct HandleCollector {
@Return: a BOOL value if the defferal process was successful on all windows collected.
*/
static BOOL organiseWindows(HandleCollector* hc) {
static BOOL organiseWindows(HandleCollector& hc, char* arg) {
// check to make sure that if 'arg' is NULL, make it an empty string
if (arg == NULL) arg = (char*)"";
// start a new defferal for simultaneous organisation of windows
HDWP defer = BeginDeferWindowPos(hc->count);
HDWP defer = BeginDeferWindowPos(hc.count);
// get the current workspace area of the desktop
RECT workArea;
if ( !SystemParametersInfo(SPI_GETWORKAREA, 0, &workArea, 0) ) return FALSE;
@ -208,11 +212,27 @@ static BOOL organiseWindows(HandleCollector* hc) {
int x, y, cx, cy;
HWND wh;
// create match for 4 different ways of organising windows
switch (hc->count) {
switch (hc.count) {
case 1: // recentre the single window
{
printf("Organising first window.\n");
// initialise handler for window
wh = hc->handles[0];
wh = hc.handles[0];
// the sizing for fullscreen windows
cx = workArea.right - workArea.left - 32;
cy = workArea.bottom - workArea.top - 32;
if ( strcmp(arg, "--fullscreen") == 0 ) {
printf("Organising to fullscreen.\n");
x = (workArea.right - workArea.left)/2 - cx/2;
y = (workArea.bottom - workArea.top)/2 - cy/2;
defer = DeferWindowPos(defer, wh, (HWND)0, x, y,
cx, cy, SWP_SHOWWINDOW | SWP_NOZORDER );
break;
}
// set the window's X,Y coordinates
x = (workArea.right - workArea.left)/2 - WIN_DEFAULT_SIZE_X/2;
y = (workArea.bottom - workArea.top)/2 - WIN_DEFAULT_SIZE_Y/2;
@ -224,8 +244,8 @@ static BOOL organiseWindows(HandleCollector* hc) {
}
case 2: // arrange tiles as left|right
{
//printf("Organising first window.\n");
wh = hc->handles[0];
printf("Organising first window.\n");
wh = hc.handles[0];
// set the window's width
cx = (workArea.right - workArea.left - 32) * 0.50f;
cy = (workArea.bottom - workArea.top) - 32;
@ -236,8 +256,8 @@ static BOOL organiseWindows(HandleCollector* hc) {
defer = DeferWindowPos(defer, wh, (HWND)0, x-4, y, cx, cy,
SWP_SHOWWINDOW | SWP_NOZORDER );
//printf("Organising second window.\n");
wh = hc->handles[1];
printf("Organising second window.\n");
wh = hc.handles[1];
defer = DeferWindowPos(defer, wh, (HWND)0, x+cx+4, y, cx, cy,
SWP_SHOWWINDOW | SWP_NOZORDER );
@ -245,8 +265,8 @@ static BOOL organiseWindows(HandleCollector* hc) {
}
case 3: // arrange tiles as 1---2|3
{
//printf("Organising first window.\n");
wh = hc->handles[0];
printf("Organising first window.\n");
wh = hc.handles[0];
// set the window's width
cx = (workArea.right - workArea.left - 32) * 0.50f;
cy = (workArea.bottom - workArea.top - 32) * 0.50f;
@ -257,16 +277,16 @@ static BOOL organiseWindows(HandleCollector* hc) {
defer = DeferWindowPos(defer, wh, (HWND)0, x-4, y-4, cx, cy,
SWP_SHOWWINDOW | SWP_NOZORDER );
//printf("Organising second window.\n");
wh = hc->handles[1];
printf("Organising second window.\n");
wh = hc.handles[1];
defer = DeferWindowPos(defer, wh, (HWND)0, x-4, y+cy+4, cx, cy,
SWP_SHOWWINDOW | SWP_NOZORDER );
cy = (workArea.bottom - workArea.top) - 24;
y = (workArea.bottom - workArea.top) * 0.50f - cy * 0.50f;
//printf("Organising third window.\n");
wh = hc->handles[2];
printf("Organising third window.\n");
wh = hc.handles[2];
defer = DeferWindowPos(defer, wh, (HWND)0, x+cx+4, y, cx, cy,
SWP_SHOWWINDOW | SWP_NOZORDER );
@ -274,8 +294,8 @@ static BOOL organiseWindows(HandleCollector* hc) {
}
case 4: // arrange tiles in quarts 1|2---3|4
{
//printf("Organising first window.\n");
wh = hc->handles[0];
printf("Organising first window.\n");
wh = hc.handles[0];
// set the window's width
cx = (workArea.right - workArea.left - 32) * 0.50f;
cy = (workArea.bottom - workArea.top - 32) * 0.50f;
@ -286,18 +306,18 @@ static BOOL organiseWindows(HandleCollector* hc) {
defer = DeferWindowPos(defer, wh, (HWND)0, x-4, y-4, cx, cy,
SWP_SHOWWINDOW | SWP_NOZORDER );
//printf("Organising second window.\n");
wh = hc->handles[1];
printf("Organising second window.\n");
wh = hc.handles[1];
defer = DeferWindowPos(defer, wh, (HWND)0, x+cx+4, y-4, cx, cy,
SWP_SHOWWINDOW | SWP_NOZORDER );
//printf("Organising third window.\n");
wh = hc->handles[2];
printf("Organising third window.\n");
wh = hc.handles[2];
defer = DeferWindowPos(defer, wh, (HWND)0, x-4, y+cy+4, cx, cy,
SWP_SHOWWINDOW | SWP_NOZORDER );
//printf("Organising second window.\n");
wh = hc->handles[3];
printf("Organising second window.\n");
wh = hc.handles[3];
defer = DeferWindowPos(defer, wh, (HWND)0, x+cx+4, y+cy+4, cx, cy,
SWP_SHOWWINDOW | SWP_NOZORDER );
@ -306,10 +326,11 @@ static BOOL organiseWindows(HandleCollector* hc) {
}
if ( !EndDeferWindowPos(defer) ) {
//printf("Failed to complete window deferral.\n");
printf("Failed to complete window deferral.\n");
return FALSE;
}
printf("Organised windows successfully.\n");
return TRUE;
}
@ -364,7 +385,6 @@ static BOOL CALLBACK HwndCollectorCallback( HWND hWnd, LPARAM lParam ) {
CloseHandle(procH); // make sure to close the handle once we've gotten the image filename
// check if the title of the image filename is one we are looking for
if ( std::regex_search(procMFN, m, re) ) {
// std::cout << "Found " << hWnd << "." << procMFN << ".\n";
// ensure that we have space to store a handle
if ( hwnds->count < 4 ) {
hwnds->handles[hwnds->count] = hWnd; // add it to the array of handles
@ -388,11 +408,12 @@ static BOOL CALLBACK HwndCollectorCallback( HWND hWnd, LPARAM lParam ) {
@Return: the number of windows gathered by the collector.
*/
static int HwndCollector( HandleCollector* hwnds ) {
static int HwndCollector( HandleCollector& hwnds ) {
// iterate over all the windows currently running on the system
// NOTE: the callback actually identifies the correct windows to collect.
EnumWindows(HwndCollectorCallback, (LPARAM)hwnds);
return hwnds->count; // return the number of handles added.
printf("Collecting windows...\n");
EnumWindows(HwndCollectorCallback, (LPARAM)&hwnds);
return hwnds.count; // return the number of handles added.
}
@ -403,21 +424,40 @@ int main(int argc, char* argv[]) {
.pids = new unsigned long[4],
.count = 0 };
if ( argc > 1 ) {
if (strcmp(argv[1], "--fullscreen") == 0) {
handles.handles[0] = GetForegroundWindow();
handles.count += 1;
RECT wa; // work area of desktop
RECT wr; // window rect
if ( !SystemParametersInfo(SPI_GETWORKAREA, 0, &wa, 0) ) return 1;
GetWindowRect(handles.handles[0], &wr);
int IsFullscreen = (wr.right - wr.left == wa.right - wa.left - 32 &&
wr.bottom - wr.top == wa.bottom - wa.top - 32) ? true : false;
// if the window is already fullscreen, then coming out of fullscreen
// means the window should be placed back to where it was - collect
// all windows again and organise.
if ( !IsFullscreen ) {
if ( !organiseWindows(handles, argv[1]) ) return 1;
return 0;
}
// remove the added foreground window ready for collector
handles.count = 0; // this will allow for the array to be overwritten
}
}
// no windows were collected, nothing to organise
if ( HwndCollector(&handles) == 0 ) return 0;
if ( HwndCollector(handles) == 0 ) return 0;
// attempt to organise all windows collected
if ( !organiseWindows(&handles) ) {
//printf("There was an issue organising the current window(s).\n");
if ( !organiseWindows(handles, (char*)0) ) {
printf("There was an issue organising the current window(s).\n");
return 1;
}
// debug stuff
// for (int i = 0; i<handles.count; i++) {
// unsigned long pid;
// GetWindowThreadProcessId(handles.handles[i], &pid);
// std::cout << "Handle: " << handles.handles[i] << ". PID: " << pid << "\n";
// }
return 0;
}