sites

public wiki contents of suckless.org
git clone git://git.suckless.org/sites
Log | Files | Refs

dwm-ipc-v1.5.3-to-v1.5.4.diff (6166B)


      1 From 29ed50543dd31718fc55f9106dfc79aada72289d Mon Sep 17 00:00:00 2001
      2 From: mihirlad55 <mihirlad55@gmail.com>
      3 Date: Wed, 29 Jul 2020 23:56:24 +0000
      4 Subject: [PATCH] Update from v1.5.3 to v1.5.4
      5 
      6 - Fix redirection of dwm-msg not working. This was caused by stdout never being
      7   flushed. stdout is now flushed after every event message is received.
      8 - Add --ignore-reply option to dwm-msg which ignores the initial success/failure
      9   reply from the run_command and subscribe commands. This is most useful for
     10   subscribing to events and ignoring the initial success reply.
     11 ---
     12  dwm-msg.c | 120 ++++++++++++++++++++++++++++++++----------------------
     13  1 file changed, 72 insertions(+), 48 deletions(-)
     14 
     15 diff --git a/dwm-msg.c b/dwm-msg.c
     16 index c957adf..606f6ce 100644
     17 --- a/dwm-msg.c
     18 +++ b/dwm-msg.c
     19 @@ -46,6 +46,7 @@ typedef unsigned long Window;
     20  
     21  const char *DEFAULT_SOCKET_PATH = "/tmp/dwm.sock";
     22  static int sock_fd = -1;
     23 +static unsigned int ignore_reply = 0;
     24  
     25  typedef enum IPCMessageType {
     26    IPC_TYPE_RUN_COMMAND = 0,
     27 @@ -273,6 +274,18 @@ is_signed_int(const char *s)
     28    return 1;
     29  }
     30  
     31 +static void
     32 +flush_socket_reply()
     33 +{
     34 +  IPCMessageType reply_type;
     35 +  uint32_t reply_size;
     36 +  char *reply;
     37 +
     38 +  read_socket(&reply_type, &reply_size, &reply);
     39 +
     40 +  free(reply);
     41 +}
     42 +
     43  static void
     44  print_socket_reply()
     45  {
     46 @@ -283,6 +296,7 @@ print_socket_reply()
     47    read_socket(&reply_type, &reply_size, &reply);
     48  
     49    printf("%.*s\n", reply_size, reply);
     50 +  fflush(stdout);
     51    free(reply);
     52  }
     53  
     54 @@ -322,7 +336,10 @@ run_command(const char *name, char *args[], int argc)
     55  
     56    send_message(IPC_TYPE_RUN_COMMAND, msg_size, (uint8_t *)msg);
     57  
     58 -  print_socket_reply();
     59 +  if (!ignore_reply)
     60 +    print_socket_reply();
     61 +  else
     62 +    flush_socket_reply();
     63  
     64    yajl_gen_free(gen);
     65  
     66 @@ -408,7 +425,10 @@ subscribe(const char *event)
     67  
     68    send_message(IPC_TYPE_SUBSCRIBE, msg_size, (uint8_t *)msg);
     69  
     70 -  print_socket_reply();
     71 +  if (!ignore_reply)
     72 +    print_socket_reply();
     73 +  else
     74 +    flush_socket_reply();
     75  
     76    yajl_gen_free(gen);
     77  
     78 @@ -433,7 +453,7 @@ usage_error(const char *prog_name, const char *format, ...)
     79  static void
     80  print_usage(const char *name)
     81  {
     82 -  printf("usage: %s <command> [...]\n", name);
     83 +  printf("usage: %s [options] <command> [...]\n", name);
     84    puts("");
     85    puts("Commands:");
     86    puts("  run_command <name> [args...]    Run an IPC command");
     87 @@ -456,14 +476,16 @@ print_usage(const char *name)
     88    puts("");
     89    puts("  help                            Display this message");
     90    puts("");
     91 +  puts("Options:");
     92 +  puts("  --ignore-reply                  Don't print reply messages from");
     93 +  puts("                                  run_command and subscribe.");
     94 +  puts("");
     95  }
     96  
     97  int
     98  main(int argc, char *argv[])
     99  {
    100    const char *prog_name = argv[0];
    101 -  // Need at least command argument
    102 -  if (argc < 2) usage_error(prog_name, "Expected an argument, got none");
    103  
    104    connect_to_socket();
    105    if (sock_fd == -1) {
    106 @@ -471,49 +493,51 @@ main(int argc, char *argv[])
    107      return 1;
    108    }
    109  
    110 -  for (int i = 1; i < argc; i++) {
    111 -    if (strcmp(argv[i], "help") == 0) {
    112 -      print_usage(prog_name);
    113 -      return 0;
    114 -    } else if (strcmp(argv[i], "run_command") == 0) {
    115 -      if (++i >= argc) usage_error(prog_name, "No command specified");
    116 -      // Command name
    117 -      char *command = argv[i];
    118 -      // Command arguments are everything after command name
    119 -      char **command_args = argv + ++i;
    120 -      // Number of command arguments
    121 -      int command_argc = argc - i;
    122 -      run_command(command, command_args, command_argc);
    123 -      return 0;
    124 -    } else if (strcmp(argv[i], "get_monitors") == 0) {
    125 -      get_monitors();
    126 -      return 0;
    127 -    } else if (strcmp(argv[i], "get_tags") == 0) {
    128 -      get_tags();
    129 -      return 0;
    130 -    } else if (strcmp(argv[i], "get_layouts") == 0) {
    131 -      get_layouts();
    132 -      return 0;
    133 -    } else if (strcmp(argv[i], "get_dwm_client") == 0) {
    134 -      if (++i < argc) {
    135 -        if (is_unsigned_int(argv[i])) {
    136 -          Window win = atol(argv[i]);
    137 -          get_dwm_client(win);
    138 -        } else
    139 -          usage_error(prog_name, "Expected unsigned integer argument");
    140 -      } else
    141 -        usage_error(prog_name, "Expected the window id");
    142 -      return 0;
    143 -    } else if (strcmp(argv[i], "subscribe") == 0) {
    144 -      if (++i < argc) {
    145 -        for (int j = i; j < argc; j++) subscribe(argv[j]);
    146 +  int i = 1;
    147 +  if (strcmp(argv[i], "--ignore-reply") == 0) {
    148 +    ignore_reply = 1;
    149 +    i++;
    150 +  }
    151 +
    152 +  if (i >= argc) usage_error(prog_name, "Expected an argument, got none");
    153 +
    154 +  if (strcmp(argv[i], "help") == 0)
    155 +    print_usage(prog_name);
    156 +  else if (strcmp(argv[i], "run_command") == 0) {
    157 +    if (++i >= argc) usage_error(prog_name, "No command specified");
    158 +    // Command name
    159 +    char *command = argv[i];
    160 +    // Command arguments are everything after command name
    161 +    char **command_args = argv + ++i;
    162 +    // Number of command arguments
    163 +    int command_argc = argc - i;
    164 +    run_command(command, command_args, command_argc);
    165 +  } else if (strcmp(argv[i], "get_monitors") == 0) {
    166 +    get_monitors();
    167 +  } else if (strcmp(argv[i], "get_tags") == 0) {
    168 +    get_tags();
    169 +  } else if (strcmp(argv[i], "get_layouts") == 0) {
    170 +    get_layouts();
    171 +  } else if (strcmp(argv[i], "get_dwm_client") == 0) {
    172 +    if (++i < argc) {
    173 +      if (is_unsigned_int(argv[i])) {
    174 +        Window win = atol(argv[i]);
    175 +        get_dwm_client(win);
    176        } else
    177 -        usage_error(prog_name, "Expected event name");
    178 -      // Keep listening for events forever
    179 -      while (1) {
    180 -        print_socket_reply();
    181 -      }
    182 +        usage_error(prog_name, "Expected unsigned integer argument");
    183      } else
    184 -      usage_error(prog_name, "Invalid argument '%s'", argv[i]);
    185 -  }
    186 +      usage_error(prog_name, "Expected the window id");
    187 +  } else if (strcmp(argv[i], "subscribe") == 0) {
    188 +    if (++i < argc) {
    189 +      for (int j = i; j < argc; j++) subscribe(argv[j]);
    190 +    } else
    191 +      usage_error(prog_name, "Expected event name");
    192 +    // Keep listening for events forever
    193 +    while (1) {
    194 +      print_socket_reply();
    195 +    }
    196 +  } else
    197 +    usage_error(prog_name, "Invalid argument '%s'", argv[i]);
    198 +
    199 +  return 0;
    200  }
    201 -- 
    202 2.27.0
    203