foo-wm

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

foo-wm.c (3920B)


      1 #include <unistd.h>
      2 #include <stdlib.h>
      3 #include <stdio.h>
      4 #include <X11/Xlib.h>
      5 #include <fcntl.h> 
      6 #include <string.h>
      7 #include <assert.h>
      8 #include <errno.h>
      9 #include <sys/types.h>
     10 #include <sys/socket.h>
     11 #include <sys/un.h>
     12 
     13 #include "config.h"
     14 #include "commands.h"
     15 #include "foo-wm.h"
     16 
     17 #include "atoms.h"
     18 #include "events.h"
     19 #include "tree.h"
     20 #include "util.h"
     21 #include "window.h"
     22 
     23 
     24 void handleEvents(void) {
     25   XEvent event;   
     26   socklen_t returnAddressSize;
     27   int socketFd, xFd, socketReturnFd;
     28   struct sockaddr_un socketAddress, returnAddress;
     29   char commands[256];  int commandsLength;
     30 
     31   /* Setup the X FD */
     32   xFd = ConnectionNumber(display);
     33 
     34   /* Get PID, Setup Socket Family, And Setup Address based on PID */
     35   socketAddress.sun_family = AF_UNIX;
     36   char * socketName = SOCKET_NAME;
     37   if (!strcmp(socketName, "NONE")) {
     38     pid_t pid = getpid();
     39     *socketName = sprintf(socketName, "foo-wm-%d.socket", pid);
     40   }
     41   sprintf(socketAddress.sun_path, "%s%s", SOCKET_PATH, socketName);
     42   unlink(socketAddress.sun_path);
     43 
     44   /* Setup Socket FD, Bind and Listen */
     45   socketFd = socket(AF_UNIX, SOCK_STREAM, 0);
     46   bind(socketFd, (struct sockaddr *)&socketAddress, sizeof(socketAddress));
     47   listen(socketFd, 5);
     48 
     49   /* Assemble the file descriptor set */
     50   fd_set descriptors; 
     51   for (;;) {
     52     FD_ZERO(&descriptors); 
     53     FD_SET(xFd, &descriptors); 
     54     FD_SET(socketFd, &descriptors);
     55 
     56     if (select(socketFd + 1, &descriptors, NULL, NULL, NULL)) {
     57       /* Anything on the Socket File Descriptor? */
     58       if (FD_ISSET(socketFd, &descriptors)) {
     59         /* Deal with events from the socket */
     60         socketReturnFd = accept(
     61             socketFd, 
     62             (struct sockaddr*)&returnAddress, 
     63 
     64             &returnAddressSize);
     65         if (socketReturnFd != -1) {
     66           if ((commandsLength = recv(socketReturnFd, commands, sizeof(commands), 0)) > 1) {
     67             commands[commandsLength] = '\0';
     68             fprintf(stderr, "Recieved the message %s, from the socket\n", commands);
     69             char * response = handleCommand(commands);
     70             send(socketReturnFd, response, bytesUntilNull(response), 0);
     71             close(socketReturnFd);
     72           }
     73         } else {
     74           fprintf(stderr, "Error on accept getting FD: %s\n", strerror(errno));
     75         }
     76       }
     77 
     78       /* Anything on the X File Descriptor? */
     79       if (FD_ISSET(xFd, &descriptors)) {
     80         while (XPending(display)) {
     81           XNextEvent(display, &event);
     82           handleXEvent(&event);
     83         }
     84       }
     85 
     86 
     87     }
     88   }
     89   close(socketFd);
     90 }
     91 
     92 void setup(void) {
     93   // Open display, set screen, set root, and select root input
     94   assert((display = XOpenDisplay(NULL)));
     95   activeScreen    = DefaultScreen(display);
     96   root            = RootWindow(display, activeScreen);
     97   XSelectInput(display, root, SubstructureRedirectMask | SubstructureNotifyMask);
     98   setCursor(&root, 68); 
     99   setupAtoms();
    100 
    101   // Setup Defaults from config.h
    102   defaultLayout          = CONTAINER_DEFAULT_LAYOUT;
    103   containerPadding       = CONTAINER_PADDING;
    104   clientPadding          = CLIENT_PADDING;
    105   border                 = CLIENT_BORDER_WIDTH;
    106   screenPaddingLeft      = SCREEN_PADDING_LEFT;
    107   screenPaddingTop       = SCREEN_PADDING_TOP;
    108   screenPaddingRight     = SCREEN_PADDING_RIGHT;
    109   screenPaddingBottom    = SCREEN_PADDING_BOTTOM;
    110   activeFocusedColor     = getColor(CLIENT_ACTIVE_FOCUSED_COLOR);
    111   activeUnfocusedColor   = getColor(CLIENT_ACTIVE_UNFOCUSED_COLOR);
    112   inactiveFocusedColor   = getColor(CLIENT_INACTIVE_FOCUSED_COLOR);
    113   inactiveUnfocusedColor = getColor(CLIENT_INACTIVE_UNFOCUSED_COLOR);
    114 
    115   // Setup the Root Node (top of tree)
    116   rootNode           = allocateNode();
    117   rootNode -> layout = defaultLayout;
    118   viewNode           = rootNode;
    119   recalculateRootDimensions();
    120 
    121   // Set Error Handlers and Flush to X
    122   XSetErrorHandler((XErrorHandler)(xError));
    123   XFlush(display);
    124 }
    125 
    126 
    127 int main(void) {
    128   setup();
    129   handleEvents();
    130   return 0;
    131 }