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>
|
2013-01-02 12:36:54 +01:00
|
|
|
#include <xcb/xcb_keysyms.h>
|
2012-10-17 16:18:40 +02:00
|
|
|
#include <xcb/xinerama.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-10-03 20:07:33 +03:00
|
|
|
#include "helpers.h"
|
2012-10-24 16:34:56 +02:00
|
|
|
#include "window.h"
|
2012-08-20 22:38:29 +02:00
|
|
|
#include "bspwm.h"
|
2012-09-12 14:56:51 +02:00
|
|
|
#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-12-26 14:39:46 +01:00
|
|
|
void cleanup(void)
|
|
|
|
{
|
|
|
|
while (mon_head != NULL)
|
|
|
|
remove_monitor(mon_head);
|
|
|
|
while (rule_head != NULL)
|
|
|
|
remove_rule(rule_head);
|
|
|
|
free(frozen_pointer);
|
|
|
|
}
|
|
|
|
|
2012-09-21 17:39:22 +02:00
|
|
|
void register_events(void)
|
2012-08-01 23:16:07 +02:00
|
|
|
{
|
2012-09-21 19:05:42 +02:00
|
|
|
uint32_t values[] = {ROOT_EVENT_MASK};
|
2013-01-02 12:36:54 +01:00
|
|
|
xcb_generic_error_t *e = xcb_request_check(dpy, xcb_change_window_attributes_checked(dpy, root, XCB_CW_EVENT_MASK, values));
|
2012-10-06 11:55:22 +02:00
|
|
|
if (e != NULL) {
|
2012-09-21 17:39:22 +02:00
|
|
|
xcb_disconnect(dpy);
|
2012-10-25 15:20:14 +02:00
|
|
|
err("another wm is already running\n");
|
2012-09-21 17:39:22 +02:00
|
|
|
}
|
2012-10-23 12:42:40 +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)
|
2012-10-25 15:20:14 +02:00
|
|
|
err("can't acquire screen\n");
|
2013-01-02 12:36:54 +01:00
|
|
|
root = screen->root;
|
2012-11-10 12:00:30 +01:00
|
|
|
register_events();
|
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-09-21 17:39:22 +02:00
|
|
|
root_depth = screen->root_depth;
|
2012-08-01 23:16:07 +02:00
|
|
|
|
2012-09-30 18:24:24 +03:00
|
|
|
xcb_atom_t net_atoms[] = {ewmh->_NET_SUPPORTED,
|
2012-09-28 13:58:03 +02:00
|
|
|
ewmh->_NET_DESKTOP_NAMES,
|
|
|
|
ewmh->_NET_NUMBER_OF_DESKTOPS,
|
|
|
|
ewmh->_NET_CURRENT_DESKTOP,
|
|
|
|
ewmh->_NET_CLIENT_LIST,
|
|
|
|
ewmh->_NET_ACTIVE_WINDOW,
|
2012-10-10 21:36:19 +02:00
|
|
|
ewmh->_NET_WM_DESKTOP,
|
2012-09-28 13:58:03 +02:00
|
|
|
ewmh->_NET_WM_STATE,
|
|
|
|
ewmh->_NET_WM_STATE_FULLSCREEN,
|
|
|
|
ewmh->_NET_WM_WINDOW_TYPE,
|
2012-10-10 23:01:38 +02:00
|
|
|
ewmh->_NET_WM_WINDOW_TYPE_DOCK,
|
|
|
|
ewmh->_NET_WM_WINDOW_TYPE_NOTIFICATION,
|
2012-09-28 13:58:03 +02:00
|
|
|
ewmh->_NET_WM_WINDOW_TYPE_DIALOG,
|
|
|
|
ewmh->_NET_WM_WINDOW_TYPE_UTILITY,
|
|
|
|
ewmh->_NET_WM_WINDOW_TYPE_TOOLBAR};
|
2012-09-16 14:16:58 +02:00
|
|
|
|
|
|
|
xcb_ewmh_set_supported(ewmh, default_screen, LENGTH(net_atoms), net_atoms);
|
2012-08-29 18:37:31 +02:00
|
|
|
|
2012-12-25 19:03:35 +01:00
|
|
|
monitor_uid = desktop_uid = client_uid = rule_uid = 0;
|
2012-10-17 16:18:40 +02:00
|
|
|
mon = last_mon = mon_head = mon_tail = NULL;
|
|
|
|
|
|
|
|
bool xinerama_is_active = false;
|
|
|
|
|
|
|
|
if (xcb_get_extension_data(dpy, &xcb_xinerama_id)->present) {
|
|
|
|
xcb_xinerama_is_active_reply_t *xia = xcb_xinerama_is_active_reply(dpy, xcb_xinerama_is_active(dpy), NULL);
|
|
|
|
if (xia != NULL) {
|
|
|
|
xinerama_is_active = xia->state;
|
|
|
|
free(xia);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (xinerama_is_active) {
|
|
|
|
xcb_xinerama_query_screens_reply_t *xsq = xcb_xinerama_query_screens_reply(dpy, xcb_xinerama_query_screens(dpy), NULL);
|
|
|
|
xcb_xinerama_screen_info_t *xsi = xcb_xinerama_query_screens_screen_info(xsq);
|
|
|
|
int n = xcb_xinerama_query_screens_screen_info_length(xsq);
|
|
|
|
PRINTF("number of monitors: %d\n", n);
|
|
|
|
for (int i = 0; i < n; i++) {
|
|
|
|
xcb_xinerama_screen_info_t info = xsi[i];
|
|
|
|
xcb_rectangle_t rect = (xcb_rectangle_t) {info.x_org, info.y_org, info.width, info.height};
|
|
|
|
add_monitor(&rect);
|
|
|
|
}
|
|
|
|
free(xsq);
|
|
|
|
} else {
|
|
|
|
warn("Xinerama is inactive");
|
|
|
|
xcb_rectangle_t rect = (xcb_rectangle_t) {0, 0, screen_width, screen_height};
|
|
|
|
add_monitor(&rect);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (monitor_t *m = mon_head; m != NULL; m = m->next)
|
|
|
|
add_desktop(m, NULL);
|
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-10-25 21:49:14 +02:00
|
|
|
ewmh_update_current_desktop();
|
2012-12-25 19:03:35 +01:00
|
|
|
rule_head = rule_tail = NULL;
|
2012-09-22 15:10:59 +02:00
|
|
|
frozen_pointer = make_pointer_state();
|
2013-01-06 18:04:55 +01:00
|
|
|
update_pointer_position(&pointer_position);
|
2012-12-20 11:14:38 +01:00
|
|
|
last_entered = XCB_NONE;
|
2012-09-01 21:55:35 +02:00
|
|
|
split_mode = MODE_AUTOMATIC;
|
2012-12-23 12:11:08 +01:00
|
|
|
visible = true;
|
2012-12-24 12:02:35 +01:00
|
|
|
exit_status = 0;
|
2012-08-01 23:16:07 +02:00
|
|
|
}
|
2012-07-29 22:46:12 +02:00
|
|
|
|
2012-10-24 12:30:37 +02:00
|
|
|
int main(int argc, char *argv[])
|
2012-07-29 22:46:12 +02:00
|
|
|
{
|
2012-07-30 12:21:22 +02:00
|
|
|
fd_set descriptors;
|
2012-10-04 14:46:41 +02:00
|
|
|
char socket_path[MAXLEN];
|
2012-12-18 12:38:46 +01:00
|
|
|
char *fifo_path = NULL;
|
2012-09-30 18:24:24 +03:00
|
|
|
int sock_fd, ret_fd, dpy_fd, sel, n;
|
2012-08-02 21:53:27 +02:00
|
|
|
struct sockaddr_un sock_address;
|
2012-10-03 23:00:03 +02:00
|
|
|
size_t rsplen = 0;
|
2012-09-30 18:24:24 +03:00
|
|
|
char msg[BUFSIZ] = {0};
|
|
|
|
char rsp[BUFSIZ] = {0};
|
2012-07-30 12:21:22 +02:00
|
|
|
xcb_generic_event_t *event;
|
2012-12-18 12:38:46 +01:00
|
|
|
char opt;
|
|
|
|
|
|
|
|
while ((opt = getopt(argc, argv, "vs:")) != -1) {
|
|
|
|
switch (opt) {
|
|
|
|
case 'v':
|
|
|
|
printf("%s\n", VERSION);
|
|
|
|
exit(EXIT_SUCCESS);
|
|
|
|
break;
|
|
|
|
case 's':
|
|
|
|
fifo_path = optarg;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2012-07-30 12:21:22 +02:00
|
|
|
|
2012-07-31 14:32:13 +02:00
|
|
|
running = true;
|
2012-07-30 12:21:22 +02:00
|
|
|
dpy = xcb_connect(NULL, &default_screen);
|
|
|
|
|
|
|
|
if (xcb_connection_has_error(dpy))
|
2012-10-25 15:20:14 +02:00
|
|
|
err("can't open display\n");
|
2012-07-30 12:21:22 +02:00
|
|
|
|
2012-09-18 19:18:02 +02:00
|
|
|
setup();
|
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-09-24 11:00:35 +02:00
|
|
|
char *sp = getenv(SOCKET_ENV_VAR);
|
2012-09-29 11:57:41 +02:00
|
|
|
strncpy(socket_path, (sp == NULL ? DEFAULT_SOCKET_PATH : sp), sizeof(socket_path));
|
2012-08-02 21:53:27 +02:00
|
|
|
|
|
|
|
sock_address.sun_family = AF_UNIX;
|
2012-09-29 11:57:41 +02:00
|
|
|
strncpy(sock_address.sun_path, socket_path, sizeof(sock_address.sun_path));
|
2012-09-21 17:39:22 +02:00
|
|
|
unlink(socket_path);
|
2012-08-02 21:53:27 +02:00
|
|
|
|
|
|
|
sock_fd = socket(AF_UNIX, SOCK_STREAM, 0);
|
2012-08-01 15:16:57 +02:00
|
|
|
|
2012-09-30 18:24:24 +03:00
|
|
|
if (sock_fd == -1)
|
2012-10-25 15:20:14 +02:00
|
|
|
err("couldn't 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-12-18 12:38:46 +01:00
|
|
|
if (fifo_path != NULL) {
|
|
|
|
int fifo_fd = open(fifo_path, O_RDWR | O_NONBLOCK);
|
|
|
|
if (fifo_fd != -1)
|
|
|
|
status_fifo = fdopen(fifo_fd, "w");
|
|
|
|
else
|
|
|
|
warn("couldn't open status fifo\n");
|
|
|
|
}
|
|
|
|
|
2012-07-30 14:54:20 +02:00
|
|
|
load_settings();
|
2012-09-11 13:23:53 +02:00
|
|
|
run_autostart();
|
2012-10-23 17:01:32 +02:00
|
|
|
ewmh_update_wm_name();
|
2012-09-12 14:56:51 +02:00
|
|
|
|
2012-07-31 14:32:13 +02:00
|
|
|
while (running) {
|
2012-08-01 23:16:07 +02:00
|
|
|
|
2012-09-16 14:16:58 +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
|
|
|
|
2013-01-02 12:36:54 +01:00
|
|
|
if (select(sel, &descriptors, NULL, NULL, NULL) > 0) {
|
2012-08-01 15:16:57 +02:00
|
|
|
|
2012-08-02 21:53:27 +02:00
|
|
|
if (FD_ISSET(sock_fd, &descriptors)) {
|
|
|
|
ret_fd = accept(sock_fd, NULL, 0);
|
2012-09-30 18:24:24 +03:00
|
|
|
if (ret_fd > 0 && (n = recv(ret_fd, msg, sizeof(msg), 0)) > 0) {
|
|
|
|
msg[n] = '\0';
|
2012-08-02 21:53:27 +02:00
|
|
|
process_message(msg, rsp);
|
2012-10-03 23:00:03 +02:00
|
|
|
rsplen = strlen(rsp);
|
|
|
|
if (rsp[rsplen - 1] == '\n')
|
|
|
|
rsp[--rsplen] = '\0';
|
|
|
|
send(ret_fd, rsp, rsplen, 0);
|
2012-08-28 21:15:29 +02:00
|
|
|
close(ret_fd);
|
2012-10-04 11:38:23 +02:00
|
|
|
rsp[0] = '\0';
|
2012-08-01 15:28:50 +02:00
|
|
|
}
|
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)) {
|
2012-10-06 11:55:22 +02:00
|
|
|
err("connection has errors\n");
|
2012-07-30 12:21:22 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-26 14:39:46 +01:00
|
|
|
cleanup();
|
2012-08-02 21:53:27 +02:00
|
|
|
close(sock_fd);
|
2012-12-18 12:38:46 +01:00
|
|
|
if (status_fifo != NULL)
|
|
|
|
fclose(status_fifo);
|
2012-09-16 14:16:58 +02:00
|
|
|
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-12-24 12:02:35 +01:00
|
|
|
return exit_status;
|
2012-07-29 22:46:12 +02:00
|
|
|
}
|