diff --git a/cagebreak.c b/cagebreak.c index df8cde1..8b9184a 100644 --- a/cagebreak.c +++ b/cagebreak.c @@ -61,6 +61,8 @@ #define WAIT_ANY -1 #endif +bool show_info=false; + void set_sig_handler(int sig, void (*action)(int)) { struct sigaction act; @@ -134,7 +136,8 @@ usage(FILE *file, const char *const cage) { " -D\t Turn on damage tracking debugging\n" #endif " -h\t Display this help message\n" - " -v\t Show the version number and exit\n", + " -v\t Show the version number and exit\n" + " -s\t Show information about the current setup and exit\n", cage); } @@ -142,9 +145,9 @@ static bool parse_args(struct cg_server *server, int argc, char *argv[]) { int c; #ifdef DEBUG - while((c = getopt(argc, argv, "rDhv")) != -1) { + while((c = getopt(argc, argv, "rDhvs")) != -1) { #else - while((c = getopt(argc, argv, "rhv")) != -1) { + while((c = getopt(argc, argv, "rhvs")) != -1) { #endif switch(c) { case 'r': @@ -164,6 +167,9 @@ parse_args(struct cg_server *server, int argc, char *argv[]) { case 'v': fprintf(stdout, "Cagebreak version " CG_VERSION "\n"); exit(0); + case 's': + show_info=true; + break; default: usage(stderr, argv[0]); return false; @@ -528,6 +534,17 @@ main(int argc, char *argv[]) { wlr_xwayland_set_seat(xwayland, server.seat->seat); #endif + if(show_info) { + char *msg=server_show_info(&server); + if(msg != NULL) { + fprintf(stderr,"%s",msg); + free(msg); + } else { + wlr_log(WLR_ERROR,"Failed to get info on cagebreak setup\n"); + } + exit(0); + } + if(ipc_init(&server) != 0) { wlr_log(WLR_ERROR, "Failed to initialize IPC"); ret = 1; diff --git a/keybinding.c b/keybinding.c index 536e13c..26b2ae2 100644 --- a/keybinding.c +++ b/keybinding.c @@ -701,6 +701,18 @@ keybinding_show_time(struct cg_server *server) { free(msg); } +void +keybinding_show_info(struct cg_server *server) { + char *msg=server_show_info(server); + + if(!msg) { + return; + } + + message_printf(server->curr_output, "%s", msg); + free(msg); +} + void keybinding_move_view_to_next_output(struct cg_server *server) { if(wl_list_length(&server->outputs) <= 1) { @@ -940,6 +952,9 @@ run_action(enum keybinding_action action, struct cg_server *server, case KEYBINDING_SHOW_TIME: keybinding_show_time(server); break; + case KEYBINDING_SHOW_INFO: + keybinding_show_info(server); + break; case KEYBINDING_RESIZE_TILE_HORIZONTAL: resize_tile(server, data.i, 0); break; diff --git a/keybinding.h b/keybinding.h index dcc696f..4cd0e87 100644 --- a/keybinding.h +++ b/keybinding.h @@ -39,6 +39,7 @@ enum keybinding_action { KEYBINDING_MOVE_VIEW_TO_WORKSPACE, // data.u is the desired workspace KEYBINDING_MOVE_VIEW_TO_NEXT_OUTPUT, KEYBINDING_SHOW_TIME, + KEYBINDING_SHOW_INFO, KEYBINDING_SWAP_LEFT, KEYBINDING_SWAP_RIGHT, diff --git a/man/cagebreak-config.5.md b/man/cagebreak-config.5.md index 8c4b4b2..4472431 100644 --- a/man/cagebreak-config.5.md +++ b/man/cagebreak-config.5.md @@ -94,7 +94,7 @@ by prepending a line with the # symbol. Split current tile horizontally *input * - Set the setting "" to "" for device "". The identifier can either be "\*" (wildcard), of the form "type:" or the identifier of the device as printed for example by *cagebreak --show-info*. The supported input types are + Set the setting "" to "" for device "". The identifier can either be "\*" (wildcard), of the form "type:" or the identifier of the device as printed for example by *cagebreak -s*. The supported input types are - touchpad - pointer - keyboard @@ -209,6 +209,10 @@ by prepending a line with the # symbol. *resizeup* Resize the current tile towards the top +*show_info* + Display info about the current setup. In particular, print the identifiers + of the available inputs and outputs. + *setmode * Set the default mode to diff --git a/message.c b/message.c index 2ff2075..e544813 100644 --- a/message.c +++ b/message.c @@ -8,6 +8,7 @@ #include #include +#include "util.h" #include "message.h" #include "output.h" #include "pango.h" @@ -32,14 +33,14 @@ to_cairo_subpixel_order(const enum wl_output_subpixel subpixel) { struct wlr_texture * create_message_texture(const char *string, const struct cg_output *output) { - const int PADDING = 2; - const int MESSAGE_HEIGHT = 17 + 2 * PADDING; + const int WIDTH_PADDING = 8; + const int HEIGHT_PADDING = 2; const char *font = "pango:Monospace 10"; struct wlr_texture *texture; double scale = output->wlr_output->scale; int width = 0; - int height = MESSAGE_HEIGHT * scale; + int height = 0; // We must use a non-nil cairo_t for cairo_set_font_options to work. // Therefore, we cannot use cairo_create(NULL). @@ -59,8 +60,9 @@ create_message_texture(const char *string, const struct cg_output *output) { cairo_font_options_set_subpixel_order( fo, to_cairo_subpixel_order(output->wlr_output->subpixel)); cairo_set_font_options(c, fo); - get_text_size(c, font, &width, NULL, NULL, scale, "%s", string); - width += 2 * PADDING; + get_text_size(c, font, &width, &height, NULL, scale, "%s", string); + width += 2 * WIDTH_PADDING; + height += 2 * HEIGHT_PADDING; cairo_surface_destroy(dummy_surface); cairo_destroy(c); @@ -77,7 +79,7 @@ create_message_texture(const char *string, const struct cg_output *output) { cairo_set_line_width(cairo, 2); cairo_rectangle(cairo, 0, 0, width, height); cairo_stroke(cairo); - cairo_move_to(cairo, PADDING, PADDING); + cairo_move_to(cairo, WIDTH_PADDING, HEIGHT_PADDING); pango_printf(cairo, font, scale, "%s", string); @@ -149,17 +151,14 @@ message_set_output(struct cg_output *output, const char *string, void message_printf(struct cg_output *output, const char *fmt, ...) { - uint16_t buf_len = 256; - char *buffer = (char *)malloc(buf_len * sizeof(char)); + va_list ap; + va_start(ap, fmt); + char *buffer = malloc_vsprintf_va_list(fmt,ap); + va_end(ap); if(buffer == NULL) { wlr_log(WLR_ERROR, "Failed to allocate buffer in message_printf"); return; } - va_list ap; - - va_start(ap, fmt); - vsnprintf(buffer, buf_len, fmt, ap); - va_end(ap); struct wlr_box *box = malloc(sizeof(struct wlr_box)); if(box == NULL) { diff --git a/pango.c b/pango.c index c1ca414..e9d04d3 100644 --- a/pango.c +++ b/pango.c @@ -25,7 +25,7 @@ get_pango_layout(cairo_t *cairo, const char *font, const char *text, pango_attr_list_insert(attrs, pango_attr_scale_new(scale)); PangoFontDescription *desc = pango_font_description_from_string(font); pango_layout_set_font_description(layout, desc); - pango_layout_set_single_paragraph_mode(layout, 1); + pango_layout_set_single_paragraph_mode(layout, false); pango_layout_set_attributes(layout, attrs); pango_attr_list_unref(attrs); pango_font_description_free(desc); diff --git a/parse.c b/parse.c index f38f761..4bbf72d 100644 --- a/parse.c +++ b/parse.c @@ -11,23 +11,13 @@ #include "output.h" #include "parse.h" #include "server.h" - -char * -malloc_vsprintf(const char *fmt, va_list ap) { - va_list ap2; - va_copy(ap2, ap); - int len = vsnprintf(NULL, 0, fmt, ap); - char *ret = malloc(sizeof(char) * (len + 1)); - vsnprintf(ret, len + 1, fmt, ap2); - va_end(ap2); - return ret; -} +#include "util.h" char * log_error(const char *fmt, ...) { va_list args; va_start(args, fmt); - char *ret = malloc_vsprintf(fmt, args); + char *ret = malloc_vsprintf_va_list(fmt, args); if(ret != NULL) { wlr_log(WLR_ERROR, "%s", ret); } @@ -631,6 +621,8 @@ parse_command(struct cg_server *server, struct keybinding *keybinding, keybinding->action = KEYBINDING_SPLIT_HORIZONTAL; } else if(strcmp(action, "quit") == 0) { keybinding->action = KEYBINDING_QUIT; + } else if(strcmp(action, "show_info") == 0) { + keybinding->action = KEYBINDING_SHOW_INFO; } else if(strcmp(action, "close") == 0) { keybinding->action = KEYBINDING_CLOSE_VIEW; } else if(strcmp(action, "focus") == 0) { diff --git a/parse.h b/parse.h index ba1b2e8..798d732 100644 --- a/parse.h +++ b/parse.h @@ -4,9 +4,15 @@ #define MAX_LINE_SIZE 256 +#include + struct cg_server; int parse_rc_line(struct cg_server *server, char *line, char **errstr); +char * +parse_malloc_vsprintf(const char *fmt, ...); +char * +parse_malloc_vsprintf_va_list(const char *fmt, va_list list); #endif /* end of include guard PARSE_H */ diff --git a/server.c b/server.c index c82296f..b69f6dd 100644 --- a/server.c +++ b/server.c @@ -9,11 +9,17 @@ #define _POSIX_C_SOURCE 200809L + #include +#include #include #include +#include #include "server.h" +#include "input_manager.h" +#include "output.h" +#include "util.h" void display_terminate(struct cg_server *server) { @@ -35,3 +41,33 @@ get_mode_index_from_name(char *const *modes, const char *mode_name) { } return -1; } + +char * +server_show_info(struct cg_server *server) { + char *output_str=strdup(""), *output_str_tmp; + struct cg_output *output; + wl_list_for_each(output, &server->outputs, link) { + if(!output_str) { + return NULL; + } + output_str_tmp=output_str; + output_str=malloc_vsprintf("%s\t * %s\n",output_str, output->wlr_output->name); + free(output_str_tmp); + } + char *input_str=strdup(""), *input_str_tmp; + struct cg_input_device *input; + wl_list_for_each(input, &server->input->devices, link) { + if(!input_str) { + return NULL; + } + input_str_tmp=input_str; + if(strcmp(input->identifier,"") != 0) { + input_str=malloc_vsprintf("%s\t * H:%s\n",input_str, input->identifier); + } + free(input_str_tmp); + } + char *ret=malloc_vsprintf("Outputs:\n%sInputs:\n%s",output_str,input_str); + free(output_str); + free(input_str); + return ret; +} diff --git a/server.h b/server.h index 79bb408..9ee21e1 100644 --- a/server.h +++ b/server.h @@ -61,5 +61,7 @@ void display_terminate(struct cg_server *server); int get_mode_index_from_name(char *const *modes, const char *mode_name); +char * +server_show_info(struct cg_server *server); #endif diff --git a/util.c b/util.c index a877092..b74a589 100644 --- a/util.c +++ b/util.c @@ -10,6 +10,7 @@ #include #include "util.h" +#include int scale_length(int length, int offset, double scale) { @@ -33,3 +34,24 @@ scale_box(struct wlr_box *box, double scale) { box->x = (int)round(box->x * scale); box->y = (int)round(box->y * scale); } + +char * +malloc_vsprintf_va_list(const char *fmt, va_list ap) { + va_list ap2; + va_copy(ap2, ap); + int len = vsnprintf(NULL, 0, fmt, ap); + char *ret = malloc(sizeof(char) * (len + 1)); + vsnprintf(ret, len + 1, fmt, ap2); + va_end(ap2); + return ret; +} + +char * +malloc_vsprintf(const char *fmt, ...) { + va_list args; + va_start(args,fmt); + char *ret=malloc_vsprintf_va_list(fmt,args); + return ret; +} + + diff --git a/util.h b/util.h index 8ddc7be..01e403e 100644 --- a/util.h +++ b/util.h @@ -1,6 +1,8 @@ #ifndef CG_UTIL_H #define CG_UTIL_H +#include + struct wlr_box; /** Apply scale to a width or height. */ @@ -10,4 +12,9 @@ scale_length(int length, int offset, double scale); void scale_box(struct wlr_box *box, double scale); +char * +malloc_vsprintf(const char *fmt, ...); +char * +malloc_vsprintf_va_list(const char *fmt, va_list list); + #endif