4 Copyright (c) 2012 Samsung Electronics Co., Ltd All Rights Reserved
6 Licensed under the Apache License, Version 2.0 (the "License");
7 you may not use this file except in compliance with the License.
8 You may obtain a copy of the License at
10 http://www.apache.org/licenses/LICENSE-2.0
12 Unless required by applicable law or agreed to in writing, software
13 distributed under the License is distributed on an "AS IS" BASIS,
14 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 See the License for the specific language governing permissions and
16 limitations under the License.
26 #include <X11/Xutil.h>
27 #include <X11/Xatom.h>
33 #include <X11/extensions/Xvlib.h>
34 #include <X11/extensions/Xvproto.h>
35 #include <X11/extensions/Xdamage.h>
37 #include <drm_slp_bufmgr.h>
42 #define UTILX_TRACE printf
44 #define UTILX_TRACE(...)
47 static Atom _atom_grab_key = None;
48 static Atom _atom_grab_excl_win = None;
49 static Atom _atom_grab_or_excl_win = None;
51 static Atom _atom_notification_level = None;
52 static Atom _atom_indicator_visible_state = None;
53 static Atom _atom_indicator_visible_state_on = None;
54 static Atom _atom_indicator_visible_state_off = None;
56 static Atom _atom_comp_effect_state = None;
57 static Atom _atom_comp_fake_launch = None;
58 static Atom _atom_comp_fake_launch_image = None;
60 static Atom _atom_comp_window_effect_type = None;
61 static Atom _atom_comp_effect_default = None;
62 static Atom _atom_comp_effect_none = None;
63 static Atom _atom_comp_effect_custom0 = None;
64 static Atom _atom_comp_effect_custom1 = None;
65 static Atom _atom_comp_effect_custom2 = None;
66 static Atom _atom_comp_effect_custom3 = None;
67 static Atom _atom_comp_effect_custom4 = None;
68 static Atom _atom_comp_effect_custom5 = None;
69 static Atom _atom_comp_effect_custom6 = None;
70 static Atom _atom_comp_effect_custom7 = None;
71 static Atom _atom_comp_effect_custom8 = None;
72 static Atom _atom_comp_effect_custom9 = None;
74 static Atom _atom_window_opaque = None;
76 static Atom _atom_screen_capture_disable = None;
78 static Atom _atom_comp_capture_effect = None;
80 const unsigned long maxlen = 1024l;
82 static void _utilx_set_window_property (Display* dpy, Window win, Atom atom, Atom type, unsigned int *val, unsigned int num);
83 static int _utilx_get_window_property (Display* dpy, Window win, Atom atom, Atom type, unsigned int *val, unsigned int len);
85 static void _utilx_effect_atom_check( Display* dpy );
86 static Atom _utilx_convert_style_to_atom( Display* dpy, Utilx_Effect_Style style );
87 static Utilx_Effect_Style _utilx_convert_atom_to_style( Display* dpy, Atom style );
89 static int _utilx_get_indicator_atoms(Display *dpy);
91 API void utilx_set_system_notification_level (Display* dpy, Window win, Utilx_Notification_Level level)
93 UTILX_TRACE ("[UTILX] utilx_set_system_notification_level... win = %x, level = %d\n", win, level);
99 fprintf (stderr, "[UTILX] Error.. Invald Display.. %s (%d)\n", __func__, __LINE__);
105 case UTILX_NOTIFICATION_LEVEL_LOW:
109 case UTILX_NOTIFICATION_LEVEL_NORMAL:
113 case UTILX_NOTIFICATION_LEVEL_HIGH:
122 if (!_atom_notification_level)
124 _atom_notification_level = XInternAtom (dpy, "_E_ILLUME_NOTIFICATION_LEVEL", False);
125 if (!_atom_notification_level)
127 fprintf (stderr, "[UTILX] Error.. Cannot create _E_ILLUME_NOTIFICATION_LEVEL atom.. %s (%d)\n", __func__, __LINE__);
132 _utilx_set_window_property (dpy, win, _atom_notification_level, XA_CARDINAL,
133 (unsigned int *)¬i_level, 1);
137 API Utilx_Notification_Level utilx_get_system_notification_level (Display* dpy, Window win)
139 UTILX_TRACE ("[UTILX] utilx_get_system_notification_level... win = %x\n", win);
141 Utilx_Notification_Level noti_level;
145 noti_level = UTILX_NOTIFICATION_LEVEL_LOW;
149 fprintf (stderr, "[UTILX] Error.. Invald Display.. %s (%d)\n", __func__, __LINE__);
153 if (!_atom_notification_level)
155 _atom_notification_level = XInternAtom (dpy, "_E_ILLUME_NOTIFICATION_LEVEL", False);
156 if (!_atom_notification_level)
158 fprintf (stderr, "[UTILX] Error.. Cannot create _E_ILLUME_NOTIFICATION_LEVEL atom.. %s (%d)\n", __func__, __LINE__);
163 ret = _utilx_get_window_property (dpy, win, _atom_notification_level, XA_CARDINAL,
164 (unsigned int *)&level, 1);
171 noti_level = UTILX_NOTIFICATION_LEVEL_LOW;
175 noti_level = UTILX_NOTIFICATION_LEVEL_NORMAL;
179 noti_level = UTILX_NOTIFICATION_LEVEL_HIGH;
183 noti_level = UTILX_NOTIFICATION_LEVEL_LOW;
189 noti_level = UTILX_NOTIFICATION_LEVEL_LOW;
196 static int _utilx_get_indicator_atoms(Display *dpy)
198 if (!_atom_indicator_visible_state)
200 _atom_indicator_visible_state = XInternAtom (dpy, "_E_ILLUME_INDICATOR_STATE", False);
201 if (!_atom_indicator_visible_state)
203 fprintf (stderr, "[UTILX] Error.. Cannot create _E_ILLUME_INDICATOR_STATE atom.. %s (%d)\n", __func__, __LINE__);
208 if (!_atom_indicator_visible_state_on)
210 _atom_indicator_visible_state_on = XInternAtom (dpy, "_E_ILLUME_INDICATOR_ON", False);
211 if (!_atom_indicator_visible_state_on)
213 fprintf (stderr, "[UTILX] Error.. Cannot create _E_ILLUME_INDICATOR_ON atom.. %s (%d)\n", __func__, __LINE__);
218 if (!_atom_indicator_visible_state_off)
220 _atom_indicator_visible_state_off = XInternAtom (dpy, "_E_ILLUME_INDICATOR_OFF", False);
221 if (!_atom_indicator_visible_state_off)
223 fprintf (stderr, "[UTILX] Error.. Cannot create _E_ILLUME_INDICATOR_OFF atom.. %s (%d)\n", __func__, __LINE__);
231 API void utilx_enable_indicator (Display* dpy, Window win, int enable)
233 UTILX_TRACE ("[UTILX] utilx_indicator_set_visible_state... win = %x, show_state = %d\n", win, enable);
237 fprintf (stderr, "[UTILX] Error.. Invald Display.. %s (%d)\n", __func__, __LINE__);
241 if (!_utilx_get_indicator_atoms(dpy))
243 fprintf (stderr, "[UTILX] Error.. Cannot create atoms.. %s (%d)\n", __func__, __LINE__);
249 _utilx_set_window_property (dpy, win, _atom_indicator_visible_state, XA_ATOM,
250 (unsigned int *)&_atom_indicator_visible_state_on, 1);
254 _utilx_set_window_property (dpy, win, _atom_indicator_visible_state, XA_ATOM,
255 (unsigned int *)&_atom_indicator_visible_state_off, 1);
260 API int utilx_get_indicator_state (Display* dpy, Window win)
262 UTILX_TRACE ("[UTILX] utilx_indicator_set_visible_state... win = %x, show_state = %d\n", win, enable);
269 fprintf (stderr, "[UTILX] Error.. Invald Display.. %s (%d)\n", __func__, __LINE__);
273 if (!_utilx_get_indicator_atoms(dpy))
275 fprintf (stderr, "[UTILX] Error.. Cannot create atoms.. %s (%d)\n", __func__, __LINE__);
279 ret = _utilx_get_window_property (dpy, win, _atom_indicator_visible_state, XA_ATOM,
280 (unsigned int *)&state, 1);
284 if (state == _atom_indicator_visible_state_on)
286 else if (state == _atom_indicator_visible_state_off)
296 _utilx_set_window_property (Display* dpy, Window win, Atom atom, Atom type, unsigned int *val, unsigned int num)
298 XChangeProperty (dpy, win, atom, type, 32, PropModeReplace, (unsigned char *)val, num);
304 _utilx_get_window_property (Display* dpy, Window win, Atom atom, Atom type, unsigned int *val, unsigned int len)
306 unsigned char* prop_ret;
308 unsigned long bytes_after, num_ret;
314 if (XGetWindowProperty(dpy, win, atom, 0, 0x7fffffff, False,
315 type, &type_ret, &format_ret, &num_ret,
316 &bytes_after, &prop_ret) != Success)
321 if (type_ret != type || format_ret != 32)
325 else if (num_ret == 0 || !prop_ret)
333 for (i = 0; i < len; i++)
335 val[i] = ((unsigned long *)prop_ret)[i];
348 static unsigned long _get_list_of_grabbed_key (Display *disp, Window win, int **key_list)
352 unsigned long nr_item;
353 unsigned long sz_remains_data;
355 if (XGetWindowProperty(disp, win,
356 _atom_grab_key, 0, 0x7fffffff, False, XA_CARDINAL,
357 &ret_type, &ret_format, &nr_item,
358 &sz_remains_data, (unsigned char**)key_list) != Success)
363 // fprintf(stderr, "
\e[31m%d - %lu
\e[0m\n", ret_format, nr_item);
369 static void _free_list_of_grabbed_key (int *key_list)
377 static void _free_new_list_of_grabbed_key (int *new_key_list)
385 static int _search_grabbed_key (int *key_list, int key, unsigned long cnt)
389 for (i = cnt - 1; i >= 0; i --) {
390 if (key_list[i] == key) break;
397 static int *_del_grabbed_key (int *key_list, int i, unsigned long *cnt)
399 int *new_key_list = NULL;
401 // Only one element is exists in the list of grabbed key
404 if (*cnt == 0) return NULL;
407 new_key_list = malloc((*cnt) * sizeof(int));
408 if (new_key_list == NULL) {
415 memcpy(new_key_list, key_list, sizeof(int) * i);
419 if ((*cnt) - i > 0) {
420 memcpy(new_key_list + i,
422 sizeof(int) * ((*cnt) - i)
429 static void _set_exclusive_grab_info_to_root (Display *disp, int keycode, Window win, int grab_mode)
432 int *key_list = NULL;
436 unsigned long nr_item;
437 unsigned long sz_remains_data;
440 if( grab_mode == EXCLUSIVE_GRAB )
442 if( _atom_grab_excl_win == None )
443 _atom_grab_excl_win = XInternAtom(disp, STR_ATOM_GRAB_EXCL_WIN, False);
444 ex_grabwin = _atom_grab_excl_win;
446 else if( grab_mode == OR_EXCLUSIVE_GRAB )
448 if( _atom_grab_or_excl_win == None )
449 _atom_grab_or_excl_win = XInternAtom(disp, STR_ATOM_GRAB_OR_EXCL_WIN, False);
450 ex_grabwin = _atom_grab_or_excl_win;
455 if (XGetWindowProperty(disp, DefaultRootWindow(disp),
456 ex_grabwin, 0, 0x7fffffff, False, XA_CARDINAL,
457 &ret_type, &ret_format, &nr_item,
458 &sz_remains_data, (unsigned char**)&key_list) != Success)
460 fprintf(stderr, "[utilX][%s] Fail to get root window property !\n", __FUNCTION__);
465 printf("[%s] keycode = %d\n", __FUNCTION__, keycode);
468 for( i=0 ; i < nr_item ; i++ )
470 if( key_list && (key_list[i] == keycode) )
474 XChangeProperty(disp, DefaultRootWindow(disp), ex_grabwin, XA_CARDINAL, 32,
475 nr_item ? PropModeAppend : PropModeReplace, (unsigned char *)&keycode, 1);
483 static void _unset_exclusive_grab_info_to_root (Display *disp, int keycode, int grab_mode)
486 unsigned long cnt = 0;
487 int *key_list = NULL;
488 int *new_key_list = NULL;
492 unsigned long nr_item;
493 unsigned long sz_remains_data;
496 if( grab_mode == EXCLUSIVE_GRAB )
498 if( _atom_grab_excl_win == None )
499 _atom_grab_excl_win = XInternAtom(disp, STR_ATOM_GRAB_EXCL_WIN, False);
500 ex_grabwin = _atom_grab_excl_win;
502 else if( grab_mode == OR_EXCLUSIVE_GRAB )
504 if( _atom_grab_or_excl_win == None )
505 _atom_grab_or_excl_win = XInternAtom(disp, STR_ATOM_GRAB_OR_EXCL_WIN, False);
506 ex_grabwin = _atom_grab_or_excl_win;
511 if (XGetWindowProperty(disp, DefaultRootWindow(disp),
512 ex_grabwin, 0, 0x7fffffff, False, XA_CARDINAL,
513 &ret_type, &ret_format, &nr_item,
514 &sz_remains_data, (unsigned char**)&key_list) != Success)
521 fprintf(stderr, "
\e[32m[utilX][%s] keycode = %d
\e[0m\n", __FUNCTION__, keycode);
525 for( i=0 ; i < nr_item ; i++ )
527 if( key_list[i] == keycode )//&& grab_mode == EXCLUSIVE_GRAB )
535 fprintf(stderr, "[utilX][%s] cnt = %d, nr_item = %d\n", __FUNCTION__, cnt, nr_item);
540 new_key_list = malloc(sizeof(int)*cnt);
548 //fprintf(stderr, "
\e[32m[utilX][%s] Fail to allocation memory for new_key_list !
\e[0m\n", __FUNCTION__);
549 XDeleteProperty(disp, DefaultRootWindow(disp), ex_grabwin);
554 for( i=0 ; i < nr_item ; i++ )
556 if( key_list[i] == keycode )//&& grab_mode == EXCLUSIVE_GRAB )
561 new_key_list[cnt++] = key_list[i];
565 XChangeProperty(disp, DefaultRootWindow(disp), ex_grabwin, XA_CARDINAL, 32,
566 PropModeReplace, (unsigned char *)new_key_list, cnt);
569 XDeleteProperty(disp, DefaultRootWindow(disp), ex_grabwin);
581 static int _is_grabbed_key_exclusively (Display* disp, int keycode, int grab_mode)
584 int *key_list = NULL;
588 unsigned long nr_item;
589 unsigned long sz_remains_data;
592 if( grab_mode == EXCLUSIVE_GRAB )
594 if( _atom_grab_excl_win == None )
595 _atom_grab_excl_win = XInternAtom(disp, STR_ATOM_GRAB_EXCL_WIN, False);
596 ex_grabwin = _atom_grab_excl_win;
598 else if( grab_mode == OR_EXCLUSIVE_GRAB )
600 if( _atom_grab_or_excl_win == None )
601 _atom_grab_or_excl_win = XInternAtom(disp, STR_ATOM_GRAB_OR_EXCL_WIN, False);
602 ex_grabwin = _atom_grab_or_excl_win;
607 if (XGetWindowProperty(disp, DefaultRootWindow(disp),
608 ex_grabwin, 0, 0x7fffffff, False, XA_CARDINAL,
609 &ret_type, &ret_format, &nr_item,
610 &sz_remains_data, (unsigned char**)&key_list) != Success)
612 fprintf(stderr, "[%s] Fail to get root window property !\n", __FUNCTION__);
616 for( i=0 ; i < nr_item ; i++ )
618 if( key_list[i] == keycode )
619 return EXCLUSIVE_GRABBED_ALREADY;
626 API int utilx_grab_key (Display* disp, Window win, const char* key, int grab_mode)
629 int *key_list = NULL;
637 fprintf(stderr, "[%s] Display is NULL\n", __FUNCTION__);
641 if (_atom_grab_key == None) {
642 _atom_grab_key = XInternAtom(disp, STR_ATOM_GRAB_KEY, False);
645 if (!strncmp(key, "Keycode-", 8)) {
646 keycode = atoi(key + 8);
648 keysym = XStringToKeysym(key);
649 if (keysym == NoSymbol) goto out;
650 keycode = XKeysymToKeycode(disp, XStringToKeysym(key));
652 if (keycode == 0) goto out;
654 if( grab_mode == EXCLUSIVE_GRAB )
657 result = _is_grabbed_key_exclusively(disp, keycode, grab_mode);
660 printf("[%s] _is_grabbed_key_exclusively returns result = %d\n", __FUNCTION__, result);
665 fprintf(stderr, "[%s] keycode(%d) was already grabbed exclusively (grab_mode=0x%X) !\n", __FUNCTION__, keycode, grab_mode);
669 else if( grab_mode == OR_EXCLUSIVE_GRAB )
671 result = _is_grabbed_key_exclusively(disp, keycode, grab_mode);
675 fprintf(stderr, "[%s] Keycode(%d) was already grabbed with overridable exclusive mode (grab_mode=0x%x)\n", __FUNCTION__, keycode, grab_mode);
676 fprintf(stderr, "[%s] Now it will be overridden by a new window(0x%x) !\n", __FUNCTION__, win);
677 utilx_ungrab_key(disp, win, key);
681 keycode |= grab_mode;
683 cnt = _get_list_of_grabbed_key(disp, win, &key_list);
685 i = _search_grabbed_key(key_list, keycode, cnt);
686 _free_list_of_grabbed_key(key_list);
688 if( grab_mode == OR_EXCLUSIVE_GRAB )
690 utilx_ungrab_key(disp, win, key);
694 fprintf(stderr, "Key is already grabbed\n");
700 XChangeProperty(disp, win, _atom_grab_key, XA_CARDINAL, 32,
701 cnt ? PropModeAppend : PropModeReplace, (unsigned char *)&keycode, 1);
703 keycode = keycode & (~GRAB_MODE_MASK);
705 printf("[%s] keycode = %d\n", __FUNCTION__, keycode);
708 if( EXCLUSIVE_GRAB == grab_mode || OR_EXCLUSIVE_GRAB == grab_mode )
709 _set_exclusive_grab_info_to_root(disp, keycode, win, grab_mode);
719 API int utilx_ungrab_key (Display* disp, Window win, const char* key)
723 int *key_list = NULL;
724 int *new_key_list = NULL;
732 fprintf(stderr, "[%s] Display is NULL\n", __FUNCTION__);
736 if (_atom_grab_key == None) {
737 _atom_grab_key = XInternAtom(disp, STR_ATOM_GRAB_KEY, False);
740 if (!strncmp(key, "Keycode-", 8)) {
741 keycode = atoi(key + 8);
743 keysym = XStringToKeysym(key);
744 if (keysym == NoSymbol) goto out;
745 keycode = XKeysymToKeycode(disp, XStringToKeysym(key));
747 if (keycode == 0) goto out;
749 cnt = _get_list_of_grabbed_key(disp, win, &key_list);
750 if (cnt == 0) goto out;
753 i = _search_grabbed_key(key_list, keycode | EXCLUSIVE_GRAB, cnt);
758 i = _search_grabbed_key(key_list, keycode | OR_EXCLUSIVE_GRAB, cnt);
763 i = _search_grabbed_key(key_list, keycode | TOP_POSITION_GRAB, cnt);
768 i = _search_grabbed_key(key_list, keycode | SHARED_GRAB, cnt);
772 _free_list_of_grabbed_key(key_list);
779 _unset_exclusive_grab_info_to_root(disp, keycode, OR_EXCLUSIVE_GRAB);
784 _unset_exclusive_grab_info_to_root(disp, keycode, EXCLUSIVE_GRAB);
787 new_key_list = _del_grabbed_key(key_list, i, &cnt);
788 _free_list_of_grabbed_key(key_list);
791 XChangeProperty(disp, win, _atom_grab_key, XA_CARDINAL, 32,
792 PropModeReplace, (unsigned char *)new_key_list, cnt);
795 XDeleteProperty(disp, win, _atom_grab_key);
799 _free_new_list_of_grabbed_key(new_key_list);
808 API Utilx_Key_Status utilx_get_key_status(Display* dpy, char *key_name)
810 unsigned char keymap[32];
811 static unsigned int masktable[8] = { 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80 };
812 Utilx_Key_Status status = UTILX_KEY_STATUS_UNKNOWN;
814 if( !strncmp(key_name, KEY_VOLUMEDOWN, LEN_KEY_VOLUMEDOWN) ||
815 !strncmp(key_name, KEY_VOLUMEUP, LEN_KEY_VOLUMEUP) ||
816 !strncmp(key_name, KEY_PAUSE, LEN_KEY_PAUSE) ||
817 !strncmp(key_name, KEY_SEND, LEN_KEY_SEND) ||
818 !strncmp(key_name, KEY_SELECT, LEN_KEY_VOLUMEDOWN) ||
819 !strncmp(key_name, KEY_END, LEN_KEY_END) ||
820 !strncmp(key_name, KEY_POWER, LEN_KEY_POWER) ||
821 !strncmp(key_name, KEY_CAMERA, LEN_KEY_CAMERA) ||
822 !strncmp(key_name, KEY_CONFIG, LEN_KEY_CONFIG) ||
823 !strncmp(key_name, KEY_PLAYCD, LEN_KEY_PLAYCD) ||
824 !strncmp(key_name, KEY_STOPCD, LEN_KEY_STOPCD) ||
825 !strncmp(key_name, KEY_PAUSECD, LEN_KEY_PAUSECD) ||
826 !strncmp(key_name, KEY_NEXTSONG, LEN_KEY_NEXTSONG) ||
827 !strncmp(key_name, KEY_PREVIOUSSONG, LEN_KEY_PREVIOUSSONG) ||
828 !strncmp(key_name, KEY_REWIND, LEN_KEY_REWIND) ||
829 !strncmp(key_name, KEY_FASTFORWARD, LEN_KEY_FASTFORWARD) ||
830 !strncmp(key_name, KEY_MEDIA, LEN_KEY_MEDIA) )
832 KeySym ks = XStringToKeysym(key_name);
833 KeyCode kc = XKeysymToKeycode(dpy, ks);
837 XQueryKeymap(dpy, (char *)keymap);
838 if( keymap[kc >> 3] & masktable[kc & 7] )
839 status = UTILX_KEY_STATUS_PRESSED;
841 status = UTILX_KEY_STATUS_RELEASED;
848 API void utilx_set_window_effect_state(Display* dpy, Window win, int state)
852 fprintf (stderr, "[UTILX] Error.. Invald Display.. %s (%d)\n", __func__, __LINE__);
855 if( !_atom_comp_effect_state)
856 _atom_comp_effect_state = XInternAtom(dpy, "_NET_CM_WINDOW_EFFECT_ENABLE",False);
858 _utilx_set_window_property(dpy, win, _atom_comp_effect_state, XA_CARDINAL, (unsigned int *)&state, 1);
863 API int utilx_get_window_effect_state(Display* dpy, Window win)
866 if( !_atom_comp_effect_state)
867 _atom_comp_effect_state = XInternAtom(dpy, "_NET_CM_WINDOW_EFFECT_ENABLE",False);
868 _utilx_get_window_property(dpy, win, _atom_comp_effect_state, XA_CARDINAL, (unsigned int *)&state, 1);
873 _utilx_string_set_window_property( Display *dpy, Window win, Atom atom, char *val, unsigned int num)
875 XChangeProperty( dpy, win, atom, XA_STRING, 8, PropModeReplace, (unsigned char*)val, val ? strlen(val):0 );
878 API void utilx_set_fake_launch_img(Display* dpy, Window win, char *file_name)
880 //UTILX_TRACE ("[UTILX] utilx_set_effect_state... win = %x, show_state = %d\n", win, enable);
884 fprintf (stderr, "[UTILX] Error.. Invald Display.. %s (%d)\n", __func__, __LINE__);
887 if( !_atom_comp_fake_launch_image)
888 _atom_comp_fake_launch_image = XInternAtom(dpy, "_E_COMP_FAKE_LAUNCH_IMAGE",False);
890 _utilx_string_set_window_property(dpy, win, _atom_comp_fake_launch_image, file_name, 1);
893 API void utilx_show_fake_effect( Display *dpy, Window win, char *fake_image_file )
897 _atom_comp_fake_launch = XInternAtom( dpy, "_E_COMP_FAKE_LAUNCH", False );
899 if( !_atom_comp_fake_launch )
901 fprintf( stderr, "XInternAtom(_E_COMP_FAKE_LAUNCH) failed.\n" );
905 utilx_set_fake_launch_img(dpy, win, fake_image_file);
909 // send fake client message
910 xev.xclient.type = ClientMessage;
911 xev.xclient.display = dpy;
912 xev.xclient.window = win;
913 xev.xclient.message_type = _atom_comp_fake_launch;
914 xev.xclient.format = 32;
915 xev.xclient.data.l[0] = 1; // 1 : start effect , 0 : end effect
916 xev.xclient.data.l[1] = 0;
917 xev.xclient.data.l[2] = 0;
918 xev.xclient.data.l[3] = 0;
919 xev.xclient.data.l[4] = 0;
921 XSendEvent( dpy, win, False,
922 SubstructureRedirectMask | SubstructureNotifyMask,
929 API void utilx_hide_fake_effect( Display *dpy, Window win)
933 _atom_comp_fake_launch = XInternAtom( dpy, "_E_COMP_FAKE_LAUNCH", False );
934 if( !_atom_comp_fake_launch )
936 fprintf( stderr, "XInternAtom(_E_COMP_FAKE_LAUNCH) failed.\n" );
940 // send fake client message
941 xev.xclient.type = ClientMessage;
942 xev.xclient.display = dpy;
943 xev.xclient.window = win;
944 xev.xclient.message_type = _atom_comp_fake_launch;
945 xev.xclient.format = 32;
946 xev.xclient.data.l[0] = 0; // 1 : start effect , 0 : end effect
947 xev.xclient.data.l[1] = 0;
948 xev.xclient.data.l[2] = 0;
949 xev.xclient.data.l[3] = 0;
950 xev.xclient.data.l[4] = 0;
952 XSendEvent( dpy, win, False,
953 SubstructureRedirectMask | SubstructureNotifyMask,
958 static void _utilx_effect_atom_check( Display* dpy )
962 fprintf (stderr, "[UTILX] Error.. Invald Display.. %s (%d)\n", __func__, __LINE__);
966 if( !_atom_comp_window_effect_type)
967 _atom_comp_window_effect_type = XInternAtom(dpy, "_NET_CM_WINDOW_EFFECT_TYPE",False);
968 if( !_atom_comp_effect_default )
969 _atom_comp_effect_default = XInternAtom(dpy, "_NET_CM_EFFECT_DEFAULT",False);
970 if( !_atom_comp_effect_none )
971 _atom_comp_effect_none = XInternAtom(dpy, "_NET_CM_EFFECT_NONE",False);
972 if( !_atom_comp_effect_custom0 )
973 _atom_comp_effect_custom0 = XInternAtom(dpy, "_NET_CM_EFFECT_CUSTOM0",False);
974 if( !_atom_comp_effect_custom1 )
975 _atom_comp_effect_custom1 = XInternAtom(dpy, "_NET_CM_EFFECT_CUSTOM1",False);
976 if( !_atom_comp_effect_custom2 )
977 _atom_comp_effect_custom2 = XInternAtom(dpy, "_NET_CM_EFFECT_CUSTOM2",False);
978 if( !_atom_comp_effect_custom3 )
979 _atom_comp_effect_custom3 = XInternAtom(dpy, "_NET_CM_EFFECT_CUSTOM3",False);
980 if( !_atom_comp_effect_custom4 )
981 _atom_comp_effect_custom4 = XInternAtom(dpy, "_NET_CM_EFFECT_CUSTOM4",False);
982 if( !_atom_comp_effect_custom5 )
983 _atom_comp_effect_custom5 = XInternAtom(dpy, "_NET_CM_EFFECT_CUSTOM5",False);
984 if( !_atom_comp_effect_custom6 )
985 _atom_comp_effect_custom6 = XInternAtom(dpy, "_NET_CM_EFFECT_CUSTOM6",False);
986 if( !_atom_comp_effect_custom7 )
987 _atom_comp_effect_custom7 = XInternAtom(dpy, "_NET_CM_EFFECT_CUSTOM7",False);
988 if( !_atom_comp_effect_custom8 )
989 _atom_comp_effect_custom8 = XInternAtom(dpy, "_NET_CM_EFFECT_CUSTOM8",False);
990 if( !_atom_comp_effect_custom9 )
991 _atom_comp_effect_custom9 = XInternAtom(dpy, "_NET_CM_EFFECT_CUSTOM9",False);
994 static Atom _utilx_convert_style_to_atom( Display* dpy, Utilx_Effect_Style style )
998 fprintf (stderr, "[UTILX] Error.. Invald Display.. %s (%d)\n", __func__, __LINE__);
999 return _atom_comp_effect_none;
1001 _utilx_effect_atom_check(dpy);
1003 if ( style == UTILX_EFFECT_STYLE_DEFAULT ) return _atom_comp_effect_default;
1004 else if ( style == UTILX_EFFECT_STYLE_NONE ) return _atom_comp_effect_none;
1005 else if ( style == UTILX_EFFECT_STYLE_CUSTOM0 ) return _atom_comp_effect_custom0;
1006 else if ( style == UTILX_EFFECT_STYLE_CUSTOM1 ) return _atom_comp_effect_custom1;
1007 else if ( style == UTILX_EFFECT_STYLE_CUSTOM2 ) return _atom_comp_effect_custom2;
1008 else if ( style == UTILX_EFFECT_STYLE_CUSTOM3 ) return _atom_comp_effect_custom3;
1009 else if ( style == UTILX_EFFECT_STYLE_CUSTOM4 ) return _atom_comp_effect_custom4;
1010 else if ( style == UTILX_EFFECT_STYLE_CUSTOM5 ) return _atom_comp_effect_custom5;
1011 else if ( style == UTILX_EFFECT_STYLE_CUSTOM6 ) return _atom_comp_effect_custom6;
1012 else if ( style == UTILX_EFFECT_STYLE_CUSTOM7 ) return _atom_comp_effect_custom7;
1013 else if ( style == UTILX_EFFECT_STYLE_CUSTOM8 ) return _atom_comp_effect_custom8;
1014 else if ( style == UTILX_EFFECT_STYLE_CUSTOM9 ) return _atom_comp_effect_custom9;
1015 else return _atom_comp_effect_none;
1019 static Utilx_Effect_Style _utilx_convert_atom_to_style( Display* dpy, Atom style )
1023 fprintf (stderr, "[UTILX] Error.. Invald Display.. %s (%d)\n", __func__, __LINE__);
1024 return UTILX_EFFECT_STYLE_NONE;
1026 _utilx_effect_atom_check(dpy);
1028 if ( style == _atom_comp_effect_default ) return UTILX_EFFECT_STYLE_DEFAULT;
1029 else if ( style == _atom_comp_effect_none ) return UTILX_EFFECT_STYLE_NONE;
1030 else if ( style == _atom_comp_effect_custom0 ) return UTILX_EFFECT_STYLE_CUSTOM0;
1031 else if ( style == _atom_comp_effect_custom1 ) return UTILX_EFFECT_STYLE_CUSTOM1;
1032 else if ( style == _atom_comp_effect_custom2 ) return UTILX_EFFECT_STYLE_CUSTOM2;
1033 else if ( style == _atom_comp_effect_custom3 ) return UTILX_EFFECT_STYLE_CUSTOM3;
1034 else if ( style == _atom_comp_effect_custom4 ) return UTILX_EFFECT_STYLE_CUSTOM4;
1035 else if ( style == _atom_comp_effect_custom5 ) return UTILX_EFFECT_STYLE_CUSTOM5;
1036 else if ( style == _atom_comp_effect_custom6 ) return UTILX_EFFECT_STYLE_CUSTOM6;
1037 else if ( style == _atom_comp_effect_custom7 ) return UTILX_EFFECT_STYLE_CUSTOM7;
1038 else if ( style == _atom_comp_effect_custom8 ) return UTILX_EFFECT_STYLE_CUSTOM8;
1039 else if ( style == _atom_comp_effect_custom9 ) return UTILX_EFFECT_STYLE_CUSTOM9;
1040 else return UTILX_EFFECT_STYLE_DEFAULT;
1043 API void utilx_set_window_effect_style(Display* dpy, Window win, Utilx_Effect_Type type, Utilx_Effect_Style style)
1045 Atom *window_effect_type_list = NULL;
1048 fprintf (stderr, "[UTILX] Error.. Invald Display.. %s (%d)\n", __func__, __LINE__);
1051 _utilx_effect_atom_check(dpy);
1053 window_effect_type_list = (Atom *)malloc(sizeof(Atom) * 6);
1055 if ( !window_effect_type_list )
1057 fprintf (stderr, "[UTILX] Error.. malloc().. %s (%d)\n", __func__, __LINE__);
1061 window_effect_type_list[0] = _atom_comp_effect_default;
1062 window_effect_type_list[1] = _atom_comp_effect_default;
1063 window_effect_type_list[2] = _atom_comp_effect_default;
1064 window_effect_type_list[3] = _atom_comp_effect_default;
1065 window_effect_type_list[4] = _atom_comp_effect_default;
1066 window_effect_type_list[5] = _atom_comp_effect_default;
1068 _utilx_get_window_property(dpy, win, _atom_comp_window_effect_type, XA_ATOM, (unsigned int *)window_effect_type_list, 6);
1070 if ( type == UTILX_EFFECT_TYPE_MAP ) window_effect_type_list[0] = _utilx_convert_style_to_atom(dpy, style);
1071 else if ( type == UTILX_EFFECT_TYPE_UNMAP ) window_effect_type_list[1] = _utilx_convert_style_to_atom(dpy, style);
1072 else if ( type == UTILX_EFFECT_TYPE_RAISEABOVE ) window_effect_type_list[2] = _utilx_convert_style_to_atom(dpy, style);
1073 else if ( type == UTILX_EFFECT_TYPE_ROTATION ) window_effect_type_list[3] = _utilx_convert_style_to_atom(dpy, style);
1074 else if ( type == UTILX_EFFECT_TYPE_FOCUSIN ) window_effect_type_list[4] = _utilx_convert_style_to_atom(dpy, style);
1075 else if ( type == UTILX_EFFECT_TYPE_FOCUSOUT ) window_effect_type_list[5] = _utilx_convert_style_to_atom(dpy, style);
1077 _utilx_set_window_property(dpy, win, _atom_comp_window_effect_type, XA_ATOM, (unsigned int *)window_effect_type_list, 6);
1080 free(window_effect_type_list);
1083 API Utilx_Effect_Style utilx_get_window_effect_style(Display* dpy, Window win, Utilx_Effect_Type type)
1085 Atom *window_effect_type_list = NULL;
1086 Utilx_Effect_Style style = UTILX_EFFECT_STYLE_DEFAULT;
1090 fprintf (stderr, "[UTILX] Error.. Invald Display.. %s (%d)\n", __func__, __LINE__);
1091 return UTILX_EFFECT_STYLE_NONE;
1093 _utilx_effect_atom_check(dpy);
1095 window_effect_type_list = (Atom *)malloc(sizeof(Atom) * 6);
1097 if ( !window_effect_type_list )
1099 fprintf (stderr, "[UTILX] Error.. malloc().. %s (%d)\n", __func__, __LINE__);
1100 return UTILX_EFFECT_STYLE_NONE;
1103 if ( _utilx_get_window_property(dpy, win, _atom_comp_window_effect_type, XA_ATOM, (unsigned int *)window_effect_type_list, 6) != 6 )
1105 fprintf (stderr, "[UTILX] Error.. get property failed!.. %s (%d)\n", __func__, __LINE__);
1106 free(window_effect_type_list);
1107 return UTILX_EFFECT_STYLE_NONE;
1110 if ( type == UTILX_EFFECT_TYPE_MAP ) style = _utilx_convert_atom_to_style(dpy, window_effect_type_list[0]);
1111 else if ( type == UTILX_EFFECT_TYPE_UNMAP ) style = _utilx_convert_atom_to_style(dpy, window_effect_type_list[1]);
1112 else if ( type == UTILX_EFFECT_TYPE_RAISEABOVE ) style = _utilx_convert_atom_to_style(dpy, window_effect_type_list[2]);
1113 else if ( type == UTILX_EFFECT_TYPE_ROTATION ) style = _utilx_convert_atom_to_style(dpy, window_effect_type_list[3]);
1114 else if ( type == UTILX_EFFECT_TYPE_FOCUSIN ) style = _utilx_convert_atom_to_style(dpy, window_effect_type_list[4]);
1115 else if ( type == UTILX_EFFECT_TYPE_FOCUSOUT ) style = _utilx_convert_atom_to_style(dpy, window_effect_type_list[5]);
1118 free(window_effect_type_list);
1122 API int utilx_set_window_opaque_state (Display* dpy, Window win, Utilx_Opaque_State state)
1124 UTILX_TRACE ("[UTILX] utilx_set_window_opaque_state... win = %x, show_state = %d\n", win, state);
1126 unsigned int is_opaque;
1130 fprintf (stderr, "[UTILX] Error.. Invald Display.. %s (%d)\n", __func__, __LINE__);
1136 case UTILX_OPAQUE_STATE_OFF:
1140 case UTILX_OPAQUE_STATE_ON:
1145 fprintf (stderr, "[UTILX] Error.. Invald State.. %s (%d)\n", __func__, __LINE__);
1149 if (!_atom_window_opaque)
1151 _atom_window_opaque = XInternAtom (dpy, "_E_ILLUME_WINDOW_REGION_OPAQUE", False);
1152 if (!_atom_window_opaque)
1154 fprintf (stderr, "[UTILX] Error.. Cannot create _E_ILLUME_WINDOW_REGION_OPAQUE atom.. %s (%d)\n", __func__, __LINE__);
1159 _utilx_set_window_property (dpy, win, _atom_window_opaque, XA_CARDINAL,
1160 (unsigned int *)&is_opaque, 1);
1166 _utilx_screen_capture_atom_ensure (Display* dpy)
1168 if (_atom_screen_capture_disable)
1171 _atom_screen_capture_disable = XInternAtom (dpy, "_CB_SCREEN_CAPTURE_DISABLE", False);
1172 if (_atom_screen_capture_disable)
1175 fprintf (stderr, "[UTILX] Error.. Cannot create _CB_SCREEN_CAPTURE_DISABLE atom.. %s (%d)\n", __func__, __LINE__);
1179 utilx_set_screen_capture(Display* dpy, int enable)
1186 fprintf (stderr, "[UTILX] Error.. dpy is NULL %s (%d)\n", __func__, __LINE__);
1190 root = RootWindow (dpy, DefaultScreen(dpy));
1191 disable = (enable) ? 0 : 1;
1193 _utilx_screen_capture_atom_ensure (dpy);
1195 _utilx_set_window_property (dpy, root, _atom_screen_capture_disable, XA_CARDINAL, (unsigned int *)&disable, 1);
1201 utilx_get_screen_capture(Display* dpy)
1208 fprintf (stderr, "[UTILX] Error.. dpy is NULL %s (%d)\n", __func__, __LINE__);
1212 root = RootWindow (dpy, DefaultScreen(dpy));
1214 _utilx_screen_capture_atom_ensure (dpy);
1216 _utilx_get_window_property(dpy, root, _atom_screen_capture_disable, XA_CARDINAL,
1217 (unsigned int *)&disable, 1);
1219 return (disable) ? 0 : 1;
1222 API void utilx_set_window_cardinal_property(Display* dpy, Window win, Atom atom, unsigned int *value)
1224 _utilx_set_window_property(dpy, win, atom, XA_CARDINAL, value, 1);
1227 API int utilx_get_window_cardinal_property (Display* dpy, Window win, Atom atom, unsigned int *value)
1229 return _utilx_get_window_property(dpy, win, atom, XA_CARDINAL, value, 1);
1232 API void utilx_show_capture_effect( Display *dpy, Window win)
1236 _atom_comp_capture_effect = XInternAtom( dpy, "_E_COMP_CAPTURE_EFFECT", False );
1237 if( !_atom_comp_capture_effect )
1239 fprintf( stderr, "XInternAtom(_E_COMP_CAPTURE_EFFECT) failed.\n" );
1243 // send capture effect client message
1244 xev.xclient.type = ClientMessage;
1245 xev.xclient.display = dpy;
1246 xev.xclient.window = win;
1247 xev.xclient.message_type = _atom_comp_capture_effect;
1248 xev.xclient.format = 32;
1249 xev.xclient.data.l[0] = 0;
1250 xev.xclient.data.l[1] = 0;
1251 xev.xclient.data.l[2] = 0;
1252 xev.xclient.data.l[3] = 0;
1253 xev.xclient.data.l[4] = 0;
1255 XSendEvent( dpy, win, False,
1256 SubstructureRedirectMask | SubstructureNotifyMask,
1261 API UtilxScrnConf *utilx_scrnconf_allocate (void)
1263 UtilxScrnConf *scrnconf = calloc (1, sizeof(UtilxScrnConf));
1266 fprintf (stderr, "fail to allocate UtilxScrnConf\n");
1273 API void utilx_scrnconf_free (UtilxScrnConf *scrnconf)
1278 if (scrnconf->str_output)
1279 free (scrnconf->str_output);
1281 if (scrnconf->str_resolution)
1282 free (scrnconf->str_resolution);
1288 API void utilx_scrnconf_get_info (Display *dpy, UtilxScrnConf *scrnconf)
1290 Window win = DefaultRootWindow(dpy);
1291 Atom scrnconf_atom = None;
1300 scrnconf_atom = XInternAtom (dpy, "_SCRNCONF_INFO", False);
1303 if (XGetTextProperty (dpy, win, &xtp, scrnconf_atom))
1305 s = XmbTextPropertyToTextList (dpy, &xtp, &list, &items);
1306 if ((s == XLocaleNotSupported) ||
1307 (s == XNoMemory) || (s == XConverterNotFound))
1308 str = strdup((char *)xtp.value);
1309 else if ((s >= Success) && (items > 0))
1310 str = strdup(list[0]);
1313 XFreeStringList (list);
1318 ptr = strtok (str, ",");
1323 scrnconf->str_output = calloc (1, strlen(ptr));
1324 if (!scrnconf->str_output)
1327 strcpy (scrnconf->str_output, ptr);
1331 if (!strcmp(ptr, "CONNECT"))
1332 scrnconf->status = UTILX_SCRNCONF_STATUS_CONNECT;
1333 else if (!strcmp(ptr, "ACTIVE"))
1334 scrnconf->status = UTILX_SCRNCONF_STATUS_ACTIVE;
1336 scrnconf->status = UTILX_SCRNCONF_STATUS_NULL;
1340 scrnconf->str_resolution = calloc (1, strlen(ptr));
1341 if (!scrnconf->str_resolution)
1344 strcpy (scrnconf->str_resolution, ptr);
1348 if (!strcmp(ptr, "CLONE"))
1349 scrnconf->dispmode = UTILX_SCRNCONF_DISPMODE_CLONE;
1350 else if (!strcmp(ptr, "EXTENDED"))
1351 scrnconf->dispmode = UTILX_SCRNCONF_DISPMODE_EXTENDED;
1353 scrnconf->dispmode = UTILX_SCRNCONF_DISPMODE_NULL;
1358 ptr = strtok (NULL, ",");
1369 if (scrnconf->str_output)
1370 free (scrnconf->str_output);
1371 if (scrnconf->str_resolution)
1372 free (scrnconf->str_resolution);
1381 API int utilx_scrnconf_set_dispmode (Display *dpy, Utilx_Scrnconf_Dispmode dispmode)
1383 Window win = DefaultRootWindow(dpy);
1385 Atom scrnconf_atom = None;
1386 UtilxScrnConf *scrnconf = NULL;
1388 scrnconf = utilx_scrnconf_allocate ();
1392 utilx_scrnconf_get_info (dpy, scrnconf);
1394 if (scrnconf->status == UTILX_SCRNCONF_STATUS_NULL)
1396 fprintf (stderr, "[utilx_scrnconf]: the status of screen configuration is null\n");
1400 if (scrnconf->dispmode == dispmode)
1402 fprintf (stderr, "[utilx_scrnconf]: dispmode (%d) already set\n", dispmode);
1406 utilx_scrnconf_free (scrnconf);
1408 scrnconf_atom = XInternAtom (dpy, "_SCRNCONF_DISPMODE_SET", False);
1410 xev.xclient.window = win;
1411 xev.xclient.type = ClientMessage;
1412 xev.xclient.message_type = scrnconf_atom;
1413 xev.xclient.format = 32;
1414 xev.xclient.data.s[0] = dispmode;
1416 XSendEvent(dpy, win, False, StructureNotifyMask, &xev);
1422 typedef struct _ShotInfo
1428 unsigned int height;
1433 drm_slp_bufmgr bufmgr;
1436 DRI2Buffer* dri2_buffers;
1443 #define FOURCC(a,b,c,d) (((unsigned)d&0xff)<<24 | ((unsigned)c&0xff)<<16 | ((unsigned)b&0xff)<<8 | ((unsigned)a&0xff))
1445 #define FOURCC_RGB32 FOURCC('R','G','B','4')
1447 static ShotInfo *shot_info;
1450 _get_port (Display *dpy, unsigned int id)
1452 unsigned int ver, rev, req_base, evt_base, err_base;
1453 unsigned int adaptors;
1454 XvAdaptorInfo *ai = NULL;
1455 XvImageFormatValues *fo = NULL;
1459 if (XvQueryExtension (dpy, &ver, &rev, &req_base, &evt_base, &err_base) != Success)
1462 if (XvQueryAdaptors (dpy, DefaultRootWindow (dpy), &adaptors, &ai) != Success)
1468 for (i = 0; i < adaptors; i++)
1470 int support_format = False;
1472 if (!(ai[i].type & XvStillMask))
1477 fo = XvListImageFormats (dpy, p, &formats);
1478 for (j = 0; j < formats; j++)
1479 if (fo[j].id == (int)id)
1480 support_format = True;
1485 if (!support_format)
1488 if (XvGrabPort (dpy, p, 0) == Success)
1490 XvFreeAdaptorInfo (ai);
1494 fprintf (stderr, "[UTILX] fail : grab port. \n");
1497 XvFreeAdaptorInfo (ai);
1503 _deinit_screen_shot (ShotInfo *info)
1505 Atom atom_stream_off;
1510 atom_stream_off = XInternAtom (info->dpy, "_USER_WM_PORT_ATTRIBUTE_STREAM_OFF", False);
1511 if (atom_stream_off > 0)
1512 XvSetPortAttribute (info->dpy, info->port, atom_stream_off, 1);
1514 if (info->dri2_buffers)
1515 free(info->dri2_buffers);
1517 drm_slp_bo_unref(info->bo);
1519 drm_slp_bufmgr_destroy (info->bufmgr);
1521 XFreeGC (info->dpy, info->gc);
1522 if (info->pixmap > 0)
1523 XFreePixmap (info->dpy, info->pixmap);
1525 XvUngrabPort (info->dpy, info->port, 0);
1527 XCloseDisplay (info->dpy);
1534 _init_screen_shot (Display* dpy, unsigned int width, unsigned int height)
1536 ShotInfo *info = NULL;
1539 int dri2_err_base = 0;
1540 int damage_err_base = 0;
1541 int dri2Major, dri2Minor;
1542 char *driverName = NULL, *deviceName = NULL;
1544 unsigned int attachments[1];
1545 int dri2_count, dri2_out_count;
1546 int dri2_width, dri2_height, dri2_stride;
1551 if (shot_info->width == width && shot_info->height == height)
1554 _deinit_screen_shot (shot_info);
1557 info = calloc (1, sizeof (ShotInfo));
1565 info->dpy = XOpenDisplay (NULL);
1571 info->port = _get_port (info->dpy, FOURCC_RGB32);
1572 if (info->port <= 0)
1576 atom_capture = XInternAtom (info->dpy, "_USER_WM_PORT_ATTRIBUTE_CAPTURE", False);
1577 XvSetPortAttribute (info->dpy, info->port, atom_capture, 1);
1578 XvQueryBestSize (info->dpy, info->port, 0, 0, 0, width, height, &width, &height);
1579 if (width <= 0 || height <= 0)
1581 info->width = width;
1582 info->height = height;
1585 info->pixmap = XCreatePixmap (info->dpy,
1586 DefaultRootWindow (info->dpy),
1588 DefaultDepth (info->dpy, DefaultScreen (info->dpy)));
1589 if (info->pixmap <= 0)
1593 info->gc = XCreateGC (info->dpy, info->pixmap, 0, 0);
1597 XSetForeground (info->dpy, info->gc, 0xFF000000);
1598 XFillRectangle (info->dpy, info->pixmap, info->gc, 0, 0, width, height);
1600 screen = DefaultScreen(info->dpy);
1601 if (!DRI2QueryExtension (info->dpy, &dri2_base, &dri2_err_base))
1603 fprintf (stderr, "[UTILX] fail : DRI2QueryExtension !!\n");
1607 if (!DRI2QueryVersion (info->dpy, &dri2Major, &dri2Minor))
1609 fprintf (stderr, "[UTILX] fail : DRI2QueryVersion !!\n");
1613 if (!DRI2Connect (info->dpy, RootWindow(info->dpy, screen), &driverName, &deviceName))
1615 fprintf (stderr, "[UTILX] fail : DRI2Connect !!\n");
1620 info->drm_fd = open (deviceName, O_RDWR);
1621 if (info->drm_fd < 0)
1623 fprintf (stderr, "[UTILX] fail : open drm device (%s)\n", deviceName);
1627 /* get the drm magic */
1628 drmGetMagic(info->drm_fd, &magic);
1629 if (!DRI2Authenticate(info->dpy, RootWindow(info->dpy, screen), magic))
1631 fprintf (stderr, "[UTILX] fail : DRI2Authenticate (%d)\n", magic);
1636 info->bufmgr = drm_slp_bufmgr_init (info->drm_fd, NULL);
1639 fprintf (stderr, "[UTILX] fail : init buffer manager \n");
1643 DRI2CreateDrawable (info->dpy, info->pixmap);
1645 attachments[0] = DRI2BufferFrontLeft;
1647 info->dri2_buffers = DRI2GetBuffers (info->dpy, info->pixmap, &dri2_width, &dri2_height,
1648 attachments, dri2_count, &dri2_out_count);
1650 if (!info->dri2_buffers)
1652 fprintf (stderr, "[UTILX] fail : get buffers\n");
1656 if (!info->dri2_buffers[0].name)
1658 fprintf (stderr, "[UTILX] fail : a handle of the dri2 buffer is null \n ");
1662 info->bo = drm_slp_bo_import (info->bufmgr, info->dri2_buffers[0].name);
1665 fprintf (stderr, "[UTILX] fail : import bo (key:%d)\n", info->dri2_buffers[0].name);
1669 dri2_stride = info->dri2_buffers[0].pitch;
1672 info->virtual = (void*)drm_slp_bo_get_handle (info->bo, DRM_SLP_DEVICE_CPU);
1675 fprintf (stderr, "[UTILX] fail : map \n");
1679 if (!XDamageQueryExtension(info->dpy, &info->damage_base, &damage_err_base))
1682 info->damage = XDamageCreate (info->dpy, info->pixmap, XDamageReportNonEmpty);
1683 if (info->damage <= 0)
1685 fprintf (stderr, "[UTILX] fail : create damage \n");
1694 _deinit_screen_shot (info);
1699 utilx_create_screen_shot (Display* dpy, int width, int height)
1706 fprintf (stderr, "[UTILX] invalid display(%p) \n", dpy);
1710 if (width <= 0 || height <= 0)
1712 fprintf (stderr, "[UTILX] invalid size(%dx%d) \n", width, height);
1716 info = _init_screen_shot (dpy, width, height);
1719 fprintf (stderr, "[UTILX] fail : initialize screenshot. \n");
1723 XSync (info->dpy, 0);
1725 XvGetStill (info->dpy, info->port, info->pixmap, info->gc,
1726 0, 0, info->width, info->height,
1727 0, 0, info->width, info->height);
1729 XSync (info->dpy, 0);
1731 XNextEvent (info->dpy, &ev); /* wating for x event */
1733 if (ev.type == (info->damage_base + XDamageNotify))
1735 XDamageNotifyEvent *damage_ev = (XDamageNotifyEvent *)&ev;
1736 if (damage_ev->drawable == info->pixmap)
1738 XDamageSubtract (info->dpy, info->damage, None, None );
1739 return info->virtual;
1742 XDamageSubtract (info->dpy, info->damage, None, None );
1745 utilx_release_screen_shot ();
1751 utilx_release_screen_shot (void)
1753 _deinit_screen_shot (shot_info);