/*! @brief Executed automaticaly at exit * * This function is executed whenever the program finished, so * its good to do some finalization code here. */ void f_atexit(void) { close_network(); fflush(stdout); write_log_fmt("Closing app\n"); close_log(); }
void end_session(void) { char user[MAX_USER_LENGTH]; pid_t pid; pid = get_user(user); write_log_fmt("Try to end session for user %s (PID %d).\n", user, pid); if (pid > 0) kill(pid, SIGTERM); }
int unlockScreen(void) { if (dsp == NULL) return -1; if (is_blocked()) { write_log_fmt("Unlocking Screen\n"); XDestroyWindow(dsp, wnd1); XCloseDisplay(dsp); set_blocked(0); } return (0); }
int lockScreen(void) { XSetWindowAttributes attr, attr1, attr2; Pixmap shape, pic, lock, shp, bg_pic, bg_shp; int screen_number = 0; long win_mask = CWBackPixel | CWBorderPixel | CWOverrideRedirect; if (is_blocked()) return 0; set_blocked(1); x = 5; y = 35; (dsp) = XOpenDisplay(":0.0"); if ((dsp) == NULL) { write_log("Could not open Display 0:0\n"); return (-1); } dW = XDisplayWidth(dsp, screen_number); dH = XDisplayHeight(dsp, screen_number); root = XRootWindow(dsp, screen_number); /* If a extern XPM background file specified on the command line */ if (background_filename != NULL) { if (XpmReadFileToPixmap (dsp, root, background_filename, &bg_pic, &bg_shp, NULL) != XpmOpenFailed) win_mask = CWBackPixmap | CWBorderPixel | CWOverrideRedirect; } attr.background_pixel = 700; attr.background_pixmap = bg_pic; attr.border_pixel = 100; attr.override_redirect = True; (wnd1) = XCreateWindow(dsp, root, 0, 0, dW, dH, 0, CopyFromParent, CopyFromParent, CopyFromParent, win_mask, &attr); XMapRaised(dsp, wnd1); if (logo_filename != NULL) { XpmReadFileToPixmap(dsp, root, logo_filename, &pic, &shape, NULL); } attr1.background_pixmap = pic; logo = XCreateWindow(dsp, wnd1, dW - 150, 0, 150, 30, 0, CopyFromParent, CopyFromParent, CopyFromParent, CWBackPixmap, &attr1); XMapWindow(dsp, logo); logo2 = XCreateSimpleWindow(dsp, wnd1, 0, 0, dW - 150, 30, 0, 0, 300); XMapWindow(dsp, logo2); if (foreground_filename != NULL) { XpmReadFileToPixmap(dsp, root, foreground_filename, &lock, &shp, NULL); } attr2.background_pixmap = lock; wnd = XCreateWindow(dsp, wnd1, dW / 2 - 150, dH / 2 - 100, 300, 200, 0, CopyFromParent, CopyFromParent, CopyFromParent, CWBackPixmap, &attr2); //wnd = XCreateSimpleWindow(dsp, wnd1, x, y, 200, 200, 1, 0, 400); XMapWindow(dsp, wnd); XGrabPointer(dsp, wnd1, False, ButtonPressMask | ButtonReleaseMask | PointerMotionMask, GrabModeAsync, GrabModeAsync, None, None, CurrentTime); XGrabKeyboard(dsp, wnd1, False, GrabModeAsync, GrabModeAsync, CurrentTime); XSelectInput(dsp, wnd1, KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask | PointerMotionMask | ShiftMask | LockMask | ControlMask | Mod1Mask | Mod2Mask | Mod3Mask | Mod4Mask | Mod5Mask); write_log_fmt("locking screen\n"); return (0); }
int main(int argc, char *argv[]) { /* getopt */ int next_option; int ret; int will_block = 0; /*! short options. See man getopt */ const char *const short_options = "hc:l:L:b:f:xp:s:"; /*! the array for the long options. */ const struct option long_options[] = { {"help", 0, NULL, 'h'}, {"config", 1, NULL, 'c'}, {"logfile", 1, NULL, 'L'}, {"background", 1, NULL, 'b'}, {"foreground", 1, NULL, 'f'}, {"logo", 1, NULL, 'l'}, {"blocked", 0, NULL, 'x'}, {"port", 1, NULL, 'p'}, {"server", 1, NULL, 's'}, {NULL, 0, NULL, 0} }; /* Initializing exit function */ if (atexit(f_atexit) != 0) { fprintf(stderr, "failed to register f_atexit() exit function\n"); exit(EXIT_FAILURE); } if (signal(SIGINT, sig_ctrlc) == SIG_ERR) { fprintf(stderr, "Warning - Could not set CTRL-C handler\n"); } /* First, set defaults */ set_listen_port(PORT); set_foreground(FOREGROUND_IMAGE); set_logo(LOGO_IMAGE); /* Second, read entries from default config file. * They will eventually be replaced by command line settings. */ read_config_file(CONFIGFILE); /* Then, overwrite with command line options... */ do { next_option = getopt_long(argc, argv, short_options, long_options, NULL); switch (next_option) { case 'h': /* -h or --help */ print_usage(); exit(1); case 'c':/* -c or --config */ /* Things like "mdclient -p 7000 -c /etc/mymedusa.conf" * when port is set to a different value in /etc/mymedusa.conf * may result in an unpredictable value for listen_port. Just don't do that. */ read_config_file(strdup(optarg)); break; case 'p': /* -p or --port */ set_listen_port(strdup(optarg)); break; case 'x': /* -x or --blocked */ will_block = 1; break; case 'b': /* -b or --background */ set_background(strdup(optarg)); break; case 'l': /* -l or --logo */ set_logo(strdup(optarg)); break; case 'f': /* -f or --foreground */ set_foreground(strdup(optarg)); break; case 'L': /* -L or --logfile */ close_log(); open_log(strdup(optarg)); break; case 's': /* -s or --server */ ret = set_server(strdup(optarg)); if (ret) { /* error */ fflush(stderr); fflush(stdout); fprintf(stderr, "Server IP unknown.\n"); exit(1); } break; case '?':/* The user specified an invalid option */ /* Write usage info on the screen */ print_usage(); exit(1); case -1: /* No more options */ break; default: /* Garbage */ abort(); } } while (next_option != -1); if (will_block) { if (lockScreen() < 0) { write_log_fmt("Could not open display.\n"); exit(EXIT_FAILURE); } } write_log_fmt("Starting...\n"); listen_network(); close_log(); return 0; }
/*! @brief Handles Control-C * * This function is executed when the SIGINT signal is sent to the program */ void sig_ctrlc(void) { write_log_fmt("<< CTRL-C >> pressed. Exiting...\n"); f_atexit(); exit(0); }