foo-wm

(Archived) Experimental Zooming IPC-based WM
git clone http://milesalan.com/git/foo-wm
Log | Files | Refs | Mirror | README | LICENSE

util.c (2062B)


      1 #include <X11/Xlib.h>
      2 #include <stdio.h>
      3 #include <stdlib.h>
      4 #include <string.h>
      5 #include <math.h>
      6 
      7 
      8 #include "foo-wm.h"
      9 #include "util.h"
     10 
     11 //Thank you DWM ;)
     12 unsigned long getColor(const char *colstr) {
     13   Colormap cmap = DefaultColormap(display, activeScreen);
     14   XColor color;
     15 
     16   if(!XAllocNamedColor(display, cmap, colstr, &color, &color)) { return 0; }
     17   return color.pixel;
     18 }
     19 
     20 
     21 int xError(XErrorEvent *e) {
     22   fprintf(stderr, "XErrorEvent of Request Code: %d and Error Code of %d\n", e -> request_code, e -> error_code);
     23   return 0;
     24 }
     25 
     26 Node * allocateNode() {
     27 
     28   Node *n = malloc(sizeof(Node));
     29   n -> previous = NULL; n -> next = NULL;
     30   n -> parent = NULL;   n -> child = NULL;
     31   n -> focus  = NULL;    
     32   n -> window = (Window) NULL; 
     33   n -> layout = defaultLayout;
     34   return n;
     35 }
     36 
     37 
     38 void recalculateRootDimensions (void) {
     39   if (!rootNode) return;
     40   rootNode -> x         = rootX      = screenPaddingLeft;
     41   rootNode -> y         = rootY     = screenPaddingTop;
     42   rootNode -> width     = rootWidth = DisplayWidth(display, activeScreen) - screenPaddingLeft - screenPaddingRight;
     43   rootNode -> height    = rootHeight = DisplayHeight(display, activeScreen) - screenPaddingTop - screenPaddingBottom;
     44 }
     45 
     46 void gridDimensions(int children, int * rows, int * cols) {
     47   if (children == 2) { *rows = 1; *cols = 2; return; } /* Edgecase */
     48 
     49   int square = (int) sqrt(children);
     50   int r = square;
     51 
     52   while (((children % r) != 0)) { r++; }
     53   int c = children / r;
     54 
     55   if ((r == 1 && c != 1) || (c == 1 && r != 1)) {
     56     gridDimensions(children + 1, rows, cols);
     57   } else {
     58     *rows = r;
     59     *cols = c;
     60   }
     61 }
     62 
     63 Bool isPrime(int number) {
     64   Bool prime = True;
     65   int i;
     66   for(i = 2; i <= number / 2; i++)
     67     if(number % i == 0) { prime = False; break; } 
     68   return prime;
     69 }
     70 
     71 Node * focusOrChildOf(Node * node) {
     72   if (!node) return NULL;
     73   else if (node -> focus) return node -> focus;
     74   else return node -> child;
     75 }
     76 int bytesUntilNull(char * string) {
     77   int counter = 0;
     78   while (string[counter] != '\0')
     79     counter++;
     80 
     81   /* Include the NULL Byte */
     82   return ++counter;
     83 }