2 #define I3__FILE__ "main.c"
14 #include <sys/types.h>
15 #include <sys/socket.h>
18 #include <sys/resource.h>
114 xcb_generic_event_t *event;
116 while ((event = xcb_poll_for_event(
conn)) != NULL) {
117 if (event->response_type == 0) {
119 DLOG(
"Expected X11 Error received for sequence %x\n", event->sequence);
121 xcb_generic_error_t *error = (xcb_generic_error_t *)event;
122 DLOG(
"X11 Error received (probably harmless)! sequence 0x%x, error_code = %d\n",
123 error->sequence, error->error_code);
130 int type = (
event->response_type & 0x7F);
145 DLOG(
"Setting main X11 callback to enabled=%d\n", enable);
164 #if EV_VERSION_MAJOR >= 4
169 fprintf(stderr,
"Closing SHM log \"%s\"\n",
shmlogname);
188 int main(
int argc,
char *argv[]) {
192 char *override_configpath = NULL;
193 bool autostart =
true;
194 char *layout_path = NULL;
195 bool delete_layout_path =
false;
196 bool force_xinerama =
false;
197 char *fake_outputs = NULL;
198 bool disable_signalhandler =
false;
200 static struct option long_options[] = {
201 {
"no-autostart", no_argument, 0,
'a'},
202 {
"config", required_argument, 0,
'c'},
203 {
"version", no_argument, 0,
'v'},
204 {
"moreversion", no_argument, 0,
'm'},
205 {
"more-version", no_argument, 0,
'm'},
206 {
"more_version", no_argument, 0,
'm'},
207 {
"help", no_argument, 0,
'h'},
208 {
"layout", required_argument, 0,
'L'},
209 {
"restart", required_argument, 0, 0},
210 {
"force-xinerama", no_argument, 0, 0},
211 {
"force_xinerama", no_argument, 0, 0},
212 {
"disable-signalhandler", no_argument, 0, 0},
213 {
"shmlog-size", required_argument, 0, 0},
214 {
"shmlog_size", required_argument, 0, 0},
215 {
"get-socketpath", no_argument, 0, 0},
216 {
"get_socketpath", no_argument, 0, 0},
217 {
"fake_outputs", required_argument, 0, 0},
218 {
"fake-outputs", required_argument, 0, 0},
219 {
"force-old-config-parser-v4.4-only", no_argument, 0, 0},
221 int option_index = 0, opt;
223 setlocale(LC_ALL,
"");
230 if (!isatty(fileno(stdout)))
231 setbuf(stdout, NULL);
244 while ((opt = getopt_long(argc, argv,
"c:CvmaL:hld:V", long_options, &option_index)) != -1) {
247 LOG(
"Autostart disabled using -a\n");
253 delete_layout_path =
false;
256 FREE(override_configpath);
257 override_configpath =
sstrdup(optarg);
260 LOG(
"Checking configuration file only (-C)\n");
261 only_check_config =
true;
264 printf(
"i3 version " I3_VERSION
" © 2009-2014 Michael Stapelberg and contributors\n");
268 printf(
"Binary i3 version: " I3_VERSION
" © 2009-2014 Michael Stapelberg and contributors\n");
276 LOG(
"Enabling debug logging\n");
283 if (strcmp(long_options[option_index].name,
"force-xinerama") == 0 ||
284 strcmp(long_options[option_index].name,
"force_xinerama") == 0) {
285 force_xinerama =
true;
286 ELOG(
"Using Xinerama instead of RandR. This option should be "
287 "avoided at all cost because it does not refresh the list "
288 "of screens, so you cannot configure displays at runtime. "
289 "Please check if your driver really does not support RandR "
290 "and disable this option as soon as you can.\n");
292 }
else if (strcmp(long_options[option_index].name,
"disable-signalhandler") == 0) {
293 disable_signalhandler =
true;
295 }
else if (strcmp(long_options[option_index].name,
"get-socketpath") == 0 ||
296 strcmp(long_options[option_index].name,
"get_socketpath") == 0) {
299 printf(
"%s\n", socket_path);
304 }
else if (strcmp(long_options[option_index].name,
"shmlog-size") == 0 ||
305 strcmp(long_options[option_index].name,
"shmlog_size") == 0) {
312 }
else if (strcmp(long_options[option_index].name,
"restart") == 0) {
315 delete_layout_path =
true;
317 }
else if (strcmp(long_options[option_index].name,
"fake-outputs") == 0 ||
318 strcmp(long_options[option_index].name,
"fake_outputs") == 0) {
319 LOG(
"Initializing fake outputs: %s\n", optarg);
320 fake_outputs =
sstrdup(optarg);
322 }
else if (strcmp(long_options[option_index].name,
"force-old-config-parser-v4.4-only") == 0) {
323 ELOG(
"You are passing --force-old-config-parser-v4.4-only, but that flag was removed by now.\n");
328 fprintf(stderr,
"Usage: %s [-c configfile] [-d all] [-a] [-v] [-V] [-C]\n", argv[0]);
329 fprintf(stderr,
"\n");
330 fprintf(stderr,
"\t-a disable autostart ('exec' lines in config)\n");
331 fprintf(stderr,
"\t-c <file> use the provided configfile instead\n");
332 fprintf(stderr,
"\t-C validate configuration file and exit\n");
333 fprintf(stderr,
"\t-d all enable debug output\n");
334 fprintf(stderr,
"\t-L <file> path to the serialized layout during restarts\n");
335 fprintf(stderr,
"\t-v display version and exit\n");
336 fprintf(stderr,
"\t-V enable verbose mode\n");
337 fprintf(stderr,
"\n");
338 fprintf(stderr,
"\t--force-xinerama\n"
339 "\tUse Xinerama instead of RandR.\n"
340 "\tThis option should only be used if you are stuck with the\n"
341 "\told nVidia closed source driver (older than 302.17), which does\n"
342 "\tnot support RandR.\n");
343 fprintf(stderr,
"\n");
344 fprintf(stderr,
"\t--get-socketpath\n"
345 "\tRetrieve the i3 IPC socket path from X11, print it, then exit.\n");
346 fprintf(stderr,
"\n");
347 fprintf(stderr,
"\t--shmlog-size <limit>\n"
348 "\tLimits the size of the i3 SHM log to <limit> bytes. Setting this\n"
349 "\tto 0 disables SHM logging entirely.\n"
350 "\tThe default is %d bytes.\n",
352 fprintf(stderr,
"\n");
353 fprintf(stderr,
"If you pass plain text arguments, i3 will interpret them as a command\n"
354 "to send to a currently running i3 (like i3-msg). This allows you to\n"
355 "use nice and logical commands, such as:\n"
358 "\ti3 floating toggle\n"
365 if (only_check_config) {
378 LOG(
"Additional arguments passed. Sending them as a command to i3.\n");
379 char *payload = NULL;
380 while (optind < argc) {
382 payload =
sstrdup(argv[optind]);
385 sasprintf(&both,
"%s %s", payload, argv[optind]);
391 DLOG(
"Command is: %s (%zd bytes)\n", payload, strlen(payload));
394 ELOG(
"Could not get i3 IPC socket path\n");
398 int sockfd = socket(AF_LOCAL, SOCK_STREAM, 0);
400 err(EXIT_FAILURE,
"Could not create socket");
402 struct sockaddr_un addr;
403 memset(&addr, 0,
sizeof(
struct sockaddr_un));
404 addr.sun_family = AF_LOCAL;
405 strncpy(addr.sun_path, socket_path,
sizeof(addr.sun_path) - 1);
406 if (connect(sockfd, (
const struct sockaddr *)&addr,
sizeof(
struct sockaddr_un)) < 0)
407 err(EXIT_FAILURE,
"Could not connect to i3");
410 (uint8_t *)payload) == -1)
411 err(EXIT_FAILURE,
"IPC: write()");
413 uint32_t reply_length;
417 if ((ret =
ipc_recv_message(sockfd, &reply_type, &reply_length, &reply)) != 0) {
419 err(EXIT_FAILURE,
"IPC: read()");
422 if (reply_type != I3_IPC_MESSAGE_TYPE_COMMAND)
423 errx(EXIT_FAILURE,
"IPC: received reply of type %d but expected %d (COMMAND)", reply_type, I3_IPC_MESSAGE_TYPE_COMMAND);
424 printf(
"%.*s\n", reply_length, reply);
433 struct rlimit limit = {RLIM_INFINITY, RLIM_INFINITY};
434 setrlimit(RLIMIT_CORE, &limit);
438 LOG(
"CORE DUMPS: You are running a development version of i3, so coredumps were automatically enabled (ulimit -c unlimited).\n");
439 size_t cwd_size = 1024;
442 while ((cwd_ret = getcwd(cwd, cwd_size)) == NULL && errno == ERANGE) {
443 cwd_size = cwd_size * 2;
447 LOG(
"CORE DUMPS: Your current working directory is \"%s\".\n", cwd);
449 if ((patternfd = open(
"/proc/sys/kernel/core_pattern", O_RDONLY)) >= 0) {
450 memset(cwd,
'\0', cwd_size);
451 if (read(patternfd, cwd, cwd_size) > 0)
453 LOG(
"CORE DUMPS: Your core_pattern is: %s", cwd);
459 LOG(
"i3 " I3_VERSION
" starting\n");
462 if (xcb_connection_has_error(
conn))
463 errx(EXIT_FAILURE,
"Cannot open display\n");
472 die(
"Could not initialize libev. Bad LIBEV_FLAGS?\n");
486 DLOG(
"root_screen->height_in_pixels = %d, root_screen->height_in_millimeters = %d, dpi = %d\n",
489 DLOG(
"One logical pixel corresponds to %d physical pixels on this display.\n",
logical_px(1));
491 xcb_get_geometry_cookie_t gcookie = xcb_get_geometry(
conn,
root);
492 xcb_query_pointer_cookie_t pointercookie = xcb_query_pointer(
conn,
root);
504 xcb_void_cookie_t cookie;
506 check_error(
conn, cookie,
"Another window manager seems to be running");
508 xcb_get_geometry_reply_t *greply = xcb_get_geometry_reply(
conn, gcookie, NULL);
509 if (greply == NULL) {
510 ELOG(
"Could not get geometry of the root window, exiting\n");
513 DLOG(
"root geometry reply: (%d, %d) %d x %d\n", greply->x, greply->y, greply->width, greply->height);
516 #define xmacro(atom) \
517 xcb_intern_atom_cookie_t atom##_cookie = xcb_intern_atom(conn, 0, strlen(#atom), #atom);
518 #include "atoms.xmacro"
530 const xcb_query_extension_reply_t *extreply;
531 extreply = xcb_get_extension_data(
conn, &xcb_xkb_id);
532 if (!extreply->present) {
533 DLOG(
"xkb is not present on this server\n");
535 DLOG(
"initializing xcb-xkb\n");
536 xcb_xkb_use_extension(
conn, XCB_XKB_MAJOR_VERSION, XCB_XKB_MINOR_VERSION);
537 xcb_xkb_select_events(
conn,
538 XCB_XKB_ID_USE_CORE_KBD,
539 XCB_XKB_EVENT_TYPE_STATE_NOTIFY | XCB_XKB_EVENT_TYPE_MAP_NOTIFY | XCB_XKB_EVENT_TYPE_NEW_KEYBOARD_NOTIFY,
541 XCB_XKB_EVENT_TYPE_STATE_NOTIFY | XCB_XKB_EVENT_TYPE_MAP_NOTIFY | XCB_XKB_EVENT_TYPE_NEW_KEYBOARD_NOTIFY,
551 #define xmacro(name) \
553 xcb_intern_atom_reply_t *reply = xcb_intern_atom_reply(conn, name##_cookie, NULL); \
555 ELOG("Could not get atom " #name "\n"); \
558 A_##name = reply->atom; \
561 #include "atoms.xmacro"
575 bool needs_tree_init =
true;
577 LOG(
"Trying to restore the layout from %s...", layout_path);
579 if (delete_layout_path) {
581 const char *dir = dirname(layout_path);
597 if (fake_outputs != NULL) {
607 DLOG(
"Checking for XRandR...\n");
613 xcb_query_pointer_reply_t *pointerreply;
615 if (!(pointerreply = xcb_query_pointer_reply(
conn, pointercookie, NULL))) {
616 ELOG(
"Could not query pointer position, using first screen\n");
618 DLOG(
"Pointer at %d, %d\n", pointerreply->root_x, pointerreply->root_y);
621 ELOG(
"ERROR: No screen at (%d, %d), starting on the first screen\n",
622 pointerreply->root_x, pointerreply->root_y);
633 if (ipc_socket == -1) {
634 ELOG(
"Could not create the IPC socket, IPC disabled\n");
636 struct ev_io *ipc_io =
scalloc(
sizeof(
struct ev_io));
646 ELOG(
"socket activation: Error in sd_listen_fds\n");
648 DLOG(
"socket activation: no sockets passed\n");
654 DLOG(
"socket activation: also listening on fd %d\n", fd);
659 if ((flags = fcntl(fd, F_GETFD)) < 0 ||
660 fcntl(fd, F_SETFD, flags & ~FD_CLOEXEC) < 0) {
661 ELOG(
"Could not disable FD_CLOEXEC on fd %d\n", fd);
664 struct ev_io *ipc_io =
scalloc(
sizeof(
struct ev_io));
680 struct ev_io *xcb_watcher =
scalloc(
sizeof(
struct ev_io));
682 struct ev_prepare *xcb_prepare =
scalloc(
sizeof(
struct ev_prepare));
691 ev_prepare_start(
main_loop, xcb_prepare);
708 xcb_grab_server(
conn);
711 xcb_generic_event_t *event;
712 while ((event = xcb_poll_for_event(
conn)) != NULL) {
713 if (event->response_type == 0) {
719 int type = (
event->response_type & 0x7F);
724 if (type == XCB_MAP_REQUEST)
731 xcb_ungrab_server(
conn);
734 LOG(
"This is not an in-place restart, copying root window contents to a pixmap\n");
736 uint16_t
width = root->width_in_pixels;
737 uint16_t
height = root->height_in_pixels;
739 xcb_gcontext_t gc = xcb_generate_id(
conn);
741 xcb_create_pixmap(
conn, root->root_depth, pixmap, root->root, width, height);
743 xcb_create_gc(
conn, gc, root->root,
744 XCB_GC_FUNCTION | XCB_GC_PLANE_MASK | XCB_GC_FILL_STYLE | XCB_GC_SUBWINDOW_MODE,
745 (uint32_t[]){XCB_GX_COPY, ~0, XCB_FILL_STYLE_SOLID, XCB_SUBWINDOW_MODE_INCLUDE_INFERIORS});
747 xcb_copy_area(
conn, root->root, pixmap, gc, 0, 0, 0, 0, width, height);
748 xcb_change_window_attributes_checked(
conn, root->root, XCB_CW_BACK_PIXMAP, (uint32_t[]){pixmap});
750 xcb_free_gc(
conn, gc);
751 xcb_free_pixmap(
conn, pixmap);
754 struct sigaction action;
757 action.sa_flags = SA_NODEFER | SA_RESETHAND | SA_SIGINFO;
758 sigemptyset(&action.sa_mask);
760 if (!disable_signalhandler)
764 if (sigaction(SIGQUIT, &action, NULL) == -1 ||
765 sigaction(SIGILL, &action, NULL) == -1 ||
766 sigaction(SIGABRT, &action, NULL) == -1 ||
767 sigaction(SIGFPE, &action, NULL) == -1 ||
768 sigaction(SIGSEGV, &action, NULL) == -1)
769 ELOG(
"Could not setup signal handler");
773 if (sigaction(SIGHUP, &action, NULL) == -1 ||
774 sigaction(SIGINT, &action, NULL) == -1 ||
775 sigaction(SIGALRM, &action, NULL) == -1 ||
776 sigaction(SIGUSR1, &action, NULL) == -1 ||
777 sigaction(SIGUSR2, &action, NULL) == -1)
778 ELOG(
"Could not setup signal handler");
782 signal(SIGPIPE, SIG_IGN);
796 LOG(
"auto-starting (always!) %s\n", exec_always->
command);
804 sasprintf(&command,
"%s --bar_id=%s --socket=\"%s\"",
807 LOG(
"Starting bar process: %s\n", command);
#define SD_LISTEN_FDS_START
struct reservedpx __attribute__
bool is_debug_build() __attribute__((const ))
Returns true if this version of i3 is a debug build (anything which is not a release version)...
int logical_px(const int logical)
Convert a logical amount of pixels (e.g.
char * sstrdup(const char *str)
Safe-wrapper around strdup which exits if malloc returns NULL (meaning that there is no more memory a...
int sd_listen_fds(int unset_environment)
void tree_render(void)
Renders the tree, that is rendering all outputs using render_con() and pushing the changes to X11 usi...
void ewmh_update_number_of_desktops(void)
Updates _NET_NUMBER_OF_DESKTOPS which we interpret as the number of noninternal workspaces.
int listen_fds
The number of file descriptors passed via socket activation.
char * get_process_filename(const char *prefix)
Returns the name of a temporary file with the specified prefix.
void load_configuration(xcb_connection_t *conn, const char *override_configpath, bool reload)
Reads the configuration from ~/.i3/config or /etc/i3/config if not found.
void xinerama_init(void)
We have just established a connection to the X server and need the initial Xinerama information to se...
void translate_keysyms(void)
Translates keysymbols to keycodes for all bindings which use keysyms.
void ewmh_update_desktop_names(void)
Updates _NET_DESKTOP_NAMES: "The names of all virtual desktops.
Con * con_descend_focused(Con *con)
Returns the focused con inside this client, descending the tree as far as possible.
char * fake_outputs
Overwrites output detection (for testing), see src/fake_outputs.c.
xcb_screen_t * root_screen
void ewmh_update_desktop_viewport(void)
Updates _NET_DESKTOP_VIEWPORT, which is an array of pairs of cardinals that define the top left corne...
bool parse_configuration(const char *override_configpath, bool use_nagbar)
Finds the configuration file to use (either the one specified by override_configpath), the user’s one or the system default) and calls parse_file().
static void xcb_got_event(EV_P_ struct ev_io *w, int revents)
struct ev_loop * main_loop
xcb_timestamp_t last_timestamp
The last timestamp we got from X11 (timestamps are included in some events and are used for some thin...
An Output is a physical output on your graphics driver.
int ipc_recv_message(int sockfd, uint32_t *message_type, uint32_t *reply_length, uint8_t **reply)
Reads a message from the given socket file descriptor and stores its length (reply_length) as well as...
void grab_all_keys(xcb_connection_t *conn, bool bind_mode_switch)
Grab the bound keys (tell X to send us keypress events for those keycodes)
void display_running_version(void)
Connects to i3 to find out the currently running version.
void set_verbosity(bool _verbose)
Set verbosity of i3.
static void handle_signal(int sig, siginfo_t *info, void *data)
void manage_existing_windows(xcb_window_t root)
Go through all existing windows (if the window manager is restarted) and manage them.
char * root_atom_contents(const char *atomname, xcb_connection_t *provided_conn, int screen)
Try to get the contents of the given atom (for example I3_SOCKET_PATH) from the X11 root window and r...
static void xcb_prepare_cb(EV_P_ ev_prepare *w, int revents)
static xcb_pixmap_t pixmap
int sasprintf(char **strp, const char *fmt,...)
Safe-wrapper around asprintf which exits if it returns -1 (meaning that there is no more memory avail...
int ipc_create_socket(const char *filename)
Creates the UNIX domain socket at the given path, sets it to non-blocking mode, bind()s and listen()s...
bool tree_restore(const char *path, xcb_get_geometry_reply_t *geometry)
Loads tree from ~/.i3/_restart.json (used for in-place restarts).
Con * con
Pointer to the Con which represents this output.
void ewmh_update_workarea(void)
i3 currently does not support _NET_WORKAREA, because it does not correspond to i3’s concept of works...
char * id
Automatically generated ID for this bar config.
void main_set_x11_cb(bool enable)
Enable or disable the main X11 event handling function.
bool event_is_ignored(const int sequence, const int response_type)
Checks if the given sequence is ignored and returns true if so.
void ewmh_update_current_desktop(void)
Updates _NET_CURRENT_DESKTOP with the current desktop number.
struct autostarts_head autostarts
bool no_startup_id
no_startup_id flag for start_application().
void con_focus(Con *con)
Sets input focus to the given container.
void xcursor_set_root_cursor(int cursor_id)
Sets the cursor of the root window to the 'pointer' cursor.
void setup_signal_handler(void)
Setup signal handlers to safely handle SIGSEGV and SIGFPE.
int ipc_send_message(int sockfd, const uint32_t message_size, const uint32_t message_type, const uint8_t *payload)
Formats a message (payload) of the given size and type and sends it to i3 via the given socket file d...
Output * get_first_output(void)
Returns the first output which is active.
static void i3_exit(void)
struct assignments_head assignments
void fake_outputs_init(const char *output_spec)
Creates outputs according to the given specification.
const int default_shmlog_size
void check_error(xcb_connection_t *conn, xcb_void_cookie_t cookie, char *err_message)
Checks a generic cookie for errors and quits with the given message if there was an error...
char * i3bar_command
Command that should be run to execute i3bar, give a full path if i3bar is not in your $PATH...
void ewmh_setup_hints(void)
Set up the EWMH hints on the root window.
void xcursor_load_cursors(void)
void randr_init(int *event_base)
We have just established a connection to the X server and need the initial XRandR information to setu...
unsigned int xcb_numlock_mask
A 'Con' represents everything from the X11 root window down to a single X11 window.
void xcb_set_root_cursor(int cursor)
Set the cursor of the root window to the given cursor id.
void * scalloc(size_t size)
Safe-wrapper around calloc which exits if malloc returns NULL (meaning that there is no more memory a...
struct bindings_head * bindings
char * current_socketpath
uint32_t aio_get_mod_mask_for(uint32_t keysym, xcb_key_symbols_t *symbols)
All-in-one function which returns the modifier mask (XCB_MOD_MASK_*) for the given keysymbol...
void scratchpad_fix_resolution(void)
When starting i3 initially (and after each change to the connected outputs), this function fixes the ...
bool force_xinerama
By default, use the RandR API for multi-monitor setups.
Output * get_output_containing(unsigned int x, unsigned int y)
Returns the active (!) output which contains the coordinates x, y or NULL if there is no output which...
void tree_init(xcb_get_geometry_reply_t *geometry)
Initializes the tree by creating the root node, adding all RandR outputs to the tree (that means rand...
#define TAILQ_HEAD_INITIALIZER(head)
char * command
Command, like in command mode.
static void xcb_check_cb(EV_P_ ev_check *w, int revents)
void ipc_new_client(EV_P_ struct ev_io *w, int revents)
Handler for activity on the listening socket, meaning that a new client has just connected and we sho...
struct barconfig_head barconfigs
void init_logging(void)
Initializes logging by creating an error logfile in /tmp (or XDG_RUNTIME_DIR, see get_process_filenam...
int main(int argc, char *argv[])
Con * output_get_content(Con *output)
Returns the output container below the given output container.
void start_application(const char *command, bool no_startup_id)
Starts the given application by passing it through a shell.
void * smalloc(size_t size)
Safe-wrapper around malloc which exits if malloc returns NULL (meaning that there is no more memory a...
void x_set_i3_atoms(void)
Sets up i3 specific atoms (I3_SOCKET_PATH and I3_CONFIG_PATH)
xcb_key_symbols_t * keysyms
#define TAILQ_FOREACH(var, head, field)
void restore_connect(void)
Opens a separate connection to X11 for placeholder windows when restoring layouts.
static struct ev_check * xcb_check
void * srealloc(void *ptr, size_t size)
Safe-wrapper around realloc which exits if realloc returns NULL (meaning that there is no more memory...
Holds the status bar configuration (i3bar).
struct rlimit original_rlimit_core
The original value of RLIMIT_CORE when i3 was started.
struct autostarts_always_head autostarts_always
void property_handlers_init(void)
Sets the appropriate atoms for the property handlers after the atoms were received from X11...
Holds a command specified by either an:
struct ws_assignments_head ws_assignments
void set_debug_logging(const bool _debug_logging)
Set debug logging.
void handle_event(int type, xcb_generic_event_t *event)
Takes an xcb_generic_event_t and calls the appropriate handler, based on the event type...