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.
28 #include <X11/Xutil.h>
29 #include <X11/Xatom.h>
30 #include <X11/extensions/Xrandr.h>
36 #include <X11/extensions/Xvlib.h>
37 #include <X11/extensions/Xvproto.h>
38 #include <X11/extensions/Xdamage.h>
39 #include <X11/extensions/XShm.h>
41 #include <drm_slp_bufmgr.h>
46 #define UTILX_TRACE printf
48 #define UTILX_TRACE(...)
51 static Atom _atom_grab_key = None;
52 static Atom _atom_grab_excl_win = None;
53 static Atom _atom_grab_or_excl_win = None;
55 static Atom _atom_notification_level = None;
56 static Atom _atom_indicator_visible_state = None;
57 static Atom _atom_indicator_visible_state_on = None;
58 static Atom _atom_indicator_visible_state_off = None;
60 static Atom _atom_comp_effect_state = None;
61 static Atom _atom_comp_fake_launch = None;
62 static Atom _atom_comp_fake_launch_image = None;
64 static Atom _atom_comp_window_effect_type = None;
65 static Atom _atom_comp_effect_default = None;
66 static Atom _atom_comp_effect_none = None;
67 static Atom _atom_comp_effect_custom0 = None;
68 static Atom _atom_comp_effect_custom1 = None;
69 static Atom _atom_comp_effect_custom2 = None;
70 static Atom _atom_comp_effect_custom3 = None;
71 static Atom _atom_comp_effect_custom4 = None;
72 static Atom _atom_comp_effect_custom5 = None;
73 static Atom _atom_comp_effect_custom6 = None;
74 static Atom _atom_comp_effect_custom7 = None;
75 static Atom _atom_comp_effect_custom8 = None;
76 static Atom _atom_comp_effect_custom9 = None;
78 static Atom _atom_window_opaque = None;
80 static Atom _atom_screen_capture_disable = None;
82 static Atom _atom_comp_capture_effect = None;
84 const unsigned long maxlen = 1024l;
86 static void _utilx_set_window_property (Display* dpy, Window win, Atom atom, Atom type, unsigned int *val, unsigned int num);
87 static int _utilx_get_window_property (Display* dpy, Window win, Atom atom, Atom type, unsigned int *val, unsigned int len);
89 static void _utilx_effect_atom_check( Display* dpy );
90 static Atom _utilx_convert_style_to_atom( Display* dpy, Utilx_Effect_Style style );
91 static Utilx_Effect_Style _utilx_convert_atom_to_style( Display* dpy, Atom style );
93 static int _utilx_get_indicator_atoms(Display *dpy);
95 API void utilx_set_system_notification_level (Display* dpy, Window win, Utilx_Notification_Level level)
97 UTILX_TRACE ("[UTILX] utilx_set_system_notification_level... win = %x, level = %d\n", win, level);
103 fprintf (stderr, "[UTILX] Error.. Invald Display.. %s (%d)\n", __func__, __LINE__);
109 case UTILX_NOTIFICATION_LEVEL_LOW:
113 case UTILX_NOTIFICATION_LEVEL_NORMAL:
117 case UTILX_NOTIFICATION_LEVEL_HIGH:
126 if (!_atom_notification_level)
128 _atom_notification_level = XInternAtom (dpy, "_E_ILLUME_NOTIFICATION_LEVEL", False);
129 if (!_atom_notification_level)
131 fprintf (stderr, "[UTILX] Error.. Cannot create _E_ILLUME_NOTIFICATION_LEVEL atom.. %s (%d)\n", __func__, __LINE__);
136 _utilx_set_window_property (dpy, win, _atom_notification_level, XA_CARDINAL,
137 (unsigned int *)¬i_level, 1);
141 API Utilx_Notification_Level utilx_get_system_notification_level (Display* dpy, Window win)
143 UTILX_TRACE ("[UTILX] utilx_get_system_notification_level... win = %x\n", win);
145 Utilx_Notification_Level noti_level;
149 noti_level = UTILX_NOTIFICATION_LEVEL_LOW;
153 fprintf (stderr, "[UTILX] Error.. Invald Display.. %s (%d)\n", __func__, __LINE__);
157 if (!_atom_notification_level)
159 _atom_notification_level = XInternAtom (dpy, "_E_ILLUME_NOTIFICATION_LEVEL", False);
160 if (!_atom_notification_level)
162 fprintf (stderr, "[UTILX] Error.. Cannot create _E_ILLUME_NOTIFICATION_LEVEL atom.. %s (%d)\n", __func__, __LINE__);
167 ret = _utilx_get_window_property (dpy, win, _atom_notification_level, XA_CARDINAL,
168 (unsigned int *)&level, 1);
175 noti_level = UTILX_NOTIFICATION_LEVEL_LOW;
179 noti_level = UTILX_NOTIFICATION_LEVEL_NORMAL;
183 noti_level = UTILX_NOTIFICATION_LEVEL_HIGH;
187 noti_level = UTILX_NOTIFICATION_LEVEL_LOW;
193 noti_level = UTILX_NOTIFICATION_LEVEL_LOW;
200 static int _utilx_get_indicator_atoms(Display *dpy)
202 if (!_atom_indicator_visible_state)
204 _atom_indicator_visible_state = XInternAtom (dpy, "_E_ILLUME_INDICATOR_STATE", False);
205 if (!_atom_indicator_visible_state)
207 fprintf (stderr, "[UTILX] Error.. Cannot create _E_ILLUME_INDICATOR_STATE atom.. %s (%d)\n", __func__, __LINE__);
212 if (!_atom_indicator_visible_state_on)
214 _atom_indicator_visible_state_on = XInternAtom (dpy, "_E_ILLUME_INDICATOR_ON", False);
215 if (!_atom_indicator_visible_state_on)
217 fprintf (stderr, "[UTILX] Error.. Cannot create _E_ILLUME_INDICATOR_ON atom.. %s (%d)\n", __func__, __LINE__);
222 if (!_atom_indicator_visible_state_off)
224 _atom_indicator_visible_state_off = XInternAtom (dpy, "_E_ILLUME_INDICATOR_OFF", False);
225 if (!_atom_indicator_visible_state_off)
227 fprintf (stderr, "[UTILX] Error.. Cannot create _E_ILLUME_INDICATOR_OFF atom.. %s (%d)\n", __func__, __LINE__);
235 API void utilx_enable_indicator (Display* dpy, Window win, int enable)
237 UTILX_TRACE ("[UTILX] utilx_indicator_set_visible_state... win = %x, show_state = %d\n", win, enable);
241 fprintf (stderr, "[UTILX] Error.. Invald Display.. %s (%d)\n", __func__, __LINE__);
245 if (!_utilx_get_indicator_atoms(dpy))
247 fprintf (stderr, "[UTILX] Error.. Cannot create atoms.. %s (%d)\n", __func__, __LINE__);
253 _utilx_set_window_property (dpy, win, _atom_indicator_visible_state, XA_ATOM,
254 (unsigned int *)&_atom_indicator_visible_state_on, 1);
258 _utilx_set_window_property (dpy, win, _atom_indicator_visible_state, XA_ATOM,
259 (unsigned int *)&_atom_indicator_visible_state_off, 1);
264 API int utilx_get_indicator_state (Display* dpy, Window win)
266 UTILX_TRACE ("[UTILX] utilx_indicator_set_visible_state... win = %x, show_state = %d\n", win, enable);
273 fprintf (stderr, "[UTILX] Error.. Invald Display.. %s (%d)\n", __func__, __LINE__);
277 if (!_utilx_get_indicator_atoms(dpy))
279 fprintf (stderr, "[UTILX] Error.. Cannot create atoms.. %s (%d)\n", __func__, __LINE__);
283 ret = _utilx_get_window_property (dpy, win, _atom_indicator_visible_state, XA_ATOM,
284 (unsigned int *)&state, 1);
288 if (state == _atom_indicator_visible_state_on)
290 else if (state == _atom_indicator_visible_state_off)
300 _utilx_set_window_property (Display* dpy, Window win, Atom atom, Atom type, unsigned int *val, unsigned int num)
302 XChangeProperty (dpy, win, atom, type, 32, PropModeReplace, (unsigned char *)val, num);
308 _utilx_get_window_property (Display* dpy, Window win, Atom atom, Atom type, unsigned int *val, unsigned int len)
310 unsigned char* prop_ret;
312 unsigned long bytes_after, num_ret;
318 if (XGetWindowProperty(dpy, win, atom, 0, 0x7fffffff, False,
319 type, &type_ret, &format_ret, &num_ret,
320 &bytes_after, &prop_ret) != Success)
325 if (type_ret != type || format_ret != 32)
329 else if (num_ret == 0 || !prop_ret)
337 for (i = 0; i < len; i++)
339 val[i] = ((unsigned long *)prop_ret)[i];
352 static unsigned long _get_list_of_grabbed_key (Display *disp, Window win, int **key_list)
356 unsigned long nr_item;
357 unsigned long sz_remains_data;
359 if (XGetWindowProperty(disp, win,
360 _atom_grab_key, 0, 0x7fffffff, False, XA_CARDINAL,
361 &ret_type, &ret_format, &nr_item,
362 &sz_remains_data, (unsigned char**)key_list) != Success)
367 // fprintf(stderr, "
\e[31m%d - %lu
\e[0m\n", ret_format, nr_item);
373 static void _free_list_of_grabbed_key (int *key_list)
381 static void _free_new_list_of_grabbed_key (int *new_key_list)
389 static int _search_grabbed_key (int *key_list, int key, unsigned long cnt)
393 for (i = cnt - 1; i >= 0; i --) {
394 if (key_list[i] == key) break;
401 static int *_del_grabbed_key (int *key_list, int i, unsigned long *cnt)
403 int *new_key_list = NULL;
405 // Only one element is exists in the list of grabbed key
408 if (*cnt == 0) return NULL;
411 new_key_list = malloc((*cnt) * sizeof(int));
412 if (new_key_list == NULL) {
419 memcpy(new_key_list, key_list, sizeof(int) * i);
423 if ((*cnt) - i > 0) {
424 memcpy(new_key_list + i,
426 sizeof(int) * ((*cnt) - i)
433 static void _set_exclusive_grab_info_to_root (Display *disp, int keycode, Window win, int grab_mode)
436 int *key_list = NULL;
440 unsigned long nr_item;
441 unsigned long sz_remains_data;
444 if( grab_mode == EXCLUSIVE_GRAB )
446 if( _atom_grab_excl_win == None )
447 _atom_grab_excl_win = XInternAtom(disp, STR_ATOM_GRAB_EXCL_WIN, False);
448 ex_grabwin = _atom_grab_excl_win;
450 else if( grab_mode == OR_EXCLUSIVE_GRAB )
452 if( _atom_grab_or_excl_win == None )
453 _atom_grab_or_excl_win = XInternAtom(disp, STR_ATOM_GRAB_OR_EXCL_WIN, False);
454 ex_grabwin = _atom_grab_or_excl_win;
459 if (XGetWindowProperty(disp, DefaultRootWindow(disp),
460 ex_grabwin, 0, 0x7fffffff, False, XA_CARDINAL,
461 &ret_type, &ret_format, &nr_item,
462 &sz_remains_data, (unsigned char**)&key_list) != Success)
464 fprintf(stderr, "[utilX][%s] Fail to get root window property !\n", __FUNCTION__);
469 printf("[%s] keycode = %d\n", __FUNCTION__, keycode);
472 for( i=0 ; i < nr_item ; i++ )
474 if( key_list && (key_list[i] == keycode) )
478 XChangeProperty(disp, DefaultRootWindow(disp), ex_grabwin, XA_CARDINAL, 32,
479 nr_item ? PropModeAppend : PropModeReplace, (unsigned char *)&keycode, 1);
487 static void _unset_exclusive_grab_info_to_root (Display *disp, int keycode, int grab_mode)
490 unsigned long cnt = 0;
491 int *key_list = NULL;
492 int *new_key_list = NULL;
496 unsigned long nr_item;
497 unsigned long sz_remains_data;
500 if( grab_mode == EXCLUSIVE_GRAB )
502 if( _atom_grab_excl_win == None )
503 _atom_grab_excl_win = XInternAtom(disp, STR_ATOM_GRAB_EXCL_WIN, False);
504 ex_grabwin = _atom_grab_excl_win;
506 else if( grab_mode == OR_EXCLUSIVE_GRAB )
508 if( _atom_grab_or_excl_win == None )
509 _atom_grab_or_excl_win = XInternAtom(disp, STR_ATOM_GRAB_OR_EXCL_WIN, False);
510 ex_grabwin = _atom_grab_or_excl_win;
515 if (XGetWindowProperty(disp, DefaultRootWindow(disp),
516 ex_grabwin, 0, 0x7fffffff, False, XA_CARDINAL,
517 &ret_type, &ret_format, &nr_item,
518 &sz_remains_data, (unsigned char**)&key_list) != Success)
525 fprintf(stderr, "
\e[32m[utilX][%s] keycode = %d
\e[0m\n", __FUNCTION__, keycode);
529 for( i=0 ; i < nr_item ; i++ )
531 if( key_list[i] == keycode )//&& grab_mode == EXCLUSIVE_GRAB )
539 fprintf(stderr, "[utilX][%s] cnt = %d, nr_item = %d\n", __FUNCTION__, cnt, nr_item);
544 new_key_list = malloc(sizeof(int)*cnt);
552 //fprintf(stderr, "
\e[32m[utilX][%s] Fail to allocation memory for new_key_list !
\e[0m\n", __FUNCTION__);
553 XDeleteProperty(disp, DefaultRootWindow(disp), ex_grabwin);
558 for( i=0 ; i < nr_item ; i++ )
560 if( key_list[i] == keycode )//&& grab_mode == EXCLUSIVE_GRAB )
565 new_key_list[cnt++] = key_list[i];
569 XChangeProperty(disp, DefaultRootWindow(disp), ex_grabwin, XA_CARDINAL, 32,
570 PropModeReplace, (unsigned char *)new_key_list, cnt);
573 XDeleteProperty(disp, DefaultRootWindow(disp), ex_grabwin);
585 static int _is_grabbed_key_exclusively (Display* disp, int keycode, int grab_mode)
588 int *key_list = NULL;
592 unsigned long nr_item;
593 unsigned long sz_remains_data;
596 if( grab_mode == EXCLUSIVE_GRAB )
598 if( _atom_grab_excl_win == None )
599 _atom_grab_excl_win = XInternAtom(disp, STR_ATOM_GRAB_EXCL_WIN, False);
600 ex_grabwin = _atom_grab_excl_win;
602 else if( grab_mode == OR_EXCLUSIVE_GRAB )
604 if( _atom_grab_or_excl_win == None )
605 _atom_grab_or_excl_win = XInternAtom(disp, STR_ATOM_GRAB_OR_EXCL_WIN, False);
606 ex_grabwin = _atom_grab_or_excl_win;
611 if (XGetWindowProperty(disp, DefaultRootWindow(disp),
612 ex_grabwin, 0, 0x7fffffff, False, XA_CARDINAL,
613 &ret_type, &ret_format, &nr_item,
614 &sz_remains_data, (unsigned char**)&key_list) != Success)
616 fprintf(stderr, "[%s] Fail to get root window property !\n", __FUNCTION__);
620 for( i=0 ; i < nr_item ; i++ )
622 if( key_list[i] == keycode )
623 return EXCLUSIVE_GRABBED_ALREADY;
630 API int utilx_grab_key (Display* disp, Window win, const char* key, int grab_mode)
633 int *key_list = NULL;
641 fprintf(stderr, "[%s] Display is NULL\n", __FUNCTION__);
645 if (_atom_grab_key == None) {
646 _atom_grab_key = XInternAtom(disp, STR_ATOM_GRAB_KEY, False);
649 if (!strncmp(key, "Keycode-", 8)) {
650 keycode = atoi(key + 8);
652 keysym = XStringToKeysym(key);
653 if (keysym == NoSymbol) goto out;
654 keycode = XKeysymToKeycode(disp, XStringToKeysym(key));
656 if (keycode == 0) goto out;
658 if( grab_mode == EXCLUSIVE_GRAB )
661 result = _is_grabbed_key_exclusively(disp, keycode, grab_mode);
664 printf("[%s] _is_grabbed_key_exclusively returns result = %d\n", __FUNCTION__, result);
669 fprintf(stderr, "[%s] keycode(%d) was already grabbed exclusively (grab_mode=0x%X) !\n", __FUNCTION__, keycode, grab_mode);
673 else if( grab_mode == OR_EXCLUSIVE_GRAB )
675 result = _is_grabbed_key_exclusively(disp, keycode, grab_mode);
679 fprintf(stderr, "[%s] Keycode(%d) was already grabbed with overridable exclusive mode (grab_mode=0x%x)\n", __FUNCTION__, keycode, grab_mode);
680 fprintf(stderr, "[%s] Now it will be overridden by a new window(0x%x) !\n", __FUNCTION__, win);
681 utilx_ungrab_key(disp, win, key);
685 keycode |= grab_mode;
687 cnt = _get_list_of_grabbed_key(disp, win, &key_list);
689 i = _search_grabbed_key(key_list, keycode, cnt);
690 _free_list_of_grabbed_key(key_list);
692 if( grab_mode == OR_EXCLUSIVE_GRAB )
694 utilx_ungrab_key(disp, win, key);
698 fprintf(stderr, "Key is already grabbed\n");
704 XChangeProperty(disp, win, _atom_grab_key, XA_CARDINAL, 32,
705 cnt ? PropModeAppend : PropModeReplace, (unsigned char *)&keycode, 1);
707 keycode = keycode & (~GRAB_MODE_MASK);
709 printf("[%s] keycode = %d\n", __FUNCTION__, keycode);
712 if( EXCLUSIVE_GRAB == grab_mode || OR_EXCLUSIVE_GRAB == grab_mode )
713 _set_exclusive_grab_info_to_root(disp, keycode, win, grab_mode);
723 API int utilx_ungrab_key (Display* disp, Window win, const char* key)
727 int *key_list = NULL;
728 int *new_key_list = NULL;
736 fprintf(stderr, "[%s] Display is NULL\n", __FUNCTION__);
740 if (_atom_grab_key == None) {
741 _atom_grab_key = XInternAtom(disp, STR_ATOM_GRAB_KEY, False);
744 if (!strncmp(key, "Keycode-", 8)) {
745 keycode = atoi(key + 8);
747 keysym = XStringToKeysym(key);
748 if (keysym == NoSymbol) goto out;
749 keycode = XKeysymToKeycode(disp, XStringToKeysym(key));
751 if (keycode == 0) goto out;
753 cnt = _get_list_of_grabbed_key(disp, win, &key_list);
754 if (cnt == 0) goto out;
757 i = _search_grabbed_key(key_list, keycode | EXCLUSIVE_GRAB, cnt);
762 i = _search_grabbed_key(key_list, keycode | OR_EXCLUSIVE_GRAB, cnt);
767 i = _search_grabbed_key(key_list, keycode | TOP_POSITION_GRAB, cnt);
772 i = _search_grabbed_key(key_list, keycode | SHARED_GRAB, cnt);
776 _free_list_of_grabbed_key(key_list);
783 _unset_exclusive_grab_info_to_root(disp, keycode, OR_EXCLUSIVE_GRAB);
788 _unset_exclusive_grab_info_to_root(disp, keycode, EXCLUSIVE_GRAB);
791 new_key_list = _del_grabbed_key(key_list, i, &cnt);
792 _free_list_of_grabbed_key(key_list);
795 XChangeProperty(disp, win, _atom_grab_key, XA_CARDINAL, 32,
796 PropModeReplace, (unsigned char *)new_key_list, cnt);
799 XDeleteProperty(disp, win, _atom_grab_key);
803 _free_new_list_of_grabbed_key(new_key_list);
812 API Utilx_Key_Status utilx_get_key_status(Display* dpy, char *key_name)
814 unsigned char keymap[32];
815 static unsigned int masktable[8] = { 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80 };
816 Utilx_Key_Status status = UTILX_KEY_STATUS_UNKNOWN;
818 if( !strncmp(key_name, KEY_VOLUMEDOWN, LEN_KEY_VOLUMEDOWN) ||
819 !strncmp(key_name, KEY_VOLUMEUP, LEN_KEY_VOLUMEUP) ||
820 !strncmp(key_name, KEY_PAUSE, LEN_KEY_PAUSE) ||
821 !strncmp(key_name, KEY_SEND, LEN_KEY_SEND) ||
822 !strncmp(key_name, KEY_SELECT, LEN_KEY_VOLUMEDOWN) ||
823 !strncmp(key_name, KEY_END, LEN_KEY_END) ||
824 !strncmp(key_name, KEY_POWER, LEN_KEY_POWER) ||
825 !strncmp(key_name, KEY_CAMERA, LEN_KEY_CAMERA) ||
826 !strncmp(key_name, KEY_CONFIG, LEN_KEY_CONFIG) ||
827 !strncmp(key_name, KEY_PLAYCD, LEN_KEY_PLAYCD) ||
828 !strncmp(key_name, KEY_STOPCD, LEN_KEY_STOPCD) ||
829 !strncmp(key_name, KEY_PAUSECD, LEN_KEY_PAUSECD) ||
830 !strncmp(key_name, KEY_NEXTSONG, LEN_KEY_NEXTSONG) ||
831 !strncmp(key_name, KEY_PREVIOUSSONG, LEN_KEY_PREVIOUSSONG) ||
832 !strncmp(key_name, KEY_REWIND, LEN_KEY_REWIND) ||
833 !strncmp(key_name, KEY_FASTFORWARD, LEN_KEY_FASTFORWARD) ||
834 !strncmp(key_name, KEY_MEDIA, LEN_KEY_MEDIA) )
836 KeySym ks = XStringToKeysym(key_name);
837 KeyCode kc = XKeysymToKeycode(dpy, ks);
841 XQueryKeymap(dpy, (char *)keymap);
842 if( keymap[kc >> 3] & masktable[kc & 7] )
843 status = UTILX_KEY_STATUS_PRESSED;
845 status = UTILX_KEY_STATUS_RELEASED;
852 API void utilx_set_window_effect_state(Display* dpy, Window win, int state)
856 fprintf (stderr, "[UTILX] Error.. Invald Display.. %s (%d)\n", __func__, __LINE__);
859 if( !_atom_comp_effect_state)
860 _atom_comp_effect_state = XInternAtom(dpy, "_NET_CM_WINDOW_EFFECT_ENABLE",False);
862 _utilx_set_window_property(dpy, win, _atom_comp_effect_state, XA_CARDINAL, (unsigned int *)&state, 1);
867 API int utilx_get_window_effect_state(Display* dpy, Window win)
870 if( !_atom_comp_effect_state)
871 _atom_comp_effect_state = XInternAtom(dpy, "_NET_CM_WINDOW_EFFECT_ENABLE",False);
872 _utilx_get_window_property(dpy, win, _atom_comp_effect_state, XA_CARDINAL, (unsigned int *)&state, 1);
877 _utilx_string_set_window_property( Display *dpy, Window win, Atom atom, char *val, unsigned int num)
879 XChangeProperty( dpy, win, atom, XA_STRING, 8, PropModeReplace, (unsigned char*)val, val ? strlen(val):0 );
882 API void utilx_set_fake_launch_img(Display* dpy, Window win, char *file_name)
884 //UTILX_TRACE ("[UTILX] utilx_set_effect_state... win = %x, show_state = %d\n", win, enable);
888 fprintf (stderr, "[UTILX] Error.. Invald Display.. %s (%d)\n", __func__, __LINE__);
891 if( !_atom_comp_fake_launch_image)
892 _atom_comp_fake_launch_image = XInternAtom(dpy, "_E_COMP_FAKE_LAUNCH_IMAGE",False);
894 _utilx_string_set_window_property(dpy, win, _atom_comp_fake_launch_image, file_name, 1);
897 API void utilx_show_fake_effect( Display *dpy, Window win, char *fake_image_file )
901 _atom_comp_fake_launch = XInternAtom( dpy, "_E_COMP_FAKE_LAUNCH", False );
903 if( !_atom_comp_fake_launch )
905 fprintf( stderr, "XInternAtom(_E_COMP_FAKE_LAUNCH) failed.\n" );
909 utilx_set_fake_launch_img(dpy, win, fake_image_file);
913 // send fake client message
914 xev.xclient.type = ClientMessage;
915 xev.xclient.display = dpy;
916 xev.xclient.window = win;
917 xev.xclient.message_type = _atom_comp_fake_launch;
918 xev.xclient.format = 32;
919 xev.xclient.data.l[0] = 1; // 1 : start effect , 0 : end effect
920 xev.xclient.data.l[1] = 0;
921 xev.xclient.data.l[2] = 0;
922 xev.xclient.data.l[3] = 0;
923 xev.xclient.data.l[4] = 0;
925 XSendEvent( dpy, win, False,
926 SubstructureRedirectMask | SubstructureNotifyMask,
933 API void utilx_hide_fake_effect( Display *dpy, Window win)
937 _atom_comp_fake_launch = XInternAtom( dpy, "_E_COMP_FAKE_LAUNCH", False );
938 if( !_atom_comp_fake_launch )
940 fprintf( stderr, "XInternAtom(_E_COMP_FAKE_LAUNCH) failed.\n" );
944 // send fake client message
945 xev.xclient.type = ClientMessage;
946 xev.xclient.display = dpy;
947 xev.xclient.window = win;
948 xev.xclient.message_type = _atom_comp_fake_launch;
949 xev.xclient.format = 32;
950 xev.xclient.data.l[0] = 0; // 1 : start effect , 0 : end effect
951 xev.xclient.data.l[1] = 0;
952 xev.xclient.data.l[2] = 0;
953 xev.xclient.data.l[3] = 0;
954 xev.xclient.data.l[4] = 0;
956 XSendEvent( dpy, win, False,
957 SubstructureRedirectMask | SubstructureNotifyMask,
962 static void _utilx_effect_atom_check( Display* dpy )
966 fprintf (stderr, "[UTILX] Error.. Invald Display.. %s (%d)\n", __func__, __LINE__);
970 if( !_atom_comp_window_effect_type)
971 _atom_comp_window_effect_type = XInternAtom(dpy, "_NET_CM_WINDOW_EFFECT_TYPE",False);
972 if( !_atom_comp_effect_default )
973 _atom_comp_effect_default = XInternAtom(dpy, "_NET_CM_EFFECT_DEFAULT",False);
974 if( !_atom_comp_effect_none )
975 _atom_comp_effect_none = XInternAtom(dpy, "_NET_CM_EFFECT_NONE",False);
976 if( !_atom_comp_effect_custom0 )
977 _atom_comp_effect_custom0 = XInternAtom(dpy, "_NET_CM_EFFECT_CUSTOM0",False);
978 if( !_atom_comp_effect_custom1 )
979 _atom_comp_effect_custom1 = XInternAtom(dpy, "_NET_CM_EFFECT_CUSTOM1",False);
980 if( !_atom_comp_effect_custom2 )
981 _atom_comp_effect_custom2 = XInternAtom(dpy, "_NET_CM_EFFECT_CUSTOM2",False);
982 if( !_atom_comp_effect_custom3 )
983 _atom_comp_effect_custom3 = XInternAtom(dpy, "_NET_CM_EFFECT_CUSTOM3",False);
984 if( !_atom_comp_effect_custom4 )
985 _atom_comp_effect_custom4 = XInternAtom(dpy, "_NET_CM_EFFECT_CUSTOM4",False);
986 if( !_atom_comp_effect_custom5 )
987 _atom_comp_effect_custom5 = XInternAtom(dpy, "_NET_CM_EFFECT_CUSTOM5",False);
988 if( !_atom_comp_effect_custom6 )
989 _atom_comp_effect_custom6 = XInternAtom(dpy, "_NET_CM_EFFECT_CUSTOM6",False);
990 if( !_atom_comp_effect_custom7 )
991 _atom_comp_effect_custom7 = XInternAtom(dpy, "_NET_CM_EFFECT_CUSTOM7",False);
992 if( !_atom_comp_effect_custom8 )
993 _atom_comp_effect_custom8 = XInternAtom(dpy, "_NET_CM_EFFECT_CUSTOM8",False);
994 if( !_atom_comp_effect_custom9 )
995 _atom_comp_effect_custom9 = XInternAtom(dpy, "_NET_CM_EFFECT_CUSTOM9",False);
998 static Atom _utilx_convert_style_to_atom( Display* dpy, Utilx_Effect_Style style )
1002 fprintf (stderr, "[UTILX] Error.. Invald Display.. %s (%d)\n", __func__, __LINE__);
1003 return _atom_comp_effect_none;
1005 _utilx_effect_atom_check(dpy);
1007 if ( style == UTILX_EFFECT_STYLE_DEFAULT ) return _atom_comp_effect_default;
1008 else if ( style == UTILX_EFFECT_STYLE_NONE ) return _atom_comp_effect_none;
1009 else if ( style == UTILX_EFFECT_STYLE_CUSTOM0 ) return _atom_comp_effect_custom0;
1010 else if ( style == UTILX_EFFECT_STYLE_CUSTOM1 ) return _atom_comp_effect_custom1;
1011 else if ( style == UTILX_EFFECT_STYLE_CUSTOM2 ) return _atom_comp_effect_custom2;
1012 else if ( style == UTILX_EFFECT_STYLE_CUSTOM3 ) return _atom_comp_effect_custom3;
1013 else if ( style == UTILX_EFFECT_STYLE_CUSTOM4 ) return _atom_comp_effect_custom4;
1014 else if ( style == UTILX_EFFECT_STYLE_CUSTOM5 ) return _atom_comp_effect_custom5;
1015 else if ( style == UTILX_EFFECT_STYLE_CUSTOM6 ) return _atom_comp_effect_custom6;
1016 else if ( style == UTILX_EFFECT_STYLE_CUSTOM7 ) return _atom_comp_effect_custom7;
1017 else if ( style == UTILX_EFFECT_STYLE_CUSTOM8 ) return _atom_comp_effect_custom8;
1018 else if ( style == UTILX_EFFECT_STYLE_CUSTOM9 ) return _atom_comp_effect_custom9;
1019 else return _atom_comp_effect_none;
1023 static Utilx_Effect_Style _utilx_convert_atom_to_style( Display* dpy, Atom style )
1027 fprintf (stderr, "[UTILX] Error.. Invald Display.. %s (%d)\n", __func__, __LINE__);
1028 return UTILX_EFFECT_STYLE_NONE;
1030 _utilx_effect_atom_check(dpy);
1032 if ( style == _atom_comp_effect_default ) return UTILX_EFFECT_STYLE_DEFAULT;
1033 else if ( style == _atom_comp_effect_none ) return UTILX_EFFECT_STYLE_NONE;
1034 else if ( style == _atom_comp_effect_custom0 ) return UTILX_EFFECT_STYLE_CUSTOM0;
1035 else if ( style == _atom_comp_effect_custom1 ) return UTILX_EFFECT_STYLE_CUSTOM1;
1036 else if ( style == _atom_comp_effect_custom2 ) return UTILX_EFFECT_STYLE_CUSTOM2;
1037 else if ( style == _atom_comp_effect_custom3 ) return UTILX_EFFECT_STYLE_CUSTOM3;
1038 else if ( style == _atom_comp_effect_custom4 ) return UTILX_EFFECT_STYLE_CUSTOM4;
1039 else if ( style == _atom_comp_effect_custom5 ) return UTILX_EFFECT_STYLE_CUSTOM5;
1040 else if ( style == _atom_comp_effect_custom6 ) return UTILX_EFFECT_STYLE_CUSTOM6;
1041 else if ( style == _atom_comp_effect_custom7 ) return UTILX_EFFECT_STYLE_CUSTOM7;
1042 else if ( style == _atom_comp_effect_custom8 ) return UTILX_EFFECT_STYLE_CUSTOM8;
1043 else if ( style == _atom_comp_effect_custom9 ) return UTILX_EFFECT_STYLE_CUSTOM9;
1044 else return UTILX_EFFECT_STYLE_DEFAULT;
1047 API void utilx_set_window_effect_style(Display* dpy, Window win, Utilx_Effect_Type type, Utilx_Effect_Style style)
1049 Atom *window_effect_type_list = NULL;
1052 fprintf (stderr, "[UTILX] Error.. Invald Display.. %s (%d)\n", __func__, __LINE__);
1055 _utilx_effect_atom_check(dpy);
1057 window_effect_type_list = (Atom *)malloc(sizeof(Atom) * 6);
1059 if ( !window_effect_type_list )
1061 fprintf (stderr, "[UTILX] Error.. malloc().. %s (%d)\n", __func__, __LINE__);
1065 window_effect_type_list[0] = _atom_comp_effect_default;
1066 window_effect_type_list[1] = _atom_comp_effect_default;
1067 window_effect_type_list[2] = _atom_comp_effect_default;
1068 window_effect_type_list[3] = _atom_comp_effect_default;
1069 window_effect_type_list[4] = _atom_comp_effect_default;
1070 window_effect_type_list[5] = _atom_comp_effect_default;
1072 _utilx_get_window_property(dpy, win, _atom_comp_window_effect_type, XA_ATOM, (unsigned int *)window_effect_type_list, 6);
1074 if ( type == UTILX_EFFECT_TYPE_MAP ) window_effect_type_list[0] = _utilx_convert_style_to_atom(dpy, style);
1075 else if ( type == UTILX_EFFECT_TYPE_UNMAP ) window_effect_type_list[1] = _utilx_convert_style_to_atom(dpy, style);
1076 else if ( type == UTILX_EFFECT_TYPE_RAISEABOVE ) window_effect_type_list[2] = _utilx_convert_style_to_atom(dpy, style);
1077 else if ( type == UTILX_EFFECT_TYPE_ROTATION ) window_effect_type_list[3] = _utilx_convert_style_to_atom(dpy, style);
1078 else if ( type == UTILX_EFFECT_TYPE_FOCUSIN ) window_effect_type_list[4] = _utilx_convert_style_to_atom(dpy, style);
1079 else if ( type == UTILX_EFFECT_TYPE_FOCUSOUT ) window_effect_type_list[5] = _utilx_convert_style_to_atom(dpy, style);
1081 _utilx_set_window_property(dpy, win, _atom_comp_window_effect_type, XA_ATOM, (unsigned int *)window_effect_type_list, 6);
1084 free(window_effect_type_list);
1087 API Utilx_Effect_Style utilx_get_window_effect_style(Display* dpy, Window win, Utilx_Effect_Type type)
1089 Atom *window_effect_type_list = NULL;
1090 Utilx_Effect_Style style = UTILX_EFFECT_STYLE_DEFAULT;
1094 fprintf (stderr, "[UTILX] Error.. Invald Display.. %s (%d)\n", __func__, __LINE__);
1095 return UTILX_EFFECT_STYLE_NONE;
1097 _utilx_effect_atom_check(dpy);
1099 window_effect_type_list = (Atom *)malloc(sizeof(Atom) * 6);
1101 if ( !window_effect_type_list )
1103 fprintf (stderr, "[UTILX] Error.. malloc().. %s (%d)\n", __func__, __LINE__);
1104 return UTILX_EFFECT_STYLE_NONE;
1107 if ( _utilx_get_window_property(dpy, win, _atom_comp_window_effect_type, XA_ATOM, (unsigned int *)window_effect_type_list, 6) != 6 )
1109 fprintf (stderr, "[UTILX] Error.. get property failed!.. %s (%d)\n", __func__, __LINE__);
1110 free(window_effect_type_list);
1111 return UTILX_EFFECT_STYLE_NONE;
1114 if ( type == UTILX_EFFECT_TYPE_MAP ) style = _utilx_convert_atom_to_style(dpy, window_effect_type_list[0]);
1115 else if ( type == UTILX_EFFECT_TYPE_UNMAP ) style = _utilx_convert_atom_to_style(dpy, window_effect_type_list[1]);
1116 else if ( type == UTILX_EFFECT_TYPE_RAISEABOVE ) style = _utilx_convert_atom_to_style(dpy, window_effect_type_list[2]);
1117 else if ( type == UTILX_EFFECT_TYPE_ROTATION ) style = _utilx_convert_atom_to_style(dpy, window_effect_type_list[3]);
1118 else if ( type == UTILX_EFFECT_TYPE_FOCUSIN ) style = _utilx_convert_atom_to_style(dpy, window_effect_type_list[4]);
1119 else if ( type == UTILX_EFFECT_TYPE_FOCUSOUT ) style = _utilx_convert_atom_to_style(dpy, window_effect_type_list[5]);
1122 free(window_effect_type_list);
1126 API int utilx_set_window_opaque_state (Display* dpy, Window win, Utilx_Opaque_State state)
1128 UTILX_TRACE ("[UTILX] utilx_set_window_opaque_state... win = %x, show_state = %d\n", win, state);
1130 unsigned int is_opaque;
1134 fprintf (stderr, "[UTILX] Error.. Invald Display.. %s (%d)\n", __func__, __LINE__);
1140 case UTILX_OPAQUE_STATE_OFF:
1144 case UTILX_OPAQUE_STATE_ON:
1149 fprintf (stderr, "[UTILX] Error.. Invald State.. %s (%d)\n", __func__, __LINE__);
1153 if (!_atom_window_opaque)
1155 _atom_window_opaque = XInternAtom (dpy, "_E_ILLUME_WINDOW_REGION_OPAQUE", False);
1156 if (!_atom_window_opaque)
1158 fprintf (stderr, "[UTILX] Error.. Cannot create _E_ILLUME_WINDOW_REGION_OPAQUE atom.. %s (%d)\n", __func__, __LINE__);
1163 _utilx_set_window_property (dpy, win, _atom_window_opaque, XA_CARDINAL,
1164 (unsigned int *)&is_opaque, 1);
1170 _utilx_screen_capture_atom_ensure (Display* dpy)
1172 if (_atom_screen_capture_disable)
1175 _atom_screen_capture_disable = XInternAtom (dpy, "_CB_SCREEN_CAPTURE_DISABLE", False);
1176 if (_atom_screen_capture_disable)
1179 fprintf (stderr, "[UTILX] Error.. Cannot create _CB_SCREEN_CAPTURE_DISABLE atom.. %s (%d)\n", __func__, __LINE__);
1183 utilx_set_screen_capture(Display* dpy, int enable)
1190 fprintf (stderr, "[UTILX] Error.. dpy is NULL %s (%d)\n", __func__, __LINE__);
1194 root = RootWindow (dpy, DefaultScreen(dpy));
1195 disable = (enable) ? 0 : 1;
1197 _utilx_screen_capture_atom_ensure (dpy);
1199 _utilx_set_window_property (dpy, root, _atom_screen_capture_disable, XA_CARDINAL, (unsigned int *)&disable, 1);
1205 utilx_get_screen_capture(Display* dpy)
1212 fprintf (stderr, "[UTILX] Error.. dpy is NULL %s (%d)\n", __func__, __LINE__);
1216 root = RootWindow (dpy, DefaultScreen(dpy));
1218 _utilx_screen_capture_atom_ensure (dpy);
1220 _utilx_get_window_property(dpy, root, _atom_screen_capture_disable, XA_CARDINAL,
1221 (unsigned int *)&disable, 1);
1223 return (disable) ? 0 : 1;
1226 API void utilx_set_window_cardinal_property(Display* dpy, Window win, Atom atom, unsigned int *value)
1228 _utilx_set_window_property(dpy, win, atom, XA_CARDINAL, value, 1);
1231 API int utilx_get_window_cardinal_property (Display* dpy, Window win, Atom atom, unsigned int *value)
1233 return _utilx_get_window_property(dpy, win, atom, XA_CARDINAL, value, 1);
1236 API void utilx_show_capture_effect( Display *dpy, Window win)
1240 _atom_comp_capture_effect = XInternAtom( dpy, "_E_COMP_CAPTURE_EFFECT", False );
1241 if( !_atom_comp_capture_effect )
1243 fprintf( stderr, "XInternAtom(_E_COMP_CAPTURE_EFFECT) failed.\n" );
1247 // send capture effect client message
1248 xev.xclient.type = ClientMessage;
1249 xev.xclient.display = dpy;
1250 xev.xclient.window = win;
1251 xev.xclient.message_type = _atom_comp_capture_effect;
1252 xev.xclient.format = 32;
1253 xev.xclient.data.l[0] = 0;
1254 xev.xclient.data.l[1] = 0;
1255 xev.xclient.data.l[2] = 0;
1256 xev.xclient.data.l[3] = 0;
1257 xev.xclient.data.l[4] = 0;
1259 XSendEvent( dpy, win, False,
1260 SubstructureRedirectMask | SubstructureNotifyMask,
1265 API UtilxScrnConf *utilx_scrnconf_allocate (void)
1267 UtilxScrnConf *scrnconf = calloc (1, sizeof(UtilxScrnConf));
1270 fprintf (stderr, "fail to allocate UtilxScrnConf\n");
1277 API void utilx_scrnconf_free (UtilxScrnConf *scrnconf)
1282 if (scrnconf->str_output)
1283 free (scrnconf->str_output);
1285 if (scrnconf->str_resolution)
1286 free (scrnconf->str_resolution);
1292 API int utilx_scrnconf_get_info (Display *dpy, UtilxScrnConf *scrnconf)
1294 Window win = DefaultRootWindow(dpy);
1295 Atom scrnconf_atom = None;
1304 scrnconf_atom = XInternAtom (dpy, "_SCRNCONF_INFO", False);
1307 if (XGetTextProperty (dpy, win, &xtp, scrnconf_atom))
1309 s = XmbTextPropertyToTextList (dpy, &xtp, &list, &items);
1310 if ((s == XLocaleNotSupported) ||
1311 (s == XNoMemory) || (s == XConverterNotFound))
1312 str = strdup((char *)xtp.value);
1313 else if ((s >= Success) && (items > 0))
1314 str = strdup(list[0]);
1317 XFreeStringList (list);
1322 ptr = strtok (str, ",");
1327 scrnconf->str_output = calloc (1, strlen(ptr));
1328 if (!scrnconf->str_output)
1331 strcpy (scrnconf->str_output, ptr);
1335 if (!strcmp(ptr, "CONNECT"))
1336 scrnconf->status = UTILX_SCRNCONF_STATUS_CONNECT;
1337 else if (!strcmp(ptr, "ACTIVE"))
1338 scrnconf->status = UTILX_SCRNCONF_STATUS_ACTIVE;
1340 scrnconf->status = UTILX_SCRNCONF_STATUS_NULL;
1344 scrnconf->str_resolution = calloc (1, strlen(ptr));
1345 if (!scrnconf->str_resolution)
1348 strcpy (scrnconf->str_resolution, ptr);
1352 if (!strcmp(ptr, "CLONE"))
1353 scrnconf->dispmode = UTILX_SCRNCONF_DISPMODE_CLONE;
1354 else if (!strcmp(ptr, "EXTENDED"))
1355 scrnconf->dispmode = UTILX_SCRNCONF_DISPMODE_EXTENDED;
1357 scrnconf->dispmode = UTILX_SCRNCONF_DISPMODE_NULL;
1362 ptr = strtok (NULL, ",");
1376 API int utilx_scrnconf_set_dispmode (Display *dpy, Utilx_Scrnconf_Dispmode dispmode)
1378 Window win = DefaultRootWindow(dpy);
1380 Atom scrnconf_atom = None;
1381 UtilxScrnConf *scrnconf = NULL;
1383 scrnconf = utilx_scrnconf_allocate ();
1387 if (!utilx_scrnconf_get_info (dpy, scrnconf))
1389 utilx_scrnconf_free (scrnconf);
1393 if (scrnconf->status == UTILX_SCRNCONF_STATUS_NULL)
1395 fprintf (stderr, "[utilx_scrnconf]: the status of screen configuration is null\n");
1396 utilx_scrnconf_free (scrnconf);
1400 if (scrnconf->dispmode == dispmode)
1402 fprintf (stderr, "[utilx_scrnconf]: dispmode (%d) already set\n", dispmode);
1403 utilx_scrnconf_free (scrnconf);
1407 utilx_scrnconf_free (scrnconf);
1409 scrnconf_atom = XInternAtom (dpy, "_SCRNCONF_DISPMODE_SET", False);
1411 xev.xclient.window = win;
1412 xev.xclient.type = ClientMessage;
1413 xev.xclient.message_type = scrnconf_atom;
1414 xev.xclient.format = 32;
1415 xev.xclient.data.s[0] = dispmode;
1417 XSendEvent(dpy, win, False, StructureNotifyMask, &xev);
1423 typedef struct _ShotInfo
1430 unsigned int height;
1440 drm_slp_bufmgr bufmgr;
1442 DRI2Buffer* dri2_buffers;
1448 XShmSegmentInfo shminfo;
1451 #define FOURCC(a,b,c,d) (((unsigned)d&0xff)<<24 | ((unsigned)c&0xff)<<16 | ((unsigned)b&0xff)<<8 | ((unsigned)a&0xff))
1453 #define FOURCC_RGB32 FOURCC('R','G','B','4')
1455 /* x error handling */
1456 static Bool x_error_caught;
1458 static ShotInfo *shot_info;
1461 _get_port (Display *dpy, unsigned int id)
1463 unsigned int ver, rev, req_base, evt_base, err_base;
1464 unsigned int adaptors;
1465 XvAdaptorInfo *ai = NULL;
1466 XvImageFormatValues *fo = NULL;
1470 if (XvQueryExtension (dpy, &ver, &rev, &req_base, &evt_base, &err_base) != Success)
1472 fprintf (stderr, "[UTILX] no XV extension. \n");
1476 if (XvQueryAdaptors (dpy, DefaultRootWindow (dpy), &adaptors, &ai) != Success)
1478 fprintf (stderr, "[UTILX] fail : query adaptors. \n");
1484 fprintf (stderr, "[UTILX] fail : get adaptor info. \n");
1488 for (i = 0; i < adaptors; i++)
1490 int support_format = False;
1492 if (!(ai[i].type & XvStillMask))
1497 fo = XvListImageFormats (dpy, p, &formats);
1498 for (j = 0; j < formats; j++)
1499 if (fo[j].id == (int)id)
1500 support_format = True;
1505 if (!support_format)
1508 if (XvGrabPort (dpy, p, 0) == Success)
1510 XvFreeAdaptorInfo (ai);
1514 fprintf (stderr, "[UTILX] fail : grab port. \n");
1517 XvFreeAdaptorInfo (ai);
1523 _deinit_screen_shot (ShotInfo *info)
1525 static Atom atom_stream_off = None;
1530 if (atom_stream_off == None)
1531 atom_stream_off = XInternAtom (info->dpy, "_USER_WM_PORT_ATTRIBUTE_STREAM_OFF", False);
1533 XvSetPortAttribute (info->dpy, info->port, atom_stream_off, 1);
1536 XDestroyImage (info->image);
1537 if (info->shminfo.shmid != -1)
1539 XShmDetach (info->dpy, &info->shminfo);
1540 shmdt (info->shminfo.shmaddr);
1541 shmctl (info->shminfo.shmid, IPC_RMID, 0);
1545 drm_slp_bo_unref(info->bo);
1546 if (info->dri2_buffers)
1547 free(info->dri2_buffers);
1549 drm_slp_bufmgr_destroy (info->bufmgr);
1550 if (info->drm_fd >= 0)
1551 close (info->drm_fd);
1554 XDamageDestroy (info->dpy, info->damage);
1557 XFreeGC (info->dpy, info->gc);
1558 if (info->pixmap > 0)
1559 XFreePixmap (info->dpy, info->pixmap);
1561 XvUngrabPort (info->dpy, info->port, 0);
1568 _screen_shot_x_error_handle (Display *dpy, XErrorEvent *ev)
1570 char error_msg[1024];
1572 if (!shot_info || (dpy != shot_info->dpy))
1575 XGetErrorText (dpy, ev->error_code, error_msg, 1024);
1576 fprintf (stderr, "[UTILX] get XError: %s\n", error_msg);
1578 x_error_caught = True;
1584 _init_screen_shot_damage (ShotInfo *info)
1586 int damage_err_base = 0;
1588 if (!XDamageQueryExtension(info->dpy, &info->damage_base, &damage_err_base))
1590 fprintf (stderr, "[UTILX] no X Damage extension. \n");
1594 info->damage = XDamageCreate (info->dpy, info->pixmap, XDamageReportNonEmpty);
1595 if (info->damage <= 0)
1597 fprintf (stderr, "[UTILX] fail : create damage \n");
1605 _init_screen_shot_dri2 (ShotInfo *info)
1609 int dri2_err_base = 0;
1610 int dri2Major, dri2Minor;
1611 char *driverName = NULL, *deviceName = NULL;
1612 unsigned int attachments[1];
1613 int dri2_count, dri2_out_count;
1614 int dri2_width, dri2_height, dri2_stride;
1617 screen = DefaultScreen(info->dpy);
1618 if (!DRI2QueryExtension (info->dpy, &dri2_base, &dri2_err_base))
1620 fprintf (stderr, "[UTILX] no DRI2 extension. !!\n");
1621 goto fail_init_dri2;
1624 if (!DRI2QueryVersion (info->dpy, &dri2Major, &dri2Minor))
1626 fprintf (stderr, "[UTILX] fail : DRI2QueryVersion !!\n");
1627 goto fail_init_dri2;
1630 if (!DRI2Connect (info->dpy, RootWindow(info->dpy, screen), &driverName, &deviceName))
1632 fprintf (stderr, "[UTILX] fail : DRI2Connect !!\n");
1633 goto fail_init_dri2;
1637 info->drm_fd = open (deviceName, O_RDWR);
1638 if (info->drm_fd < 0)
1640 fprintf (stderr, "[UTILX] fail : open drm device (%s)\n", deviceName);
1641 goto fail_init_dri2;
1644 /* get the drm magic */
1645 drmGetMagic(info->drm_fd, &magic);
1646 if (!DRI2Authenticate(info->dpy, RootWindow(info->dpy, screen), magic))
1648 fprintf (stderr, "[UTILX] fail : DRI2Authenticate (%d)\n", magic);
1649 goto fail_init_dri2;
1653 info->bufmgr = drm_slp_bufmgr_init (info->drm_fd, NULL);
1656 fprintf (stderr, "[UTILX] fail : init buffer manager \n");
1657 goto fail_init_dri2;
1660 DRI2CreateDrawable (info->dpy, info->pixmap);
1662 attachments[0] = DRI2BufferFrontLeft;
1664 info->dri2_buffers = DRI2GetBuffers (info->dpy, info->pixmap, &dri2_width, &dri2_height,
1665 attachments, dri2_count, &dri2_out_count);
1667 if (!info->dri2_buffers)
1669 fprintf (stderr, "[UTILX] fail : get buffers\n");
1670 goto fail_init_dri2;
1673 if (!info->dri2_buffers[0].name)
1675 fprintf (stderr, "[UTILX] fail : a handle of the dri2 buffer is null \n ");
1676 goto fail_init_dri2;
1679 info->bo = drm_slp_bo_import (info->bufmgr, info->dri2_buffers[0].name);
1682 fprintf (stderr, "[UTILX] fail : import bo (key:%d)\n", info->dri2_buffers[0].name);
1683 goto fail_init_dri2;
1686 dri2_stride = info->dri2_buffers[0].pitch;
1689 info->virtual = (void*)drm_slp_bo_get_handle (info->bo, DRM_SLP_DEVICE_CPU);
1692 fprintf (stderr, "[UTILX] fail : map \n");
1693 goto fail_init_dri2;
1696 info->enable_xshm = False;
1703 drm_slp_bo_unref(info->bo);
1704 if (info->dri2_buffers)
1705 free(info->dri2_buffers);
1707 drm_slp_bufmgr_destroy (info->bufmgr);
1708 if (info->drm_fd >= 0)
1709 close (info->drm_fd);
1715 _init_screen_shot_shm (ShotInfo *info)
1717 if (!XShmQueryExtension (info->dpy))
1719 fprintf (stderr, "[UTILX] no XShm extension. !!\n");
1723 info->image = XShmCreateImage (info->dpy,
1724 DefaultVisual (info->dpy, DefaultScreen (info->dpy)),
1725 DefaultDepth (info->dpy, DefaultScreen (info->dpy)),
1733 fprintf (stderr, "[UTILX] fail : XShmCreateImage \n");
1737 info->shminfo.shmid = shmget (IPC_PRIVATE, info->image->bytes_per_line * info->height, IPC_CREAT | 0777);
1738 if (info->shminfo.shmid == -1)
1740 XDestroyImage (info->image);
1741 fprintf (stderr, "[UTILX] fail : shmget\n");
1745 info->shminfo.shmaddr = shmat (info->shminfo.shmid, 0, 0);
1746 if (info->shminfo.shmaddr == (void *) -1)
1748 XDestroyImage (info->image);
1749 shmctl (info->shminfo.shmid, IPC_RMID, 0);
1750 info->shminfo.shmid = -1;
1751 fprintf (stderr, "[UTILX] fail : shmat\n");
1755 info->shminfo.readOnly = False;
1757 if (!XShmAttach (info->dpy, &info->shminfo))
1759 XDestroyImage (info->image);
1760 shmdt (info->shminfo.shmaddr);
1761 shmctl (info->shminfo.shmid, IPC_RMID, 0);
1762 info->shminfo.shmid = -1;
1763 fprintf (stderr, "[UTILX] fail : XShmAttach\n");
1767 info->image->data = info->shminfo.shmaddr;
1768 info->virtual = info->shminfo.shmaddr;
1770 info->enable_xshm = True;
1776 _init_screen_shot (Display* dpy, unsigned int width, unsigned int height)
1778 ShotInfo *info = NULL;
1779 static Atom atom_capture = None;
1783 if (shot_info->width == width && shot_info->height == height)
1786 _deinit_screen_shot (shot_info);
1789 info = calloc (1, sizeof (ShotInfo));
1797 info->shminfo.shmid = -1;
1798 info->shminfo.shmaddr = (void*)-1;
1802 info->port = _get_port (info->dpy, FOURCC_RGB32);
1803 if (info->port <= 0)
1807 if (atom_capture == None)
1808 atom_capture = XInternAtom (info->dpy, "_USER_WM_PORT_ATTRIBUTE_CAPTURE", False);
1810 XvSetPortAttribute (info->dpy, info->port, atom_capture, 1);
1811 XvQueryBestSize (info->dpy, info->port, 0, 0, 0, width, height, &width, &height);
1812 if (width <= 0 || height <= 0)
1814 info->width = width;
1815 info->height = height;
1818 info->pixmap = XCreatePixmap (info->dpy,
1819 DefaultRootWindow (info->dpy),
1821 DefaultDepth (info->dpy, DefaultScreen (info->dpy)));
1822 if (info->pixmap <= 0)
1824 fprintf (stderr, "[UTILX] fail : create pixmap. \n");
1829 info->gc = XCreateGC (info->dpy, info->pixmap, 0, 0);
1832 fprintf (stderr, "[UTILX] fail : create gc. \n");
1836 XSetForeground (info->dpy, info->gc, 0xFF000000);
1837 XFillRectangle (info->dpy, info->pixmap, info->gc, 0, 0, width, height);
1839 if (!_init_screen_shot_damage (info))
1842 if (!_init_screen_shot_dri2 (info))
1844 if (!_init_screen_shot_shm (info))
1847 fprintf (stderr, "[UTILX] XShm success. !!\n");
1852 fprintf (stderr, "[UTILX] fail : get virtual \n");
1861 _deinit_screen_shot (info);
1866 utilx_create_screen_shot (Display* dpy, int width, int height)
1870 XErrorHandler old_handler = NULL;
1874 fprintf (stderr, "[UTILX] invalid display(%p) \n", dpy);
1878 if (width <= 0 || height <= 0)
1880 fprintf (stderr, "[UTILX] invalid size(%dx%d) \n", width, height);
1886 x_error_caught = False;
1887 old_handler = XSetErrorHandler (_screen_shot_x_error_handle);
1889 info = _init_screen_shot (dpy, width, height);
1893 fprintf (stderr, "[UTILX] fail : initialize screenshot. \n");
1897 XvGetStill (info->dpy, info->port, info->pixmap, info->gc,
1898 0, 0, info->width, info->height,
1899 0, 0, info->width, info->height);
1905 x_error_caught = False;
1906 XSetErrorHandler (old_handler);
1907 fprintf (stderr, "[UTILX] fail : GetStill. \n");
1911 x_error_caught = False;
1912 XSetErrorHandler (old_handler);
1914 XNextEvent (info->dpy, &ev); /* wating for x event */
1916 if (ev.type == (info->damage_base + XDamageNotify))
1918 XDamageNotifyEvent *damage_ev = (XDamageNotifyEvent *)&ev;
1919 if (damage_ev->drawable == info->pixmap)
1921 if (info->enable_xshm)
1922 XShmGetImage (info->dpy, info->pixmap, info->image, 0, 0, AllPlanes);
1924 XDamageSubtract (info->dpy, info->damage, None, None );
1925 return info->virtual;
1928 XDamageSubtract (info->dpy, info->damage, None, None );
1931 utilx_release_screen_shot ();
1937 utilx_release_screen_shot (void)
1939 _deinit_screen_shot (shot_info);
1942 #define XRR_PROPERTY_FB_VISIBLE "XRR_PROPERTY_FB_VISIBLE"
1943 #define XRR_PROPERTY_VIDEO_OFFSET "XRR_PROPERTY_VIDEO_OFFSET"
1946 _utilx_xrr_set_property (Display* dpy, Atom atom, unsigned char *buf, int buf_len, unsigned char **get)
1949 XRRScreenResources *res = NULL;
1950 RROutput rr_output = None;
1953 root = XRootWindow (dpy, 0);
1956 fprintf (stderr, "[UTILX] Warning : Root window is None.. %s (%d)\n", __func__, __LINE__);
1960 res = XRRGetScreenResources (dpy, root);
1961 if (res == NULL || res->noutput == 0)
1963 fprintf (stderr, "[UTILX] Warning : ScreenResources is None.. %s (%d)\n", __func__, __LINE__);
1967 for (i = 0; i < res->noutput; i++)
1969 XRROutputInfo *output_info = XRRGetOutputInfo (dpy, res, res->outputs[i]);
1972 if (!strcmp (output_info->name, "LVDS1"))
1974 rr_output = res->outputs[i];
1975 XRRFreeOutputInfo(output_info);
1978 XRRFreeOutputInfo(output_info);
1982 if (rr_output == None)
1984 fprintf (stderr, "[UTILX] Warning : output is None.. %s (%d)\n", __func__, __LINE__);
1985 XRRFreeScreenResources (res);
1989 XRRChangeOutputProperty (dpy, rr_output, atom,
1990 XA_CARDINAL, 8, PropModeReplace, buf, buf_len);
1996 unsigned long nitems, bytes_after;
1998 XRRGetOutputProperty (dpy, rr_output, atom,
2000 True, False, XA_CARDINAL,
2001 &actual_type, &actual_format,
2002 &nitems, &bytes_after,
2008 XRRFreeScreenResources (res);
2014 utilx_set_fb_visible (Display* dpy, Utilx_Fb_Type fb, Bool visible)
2016 static Atom property = None;
2017 char buf[8192] = {0,};
2023 fprintf (stderr, "[UTILX] invalid display(%p).. %s (%d)\n", dpy, __func__, __LINE__);
2027 if (fb <= UTILX_FB_TYPE_NONE || fb > UTILX_FB_TYPE_OVERLAY)
2029 fprintf (stderr, "[UTILX] Error.. Invald fb(%d).. %s (%d)\n", fb, __func__, __LINE__);
2033 p += sprintf (p, "%d:", 0);
2034 p += sprintf (p, "%d", fb + 2);
2035 p += sprintf (p, ":%d", (visible > 0)? 1 : 0);
2042 if (property == None)
2043 property = XInternAtom (dpy, XRR_PROPERTY_FB_VISIBLE, False);
2045 if (property == None)
2047 fprintf (stderr, "[UTILX] Warning : FB_VISIBLE property is None.. %s (%d)\n", __func__, __LINE__);
2051 if (!_utilx_xrr_set_property (dpy, property, (unsigned char*)buf, buf_len, NULL))
2053 fprintf (stderr, "[UTILX] Warning : set_property failed.. %s (%d)\n", __func__, __LINE__);
2059 utilx_get_fb_visible (Display* dpy, Utilx_Fb_Type fb)
2061 static Atom property = None;
2062 char buf[32] = {0,};
2065 unsigned char *prop = NULL;
2066 Bool visible = False;
2070 fprintf (stderr, "[UTILX] invalid display(%p).. %s (%d)\n", dpy, __func__, __LINE__);
2074 if (fb <= UTILX_FB_TYPE_NONE || fb > UTILX_FB_TYPE_OVERLAY)
2076 fprintf (stderr, "[UTILX] Error.. Invald fb(%d).. %s (%d)\n", fb, __func__, __LINE__);
2080 p += sprintf (p, "%d:", 0);
2081 p += sprintf (p, "%d", fb + 2);
2088 if (property == None)
2089 property = XInternAtom (dpy, XRR_PROPERTY_FB_VISIBLE, False);
2091 if (property == None)
2093 fprintf (stderr, "[UTILX] Warning : FB_VISIBLE property is None.. %s (%d)\n", __func__, __LINE__);
2097 if (!_utilx_xrr_set_property (dpy, property, (unsigned char*)buf, buf_len, &prop))
2099 fprintf (stderr, "[UTILX] Warning : set_property failed.. %s (%d)\n", __func__, __LINE__);
2104 visible = atoi((char*)prop);
2110 utilx_set_video_offset (Display* dpy, int x, int y)
2112 static Atom property = None;
2113 char buf[32] = {0,};
2119 fprintf (stderr, "[UTILX] invalid display(%p).. %s (%d)\n", dpy, __func__, __LINE__);
2123 p += sprintf (p, "%d,%d", x, y);
2130 if (property == None)
2131 property = XInternAtom (dpy, XRR_PROPERTY_VIDEO_OFFSET, False);
2133 if (property == None)
2135 fprintf (stderr, "[UTILX] Warning : VIDEO_OFFSET property is None.. %s (%d)\n", __func__, __LINE__);
2139 if (!_utilx_xrr_set_property (dpy, property, (unsigned char*)buf, buf_len, NULL))
2141 fprintf (stderr, "[UTILX] Warning : set_property failed.. %s (%d)\n", __func__, __LINE__);