quake-style console with xterm
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

KNF, DPRINTF macro

+37 -44
+37 -44
qconsole.c
··· 51 51 52 52 #define BORDER 4 53 53 54 + #define WIN_NAME "qconsole" 55 + 54 56 struct xinfo { 55 57 Display* dpy; 56 58 Window win; ··· 61 63 int width, height; 62 64 int cur_direction; 63 65 } main_win; 64 - 65 - char *win_name = "qconsole"; 66 66 67 67 /* args to pass to xterm; requires a trailing blank -into option */ 68 68 static char *xterm_args[6] = { 69 69 "xterm", 70 - "-name", "qconsole", 70 + "-name", WIN_NAME, 71 71 "-into", "", 72 72 NULL 73 73 }; 74 74 75 75 static int debug = 0; 76 + #define DPRINTF(x) { if (debug) { printf x; } }; 77 + 76 78 static pid_t xterm_pid = 0; 77 79 static int shutting_down = 0; 78 80 static int respawning = 0; ··· 146 148 147 149 switch (event.type) { 148 150 case ReparentNotify: { 149 - if (debug) 150 - printf("xterm spawned and reparented to us\n"); 151 + DPRINTF(("xterm spawned and reparented to us\n")); 151 152 152 153 XReparentEvent *e = (XReparentEvent *) &event; 153 154 main_win.xterm = e->window; 154 155 155 156 /* move completely off-screen */ 156 - XMoveWindow(main_win.dpy, main_win.xterm, 157 - -1, -1); 157 + XMoveWindow(main_win.dpy, main_win.xterm, -1, -1); 158 158 XResizeWindow(main_win.dpy, main_win.xterm, 159 - main_win.width, 160 - main_win.height - BORDER); 159 + main_win.width, main_win.height - BORDER); 161 160 162 161 break; 163 162 } 164 163 165 164 case UnmapNotify: 166 - if (debug) 167 - printf("xterm unmapped, respawning\n"); 165 + DPRINTF(("xterm unmapped, respawning\n")); 168 166 169 167 xterm_spawn(); 170 168 ··· 183 181 } 184 182 } 185 183 186 - exit(1); 184 + return (0); 187 185 } 188 186 189 187 void ··· 192 190 int rc; 193 191 XSetWindowAttributes attributes; 194 192 XTextProperty win_name_prop; 193 + char *win_name = WIN_NAME; 195 194 196 195 if (!(main_win.dpy = XOpenDisplay(display))) 197 196 errx(1, "Unable to open display %s", XDisplayName(display)); ··· 200 199 201 200 main_win.screen = DefaultScreen(main_win.dpy); 202 201 main_win.width = main_win.dpy_width = DisplayWidth(main_win.dpy, 203 - main_win.screen); 202 + main_win.screen); 204 203 main_win.dpy_height = DisplayHeight(main_win.dpy, main_win.screen); 205 204 main_win.win = XCreateSimpleWindow(main_win.dpy, 206 - RootWindow(main_win.dpy, main_win.screen), 207 - 0, -(main_win.height), 208 - main_win.width, main_win.height, 209 - 0, 210 - BlackPixel(main_win.dpy, main_win.screen), 211 - BlackPixel(main_win.dpy, main_win.screen)); 205 + RootWindow(main_win.dpy, main_win.screen), 206 + 0, -(main_win.height), 207 + main_win.width, main_win.height, 208 + 0, 209 + BlackPixel(main_win.dpy, main_win.screen), 210 + BlackPixel(main_win.dpy, main_win.screen)); 212 211 213 212 if (!(rc = XStringListToTextProperty(&win_name, 1, &win_name_prop))) 214 213 errx(1, "XStringListToTextProperty"); ··· 217 216 218 217 /* remove all window manager decorations and force our position/size */ 219 218 attributes.override_redirect = True; 220 - XChangeWindowAttributes(main_win.dpy, main_win.win, 221 - CWOverrideRedirect, &attributes); 219 + XChangeWindowAttributes(main_win.dpy, main_win.win, CWOverrideRedirect, 220 + &attributes); 222 221 223 222 XMapRaised(main_win.dpy, main_win.win); 224 223 ··· 227 226 228 227 /* we need to know when the xterm gets reparented to us */ 229 228 XSelectInput(main_win.dpy, main_win.win, SubstructureNotifyMask | 230 - FocusChangeMask); 229 + FocusChangeMask); 231 230 232 231 /* bind to control+o */ 233 232 /* TODO: allow this key to be configurable */ 234 233 XGrabKey(main_win.dpy, XKeysymToKeycode(main_win.dpy, XK_o), 235 - ControlMask, DefaultRootWindow(main_win.dpy), False, 236 - GrabModeAsync, GrabModeAsync); 234 + ControlMask, DefaultRootWindow(main_win.dpy), False, GrabModeAsync, 235 + GrabModeAsync); 237 236 } 238 237 239 238 void ··· 245 244 246 245 if (direction == DIR_DOWN) { 247 246 XSetInputFocus(main_win.dpy, main_win.xterm, RevertToParent, 248 - CurrentTime); 247 + CurrentTime); 249 248 dest = 0; 250 249 } else { 251 250 XSetInputFocus(main_win.dpy, PointerRoot, RevertToParent, 252 - CurrentTime); 251 + CurrentTime); 253 252 dest = -(main_win.height); 254 253 } 255 254 256 - XGetGeometry(main_win.dpy, main_win.win, &root, 257 - &cur_x, &cur_y, &width, &height, &bw, &depth); 255 + XGetGeometry(main_win.dpy, main_win.win, &root, &cur_x, &cur_y, &width, 256 + &height, &bw, &depth); 258 257 259 - if (debug) 260 - printf("scrolling from %d to %d%s\n", cur_y, dest, 261 - (quick ? " quickly" : "")); 258 + DPRINTF(("scrolling from %d to %d%s\n", cur_y, dest, 259 + (quick ? " quickly" : ""))); 262 260 263 261 if (direction == DIR_DOWN) 264 262 XRaiseWindow(main_win.dpy, main_win.win); ··· 266 264 /* smoothly scroll to our destination */ 267 265 while (!quick && cur_y != dest) { 268 266 inc = (abs(dest - cur_y) / MAX_SPEED) / (MAX_SPEED + 1 - 269 - main_win.speed); 267 + main_win.speed); 270 268 271 269 if (inc < 1) 272 270 inc = 1; ··· 296 294 if (!xterm_pid) 297 295 return; 298 296 299 - if (debug && sig) 300 - printf("got SIGCHLD, cleaning up after xterm pid %d\n", 301 - xterm_pid); 297 + if (sig) 298 + DPRINTF(("got SIGCHLD, cleaning up after xterm pid %d\n", 299 + xterm_pid)); 302 300 303 301 waitpid(-1, NULL, WNOHANG); 304 302 xterm_pid = 0; ··· 313 311 * any x11 operations */ 314 312 315 313 if (shutting_down) { 316 - if (debug) 317 - printf("shutting down, not respawning\n"); 314 + DPRINTF(("shutting down, not respawning\n")); 318 315 319 316 return; 320 317 } 321 318 322 - if (debug) 323 - printf("in xterm_spawn\n"); 319 + DPRINTF(("in xterm_spawn\n")); 324 320 325 321 /* clean up if previous xterm died */ 326 322 if (xterm_pid) { ··· 331 327 } 332 328 333 329 if (!xterm_pid && !shutting_down) { 334 - if (debug) 335 - printf("forking new xterm into %d\n", 336 - (int)main_win.win); 330 + DPRINTF(("forking new xterm into %d\n", (int)main_win.win)); 337 331 338 332 switch (pid = fork()) { 339 333 case -1: ··· 358 352 { 359 353 shutting_down = 1; 360 354 361 - if (debug) 362 - printf("in exit_handler with sig %d, shutting down\n", sig); 355 + DPRINTF(("in exit_handler with sig %d, shutting down\n", sig)); 363 356 364 357 if (xterm_pid) 365 358 kill(xterm_pid, SIGKILL);