bspwm/bspwm.c

159 lines
3.8 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-11 13:12:53 +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-09-11 13:12:53 +02:00
#include "rules.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"
#include "tree.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;
}
2012-08-02 09:27:48 +02:00
int register_events(void)
2012-08-01 23:16:07 +02:00
{
uint32_t values[] = {XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT | XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY};
2012-09-18 17:21:04 +02:00
xcb_generic_error_t *err = xcb_request_check(dpy, xcb_change_window_attributes_checked(dpy, screen->root, XCB_CW_EVENT_MASK, values));
2012-08-30 21:35:39 +02:00
if (err != NULL)
return 1;
2012-08-01 23:16:07 +02:00
return 0;
}
2012-08-30 21:35:39 +02:00
2012-09-18 19:18:02 +02:00
void setup(void)
2012-09-11 13:12:53 +02:00
{
2012-08-28 21:15:29 +02:00
ewmh_init();
2012-09-18 17:21:04 +02:00
screen = xcb_setup_roots_iterator(xcb_get_setup(dpy)).data;
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;
xcb_atom_t net_atoms[] = {ewmh->_NET_SUPPORTED, ewmh->_NET_WM_STATE_FULLSCREEN, ewmh->_NET_WM_STATE, ewmh->_NET_ACTIVE_WINDOW};
xcb_ewmh_set_supported(ewmh, default_screen, LENGTH(net_atoms), net_atoms);
2012-08-29 18:37:31 +02:00
desk = make_desktop(DEFAULT_DESK_NAME);
last_desk = NULL;
desk_head = desk;
desk_tail = desk;
num_desktops++;
2012-09-18 19:18:02 +02:00
2012-09-18 17:21:04 +02:00
ewmh_update_number_of_desktops();
ewmh_update_desktop_names();
2012-09-11 13:12:53 +02:00
rule_head = make_rule();
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-09-09 10:46:46 +02:00
int sock_fd, ret_fd, dpy_fd, sel, nbr;
2012-08-02 21:53:27 +02:00
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-09-18 19:18:02 +02:00
setup();
2012-08-01 23:16:07 +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-09-09 10:46:46 +02:00
dpy_fd = 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);
2012-09-09 10:46:46 +02:00
sel = MAX(sock_fd, dpy_fd) + 1;
2012-07-30 12:21:22 +02:00
2012-07-30 14:54:20 +02:00
load_settings();
2012-09-11 13:23:53 +02:00
run_autostart();
2012-09-14 11:30:41 +02:00
ewmh_update_wm_name();
update_root_dimensions();
2012-07-31 14:32:13 +02:00
while (running) {
2012-08-01 23:16:07 +02:00
xcb_flush(dpy);
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-09-09 10:46:46 +02:00
FD_SET(dpy_fd, &descriptors);
2012-08-01 23:16:07 +02:00
2012-08-01 15:16:57 +02:00
if (select(sel, &descriptors, NULL, NULL, NULL)) {
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-09-18 17:21:04 +02:00
if (FD_ISSET(dpy_fd, &descriptors)) {
while ((event = xcb_poll_for_event(dpy)) != NULL) {
handle_event(event);
free(event);
}
}
}
if (xcb_connection_has_error(dpy)) {
die("connection has errors\n");
2012-07-30 12:21:22 +02:00
}
}
2012-08-02 21:53:27 +02:00
close(sock_fd);
xcb_ewmh_connection_wipe(ewmh);
free(ewmh);
2012-09-18 17:21:04 +02:00
xcb_flush(dpy);
2012-07-30 12:21:22 +02:00
xcb_disconnect(dpy);
2012-07-29 22:46:12 +02:00
return 0;
}