Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed execute berryc without any arg cause seg fault; added command usage and description #165

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
109 changes: 62 additions & 47 deletions client.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,62 +37,65 @@ struct command {
bool config;
int argc;
void (*handler)(long *, bool, int, char **);
const char* usage;
const char* description;
};

static const struct command command_table[] = {
{ "window_move", IPCWindowMoveRelative, false, 2, fn_int },
{ "window_move_absolute", IPCWindowMoveAbsolute, false, 2, fn_int },
{ "window_resize", IPCWindowResizeRelative, false, 2, fn_int },
{ "window_resize_absolute", IPCWindowResizeAbsolute, false, 2, fn_int },
{ "window_raise", IPCWindowRaise, false, 0, NULL },
{ "window_monocle", IPCWindowMonocle, false, 0, NULL },
{ "window_close", IPCWindowClose, false, 0, NULL },
{ "window_center", IPCWindowCenter, false, 0, NULL },
{ "focus_color", IPCFocusColor, true, 1, fn_hex },
{ "unfocus_color", IPCUnfocusColor, true, 1, fn_hex },
{ "inner_focus_color", IPCInnerFocusColor, true, 1, fn_hex },
{ "inner_unfocus_color", IPCInnerUnfocusColor, true, 1, fn_hex },
{ "text_focus_color", IPCTitleFocusColor, true, 1, fn_hex },
{ "text_unfocus_color", IPCTitleUnfocusColor, true, 1, fn_hex },
{ "border_width", IPCBorderWidth, true, 1, fn_int },
{ "inner_border_width", IPCInnerBorderWidth, true, 1, fn_int },
{ "title_height", IPCTitleHeight, true, 1, fn_int },
{ "switch_workspace", IPCSwitchWorkspace, false, 1, fn_int },
{ "send_to_workspace", IPCSendWorkspace, false, 1, fn_int },
{ "fullscreen", IPCFullscreen, false, 0, NULL },
{ "fullscreen_state", IPCFullscreenState, false, 0, NULL },
{ "fullscreen_remove_dec", IPCFullscreenRemoveDec, true, 1, fn_bool },
{ "fullscreen_max", IPCFullscreenMax, true, 1, fn_bool },
{ "snap_left", IPCSnapLeft, false, 0, NULL },
{ "snap_right", IPCSnapRight, false, 0, NULL },
{ "cardinal_focus", IPCCardinalFocus, false, 1, fn_int },
{ "toggle_decorations", IPCWindowToggleDecorations, false, 0, NULL },
{ "cycle_focus", IPCCycleFocus, false, 0, NULL },
{ "pointer_focus", IPCPointerFocus, false, 0, NULL },
{ "quit", IPCQuit, true, 0, NULL },
{ "top_gap", IPCTopGap, true, 1, fn_int },
{ "edge_gap", IPCEdgeGap, false, 4, fn_int },
{ "save_monitor", IPCSaveMonitor, false, 2, fn_int },
{ "smart_place", IPCSmartPlace, true, 1, fn_bool },
{ "draw_text", IPCDrawText, true, 1, fn_bool },
{ "edge_lock", IPCEdgeLock, true, 1, fn_bool },
{ "set_font", IPCSetFont, false, 1, fn_font },
{ "json_status", IPCJSONStatus, true, 1, fn_bool },
{ "manage", IPCManage, true, 1, fn_str },
{ "unmanage", IPCUnmanage, true, 1, fn_str },
{ "decorate_new", IPCDecorate, true, 1, fn_bool },
{ "name_desktop", IPCNameDesktop, false, 2, fn_int_str },
{ "move_mask", IPCMoveMask, true, 1, fn_mask },
{ "resize_mask", IPCResizeMask, true, 1, fn_mask },
{ "pointer_interval", IPCPointerInterval, true, 1, fn_int },
{ "focus_follows_pointer", IPCFocusFollowsPointer, true, 1, fn_bool },
{ "warp_pointer", IPCWarpPointer, true, 1, fn_bool },
{ "window_move", IPCWindowMoveRelative, false, 2, fn_int, "x y", "move the focused window by x and y pixels, relatively"},
{ "window_move_absolute", IPCWindowMoveAbsolute, false, 2, fn_int, "x y", "move the focused window to position x and y"},
{ "window_resize", IPCWindowResizeRelative, false, 2, fn_int, "x y", "resize the focused window by x and y pixels, relatively"},
{ "window_resize_absolute", IPCWindowResizeAbsolute, false, 2, fn_int, "x y", "resize the focused window by x and y"},
{ "window_raise", IPCWindowRaise, false, 0, NULL, "", "raise the focused window"},
{ "window_monocle", IPCWindowMonocle, false, 0, NULL, "", "set the focused window the fill the screen, respecting top_gap, maintains decorations"},
{ "window_close", IPCWindowClose, false, 0, NULL, "", "close the focused window and its associated application"},
{ "window_center", IPCWindowCenter, false, 0, NULL, "", "center the focused window, maintains current size"},
{ "focus_color", IPCFocusColor, true, 1, fn_hex, "#XXXXXX", "Set the color of the outer border of the focused window"},
{ "unfocus_color", IPCUnfocusColor, true, 1, fn_hex, "#XXXXXX", "Set the color of the outer border for all unfocused windows"},
{ "inner_focus_color", IPCInnerFocusColor, true, 1, fn_hex, "#XXXXXX", "Set the color of the inner border and the titlebar of the focused window"},
{ "inner_unfocus_color", IPCInnerUnfocusColor, true, 1, fn_hex, "#XXXXXX", "Set the color of the inner border and the titlebar of the unfocused window"},
{ "text_focus_color", IPCTitleFocusColor, true, 1, fn_hex, "#XXXXXX", "Set the color of the title bar text for the focused window"},
{ "text_unfocus_color", IPCTitleUnfocusColor, true, 1, fn_hex, "#XXXXXX", "Set the color of the title bar text for all unfocused windows"},
{ "border_width", IPCBorderWidth, true, 1, fn_int, "WIDTH", "Set the border width, in pixels, of the outer border"},
{ "inner_border_width", IPCInnerBorderWidth, true, 1, fn_int, "WIDTH", "Set the border width, in pixels, of the inneer border"},
{ "title_height", IPCTitleHeight, true, 1, fn_int, "HEIGHT", "Set the height of the title bar, does not include border widths"},
{ "switch_workspace", IPCSwitchWorkspace, false, 1, fn_int, "i", "switch the active desktop"},
{ "send_to_workspace", IPCSendWorkspace, false, 1, fn_int, "i", "send the focused window to the given workspace"},
{ "fullscreen", IPCFullscreen, false, 0, NULL, "", "toggle fullscreen status of the focused window, fills the active screen"},
{ "fullscreen_state", IPCFullscreenState, false, 0, NULL, "", "toggle fullscreen status of the focused window, doesn’t resize the window"},
{ "fullscreen_remove_dec", IPCFullscreenRemoveDec, true, 1, fn_bool, "true/false", ""},
{ "fullscreen_max", IPCFullscreenMax, true, 1, fn_bool, "true/false", ""},
{ "snap_left", IPCSnapLeft, false, 0, NULL, "", "snap the focused window to fill the left half of the screen"},
{ "snap_right", IPCSnapRight, false, 0, NULL, "", "snap the focused window to fill the right half of the screen"},
{ "cardinal_focus", IPCCardinalFocus, false, 1, fn_int, "1/2/3/4", "shift focus to the nearest client in the specified direction"},
{ "toggle_decorations", IPCWindowToggleDecorations, false, 0, NULL, "", "toggle decorations for the focused client"},
{ "cycle_focus", IPCCycleFocus, false, 0, NULL, "", "change focus to the next client in the stack"},
{ "pointer_focus", IPCPointerFocus, false, 0, NULL, "", "focus the window under the current pointer (used by sxhkd)"},
{ "quit", IPCQuit, true, 0, NULL, "", "close the window manager"},
{ "top_gap", IPCTopGap, true, 1, fn_int, "GAP", "Set the offset at the top of the monitor (usually for system bars)"},
{ "edge_gap", IPCEdgeGap, false, 4, fn_int, "TOP BOTTOM LEFT RIGHT", "Set the edge gap around the monitor (must include all parameters)"},
{ "save_monitor", IPCSaveMonitor, false, 2, fn_int, "i j", "Associate the ith monitor to the jth workspace"},
{ "smart_place", IPCSmartPlace, true, 1, fn_bool, "true/false", "Determine whether or not newly placed windows should be placed in the largest available space."},
{ "draw_text", IPCDrawText, true, 1, fn_bool, "true/false", "Determine whether or not text should be draw in title bars"},
{ "edge_lock", IPCEdgeLock, true, 1, fn_bool, "true/false", ""},
{ "set_font", IPCSetFont, false, 1, fn_font, "FONT_NAME", "Set the name of the font to use (e.g. set_font 'xft:Rubik:size=10:style=Bold')"},
{ "json_status", IPCJSONStatus, true, 1, fn_bool, "true/false", "Determine whether or not BERRY_WINDOW_STATUS returns JSON formatted text."},
{ "manage", IPCManage, true, 1, fn_str, "Dialog|Toolbar|Menu|Splash|Utility", "Set berry to manage clients of the above type. Clients which are managed will be given decorations and are movable by the window manager. This is not retroactive for current clients. Only Toolbars and Splahes are not handled by default."},
{ "unmanage", IPCUnmanage, true, 1, fn_str, "Dialog|Toolbar|Menu|Splash|Utility", "Set berry to not manage clients of the above type."},
{ "decorate_new", IPCDecorate, true, 1, fn_bool, "true/false", "Determine whether or not new windows are decorated by default"},
{ "name_desktop", IPCNameDesktop, false, 2, fn_int_str, "DESKTOP NAME", "Name the ith desktop name. Used with _NET_DESKTOP_NAMES."},
{ "move_mask", IPCMoveMask, true, 1, fn_mask, "shift|lock|ctrl|mod1|mod2|mod3|mod4|mod5", ""},
{ "resize_mask", IPCResizeMask, true, 1, fn_mask, "shift|lock|ctrl|mod1|mod2|mod3|mod4|mod5", ""},
{ "pointer_interval", IPCPointerInterval, true, 1, fn_int, "INTERVAL", "The minimum interval between two motion events generated by the pointer. Should help resolve issues related to resize lag on high refresh rate monitors. Default value of 0."},
{ "focus_follows_pointer", IPCFocusFollowsPointer, true, 1, fn_bool, "true/false", ""},
{ "warp_pointer", IPCWarpPointer, true, 1, fn_bool, "true/false", ""},
};

static void
fn_hex(long *data, bool b, int i, char **argv)
{
UNUSED(b);
if (*argv[i - 1] == '#') ++argv[i - 1];
data[i+b] = strtoul(argv[i - 1], NULL, 16);
}

Expand Down Expand Up @@ -204,6 +207,14 @@ usage(FILE *out)
{
int rc = out == stderr ? EXIT_FAILURE : EXIT_SUCCESS;
fputs("Usage: berryc [-h|-v] <command> [args...]\n", out);
fputs("Available commands: \n", out);
size_t command_count = sizeof(command_table) / sizeof(struct command);
for (size_t i = 0; i < command_count; ++i) {
struct command command = command_table[i];
fprintf(out, "%s %s\n %s\n\n", command.name, command.usage, command.description);
}
fputs("Please note that for the previous commands, #XXXXXX represents a hex color, accept hex color without leading #\n", out);
fputs("All of these commands can be viewed on your system via `man berryc`\n", out);
exit(rc);
}

Expand Down Expand Up @@ -267,6 +278,10 @@ main(int argc, char **argv)
int c, c_argc;
char **c_argv;

if (argc == 1) {
usage(stderr);
}

c_argc = argc - 2;
c_argv = argv + 2;

Expand Down