i3
util.c File Reference
#include "all.h"
#include <ctype.h>
#include <fcntl.h>
#include <inttypes.h>
#include <libgen.h>
#include <locale.h>
#include <sys/wait.h>
#include <unistd.h>
Include dependency graph for util.c:

Go to the source code of this file.

Macros

#define y(x, ...)
 
#define ystr(str)
 

Functions

int min (int a, int b)
 
int max (int a, int b)
 
bool rect_contains (Rect rect, uint32_t x, uint32_t y)
 
Rect rect_add (Rect a, Rect b)
 
Rect rect_sub (Rect a, Rect b)
 
Rect rect_sanitize_dimensions (Rect rect)
 
bool rect_equals (Rect a, Rect b)
 
 __attribute__ ((pure))
 
bool layout_from_name (const char *layout_str, layout_t *out)
 Set 'out' to the layout_t value for the given layout.
 
int ws_name_to_number (const char *name)
 Parses the workspace name as a number.
 
bool update_if_necessary (uint32_t *destination, const uint32_t new_value)
 Updates *destination with new_value and returns true if it was changed or false if it was the same.
 
void exec_i3_utility (char *name, char *argv[])
 exec()s an i3 utility, for example the config file migration script or i3-nagbar.
 
static char ** add_argument (char **original, char *opt_char, char *opt_arg, char *opt_name)
 
static char * store_restart_layout (void)
 
void i3_restart (bool forget_layout)
 Restart i3 in-place appends -a to argument list to disable autostart.
 
char * pango_escape_markup (char *input)
 Escapes the given string if a pango font is currently used.
 
static void nagbar_exited (EV_P_ ev_child *watcher, int revents)
 
void start_nagbar (pid_t *nagbar_pid, char *argv[])
 Starts an i3-nagbar instance with the given parameters.
 
void kill_nagbar (pid_t nagbar_pid, bool wait_for_it)
 Kills the i3-nagbar process, if nagbar_pid != -1.
 
bool parse_long (const char *str, long *out, int base)
 Converts a string into a long using strtol().
 
ssize_t slurp (const char *path, char **buf)
 Slurp reads path in its entirety into buf, returning the length of the file or -1 if the file could not be read.
 
orientation_t orientation_from_direction (direction_t direction)
 Convert a direction to its corresponding orientation.
 
position_t position_from_direction (direction_t direction)
 Convert a direction to its corresponding position.
 
direction_t direction_from_orientation_position (orientation_t orientation, position_t position)
 Convert orientation and position to the corresponding direction.
 
const char * direction_to_string (direction_t direction)
 Converts direction to a string representation.
 
const char * position_to_string (position_t position)
 Converts position to a string representation.
 

Macro Definition Documentation

◆ y

#define y ( x,
... )
Value:
yajl_gen_##x(gen, ##__VA_ARGS__)

Definition at line 216 of file util.c.

Referenced by rect_contains(), and store_restart_layout().

◆ ystr

#define ystr ( str)
Value:
yajl_gen_string(gen, (unsigned char *)str, strlen(str))

Definition at line 217 of file util.c.

Function Documentation

◆ __attribute__()

__attribute__ ( (pure) )

Definition at line 67 of file util.c.

Referenced by main().

◆ add_argument()

static char ** add_argument ( char ** original,
char * opt_char,
char * opt_arg,
char * opt_name )
static

Definition at line 185 of file util.c.

References scalloc().

Referenced by i3_restart().

Here is the call graph for this function:

◆ direction_from_orientation_position()

direction_t direction_from_orientation_position ( orientation_t orientation,
position_t position )

Convert orientation and position to the corresponding direction.

Definition at line 482 of file util.c.

References BEFORE, D_DOWN, D_LEFT, D_RIGHT, D_UP, and HORIZ.

Referenced by cmd_focus_direction().

◆ direction_to_string()

const char * direction_to_string ( direction_t direction)

Converts direction to a string representation.

Definition at line 494 of file util.c.

References D_DOWN, D_LEFT, D_RIGHT, and D_UP.

Referenced by tiling_drag().

◆ exec_i3_utility()

void exec_i3_utility ( char * name,
char * argv[] )

exec()s an i3 utility, for example the config file migration script or i3-nagbar.

This function first searches $PATH for the given utility named, then falls back to the dirname() of the i3 executable path and then falls back to the dirname() of the target of /proc/self/exe (on linux).

This function should be called after fork()ing.

The first argument of the given argv vector will be overwritten with the executable name, so pass NULL.

If the utility cannot be found in any of these locations, it exits with return code 2.

Definition at line 149 of file util.c.

References sasprintf(), sstrdup(), and start_argv.

Referenced by start_nagbar().

Here is the call graph for this function:

◆ i3_restart()

void i3_restart ( bool forget_layout)

Restart i3 in-place appends -a to argument list to disable autostart.

Definition at line 283 of file util.c.

References add_argument(), command_error_nagbar_pid, config_error_nagbar_pid, get_debug_logging(), ipc_shutdown(), kill_nagbar(), LOG, restore_geometry(), SHUTDOWN_REASON_RESTART, start_argv, and store_restart_layout().

Referenced by cmd_restart(), and sighandler_handle_key_press().

Here is the call graph for this function:

◆ kill_nagbar()

void kill_nagbar ( pid_t nagbar_pid,
bool wait_for_it )

Kills the i3-nagbar process, if nagbar_pid != -1.

If wait_for_it is set (restarting i3), this function will waitpid(), otherwise, ev is assumed to handle it (reloading).

Definition at line 394 of file util.c.

Referenced by cmd_reload(), i3_exit(), and i3_restart().

◆ layout_from_name()

bool layout_from_name ( const char * layout_str,
layout_t * out )

Set 'out' to the layout_t value for the given layout.

The function returns true on success or false if the passed string is not a valid layout name.

Definition at line 84 of file util.c.

References L_DEFAULT, L_SPLITH, L_SPLITV, L_STACKED, and L_TABBED.

Referenced by cmd_layout(), and con_toggle_layout().

◆ max()

◆ min()

◆ nagbar_exited()

static void nagbar_exited ( EV_P_ ev_child * watcher,
int revents )
static

Definition at line 333 of file util.c.

References DLOG, and ELOG.

Referenced by start_nagbar().

◆ orientation_from_direction()

orientation_t orientation_from_direction ( direction_t direction)

Convert a direction to its corresponding orientation.

Definition at line 466 of file util.c.

References D_LEFT, D_RIGHT, HORIZ, and VERT.

Referenced by cmd_resize_floating(), con_on_side_of_parent(), get_tree_next(), resize_find_tiling_participants(), tiling_drag(), and tree_move().

◆ pango_escape_markup()

char * pango_escape_markup ( char * input)

Escapes the given string if a pango font is currently used.

If the string has to be escaped, the input string will be free'd.

Definition at line 317 of file util.c.

References font_is_pango(), and FREE.

Referenced by con_parse_title_format().

Here is the call graph for this function:

◆ parse_long()

bool parse_long ( const char * str,
long * out,
int base )

Converts a string into a long using strtol().

This is a convenience wrapper checking the parsing result. It returns true if the number could be parsed.

Definition at line 419 of file util.c.

Referenced by bindings_get_buttons_to_grab(), cmd_shmlog(), cmd_swap(), configure_binding(), match_parse_property(), parse_restart_fd(), and translate_keysyms().

◆ position_from_direction()

position_t position_from_direction ( direction_t direction)

Convert a direction to its corresponding position.

Definition at line 474 of file util.c.

References AFTER, BEFORE, D_LEFT, and D_UP.

Referenced by get_tree_next(), and tiling_drag().

◆ position_to_string()

const char * position_to_string ( position_t position)

Converts position to a string representation.

Definition at line 512 of file util.c.

References AFTER, and BEFORE.

Referenced by tiling_drag().

◆ rect_add()

Rect rect_add ( Rect a,
Rect b )

Definition at line 39 of file util.c.

References Rect::height, Rect::width, Rect::x, and Rect::y.

Referenced by con_set_border_style(), and render_con().

◆ rect_contains()

bool rect_contains ( Rect rect,
uint32_t x,
uint32_t y )

◆ rect_equals()

bool rect_equals ( Rect a,
Rect b )

◆ rect_sanitize_dimensions()

Rect rect_sanitize_dimensions ( Rect rect)

Definition at line 53 of file util.c.

References Rect::height, and Rect::width.

Referenced by render_con().

◆ rect_sub()

Rect rect_sub ( Rect a,
Rect b )

Definition at line 46 of file util.c.

References Rect::height, Rect::width, Rect::x, and Rect::y.

Referenced by con_set_border_style().

◆ slurp()

ssize_t slurp ( const char * path,
char ** buf )

Slurp reads path in its entirety into buf, returning the length of the file or -1 if the file could not be read.

buf is set to a buffer of appropriate size, or NULL if -1 is returned.

Definition at line 437 of file util.c.

References ELOG, FREE, and scalloc().

Referenced by cmd_append_layout(), and tree_restore().

Here is the call graph for this function:

◆ start_nagbar()

void start_nagbar ( pid_t * nagbar_pid,
char * argv[] )

Starts an i3-nagbar instance with the given parameters.

Takes care of handling SIGCHLD and killing i3-nagbar when i3 exits.

The resulting PID will be stored in *nagbar_pid and can be used with kill_nagbar() to kill the bar later on.

Definition at line 360 of file util.c.

References DLOG, exec_i3_utility(), main_loop, nagbar_exited(), and smalloc().

Referenced by run_binding(), and start_config_error_nagbar().

Here is the call graph for this function:

◆ store_restart_layout()

static char * store_restart_layout ( void )
static

Definition at line 219 of file util.c.

References config, croot, DEFAULT_DIR_MODE, DLOG, dump_node(), ELOG, get_process_filename(), mkdirp(), resolve_tilde(), Config::restart_state_path, sstrdup(), writeall(), and y.

Referenced by i3_restart().

Here is the call graph for this function:

◆ update_if_necessary()

bool update_if_necessary ( uint32_t * destination,
const uint32_t new_value )

Updates *destination with new_value and returns true if it was changed or false if it was the same.

Definition at line 128 of file util.c.

Referenced by handle_output(), randr_query_outputs(), and randr_query_outputs_15().

◆ ws_name_to_number()

int ws_name_to_number ( const char * name)

Parses the workspace name as a number.

Returns -1 if the workspace should be interpreted as a "named workspace".

Definition at line 111 of file util.c.

Referenced by CFGFUN(), cmd_move_con_to_workspace_number(), cmd_rename_workspace(), cmd_workspace_number(), create_workspace_on_output(), gaps_for_workspace(), get_assigned_output(), json_end_map(), manage_window(), workspace_get(), and workspace_move_to_output().