linux编程多进程和libnotify桌面通知

 

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)

 

 

 

 

转载于:https://www.cnblogs.com/huazi/archive/2012/12/27/2835758.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值