linux多进程和libnotify桌面通知

Comments(48)


Posted on 2012-12-27 15:15:04 os


OS: ubuntu 12.04 x86_64

GCC: gcc 版本 4.6.3 (Ubuntu/Linaro 4.6.3-1ubuntu5)

#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <libnotify/notify.h>

void usage(char *);
int close_std();

char * const short_options = "hf:b:";

struct option long_options[] =
{
    {"help", 2, NULL, 'h'},
    {"file", 2, NULL, 'f'},
    {"body", 2, NULL, 'b'},
    {NULL, 0, NULL, 0},
};

char * player = "/usr/bin/mplayer";
char * music_file = "/usr/share/sounds/freedesktop/stereo/alarm-clock-elapsed.oga";
char * notify_body = "You have a new mail!";

void usage(char * program_name)
{
    printf("%s -h [--help]\n"
           "-f [--file=filename] path to music file.\n"
           "-b [--body=content] the body of notify dialog.\n", program_name);
    exit(1);
}

int close_std()
{
    if(close(STDIN_FILENO) == -1 || close(STDOUT_FILENO) ==-1 || close(STDERR_FILENO) == -1)
    {
        return -1;
    }

    return 0;
}

int main(int argc, char **argv)
{

    int c, pid;
    char * program_name = argv[0];

    while((c = getopt_long(argc, argv, short_options, long_options, NULL)) != -1)
    {
        switch(c)
        {
            case 'h':
                usage(program_name);

            case 'f':
                music_file = optarg;
                break;

            case 'b':
                notify_body = optarg;
                break;

            case '?':
                usage(program_name);

            default:
                usage(program_name);
        }
    }

    if((close_std() == -1))
    {
        perror("close_std()");
        exit(1);
    }

    //player music
    if((pid = fork()) < 0)
    {
        perror("fork()");
        exit(1);
    }
    else if(pid == 0)
    {
        if(execlp(player, player, music_file, (char *)0) < 0)
        {
            perror("execlp()");
            exit(1);
        }

        exit(0);
    }

    //send notify information to user
    if((pid = fork()) < 0)
    {
        perror("fork()");
    }
    else if(pid == 0)
    {
        notify_init("Mail");
        NotifyNotification * notify = notify_notification_new("Mail", notify_body, "Mail");
        notify_notification_show(notify, NULL);

        exit(0);
    }

    exit(0);
}

Makefile:

SRC_INCLUDE=-I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include -I/usr/include/gdk-pixbuf-2.0

CC=gcc
MOD_CFLAGS=-fPIC
CFLAGS=-g -O2 -DHAVE_CONFIG_H -DNSCORE
LDFLAGS=
LIBS=-lnotify 
DBG_FLAGS= -DDEBUG=1

OUT_PROGRAM= newmail_notify


all: main

main:
    $(CC) $(MOD_CFLAGS) $(CFLAGS) $(SRC_INCLUDE) -o $(OUT_PROGRAM) newmail_notify.c $(MOD_LDFLAGS) $(LDFLAGS) $(LIBS)
前一篇: 在tornado的异步http请求中托管一个阻塞的任务到线程池 后一篇: tornado长连接断开的处理机制

Captcha:
验证码

Email:

Content: (Support Markdown Syntax)


Tukhkz  2022-09-03 13:45:37 From 127.0.0.1

purchase imuran pill buy micardis 20mg pills buy micardis 80mg sale


Cniwsh  2022-09-11 00:39:58 From 127.0.0.1

order doxycycline 100mg online cheap buy doxycycline 100mg generic ivermectin 6 tablet


Otmpuc  2022-09-21 11:15:34 From 127.0.0.1

buy lipitor 20mg sale order atenolol without prescription generic pregabalin 150mg


Qgflwq  2022-10-01 04:03:46 From 127.0.0.1

zyban uk buy zyban 150mg online viagra 150mg canada


Kcxifr  2022-11-01 10:08:51 From 127.0.0.1

tricor 160mg ca buy generic tricor buy generic uroxatral 10 mg


zocor pharmacy  2022-11-13 21:46:56 From 127.0.0.1

nortriptyline 25 mg pills https://nortriptylinen.com/# - where to buy nortriptyline nortriptyline online


Hqudan  2022-12-04 13:44:16 From 127.0.0.1

order aldactone pill buy propecia for sale fluconazole 200mg cheap


Aqmhse  2022-12-12 02:45:56 From 127.0.0.1

indomethacin for sale trimox over the counter order trimox without prescription


Nedgqu  2023-01-03 14:55:19 From 127.0.0.1

order glycomet 500mg pill buy tadalafil 20mg brand name cialis


Uszqga  2023-01-23 05:15:52 From 127.0.0.1

order simvastatin 10mg online cheap purchase zocor for sale buy propecia 5mg generic