exitdwm.c (2535B)
1 #include <stdio.h> 2 #include <string.h> 3 4 void exitdwm() { 5 #if defined S_LOCK || defined S_RESTART_DWM || defined S_SLEEP || \ 6 defined S_RESTARTER_SCRIPT || defined S_EXIT || defined S_REBOOT || \ 7 defined S_SHUTDOWN || defined S_LOCK_ICON || defined S_RESTART_DWM_ICON || \ 8 defined S_SLEEP_ICON || defined S_EXIT_ICON || \ 9 defined S_RESTARTER_SCRIPT_ICON || defined S_REBOOT_ICON || \ 10 defined S_SHUTDOWN_ICON || defined S_FORMAT || defined S_FORMAT_CLEAR 11 #error(conflicting macro names) 12 #endif 13 14 #define S_LOCK "Lock" 15 #define S_RESTART_DWM "restart birdwm" 16 #define S_SLEEP "Off-screen" 17 #define S_RESTARTER_SCRIPT "Restarter Script" 18 #define S_EXIT "Exit" 19 #define S_REBOOT "Reboot" 20 #define S_SHUTDOWN "Shutdown" 21 #define S_LOCK_ICON "\uf023" // <= FontAwesome icons 22 #define S_RESTART_DWM_ICON "\uf01e" 23 #define S_SLEEP_ICON "\uf108" 24 #define S_EXIT_ICON "\uf2f5" 25 #define S_RESTARTER_SCRIPT_ICON "\uf0c9" 26 #define S_REBOOT_ICON "\uf021" 27 #define S_SHUTDOWN_ICON "\uf011" 28 29 #define S_FORMAT(ACTION) S_##ACTION##_ICON " " S_##ACTION 30 #define S_FORMAT_CLEAR "sed 's/^..//'" 31 32 FILE *exit_menu = popen( 33 "echo \"" S_FORMAT(LOCK) "\n" S_FORMAT(RESTART_DWM) "\n" S_FORMAT(SLEEP) "\n" S_FORMAT( 34 RESTARTER_SCRIPT) "\n" S_FORMAT(EXIT) "\n" S_FORMAT(REBOOT) "\n" S_FORMAT(SHUTDOWN) "\" | birdmenu -i -p exit: | " S_FORMAT_CLEAR, 35 "r"); 36 37 char exit_action[16]; 38 39 if (exit_menu == NULL || 40 fscanf(exit_menu, "%15[a-zA-Z -]", exit_action) == EOF) { 41 fputs("Error. Failure in exit_dwm.", stderr); 42 goto close_streams; 43 } 44 45 if (strcmp(exit_action, S_LOCK) == 0) 46 system("slock & sleep 5; xset dpms force off"); 47 else if (strcmp(exit_action, S_RESTART_DWM) == 0) 48 quit(&(const Arg){1}); 49 else if (strcmp(exit_action, S_SLEEP) == 0) 50 system("pkexec loginctl suspend"); 51 else if (strcmp(exit_action, S_EXIT) == 0) 52 quit(&(const Arg){0}); 53 else if (strcmp(exit_action, S_RESTARTER_SCRIPT) == 0) 54 system("restarter"); 55 else if (strcmp(exit_action, S_REBOOT) == 0) 56 system("pkexec reboot"); 57 else if (strcmp(exit_action, S_SHUTDOWN) == 0) 58 system("pkexec poweroff now"); 59 60 close_streams: 61 pclose(exit_menu); 62 63 #undef S_LOCK 64 #undef S_RESTART_DWM 65 #undef S_SLEEP 66 #undef S_EXIT 67 #undef S_RESTARTER_SCRIPT 68 #undef S_REBOOT 69 #undef S_SHUTDOWN 70 #undef S_LOCK_ICON 71 #undef S_RESTART_DWM_ICON 72 #undef S_SLEEP_ICON 73 #undef S_EXIT_ICON 74 #undef S_RESTARTER_SCRIPT_ICON 75 #undef S_REBOOT_ICON 76 #undef S_SHUTDOWN_ICON 77 #undef S_FORMAT 78 #undef S_FORMAT_CLEAR 79 }