Chinaunix首页 | 论坛 | 博客
  • 博客访问: 278034
  • 博文数量: 46
  • 博客积分: 2021
  • 博客等级: 大尉
  • 技术积分: 406
  • 用 户 组: 普通用户
  • 注册时间: 2006-08-03 13:00
文章分类

全部博文(46)

文章存档

2011年(1)

2010年(9)

2009年(2)

2007年(13)

2006年(21)

我的朋友

分类: LINUX

2006-09-06 13:42:55

Beep for Linux and Windows

For servers it is sometimes useful to produce a beep. You can do this in Linux with the echo command:

echo -e "\007" >/dev/tty10

And on Windows (type Ctrl-G for the ^G):

echo "^G"

But if you want to change the frequency and duration you need a small program.

For Linux

Note: you need write access to /dev/tty10 or you have to run this program as root. This could be a security hole. Search on , if you want a Linux-module with user access for the beep-function.

#include
#include
#include

int main(int argc, char *argv[])
{
    int fd = open("/dev/tty10", O_RDONLY);
    if (fd == -1 || argc != 3) return -1;
    return ioctl(fd, KDMKTONE, (atoi(argv[2])<<16)+(1193180/atoi(argv[1])));
}

Copy this to a file beep.c and compile it like this:

cc beep.c -o beep
strip beep

(you can rename it from beep.bin to beep after downloading)

For Windows

#define WIN32_LEAN_AND_MEAN
#include
#include
#include

int APIENTRY _tWinMain(HINSTANCE hInstance,
    HINSTANCE hPrevInstance,
    LPTSTR lpCmdLine,
    int nCmdShow)
{
    char* ms = strchr(lpCmdLine, ' ');
    if (!ms) return 1;
    ms++;
    if (!strlen(ms)) return 1;
    Beep(atoi(lpCmdLine), atoi(ms));
    return 0;
}

For compiling with Visual Studio .NET copy this to a file beep.c, create an empty Win32 console project and add the beep.c-file to it.

Calling from Java

public class Beeper {
    public static void main(String args[]) {
        try {
            Runtime.getRuntime().exec("beep.exe 440 1000");
        } catch (Exception e) {}
    }
}


阅读(1058) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~