Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement -U. #2049

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions doc/man.md.j2
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,10 @@ file.

: Update interval.

**-U \| \--unique**

: Conky won't start if another Conky process is already running.

**-v \| -V \| \--version**

: Prints version, build information and general info. Exits after
Expand Down
5 changes: 3 additions & 2 deletions src/conky.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2106,7 +2106,7 @@ void set_current_config() {

/* : means that character before that takes an argument */
const char *getopt_string =
"vVqdDSs:t:u:i:hc:p:"
"vVqdDSs:t:u:i:hc:p:U"
#ifdef BUILD_X11
"x:y:w:a:X:m:f:"
#ifdef OWN_WINDOW
Expand Down Expand Up @@ -2136,7 +2136,8 @@ const struct option longopts[] = {
{"double-buffer", 0, nullptr, 'b'}, {"window-id", 1, nullptr, 'w'},
#endif /* BUILD_X11 */
{"text", 1, nullptr, 't'}, {"interval", 1, nullptr, 'u'},
{"pause", 1, nullptr, 'p'}, {nullptr, 0, nullptr, 0}};
{"pause", 1, nullptr, 'p'}, {"unique", 0, nullptr, 'U'},
{nullptr, 0, nullptr, 0}};

void setup_inotify() {
#ifdef HAVE_SYS_INOTIFY_H
Expand Down
34 changes: 33 additions & 1 deletion src/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -268,10 +268,32 @@ static void print_help(const char *prog_name) {
" -i COUNT number of times to update " PACKAGE_NAME
" (and quit)\n"
" -p, --pause=SECS pause for SECS seconds at startup "
"before doing anything\n",
"before doing anything\n"
" -U, --unique only one conky process can be created\n",
prog_name);
}

static bool is_conky_already_running() {
char line[512];
int instances = 0;

FILE *fp = popen("ps xo comm", "r");
if (!fp) {
NORM_ERR("unable to open ps");
return false;
}

while (fgets(line, sizeof(line), fp)) {
if (!strncmp(line, "conky\n", 6)) {
instances++;
}
}

pclose(fp);

return instances > 1;
}

inline void reset_optind() {
#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || \
defined(__OpenBSD__) || defined(__NetBSD__) || defined(__DragonFly__)
Expand All @@ -293,6 +315,8 @@ int main(int argc, char **argv) {
g_sighup_pending = 0;
g_sigusr2_pending = 0;

bool unique_process = false;

#ifdef BUILD_CURL
struct curl_global_initializer {
curl_global_initializer() {
Expand Down Expand Up @@ -349,12 +373,20 @@ int main(int argc, char **argv) {
window.window = strtol(optarg, nullptr, 0);
break;
#endif /* BUILD_X11 */
case 'U':
unique_process = true;
break;

case '?':
return EXIT_FAILURE;
}
}

if (unique_process && is_conky_already_running()) {
NORM_ERR("already running");
return 0;
}

try {
set_current_config();

Expand Down
Loading