반응형

리눅스(우분투)에서 GTK+3를 이용해 GUI 프로그래밍을 해 보자.

 

GTK+3를 설치한다.

 

단순한 window를 만드는 예제 소스를 입력한다.

 

컴파일(빌드)하고 실행하면 창이 나타나다. 단순한 창이지만 이동, 종료등의 모든 기본적인 기능을 갖고 있다.

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include <gtk/gtk.h>
 
/* This is a callback function. The data arguments are ignored
 * in this example. More on callbacks below. */
static void hello( GtkWidget *widget,
                   gpointer   data )
{
    g_print ("Hello World\n");
}
 
static gboolean delete_event( GtkWidget *widget,
                              GdkEvent  *event,
                              gpointer   data )
{
    /* If you return FALSE in the "delete-event" signal handler,
     * GTK will emit the "destroy" signal. Returning TRUE means
     * you don't want the window to be destroyed.
     * This is useful for popping up 'are you sure you want to quit?'
     * type dialogs. */
 
    g_print ("delete event occurred\n");
 
    /* Change TRUE to FALSE and the main window will be destroyed with
     * a "delete-event". */
 
    return TRUE;
}
 
/* Another callback */
static void destroy( GtkWidget *widget,
                     gpointer   data )
{
    gtk_main_quit ();
}
 
int main( int   argc,
          char *argv[] )
{
    /* GtkWidget is the storage type for widgets */
    GtkWidget *window;
    GtkWidget *button;
    
    /* This is called in all GTK applications. Arguments are parsed
     * from the command line and are returned to the application. */
    gtk_init (&argc, &argv);
    
    /* create a new window */
    window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
    
    /* When the window is given the "delete-event" signal (this is given
     * by the window manager, usually by the "close" option, or on the
     * titlebar), we ask it to call the delete_event () function
     * as defined above. The data passed to the callback
     * function is NULL and is ignored in the callback function. */
    g_signal_connect (window, "delete-event",
              G_CALLBACK (delete_event), NULL);
    
    /* Here we connect the "destroy" event to a signal handler.  
     * This event occurs when we call gtk_widget_destroy() on the window,
     * or if we return FALSE in the "delete-event" callback. */
    g_signal_connect (window, "destroy",
              G_CALLBACK (destroy), NULL);
    
    /* Sets the border width of the window. */
    gtk_container_set_border_width (GTK_CONTAINER (window), 10);
    
    /* Creates a new button with the label "Hello World". */
    button = gtk_button_new_with_label ("Hello World");
    
    /* When the button receives the "clicked" signal, it will call the
     * function hello() passing it NULL as its argument.  The hello()
     * function is defined above. */
    g_signal_connect (button, "clicked",
              G_CALLBACK (hello), NULL);
    
    /* This will cause the window to be destroyed by calling
     * gtk_widget_destroy(window) when "clicked".  Again, the destroy
     * signal could come from here, or the window manager. */
    g_signal_connect_swapped (button, "clicked",
                  G_CALLBACK (gtk_widget_destroy),
                              window);
    
    /* This packs the button into the window (a gtk container). */
    gtk_container_add (GTK_CONTAINER (window), button);
    
    /* The final step is to display this newly created widget. */
    gtk_widget_show (button);
    
    /* and the window */
    gtk_widget_show (window);
    
    /* All GTK applications must have a gtk_main(). Control ends here
     * and waits for an event to occur (like a key press or
     * mouse event). */
    gtk_main ();
    
    return 0;
}
 

 

GTK+2 스타일의 예제 소스를 입력해 보자.

 

GTK+2 스타일의 소스를 빌드해도 문제 없다. Hello World 버튼을 클릭한다.

 

터미널 출력까지 잘 된다.

 

반응형
Posted by J-sean
:
반응형

2021/02/07 - [Linux] - Linux make를 이용한 컴파일(빌드) 자동화

 

리눅스에서 소스 코드를 컴파일(빌드)할때 유용한 CMake의 간단한 사용법을 알아 보자.

 

첫 번째 헤더 파일을 만든다. (a.h)

 

헤더 파일의 소스 파일을 만든다. (a.cpp)

 

두 번째 헤더 파일을 만든다. (b.h)

 

헤더 파일의 소스 파일을 만든다. (b.cpp)

 

 

두 헤더 파일과 소스 파일을 사용하는 main() 함수가 포함된 소스 파일을 작성한다.

 

CMakeLists.txt 파일을 만든다.

 

헤더/소스 파일이 있는 디렉토리에 하위 디렉토리 build를 만들고 이동후 cmake 명령을 실행한다. (CMakeLists.txt 파일이 상위 디렉토리에 있으므로 cmake ../ 를 실행해야 한다)

 

Makefile이 생성 되면 make 명령을 실행한다. 빌드가 완료되고 실행파일(main)이 생성된다.

 

반응형
Posted by J-sean
:
반응형

최신 버전의 OpenCV를 리눅스나 라즈베리 파이에 설치하기 위해서는 길고 지루한 과정을 거쳐야 한다. 꼭 최신 버전의 OpenCV가 필요한게 아니라면 간단하게 설치하고 사용해 보자.

 

libopencv-dev를 설치한다.

 

3.2.0 버전의 OpenCV가 설치 되었다.

 

텍스트 편집기에서 간단한 소스를 입력하고 저장한다.

 

컴파일 한다. pkg-config 명령어를 감싸고 있는 ` 기호는 Tab 키 위에 있는 backtick이다.

 

 

컴파일된 파일을 실행하면 라즈베리 파이에 연결된 카메라가 작동되고 sample.jpg가 생성된다.

 

sample.jpg

 

라즈베리 파이가 아닌 PC(우분투)에서 위 내용을 진행하면 opencv2/opencv.hpp가 없다는 에러가 발생하게 된다.

 

설치한 libopencv-dev의 버전이 다르기 때문인데 PC(우분투)에서는 라즈베리 파이보다 높은 버전의(4.2.0) opencv가 설치된다.

 

 

pkg-config --list-all 명령으로 확인된 opencv4 옵션을 사용해 컴파일 한다.

 

반응형
Posted by J-sean
:
반응형

리눅스(우분투)에 OpenCV-Python을 설치하자.


opencv-python을 설치하면 numpy도 함께 설치된다.


간단한 opencv 예제를 입력하고 파이썬 파일로 저장한다.


예제를 실행하면 이미지가 출력된다.


GUI가 없는 리눅스 서버에서는 실행되지 않는다. 이미지를 디스플레이 하는 코드가 포함되었기 때문인데 이미지 읽기, 저장, 연산등의 작업만 하는 프로그램은 문제 없이 실행된다.


반응형
Posted by J-sean
:
반응형

리눅스(우분투) 서버 같은 콘솔 환경에서도 비디오 플레이어를 사용할 수 있다.


mpv를 설치한다.


--vo=drm 옵션, 영상 파일 이름과 함께 실행한다.


자막 파일이 있다면 자막도 표시된다. 주요 단축키 목록은 아래와 같다.


  • LEFT and RIGHT: Seek backward/forward 5 seconds. Shift+arrow does a 1 second exact seek

  • UP and DOWN: Seek forward/backward 1 minute. Shift+arrow does a 5 second exact seek

  • p / SPACE: Pause (pressing again unpauses).

  • q: Stop playing and quit.

  • Q: Like q, but store the current playback position. Playing the same file later will resume at the old playback position if possible.

  • 9 and 0: Decrease/increase volume.

  • m: Mute sound.

  • o (also P): Show progression bar, elapsed time and total duration on the OSD.

  • O: Toggle OSD states between normal and playback time/duration.

  • v: Toggle subtitle visibility.

  • z and Z: Adjust subtitle delay by +/- 0.1 seconds. The x key does the same as Z currently, but use is discouraged.

  • r and R: Move subtitles up/down. The t key does the same as R currently, but use is discouraged.

  • s: Take a screenshot.

  • S: Take a screenshot, without subtitles. (Whether this works depends on VO driver support.)

  • Shift+PGUP and Shift+PGDWN: Seek backward or forward by 10 minutes. (This used to be mapped to PGUP/PGDWN without Shift.)

  • 1 and 2: Adjust contrast.

  • 3 and 4: Adjust brightness.

  • 5 and 6: Adjust gamma.

  • 7 and 8: Adjust saturation.


반응형
Posted by J-sean
:
반응형

2021.01.04 - [Linux] - Open Image Files From Terminal in Linux(Ubuntu) - 리눅스(우분투) 터미널에서 이미지 파일 열기

 

리눅스(우분투) 서버 같은 콘솔 환경에서도 이미지 뷰어를 사용할 수 있다.

 

fbi를 설치한다. fbi는 리눅스의 framebuffer를 이용해 이미지를 디스플레이한다. 

 

sudo fbi '이미지 파일' 형식으로 실행한다.

 

이미지가 디스플레이 된다.

 

h키를 누르면 keyboard commands를 볼 수 있다.

 

반응형
Posted by J-sean
:
반응형

리눅스(우분투) 서버 같은 콘솔 환경에서도 Ogg, MP3, WAV 등의 음악 파일을 재생 할 수 있다.


cmus(C* Music Player)를 설치한다.


설치가 완료되면 cmus를 실행한다.


cmus가 실행되고 아무것도 표시되지 않는다. 방향키, 엔터등을 눌러도 아무런 반응이 없지만 당황하지 말고 숫자 5를 누른다.


Browser가 표시된다. 음악 파일이 있는 디렉토리로 이동한다.



원하는 파일에서 엔터키를 누르면 플레이 된다. 하지만 선택한 1개의 파일만 플레이 되므로 간단한 사용방법을 알아 보자.


각 파일에서 a키(add to library)를 눌러 준다. (y키를 누르면 playlist로 추가된다)


숫자 1을 누른다. Library 화면이 표시되고 위에서 add한 파일이 모두 추가 되었다. 원하는 곡에서 엔터키를 눌러 플레이 할 수 있다. (Artist/Album과 Track은 Tab키로 이동한다)


숫자 2를 누르면 Sorted 화면이 표시된다.



숫자 3을 누르면 Playlist 화면이 표시된다.


숫자 4를 누르면 Queue 화면이 표시된다. Queue에 추가(e)되는 노래는 순서를 무시하고 지금 재생되는 노래 다음에 재생된다. 재생 후 queue에서 자동 삭제된다.


숫자 5를 누르면 Browser 화면이 표시된다.


숫자 6을 누르면 Filters 화면이 표시된다.



숫자 7을 누르면 Settings 화면이 표시된다. 각 단축키를 확인할 수 있다. 아래는 주요 단축키 목록이다.

  • x: play

  • v: stop

  • c: pause

  • b: next

  • z: prev

  • right: seek +5

  • left: seek -5

  • -: vol -10%

  • +/=: vol +10%

  • C(대문자): toggle continue

  • r: toggle repeat

  • s: toggle shuffle

  • t: toggle show_remaining_time

  • a: add(to library)

  • e: add(to queue)

  • y: add(to playlist)

  • delete: remove

  • q: quit


WAV 파일은 aplay로 간단히 재생할 수 있다.


반응형
Posted by J-sean
:
반응형

리눅스(우분투)에서 CD-ROM/USB를 마운트하고 사용해 보자.


ls -l /dev | less 명령어로 확인해 보면 /dev/cdrom은 /dev/sr0를 가리키는 symbolic link 이다. (l)


/dev/sr0는 block device 이다. (b)


CD-ROM을 마운트할 디렉토리를 만들고 mount 한다. 마운트가 되며 쓸 수 없는(read-only) 장치라는 경고가 나온다.


mount 명령을 실행해 보면 /dev/sr0가 /home/sean/cdrom에 마운트 되어 있다.



CD의 내용을 확인할 수 있다.


다시 홈 디렉토리로 이동하고 unmount 한다.


mount 명령어로 확인해 보면 CD-ROM 마운트 정보가 없어졌다.


USB도 마찬가지 방법으로 진행한다. USB는 보통 /dev/sdb? 의 이름을 가진다.


mkdir usb

sudo mount /dev/sdb1 usb


반응형
Posted by J-sean
: