hlfw.ca

binpack

Download patch

ref: 9f9196a4688b9e99e39eb04a87f6a10501e120e4
parent: d1f3f76a2fcdfc57a58f2b834fad61ea2c71ec3d
author: Halfwit <michaelmisch1985@gmail.com>
date: Sun Oct 15 12:22:46 PDT 2017

Add back agnostic bits from old binpack

--- a/binpack.c
+++ b/binpack.c
@@ -5,7 +5,9 @@
 
 #include "binpack.h"
 
-void center(int width, int height, struct Output out[], int gaps) {
+#define MAX_BIN 64
+
+void center(unsigned width, unsigned height, struct Output out[], unsigned gaps) {
 	// r = rightmost edge of bins
 	// d = bottommost edge of bins
 	// Move all windows (width - r) / 2
@@ -14,13 +16,40 @@
 	// x - g, y + g, w - g, h - g for all windows 
 }
 
-int 
-main(int argc, char* argv[]) {
+void
+sort_input(struct Input r[], const size_t length)
+{
+	/* arrange rectangles largest to smallest, normalized some over min/max */
+	struct Input temp;
+	for (size_t i = 1; i < length; i++) {
+		for (size_t j = 0; j < length - i; j++) {
+			if ((r[j + 1].maxw * r[j + 1].minh) > (r[j].maxw * r[j].minh)) {
+				temp = r[j];
+				r[j] = r[j + 1];
+				r[j + 1] = temp;
+			}
+		}
+	}
+}
 
-	int gaps = 0;
-	int width = 0;
-	int height = 0;
-	int screens = 1;
+size_t
+input_read(struct Input r[]) {
+	size_t length = 0;
+	char line[MAX_BIN];
+	for (unsigned i = 0; fgets(line, sizeof(line), stdin); ++i) {
+		sscanf(line, "%d %d %d %d %lx", &r[i].minw, &r[i].minh, &r[i].maxw, &r[i].maxh, &r[i].wid
+);
+		length++;
+	}
+	return length;
+}
+
+int main(int argc, char* argv[]) {
+
+	unsigned gaps = 0;
+	unsigned width = 0;
+	unsigned height = 0;
+	unsigned screens = 1;
 	char c;
 	
 	while ((c = getopt(argc, argv, "hg:w:h:s:")) != -1) {
@@ -42,6 +71,17 @@
 			return EXIT_SUCCESS;
         }
 	}
+
+	struct Input r[MAX_BIN];
+	struct Output out[MAX_BIN];
+	
+	const size_t length = input_read(r);
+	
+	/* Sanity checks */
+	if (length == 0 || height == 0 || width == 0)
+		return EXIT_SUCCESS;
+
+	sort_input(r, length);
 
 	switch (screens) {
 		case 1:	   		
--- a/binpack.h
+++ b/binpack.h
@@ -1,18 +1,19 @@
+#include <stdbool.h>
+
 struct Input {
-	int minw;
-	int minh;
-	int maxw;
-	int maxh;
-	int id;
+	unsigned minw;
+	unsigned minh;
+	unsigned maxw;
+	unsigned maxh;
+	unsigned wid;
 };
 
 struct Output {
-	int w;
-	int h;
-	int x;
-	int y;
-	int id;
+	unsigned w;
+	unsigned h;
+	unsigned x;
+	unsigned y;
+	unsigned id;
 };
 
-//bool bin_pack(int width, int height, &output[], &input[]);
-
+bool bin_pack(unsigned width, unsigned height, struct Output[], struct Input[]);