bspwm/bspwm.c

189 lines
5.1 KiB
C
Raw Normal View History

2012-07-30 14:54:20 +02:00
#include <stdlib.h>
2012-07-30 22:51:33 +02:00
#include <stdio.h>
2012-08-01 23:16:07 +02:00
#include <string.h>
2012-07-30 12:21:22 +02:00
#include <unistd.h>
2012-09-07 12:51:16 +02:00
/* #include <signal.h> */
2012-08-02 21:53:27 +02:00
#include <fcntl.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <sys/select.h>
#include <sys/stat.h>
2012-08-01 23:16:07 +02:00
#include <sys/wait.h>
2012-07-29 22:46:12 +02:00
#include <xcb/xcb.h>
2012-07-30 12:21:22 +02:00
#include <xcb/xcb_event.h>
2012-08-23 22:32:11 +02:00
#include <xcb/xcb_ewmh.h>
2012-08-20 22:38:29 +02:00
#include "helpers.h"
2012-08-17 22:18:26 +02:00
#include "types.h"
2012-07-30 14:54:20 +02:00
#include "settings.h"
2012-07-31 14:32:13 +02:00
#include "messages.h"
2012-08-01 12:56:57 +02:00
#include "events.h"
2012-08-02 22:37:20 +02:00
#include "common.h"
2012-08-20 22:38:29 +02:00
#include "utils.h"
#include "bspwm.h"
2012-08-23 22:32:11 +02:00
#include "ewmh.h"
2012-09-07 12:51:16 +02:00
2012-08-01 23:16:07 +02:00
void quit(void)
{
running = false;
}
// check for other WM and initiate events capture
2012-08-02 09:27:48 +02:00
int register_events(void)
2012-08-01 23:16:07 +02:00
{
2012-08-30 21:35:39 +02:00
xcb_generic_error_t *err;
uint32_t values[] = {XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT | XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY | XCB_EVENT_MASK_PROPERTY_CHANGE | XCB_EVENT_MASK_BUTTON_PRESS};
err = xcb_request_check(dpy, xcb_change_window_attributes_checked(dpy, screen->root, XCB_CW_EVENT_MASK, values));
if (err != NULL)
return 1;
2012-08-01 23:16:07 +02:00
return 0;
}
2012-08-30 21:35:39 +02:00
2012-08-01 23:16:07 +02:00
/* wrapper to get atoms using xcb */
2012-08-20 22:38:29 +02:00
void get_atoms(char **names, xcb_atom_t *atoms, int count)
2012-08-01 23:16:07 +02:00
{
2012-08-20 22:38:29 +02:00
int i;
2012-08-01 23:16:07 +02:00
xcb_intern_atom_cookie_t cookies[count];
xcb_intern_atom_reply_t *reply;
2012-08-20 22:38:29 +02:00
for (i = 0; i < count; i++)
2012-08-01 23:16:07 +02:00
cookies[i] = xcb_intern_atom(dpy, 0, strlen(names[i]), names[i]);
2012-08-20 22:38:29 +02:00
for (i = 0; i < count; i++) {
2012-08-01 23:16:07 +02:00
reply = xcb_intern_atom_reply(dpy, cookies[i], NULL);
if (reply) {
/* PRINTF("%s : %d\n", names[i], reply->atom); */
2012-08-20 22:38:29 +02:00
atoms[i] = reply->atom;
free(reply);
2012-08-01 23:16:07 +02:00
} else {
2012-08-02 22:37:20 +02:00
PUTS("warning: failed to register atoms");
2012-08-01 23:16:07 +02:00
}
}
}
2012-08-02 10:00:02 +02:00
xcb_screen_t *screen_of_display(xcb_connection_t *dpy, int default_screen)
2012-08-01 23:16:07 +02:00
{
2012-08-29 18:37:31 +02:00
xcb_screen_iterator_t iter;
for (iter = xcb_setup_roots_iterator(xcb_get_setup(dpy)); iter.rem; --default_screen, xcb_screen_next(&iter))
if (default_screen == 0)
return iter.data;
2012-08-01 23:16:07 +02:00
return NULL;
}
2012-09-07 12:51:16 +02:00
/* void handle_zombie(int sig) */
/* { */
/* while (waitpid(-1, NULL, WNOHANG) > 0) */
/* ; */
/* signal(sig, handle_zombie); */
/* } */
2012-08-01 23:16:07 +02:00
void setup(int default_screen)
{
2012-09-07 12:51:16 +02:00
/* signal(SIGCHLD, handle_zombie); */
2012-08-28 21:15:29 +02:00
ewmh_init();
2012-08-29 18:37:31 +02:00
/* screen = ewmh.screens[default_screen]; */
/* screen = xcb_setup_roots_iterator(xcb_get_setup(dpy)).data; */
screen = screen_of_display(dpy, default_screen);
2012-08-01 23:16:07 +02:00
if (!screen)
die("error: cannot aquire screen\n");
2012-08-29 18:37:31 +02:00
2012-08-01 23:16:07 +02:00
screen_width = screen->width_in_pixels;
screen_height = screen->height_in_pixels;
2012-08-20 22:38:29 +02:00
char *WM_ATOM_NAME[] = { "WM_PROTOCOLS", "WM_DELETE_WINDOW" };
2012-08-29 18:37:31 +02:00
xcb_atom_t net_atoms[] = {ewmh._NET_SUPPORTED, ewmh._NET_WM_STATE_FULLSCREEN, ewmh._NET_WM_STATE, ewmh._NET_ACTIVE_WINDOW};
2012-08-02 10:00:02 +02:00
get_atoms(WM_ATOM_NAME, wmatoms, WM_COUNT);
2012-08-01 23:16:07 +02:00
2012-08-29 18:37:31 +02:00
/* xcb_change_property(dpy, XCB_PROP_MODE_REPLACE, screen->root, netatoms[NET_SUPPORTED], XCB_ATOM_ATOM, 32, NET_COUNT, netatoms); */
xcb_ewmh_set_supported(&ewmh, default_screen, LENGTH(net_atoms), net_atoms);
2012-08-30 21:35:39 +02:00
xcb_ewmh_set_wm_name(&ewmh, screen->root, LENGTH(WM_NAME), WM_NAME);
desk = make_desktop();
last_desk = NULL;
desk_head = desk;
desk_tail = desk;
2012-09-07 12:51:16 +02:00
split_mode = MODE_AUTOMATIC;
2012-08-01 23:16:07 +02:00
}
2012-07-29 22:46:12 +02:00
int main(void)
{
2012-07-30 12:21:22 +02:00
fd_set descriptors;
2012-08-02 21:53:27 +02:00
int sock_fd, ret_fd, xfd, sel, nbr;
struct sockaddr_un sock_address;
char *sock_path;
char msg[BUFSIZ], rsp[BUFSIZ];
2012-07-30 12:21:22 +02:00
xcb_generic_event_t *event;
2012-07-31 14:32:13 +02:00
running = true;
2012-07-30 19:43:54 +02:00
2012-07-30 12:21:22 +02:00
dpy = xcb_connect(NULL, &default_screen);
if (xcb_connection_has_error(dpy))
die("error: cannot open display\n");
2012-08-01 23:16:07 +02:00
setup(default_screen);
2012-08-02 21:53:27 +02:00
/* if (register_events() == 1) { */
/* xcb_disconnect(dpy); */
/* die("another WM is already running\n"); */
/* } */
2012-08-01 23:16:07 +02:00
2012-07-30 12:21:22 +02:00
xfd = xcb_get_file_descriptor(dpy);
2012-08-01 15:16:57 +02:00
2012-08-02 21:53:27 +02:00
sock_path = getenv(SOCK_PATH);
if (sock_path == NULL)
die("BSPWM_SOCKET environment variable is not set\n");
sock_address.sun_family = AF_UNIX;
strcpy(sock_address.sun_path, sock_path);
unlink(sock_path);
sock_fd = socket(AF_UNIX, SOCK_STREAM, 0);
2012-08-01 15:16:57 +02:00
2012-08-02 21:53:27 +02:00
if (sock_fd == -1)
die("error: could not create socket\n");
2012-07-30 12:21:22 +02:00
2012-08-02 21:53:27 +02:00
bind(sock_fd, (struct sockaddr *) &sock_address, sizeof(sock_address));
listen(sock_fd, SOMAXCONN);
sel = MAX(sock_fd, xfd) + 1;
2012-07-30 12:21:22 +02:00
2012-07-30 14:54:20 +02:00
load_settings();
2012-08-30 21:35:39 +02:00
xcb_flush(dpy);
2012-07-30 14:54:20 +02:00
2012-07-31 14:32:13 +02:00
while (running) {
2012-08-01 23:16:07 +02:00
FD_ZERO(&descriptors);
2012-08-02 21:53:27 +02:00
FD_SET(sock_fd, &descriptors);
2012-08-01 23:16:07 +02:00
FD_SET(xfd, &descriptors);
2012-08-01 15:16:57 +02:00
if (select(sel, &descriptors, NULL, NULL, NULL)) {
2012-08-01 23:16:07 +02:00
if (FD_ISSET(xfd, &descriptors)) {
while ((event = xcb_poll_for_event(dpy)) != NULL) {
handle_event(event);
free(event);
}
}
2012-07-30 12:21:22 +02:00
2012-08-02 21:53:27 +02:00
if (FD_ISSET(sock_fd, &descriptors)) {
ret_fd = accept(sock_fd, NULL, 0);
if (ret_fd > 0 && (nbr = recv(ret_fd, msg, sizeof(msg), 0)) > 0) {
msg[nbr] = '\0';
2012-08-02 22:37:20 +02:00
strcpy(rsp, EMPTY_RESPONSE);
2012-08-02 21:53:27 +02:00
process_message(msg, rsp);
send(ret_fd, rsp, strlen(rsp), 0);
2012-08-28 21:15:29 +02:00
close(ret_fd);
}
2012-07-30 12:21:22 +02:00
}
}
}
2012-08-02 21:53:27 +02:00
close(sock_fd);
2012-07-30 12:21:22 +02:00
xcb_disconnect(dpy);
2012-07-29 22:46:12 +02:00
return 0;
}