dockapps/wmArchUp/wmarchup.c

244 lines
5.6 KiB
C
Raw Normal View History

2018-04-02 11:34:00 +00:00
/* Also includes Xlib, Xresources, XPM, stdlib and stdio */
2018-03-29 13:22:34 +00:00
#include <dockapp.h>
#include <stdio.h>
2018-04-05 09:46:04 +00:00
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/wait.h>
2018-03-29 13:22:34 +00:00
2018-04-02 11:34:00 +00:00
/* Include the pixmap to use */
2018-03-29 13:22:34 +00:00
#include "archlinux.xpm"
#include "archlinux_bw.xpm"
2018-04-02 11:34:00 +00:00
#include "loading.xpm"
2018-03-29 13:22:34 +00:00
2018-04-02 11:34:00 +00:00
#define TRUE 1
#define FALSE 0
#define MAX 256
2018-04-05 09:46:04 +00:00
#define MAXPATHLEN 4096
2018-04-02 11:34:00 +00:00
#define CHK_INTERVAL 600
2018-04-05 09:46:04 +00:00
#define WMARCHUP_VER "1.2"
2018-04-02 11:34:00 +00:00
#define VERSION "wmArchUp version " WMARCHUP_VER
2018-03-29 13:22:34 +00:00
/*
* Prototypes for local functions
*/
void destroy(void);
2018-04-02 11:34:00 +00:00
void check_for_updates();
void buttonrelease(int button, int state, int x, int y);
void update();
2018-04-05 09:46:04 +00:00
char *get_update_script();
2018-03-29 13:22:34 +00:00
/*
* Global variables
*/
2018-04-02 11:34:00 +00:00
Pixmap arch, arch_mask, arch_bw, arch_bw_mask, checking, checking_mask;
unsigned short height, width;
unsigned int check_interval = CHK_INTERVAL;
int updates_available = FALSE;
2018-04-05 09:46:04 +00:00
char *script;
2018-03-29 13:22:34 +00:00
/*
* M A I N
*/
int
main(int argc, char **argv)
{
2018-04-05 09:46:04 +00:00
/* Find bash update script */
script = get_update_script();
2018-04-02 11:34:00 +00:00
/* Set callbacks for events */
2018-03-29 13:22:34 +00:00
DACallbacks eventCallbacks = {
2018-04-02 11:34:00 +00:00
destroy, /* destroy */
NULL, /* buttonPress */
&buttonrelease, /* buttonRelease */
NULL, /* motion (mouse) */
NULL, /* mouse enters window */
NULL, /* mouse leaves window */
check_for_updates /* timeout */
};
int interval = 0;
/* Set program options */
DAProgramOption options[] = {
{
"-c",
"--check-interval",
"Check interval in minutes. Default 10 minutes.",
DONatural,
False,
{&interval}
},
2018-03-29 13:22:34 +00:00
};
/* provide standard command-line options */
DAParseArguments(
2018-04-02 11:34:00 +00:00
argc, argv, /* Where the options come from */
options, 1, /* Our list with options */
2018-03-29 13:22:34 +00:00
"This is dockapp watch for available updates "
"in Arch Linux packages.\n",
2018-04-02 11:34:00 +00:00
VERSION);
2018-03-29 13:22:34 +00:00
2018-04-02 11:34:00 +00:00
/* Set the check interval */
check_interval = interval ? (interval * 60) : CHK_INTERVAL;
/* Check if correct libdockapp version exists in system */
2018-03-29 13:22:34 +00:00
DASetExpectedVersion(20050716);
DAInitialize("", "WMArchUp", 56, 56, argc, argv);
DAMakePixmapFromData(
archlinux,
&arch,
&arch_mask,
&height,
&width);
DAMakePixmapFromData(
archlinux_bw,
&arch_bw,
&arch_bw_mask,
&height,
&width);
2018-04-02 11:34:00 +00:00
DAMakePixmapFromData(
loading,
&checking,
&checking_mask,
&height,
&width);
2018-03-29 13:22:34 +00:00
/* Respond to destroy and timeout events (the ones not NULL in the
* eventCallbacks variable.
*/
DASetCallbacks(&eventCallbacks);
2018-04-02 11:34:00 +00:00
/* Set timeout in msec */
DASetTimeout(check_interval * 1000);
/* Show the dockapp window. */
DAShow();
/* Check for updates at the begining */
check_for_updates();
/* Loop for events */
DAEventLoop();
/* not reached */
exit(EXIT_SUCCESS);
}
void
update()
{
if (updates_available == TRUE) {
XSelectInput(DAGetDisplay(NULL), DAGetWindow(), NoEventMask);
2018-04-05 09:46:04 +00:00
int ret = system(script);
2018-03-29 13:22:34 +00:00
2018-04-02 11:34:00 +00:00
if (WEXITSTATUS(ret) == 0) {
updates_available = FALSE;
2018-03-29 13:22:34 +00:00
DASetShape(arch_bw_mask);
DASetPixmap(arch_bw);
}
2018-04-02 11:34:00 +00:00
XSelectInput(DAGetDisplay(NULL), DAGetWindow(),
ButtonPressMask | ButtonReleaseMask);
2018-03-29 13:22:34 +00:00
}
}
2018-04-02 11:34:00 +00:00
void
check_for_updates()
{
XSelectInput(DAGetDisplay(NULL), DAGetWindow(), NoEventMask);
2018-03-29 13:22:34 +00:00
char res[MAX];
2018-04-02 11:34:00 +00:00
DASetShape(checking_mask);
DASetPixmap(checking);
2018-03-29 13:22:34 +00:00
/* Read output from command */
2018-04-05 09:46:04 +00:00
FILE *fp = popen("checkupdates", "r");
2018-04-02 11:34:00 +00:00
if (fgets(res, MAX, fp) != NULL) {
updates_available = TRUE;
DASetShape(arch_mask);
DASetPixmap(arch);
2018-03-29 13:22:34 +00:00
} else {
2018-04-02 11:34:00 +00:00
updates_available = FALSE;
DASetShape(arch_bw_mask);
DASetPixmap(arch_bw);
2018-03-29 13:22:34 +00:00
}
2018-04-02 11:34:00 +00:00
2018-04-05 09:46:04 +00:00
if (pclose(fp) != 0) {
fprintf(stderr, " Error: Failed to close command stream \n");
}
2018-04-02 11:34:00 +00:00
XSelectInput(DAGetDisplay(NULL), DAGetWindow(),
ButtonPressMask | ButtonReleaseMask);
2018-03-29 13:22:34 +00:00
}
void
destroy(void)
{
return;
}
2018-04-02 11:34:00 +00:00
/* A mouse button was pressed and released.
* See if it was button 1 left click or button 3 right click. */
void
buttonrelease(int button, int state, int x, int y)
{
if (button == 1) {
update();
} else if (button == 3) {
check_for_updates();
}
}
2018-04-05 09:46:04 +00:00
char *
get_update_script()
{
int length;
char *p;
char *script_name = "arch_update.sh";
char *fullpath = malloc(MAXPATHLEN + strlen(script_name));
if (fullpath == NULL) {
perror("Can't allocate memory.");
exit(1);
}
/*
* /proc/self is a symbolic link to the process-ID subdir of /proc, e.g.
* /proc/4323 when the pid of the process of this program is 4323.
* Inside /proc/<pid> there is a symbolic link to the executable that is
* running as this <pid>. This symbolic link is called "exe". So if we
* read the path where the symlink /proc/self/exe points to we have the
* full path of the executable.
*/
length = readlink("/proc/self/exe", fullpath, MAXPATHLEN);
/*
* Catch some errors:
*/
if (length < 0) {
perror("resolving symlink /proc/self/exe.");
exit(1);
}
if (length >= MAXPATHLEN) {
fprintf(stderr, "Path too long.\n");
exit(1);
}
fullpath[length] = '\0';
if ((p = strrchr(fullpath, '/'))) {
*(p + 1) = '\0';
}
strcat(fullpath, script_name);
return fullpath;
}