Ubuntu下配置FLTK的一点经验及使用FLTK编写休息提醒软件
Ubuntu下配置FLTK的一點經驗及使用FLTK編寫休息提醒軟件.
Ubuntu下的FLTK配置對于經常使用QT等工具進行開發的同學來說不算很難,但對于Ubuntu不是很了解的同學來講就可能會比較困難。本文的作者也曾經幻想能像“大神”一樣只使用VIM和終端,再加上復雜的命令工作來完成編譯工作,以為這便是LInux/Ubuntu 使用C++編程的精髓,在4天時間分數次測試失敗后,找到了非常簡單的解決辦法,那就是CodeBlocks。
1.安裝CodeBlocks
sudo apt-get install codeblocks
2.安裝相關組件,這個很關鍵,也是是否能使用CodeBlocks進行FLTK編程的關鍵,作者在墻翻后找到的命名,幫助你減少出錯,直接使用。
使用CodeBlocks能減少大量重復命令的使用,對于使用FLTK的新手來說,信心很重要。請使用終端編程的大神繞行。。。。。。
-------------------------------------------------------------------------------------------------------------------
本文的作者認為使用Ubuntu(或者其他Linux發行版)的字體來閱讀大量文件資料要比在Windows下閱讀要好的多,因為字體,,,你懂的。在寫此文時本人的視力下降的很厲害,故希望在繁重的閱讀和編程過程中有軟件能提醒自己休息,于是編寫的該應用程序。
小軟件在點擊之后會出現“卡死”的效果,把它最小化,不用理會。
在輸入框輸入你想提醒休息的時間間隔,在時間結束后會彈出對話框并強制鎖屏(請原諒我為眼鏡好的“強制鎖屏”,愛你就是愛自己)。
休息時間間隔最多不超過60min,大于60min會默認60min提醒一次,覺得不爽可以在源碼中修改。
好了,放碼過來。
/*This is the collection of head file*/ #include <FL/Fl.H> #include <FL/Fl_Window.H> #include <FL/Fl_Button.H> #include <FL/Fl_Clock.H> #include <Fl/fl_ask.h> #include <Fl/Fl_Dial.H> #include <Fl/Fl_Timer.H> #include <Fl/Fl_Int_Input.h>/*Using C++ lib */ #include <iostream> #include <ctime> #include <stdlib.h>/*This is used to create guard process*/ #include <unistd.h> #include <signal.h> #include <sys/param.h> #include <sys/types.h> #include <sys/stat.h> #include <time.h>static unsigned int sec_counter = 0;/*Main.cpp*/
#include "headfile.h"using namespace std;void true_sleep(int sec); void init_deamon(void);void start_cb(Fl_Widget *w,void *v) {Fl_Int_Input *i = (Fl_Int_Input *)v;cout<<"Remind time is "<<atoi(i ->value())<<endl;if(atoi(i ->value()) >60){cout<<"Rest "<<atoi(i ->value()) <<" Min is too long "<<endl;cout<<atoi(i ->value())<<endl;sec_counter = 60;}sec_counter = atoi(i ->value())*60 ;fl_alert("Counting Begin !!!");//sleep(sec_counter);true_sleep(sec_counter);//gnome-screensaver-command -l suoping hanshufl_message("It's Time to Rest !!!");system("gnome-screensaver-command -l suoping hanshu");sec_counter = 0; }void stop_cb(Fl_Widget *w,void *v) {sec_counter = 0;system("gnome-screensaver-command -l suoping hanshu"); }void end_cb(Fl_Widget *w,void *v) {int ans = fl_ask("Are You Going to Qiut This?");if(ans)exit(0); }int main (int argc, char ** argv) {//init_deamon();Fl_Window *window;window = new Fl_Window (100,100,420, 300,"Reminding Windows");Fl_Clock *clock = new Fl_Clock(15,15,200,220,"Counter");Fl_Int_Input *int_input = new Fl_Int_Input(250,10,100,40,"Min");int_input ->value("45");Fl_Button *start = new Fl_Button(250,50,120,55,"Start");start ->callback(start_cb,int_input);Fl_Button *stop = new Fl_Button(250,130,120,55,"RestNow");stop ->callback(stop_cb,0);Fl_Button *endb = new Fl_Button(250,210,120,55,"end");endb -> callback(end_cb,0);window->end ();window->show (argc, argv);return Fl::run(); }void init_deamon(void) {int pid;int i;/*處理Sigchld信號*/if(signal(SIGCHLD,SIG_IGN) == SIG_ERR){printf("Can't signal in init_daemon\n");exit(1);}pid = fork(); //創建進程if(pid){printf("It's a father process\n");//父進程exit(0);}else if(pid ==0){printf("It's a first child process.Normal\n");}else if(pid < 0){perror("Failed to create process\n");exit(1); //創建失敗}/*得到第一子進程,后臺繼續執行*/setsid(); //第一子今晨成為新的繪畫組長和進程組長//并與控制終端分離if(pid = fork())exit(0); //terminal first child processelse if(pid < 0)exit(1);//The second process will not be gipfor(i = 0;i < getdtablesize();i++)close(i); //Close the open-file descriptorchdir("/tmp"); //change work catalogueumask(0);// Do Nothing ?return ; }void true_sleep(int sec) {struct timespec ts_start;struct timespec ts_end;clock_gettime(CLOCK_MONOTONIC, &ts_start);ts_end = ts_start;ts_end.tv_sec += sec;for(;;) {struct timespec ts_current;struct timespec ts_remaining;clock_gettime(CLOCK_MONOTONIC, &ts_current);ts_remaining.tv_sec = ts_end.tv_sec - ts_current.tv_sec;ts_remaining.tv_nsec = ts_end.tv_nsec - ts_current.tv_nsec;while (ts_remaining.tv_nsec > 1000000000) {ts_remaining.tv_sec++;ts_remaining.tv_nsec -= 1000000000;}while (ts_remaining.tv_nsec < 0) {ts_remaining.tv_sec--;ts_remaining.tv_nsec += 1000000000;}if (ts_remaining.tv_sec < 0) {break;}if (ts_remaining.tv_sec > 0) {sleep(ts_remaining.tv_sec);} else {usleep(ts_remaining.tv_nsec / 1000);}} }你會發現我寫了個守護進程,但是沒有用上。因為在運行中該程序是關不掉的,所以寫不寫守護進程沒有區別。用進程將計時和主程序分開,“卡死”的問題就會得到解決,但能力有限,歡迎修改。謝謝批評與指正,共同提高。
總結
以上是生活随笔為你收集整理的Ubuntu下配置FLTK的一点经验及使用FLTK编写休息提醒软件的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 新能源行业SCM供应链管理平台构建一站式
- 下一篇: Ubuntu18.04安装Oracle1