src/util.c File Reference

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/wait.h>
#include <stdarg.h>
#include <assert.h>
#include <iconv.h>
#include <xcb/xcb_icccm.h>
#include "i3.h"
#include "data.h"
#include "table.h"
#include "layout.h"
#include "util.h"
#include "xcb.h"
#include "client.h"
#include "log.h"
#include "ewmh.h"
#include "manage.h"
#include "workspace.h"
#include "ipc.h"
Include dependency graph for util.c:

Go to the source code of this file.

Functions

int min (int a, int b)
int max (int a, int b)
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 * smalloc (size_t size)
 Safe-wrapper around malloc which exits if malloc returns NULL (meaning that there is no more memory available).
void * scalloc (size_t size)
 Safe-wrapper around calloc which exits if malloc returns NULL (meaning that there is no more memory available).
char * sstrdup (const char *str)
 Safe-wrapper around strdup which exits if malloc returns NULL (meaning that there is no more memory available).
bool table_put (struct keyvalue_table_head *head, uint32_t key, void *value)
 Inserts an element into the given keyvalue-table using the given key.
void * table_remove (struct keyvalue_table_head *head, uint32_t key)
 Removes the element from the given keyvalue-table with the given key and returns its value;.
void * table_get (struct keyvalue_table_head *head, uint32_t key)
 Returns the value of the element of the given keyvalue-table with the given key.
void start_application (const char *command)
 Starts the given application by passing it through a shell.
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 * convert_utf8_to_ucs2 (char *input, int *real_strlen)
 Converts the given string to UCS-2 big endian for use with xcb_image_text_16().
Clientget_last_focused_client (xcb_connection_t *conn, Container *container, Client *exclude)
 Returns the client which comes next in focus stack (= was selected before) for the given container, optionally excluding the given client.
void set_focus (xcb_connection_t *conn, Client *client, bool set_anyways)
 Sets the given client as focused by updating the data structures correctly, updating the X input focus and finally re-decorating both windows (to signalize the user the new focus situation).
void leave_stack_mode (xcb_connection_t *conn, Container *container)
 Called when the user switches to another mode or when the container is destroyed and thus needs to be cleaned up.
void switch_layout_mode (xcb_connection_t *conn, Container *container, int mode)
 Switches the layout of the given container taking care of the necessary house-keeping.
Clientget_matching_client (xcb_connection_t *conn, const char *window_classtitle, Client *specific)
 Gets the first matching client for the given window class/window title.
static char ** append_argument (char **original, char *argument)
void i3_restart ()

Variables

static iconv_t conversion_descriptor = 0
struct keyvalue_table_head by_parent = TAILQ_HEAD_INITIALIZER(by_parent)
struct keyvalue_table_head by_child = TAILQ_HEAD_INITIALIZER(by_child)

Function Documentation

static char** append_argument ( char **  original,
char *  argument 
) [static]

Definition at line 492 of file util.c.

References DLOG, and smalloc().

Referenced by i3_restart().

Here is the call graph for this function:

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.

Definition at line 159 of file util.c.

Referenced by main(), and TAILQ_HEAD().

char* convert_utf8_to_ucs2 ( char *  input,
int *  real_strlen 
)

Converts the given string to UCS-2 big endian for use with xcb_image_text_16().

The amount of real glyphs is stored in real_strlen, a buffer containing the UCS-2 encoded string (16 bit per glyph) is returned. It has to be freed when done.

Definition at line 175 of file util.c.

References conversion_descriptor, and smalloc().

Referenced by get_matching_client(), handle_signal(), handle_windowname_change(), sig_draw_window(), and workspace_set_name().

Here is the call graph for this function:

Client* get_last_focused_client ( xcb_connection_t *  conn,
Container container,
Client exclude 
)

Returns the client which comes next in focus stack (= was selected before) for the given container, optionally excluding the given client.

Definition at line 217 of file util.c.

References Client::container, SLIST_FOREACH, and Container::workspace.

Referenced by handle_unmap_notify_event(), move_current_window(), set_focus(), switch_layout_mode(), and toggle_floating_mode().

Client* get_matching_client ( xcb_connection_t *  conn,
const char *  window_classtitle,
Client specific 
)

Gets the first matching client for the given window class/window title.

If the paramater specific is set to a specific client, only this one will be checked.

Definition at line 440 of file util.c.

References client_matches_class_name(), convert_utf8_to_ucs2(), DLOG, FREE, Client::name, Workspace::output, SLIST_FOREACH, sstrdup(), TAILQ_FOREACH, Client::window_class_class, Client::window_class_instance, and workspaces.

Referenced by jump_to_window(), and reparent_window().

Here is the call graph for this function:

void i3_restart (  ) 

Definition at line 514 of file util.c.

References append_argument(), global_conn, ipc_shutdown(), LOG, restore_geometry(), and start_argv.

Referenced by parse_command(), and sig_handle_key_press().

Here is the call graph for this function:

void leave_stack_mode ( xcb_connection_t *  conn,
Container container 
)

Called when the user switches to another mode or when the container is destroyed and thus needs to be cleaned up.

Definition at line 329 of file util.c.

References Cached_Pixmap::gc, Rect::height, Cached_Pixmap::id, Stack_Window::pixmap, Stack_Window::rect, SLIST_REMOVE, Container::stack_win, stack_wins, Rect::width, and Stack_Window::window.

Referenced by free_container(), and switch_layout_mode().

int max ( int  a,
int  b 
)
int min ( int  a,
int  b 
)

Definition at line 44 of file util.c.

Referenced by query_screens(), randr_query_outputs(), render_container(), and reparent_window().

void* scalloc ( size_t  size  ) 

Safe-wrapper around calloc which exits if malloc returns NULL (meaning that there is no more memory available).

Definition at line 74 of file util.c.

References exit_if_null.

Referenced by add_subscription(), disable_randr(), expand_table_cols(), expand_table_cols_at_head(), handle_output(), init_table(), IPC_HANDLER(), ipc_new_client(), load_configuration(), main(), new_container(), parse_file(), query_screens(), reparent_window(), table_put(), workspace_get(), and yyparse().

void set_focus ( xcb_connection_t *  conn,
Client client,
bool  set_anyways 
)
void* smalloc ( size_t  size  ) 

Safe-wrapper around malloc which exits if malloc returns NULL (meaning that there is no more memory available).

Definition at line 68 of file util.c.

References exit_if_null.

Referenced by append_argument(), convert_utf8_to_ucs2(), ewmh_update_workarea(), manage_existing_windows(), parse_file(), SLIST_HEAD(), TAILQ_HEAD(), translate_keysyms(), and yyparse().

char* sstrdup ( const char *  str  ) 

Safe-wrapper around strdup which exits if malloc returns NULL (meaning that there is no more memory available).

Definition at line 80 of file util.c.

References exit_if_null.

Referenced by client_mark(), get_matching_client(), glob_path(), ipc_create_socket(), jump_to_window(), load_configuration(), main(), parse_file(), TAILQ_HEAD(), and yyparse().

void start_application ( const char *  command  ) 

Starts the given application by passing it through a shell.

We use double fork to avoid zombie processes. As the started application’s parent exits (immediately), the application is reparented to init (process-id 1), which correctly handles childs, so we don’t have to do it :-).

The shell is determined by looking for the SHELL environment variable. If it does not exist, /bin/sh is used.

Definition at line 134 of file util.c.

Referenced by main(), and parse_command().

void switch_layout_mode ( xcb_connection_t *  conn,
Container container,
int  mode 
)
void* table_get ( struct keyvalue_table_head *  head,
uint32_t  key 
)
bool table_put ( struct keyvalue_table_head *  head,
uint32_t  key,
void *  value 
)

Inserts an element into the given keyvalue-table using the given key.

Definition at line 91 of file util.c.

References keyvalue_element::key, scalloc(), TAILQ_INSERT_TAIL, and keyvalue_element::value.

Referenced by reparent_window().

Here is the call graph for this function:

void* table_remove ( struct keyvalue_table_head *  head,
uint32_t  key 
)

Removes the element from the given keyvalue-table with the given key and returns its value;.

Definition at line 100 of file util.c.

References keyvalue_element::key, TAILQ_FOREACH, TAILQ_REMOVE, and keyvalue_element::value.

Referenced by handle_unmap_notify_event().

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 57 of file util.c.

Referenced by handle_output(), randr_query_outputs(), render_bars(), and render_container().


Variable Documentation

struct keyvalue_table_head by_child = TAILQ_HEAD_INITIALIZER(by_child)
struct keyvalue_table_head by_parent = TAILQ_HEAD_INITIALIZER(by_parent)
iconv_t conversion_descriptor = 0 [static]

Definition at line 40 of file util.c.

Referenced by convert_utf8_to_ucs2().

Generated by  doxygen 1.6.2-20100208