testdisplay: Destroy the cairo context once the fb is painted
[platform/upstream/intel-gpu-tools.git] / tests / testdisplay.c
1 /*
2  * Copyright 2010 Intel Corporation
3  *   Jesse Barnes <jesse.barnes@intel.com>
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in
13  * all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21  * IN THE SOFTWARE.
22  */
23
24 /*
25  * This program is intended for testing of display functionality.  It should
26  * allow for testing of
27  *   - hotplug
28  *   - mode setting
29  *   - clone & twin modes
30  *   - panel fitting
31  *   - test patterns & pixel generators
32  * Additional programs can test the detected outputs against VBT provided
33  * device lists (both docked & undocked).
34  *
35  * TODO:
36  * - pixel generator in transcoder
37  * - test pattern reg in pipe
38  * - test patterns on outputs (e.g. TV)
39  * - handle hotplug (leaks crtcs, can't handle clones)
40  * - allow mode force
41  * - expose output specific controls
42  *  - e.g. DDC-CI brightness
43  *  - HDMI controls
44  *  - panel brightness
45  *  - DP commands (e.g. poweroff)
46  * - verify outputs against VBT/physical connectors
47  */
48 #ifdef HAVE_CONFIG_H
49 #include "config.h"
50 #endif
51
52 #include <cairo.h>
53 #include <errno.h>
54 #include <math.h>
55 #include <stdint.h>
56 #include <stdbool.h>
57 #include <strings.h>
58 #include <unistd.h>
59 #include <termios.h>
60 #include <sys/poll.h>
61 #include <sys/time.h>
62 #include <sys/ioctl.h>
63 #include <sys/types.h>
64 #include <sys/stat.h>
65
66 #include "ioctl_wrappers.h"
67 #include "drmtest.h"
68 #include "testdisplay.h"
69 #include "igt_kms.h"
70
71 #include <stdlib.h>
72 #include <signal.h>
73
74 static int tio_fd;
75 struct termios saved_tio;
76
77 drmModeRes *resources;
78 int drm_fd, modes;
79 int test_all_modes = 0, test_preferred_mode = 0, force_mode = 0, test_plane,
80     test_stereo_modes, enable_tiling;
81 int sleep_between_modes = 5;
82 int do_dpms = 0; /* This aliases to DPMS_ON */
83 uint32_t depth = 24, stride, bpp;
84 int qr_code = 0;
85 int specified_mode_num = -1, specified_disp_id = -1;
86
87 drmModeModeInfo force_timing;
88
89 int crtc_x, crtc_y, crtc_w, crtc_h, width, height;
90 unsigned int plane_fb_id;
91 unsigned int plane_crtc_id;
92 unsigned int plane_id;
93 int plane_width, plane_height;
94 static const uint32_t SPRITE_COLOR_KEY = 0x00aaaaaa;
95
96 /*
97  * Mode setting with the kernel interfaces is a bit of a chore.
98  * First you have to find the connector in question and make sure the
99  * requested mode is available.
100  * Then you need to find the encoder attached to that connector so you
101  * can bind it with a free crtc.
102  */
103 struct connector {
104         uint32_t id;
105         int mode_valid;
106         drmModeModeInfo mode;
107         drmModeEncoder *encoder;
108         drmModeConnector *connector;
109         int crtc;
110         int crtc_idx;
111         int pipe;
112 };
113
114 static void dump_connectors_fd(int drmfd)
115 {
116         int i, j;
117
118         drmModeRes *mode_resources = drmModeGetResources(drmfd);
119
120         if (!mode_resources) {
121                 igt_warn("drmModeGetResources failed: %s\n", strerror(errno));
122                 return;
123         }
124
125         igt_info("Connectors:\n");
126         igt_info("id\tencoder\tstatus\t\ttype\tsize (mm)\tmodes\n");
127         for (i = 0; i < mode_resources->count_connectors; i++) {
128                 drmModeConnector *connector;
129
130                 connector = drmModeGetConnector(drmfd, mode_resources->connectors[i]);
131                 if (!connector) {
132                         igt_warn("could not get connector %i: %s\n", mode_resources->connectors[i], strerror(errno));
133                         continue;
134                 }
135
136                 igt_info("%d\t%d\t%s\t%s\t%dx%d\t\t%d\n", connector->connector_id, connector->encoder_id, kmstest_connector_status_str(connector->connection), kmstest_connector_type_str(connector->connector_type), connector->mmWidth, connector->mmHeight, connector->count_modes);
137
138                 if (!connector->count_modes)
139                         continue;
140
141                 igt_info("  modes:\n");
142                 igt_info("  name refresh (Hz) hdisp hss hse htot vdisp ""vss vse vtot flags type clock\n");
143                 for (j = 0; j < connector->count_modes; j++){
144                         igt_info("[%d]", j);
145                         kmstest_dump_mode(&connector->modes[j]);
146                 }
147
148                 drmModeFreeConnector(connector);
149         }
150         igt_info("\n");
151
152         drmModeFreeResources(mode_resources);
153 }
154
155 static void dump_crtcs_fd(int drmfd)
156 {
157         int i;
158         drmModeRes *mode_resources = drmModeGetResources(drmfd);
159
160         igt_info("CRTCs:\n");
161         igt_info("id\tfb\tpos\tsize\n");
162         for (i = 0; i < mode_resources->count_crtcs; i++) {
163                 drmModeCrtc *crtc;
164
165                 crtc = drmModeGetCrtc(drmfd, mode_resources->crtcs[i]);
166                 if (!crtc) {
167                         igt_warn("could not get crtc %i: %s\n", mode_resources->crtcs[i], strerror(errno));
168                         continue;
169                 }
170                 igt_info("%d\t%d\t(%d,%d)\t(%dx%d)\n", crtc->crtc_id, crtc->buffer_id, crtc->x, crtc->y, crtc->width, crtc->height);
171                 kmstest_dump_mode(&crtc->mode);
172
173                 drmModeFreeCrtc(crtc);
174         }
175         igt_info("\n");
176
177         drmModeFreeResources(mode_resources);
178 }
179
180 static void dump_info(void)
181 {
182         dump_connectors_fd(drm_fd);
183         dump_crtcs_fd(drm_fd);
184 }
185
186 static void connector_find_preferred_mode(uint32_t connector_id,
187                                           unsigned long crtc_idx_mask,
188                                           int mode_num, struct connector *c)
189 {
190         struct kmstest_connector_config config;
191
192         if (kmstest_get_connector_config(drm_fd, connector_id, crtc_idx_mask,
193                                          &config) < 0) {
194                 c->mode_valid = 0;
195                 return;
196         }
197
198         c->connector = config.connector;
199         c->encoder = config.encoder;
200         c->crtc = config.crtc->crtc_id;
201         c->crtc_idx = config.crtc_idx;
202         c->pipe = config.pipe;
203
204         if (mode_num != -1) {
205                 igt_assert(mode_num < config.connector->count_modes);
206                 c->mode = config.connector->modes[mode_num];
207         } else {
208                 c->mode = config.default_mode;
209         }
210         c->mode_valid = 1;
211 }
212
213 static void
214 paint_color_key(struct igt_fb *fb_info)
215 {
216         int i, j;
217         uint32_t *fb_ptr;
218
219         fb_ptr = gem_mmap(drm_fd, fb_info->gem_handle,
220                           fb_info->size, PROT_READ | PROT_WRITE);
221         igt_assert(fb_ptr);
222
223         for (i = crtc_y; i < crtc_y + crtc_h; i++)
224                 for (j = crtc_x; j < crtc_x + crtc_w; j++) {
225                         uint32_t offset;
226
227                         offset = (i * fb_info->stride / 4) + j;
228                         fb_ptr[offset] = SPRITE_COLOR_KEY;
229                 }
230
231         munmap(fb_ptr, fb_info->size);
232 }
233
234 static void paint_image(cairo_t *cr, const char *file)
235 {
236         int img_x, img_y, img_w, img_h, img_w_o, img_h_o;
237         double img_w_scale, img_h_scale;
238
239         cairo_surface_t *image;
240
241         img_y = height * (0.10 );
242         img_h = height * 0.08 * 4;
243         img_w = img_h;
244
245         img_x = (width / 2) - (img_w / 2);
246
247         image = cairo_image_surface_create_from_png(file);
248
249         img_w_o = cairo_image_surface_get_width(image);
250         img_h_o = cairo_image_surface_get_height(image);
251
252         cairo_translate(cr, img_x, img_y);
253
254         img_w_scale = (double)img_w / (double)img_w_o;
255         img_h_scale = (double)img_h / (double)img_h_o;
256         cairo_scale(cr, img_w_scale, img_h_scale);
257
258         cairo_set_source_surface(cr, image, 0, 0);
259         cairo_scale(cr, 1, 1);
260
261         cairo_paint(cr);
262         cairo_surface_destroy(image);
263 }
264
265 static void paint_output_info(struct connector *c, struct igt_fb *fb)
266 {
267         cairo_t *cr = igt_get_cairo_ctx(drm_fd, fb);
268         int l_width = fb->width;
269         int l_height = fb->height;
270         double str_width;
271         double x, y, top_y;
272         double max_width;
273         int i;
274
275         igt_paint_test_pattern(cr, l_width, l_height);
276
277         cairo_move_to(cr, l_width / 2, l_height / 2);
278
279         /* Print connector and mode name */
280         cairo_set_font_size(cr, 48);
281         igt_cairo_printf_line(cr, align_hcenter, 10, "%s",
282                  kmstest_connector_type_str(c->connector->connector_type));
283
284         cairo_set_font_size(cr, 36);
285         str_width = igt_cairo_printf_line(cr, align_hcenter, 10,
286                 "%s @ %dHz on %s encoder", c->mode.name, c->mode.vrefresh,
287                 kmstest_encoder_type_str(c->encoder->encoder_type));
288
289         cairo_rel_move_to(cr, -str_width / 2, 0);
290
291         /* List available modes */
292         cairo_set_font_size(cr, 18);
293         str_width = igt_cairo_printf_line(cr, align_left, 10,
294                                               "Available modes:");
295         cairo_rel_move_to(cr, str_width, 0);
296         cairo_get_current_point(cr, &x, &top_y);
297
298         max_width = 0;
299         for (i = 0; i < c->connector->count_modes; i++) {
300                 cairo_get_current_point(cr, &x, &y);
301                 if (y >= l_height) {
302                         x += max_width + 10;
303                         max_width = 0;
304                         cairo_move_to(cr, x, top_y);
305                 }
306                 str_width = igt_cairo_printf_line(cr, align_right, 10,
307                         "%s @ %dHz", c->connector->modes[i].name,
308                          c->connector->modes[i].vrefresh);
309                 if (str_width > max_width)
310                         max_width = str_width;
311         }
312
313         if (qr_code)
314                 paint_image(cr, IGT_DATADIR"/pass.png");
315
316         igt_assert(!cairo_status(cr));
317
318         cairo_destroy(cr);
319 }
320
321 static void sighandler(int signo)
322 {
323         return;
324 }
325
326 static void set_single(void)
327 {
328         int sigs[] = { SIGUSR1 };
329         struct sigaction sa;
330         sa.sa_handler = sighandler;
331
332         sigemptyset(&sa.sa_mask);
333
334         igt_warn_on_f(sigaction(sigs[0], &sa, NULL) == -1,
335                       "Could not set signal handler");
336 }
337
338 static void
339 set_mode(struct connector *c)
340 {
341         unsigned int fb_id = 0;
342         struct igt_fb fb_info[2] = { };
343         int j, test_mode_num, current_fb = 0, old_fb = -1;
344
345         test_mode_num = 1;
346         if (force_mode){
347                 memcpy( &c->mode, &force_timing, sizeof(force_timing));
348                 c->mode.vrefresh =(force_timing.clock*1e3)/(force_timing.htotal*force_timing.vtotal);
349                 c->mode_valid = 1;
350                 sprintf(c->mode.name, "%dx%d", force_timing.hdisplay, force_timing.vdisplay);
351         } else if (test_all_modes)
352                 test_mode_num = c->connector->count_modes;
353
354         for (j = 0; j < test_mode_num; j++) {
355
356                 if (test_all_modes)
357                         c->mode = c->connector->modes[j];
358
359                 /* set_mode() only tests 2D modes */
360                 if (c->mode.flags & DRM_MODE_FLAG_3D_MASK)
361                         continue;
362
363                 if (!c->mode_valid)
364                         continue;
365
366                 width = c->mode.hdisplay;
367                 height = c->mode.vdisplay;
368
369                 fb_id = igt_create_fb(drm_fd, width, height,
370                                           igt_bpp_depth_to_drm_format(bpp, depth),
371                                           enable_tiling, &fb_info[current_fb]);
372                 paint_output_info(c, &fb_info[current_fb]);
373                 paint_color_key(&fb_info[current_fb]);
374
375                 igt_info("CRTC(%u):[%d]", c->crtc, j);
376                 kmstest_dump_mode(&c->mode);
377                 if (drmModeSetCrtc(drm_fd, c->crtc, fb_id, 0, 0,
378                                    &c->id, 1, &c->mode)) {
379                         igt_warn("failed to set mode (%dx%d@%dHz): %s\n", width, height, c->mode.vrefresh, strerror(errno));
380                         continue;
381                 }
382
383                 if (old_fb != -1)
384                         igt_remove_fb(drm_fd, &fb_info[old_fb]);
385                 old_fb = current_fb;
386                 current_fb = 1 - current_fb;
387
388                 if (sleep_between_modes && test_all_modes && !qr_code)
389                         sleep(sleep_between_modes);
390
391                 if (do_dpms) {
392                         kmstest_set_connector_dpms(drm_fd, c->connector, do_dpms);
393                         sleep(sleep_between_modes);
394                         kmstest_set_connector_dpms(drm_fd, c->connector, DRM_MODE_DPMS_ON);
395                 }
396
397                 if (qr_code){
398                         set_single();
399                         pause();
400                 }
401         }
402
403         if (test_all_modes)
404                 igt_remove_fb(drm_fd, &fb_info[old_fb]);
405
406         drmModeFreeEncoder(c->encoder);
407         drmModeFreeConnector(c->connector);
408 }
409
410 struct box {
411         int x, y, width, height;
412 };
413
414 struct stereo_fb_layout {
415         int fb_width, fb_height;
416         struct box left, right;
417 };
418
419 static void box_init(struct box *box, int x, int y, int bwidth, int bheight)
420 {
421         box->x = x;
422         box->y = y;
423         box->width = bwidth;
424         box->height = bheight;
425 }
426
427 static void stereo_fb_layout_from_mode(struct stereo_fb_layout *layout,
428                                        drmModeModeInfo *mode)
429 {
430         unsigned int format = mode->flags & DRM_MODE_FLAG_3D_MASK;
431         const int hdisplay = mode->hdisplay, vdisplay = mode->vdisplay;
432         int middle;
433
434         switch (format) {
435         case DRM_MODE_FLAG_3D_TOP_AND_BOTTOM:
436                 layout->fb_width = hdisplay;
437                 layout->fb_height = vdisplay;
438
439                 middle = vdisplay / 2;
440                 box_init(&layout->left, 0, 0, hdisplay, middle);
441                 box_init(&layout->right,
442                          0, middle, hdisplay, vdisplay - middle);
443                 break;
444         case DRM_MODE_FLAG_3D_SIDE_BY_SIDE_HALF:
445                 layout->fb_width = hdisplay;
446                 layout->fb_height = vdisplay;
447
448                 middle = hdisplay / 2;
449                 box_init(&layout->left, 0, 0, middle, vdisplay);
450                 box_init(&layout->right,
451                          middle, 0, hdisplay - middle, vdisplay);
452                 break;
453         case DRM_MODE_FLAG_3D_FRAME_PACKING:
454         {
455                 int vactive_space = mode->vtotal - vdisplay;
456
457                 layout->fb_width = hdisplay;
458                 layout->fb_height = 2 * vdisplay + vactive_space;
459
460                 box_init(&layout->left,
461                          0, 0, hdisplay, vdisplay);
462                 box_init(&layout->right,
463                          0, vdisplay + vactive_space, hdisplay, vdisplay);
464                 break;
465         }
466         default:
467                 igt_assert(0);
468         }
469 }
470
471 static const char *stereo_mode_str(drmModeModeInfo *mode)
472 {
473         unsigned int layout = mode->flags & DRM_MODE_FLAG_3D_MASK;
474
475         switch (layout) {
476         case DRM_MODE_FLAG_3D_TOP_AND_BOTTOM:
477                 return "TB";
478         case DRM_MODE_FLAG_3D_SIDE_BY_SIDE_HALF:
479                 return "SbSH";
480         case DRM_MODE_FLAG_3D_FRAME_PACKING:
481                 return "FP";
482         default:
483                 igt_assert(0);
484         }
485 }
486
487 static uint32_t create_stereo_fb(drmModeModeInfo *mode, struct igt_fb *fb)
488 {
489         struct stereo_fb_layout layout;
490         cairo_t *cr;
491         uint32_t fb_id;
492
493         stereo_fb_layout_from_mode(&layout, mode);
494         fb_id = igt_create_fb(drm_fd, layout.fb_width, layout.fb_height,
495                                   igt_bpp_depth_to_drm_format(bpp, depth),
496                                   enable_tiling, fb);
497         cr = igt_get_cairo_ctx(drm_fd, fb);
498
499         igt_paint_image(cr, IGT_DATADIR"/1080p-left.png",
500                             layout.left.x, layout.left.y,
501                             layout.left.width, layout.left.height);
502         igt_paint_image(cr, IGT_DATADIR"/1080p-right.png",
503                             layout.right.x, layout.right.y,
504                             layout.right.width, layout.right.height);
505
506         cairo_destroy(cr);
507
508         {
509                 char buffer[64];
510
511                 snprintf(buffer, sizeof(buffer), "%dx%d@%dHz-%s.png",
512                          mode->hdisplay,
513                          mode->vdisplay,
514                          mode->vrefresh,
515                          stereo_mode_str(mode));
516
517                 igt_write_fb_to_png(drm_fd, fb, buffer);
518         }
519
520         return fb_id;
521 }
522
523 static void do_set_stereo_mode(struct connector *c)
524 {
525         uint32_t fb_id;
526         struct igt_fb fb_info;
527
528         fb_id = create_stereo_fb(&c->mode, &fb_info);
529
530         igt_warn_on_f(drmModeSetCrtc(drm_fd, c->crtc, fb_id, 0, 0, &c->id, 1, &c->mode),
531                       "failed to set mode (%dx%d@%dHz): %s\n", width, height, c->mode.vrefresh, strerror(errno));
532 }
533
534 static void
535 set_stereo_mode(struct connector *c)
536 {
537         int i, n;
538
539
540         if (specified_mode_num != -1)
541                 n = 1;
542         else
543                 n = c->connector->count_modes;
544
545         for (i = 0; i < n; i++) {
546                 if (specified_mode_num == -1)
547                         c->mode = c->connector->modes[i];
548
549                 if (!c->mode_valid)
550                         continue;
551
552                 if (!(c->mode.flags & DRM_MODE_FLAG_3D_MASK))
553                         continue;
554
555                 igt_info("CRTC(%u): [%d]", c->crtc, i);
556                 kmstest_dump_mode(&c->mode);
557                 do_set_stereo_mode(c);
558
559                 if (qr_code) {
560                         set_single();
561                         pause();
562                 } else if (sleep_between_modes)
563                         sleep(sleep_between_modes);
564
565                 if (do_dpms) {
566                         kmstest_set_connector_dpms(drm_fd, c->connector, DRM_MODE_DPMS_OFF);
567                         sleep(sleep_between_modes);
568                         kmstest_set_connector_dpms(drm_fd, c->connector, DRM_MODE_DPMS_ON);
569                 }
570         }
571
572         drmModeFreeEncoder(c->encoder);
573         drmModeFreeConnector(c->connector);
574 }
575
576 /*
577  * Re-probe outputs and light up as many as possible.
578  *
579  * On Intel, we have two CRTCs that we can drive independently with
580  * different timings and scanout buffers.
581  *
582  * Each connector has a corresponding encoder, except in the SDVO case
583  * where an encoder may have multiple connectors.
584  */
585 int update_display(void)
586 {
587         struct connector *connectors;
588         int c;
589
590         resources = drmModeGetResources(drm_fd);
591         if (!resources) {
592                 igt_warn("drmModeGetResources failed: %s\n", strerror(errno));
593                 return 0;
594         }
595
596         connectors = calloc(resources->count_connectors,
597                             sizeof(struct connector));
598         if (!connectors)
599                 return 0;
600
601         if (test_preferred_mode || test_all_modes ||
602             force_mode || specified_disp_id != -1) {
603                 unsigned long crtc_idx_mask = -1UL;
604
605                 /* Find any connected displays */
606                 for (c = 0; c < resources->count_connectors; c++) {
607                         struct connector *connector = &connectors[c];
608
609                         connector->id = resources->connectors[c];
610                         if (specified_disp_id != -1 &&
611                             connector->id != specified_disp_id)
612                                 continue;
613
614                         connector_find_preferred_mode(connector->id,
615                                                       crtc_idx_mask,
616                                                       specified_mode_num,
617                                                       connector);
618                         if (!connector->mode_valid)
619                                 continue;
620
621                         set_mode(connector);
622
623                         if (test_preferred_mode || force_mode ||
624                             specified_mode_num != -1)
625                                 crtc_idx_mask &= ~(1 << connector->crtc_idx);
626
627                 }
628         }
629
630         if (test_stereo_modes) {
631                 for (c = 0; c < resources->count_connectors; c++) {
632                         struct connector *connector = &connectors[c];
633
634                         connector->id = resources->connectors[c];
635                         if (specified_disp_id != -1 &&
636                             connector->id != specified_disp_id)
637                                 continue;
638
639                         connector_find_preferred_mode(connector->id,
640                                                       -1UL,
641                                                       specified_mode_num,
642                                                       connector);
643                         if (!connector->mode_valid)
644                                 continue;
645
646                         set_stereo_mode(connector);
647                 }
648         }
649
650         free(connectors);
651         drmModeFreeResources(resources);
652         return 1;
653 }
654
655 static char optstr[] = "3hiaf:s:d:p:mrto:j:";
656
657 static void __attribute__((noreturn)) usage(char *name)
658 {
659         igt_info("usage: %s [-hiasdpmtf]\n", name);
660         igt_info("\t-i\tdump info\n");
661         igt_info("\t-a\ttest all modes\n");
662         igt_info("\t-s\t<duration>\tsleep between each mode test\n");
663         igt_info("\t-d\t<depth>\tbit depth of scanout buffer\n");
664         igt_info("\t-p\t<planew,h>,<crtcx,y>,<crtcw,h> test overlay plane\n");
665         igt_info("\t-m\ttest the preferred mode\n");
666         igt_info("\t-3\ttest all 3D modes\n");
667         igt_info("\t-t\tuse a tiled framebuffer\n");
668         igt_info("\t-j\tdo dpms off, optional arg to select dpms leve (1-3)\n");
669         igt_info("\t-r\tprint a QR code on the screen whose content is \"pass\" for the automatic test\n");
670         igt_info("\t-o\t<id of the display>,<number of the mode>\tonly test specified mode on the specified display\n");
671         igt_info("\t-f\t<clock MHz>,<hdisp>,<hsync-start>,<hsync-end>,<htotal>,\n");
672         igt_info("\t\t<vdisp>,<vsync-start>,<vsync-end>,<vtotal>\n");
673         igt_info("\t\ttest force mode\n");
674         igt_info("\tDefault is to test all modes.\n");
675         exit(0);
676 }
677
678 #define dump_resource(res) if (res) dump_##res()
679
680 static void cleanup_and_exit(int ret)
681 {
682         close(drm_fd);
683         exit(ret);
684 }
685
686 static gboolean input_event(GIOChannel *source, GIOCondition condition,
687                                 gpointer data)
688 {
689         gchar buf[2];
690         gsize count;
691
692         count = read(g_io_channel_unix_get_fd(source), buf, sizeof(buf));
693         if (buf[0] == 'q' && (count == 1 || buf[1] == '\n')) {
694                 cleanup_and_exit(0);
695         }
696
697         return TRUE;
698 }
699
700 static void enter_exec_path( char **argv )
701 {
702         char *exec_path = NULL;
703         char *pos = NULL;
704         short len_path = 0;
705         int ret;
706
707         len_path = strlen( argv[0] );
708         exec_path = (char*) malloc(len_path);
709
710         memcpy(exec_path, argv[0], len_path);
711         pos = strrchr(exec_path, '/');
712         if (pos != NULL)
713                 *(pos+1) = '\0';
714
715         ret = chdir(exec_path);
716         igt_assert(ret == 0);
717         free(exec_path);
718 }
719
720 static void restore_termio_mode(int sig)
721 {
722         tcsetattr(tio_fd, TCSANOW, &saved_tio);
723         close(tio_fd);
724 }
725
726 static void set_termio_mode(void)
727 {
728         struct termios tio;
729
730         /* don't attempt to set terminal attributes if not in the foreground
731          * process group */
732         if (getpgrp() != tcgetpgrp(STDOUT_FILENO))
733                 return;
734
735         tio_fd = dup(STDIN_FILENO);
736         tcgetattr(tio_fd, &saved_tio);
737         igt_install_exit_handler(restore_termio_mode);
738         tio = saved_tio;
739         tio.c_lflag &= ~(ICANON | ECHO);
740         tcsetattr(tio_fd, TCSANOW, &tio);
741 }
742
743 int main(int argc, char **argv)
744 {
745         int c;
746         int ret = 0;
747         GIOChannel *stdinchannel;
748         GMainLoop *mainloop;
749         float force_clock;
750         bool opt_dump_info = false;
751
752         igt_skip_on_simulation();
753
754         enter_exec_path( argv );
755
756         while ((c = getopt(argc, argv, optstr)) != -1) {
757                 switch (c) {
758                 case '3':
759                         test_stereo_modes = 1;
760                         break;
761                 case 'i':
762                         opt_dump_info = true;
763                         break;
764                 case 'a':
765                         test_all_modes = 1;
766                         break;
767                 case 'f':
768                         force_mode = 1;
769                         if(sscanf(optarg,"%f,%hu,%hu,%hu,%hu,%hu,%hu,%hu,%hu",
770                                 &force_clock,&force_timing.hdisplay, &force_timing.hsync_start,&force_timing.hsync_end,&force_timing.htotal,
771                                 &force_timing.vdisplay, &force_timing.vsync_start, &force_timing.vsync_end, &force_timing.vtotal)!= 9)
772                                 usage(argv[0]);
773                         force_timing.clock = force_clock*1000;
774
775                         break;
776                 case 's':
777                         sleep_between_modes = atoi(optarg);
778                         break;
779                 case 'j':
780                         do_dpms = atoi(optarg);
781                         if (do_dpms == 0)
782                                 do_dpms = DRM_MODE_DPMS_OFF;
783                         break;
784                 case 'd':
785                         depth = atoi(optarg);
786                         igt_info("using depth %d\n", depth);
787                         break;
788                 case 'p':
789                         if (sscanf(optarg, "%d,%d,%d,%d,%d,%d", &plane_width,
790                                    &plane_height, &crtc_x, &crtc_y,
791                                    &crtc_w, &crtc_h) != 6)
792                                 usage(argv[0]);
793                         test_plane = 1;
794                         break;
795                 case 'm':
796                         test_preferred_mode = 1;
797                         break;
798                 case 't':
799                         enable_tiling = 1;
800                         break;
801                 case 'r':
802                         qr_code = 1;
803                         break;
804                 case 'o':
805                         sscanf(optarg, "%d,%d", &specified_disp_id, &specified_mode_num);
806                         break;
807                 default:
808                         /* fall through */
809                 case 'h':
810                         usage(argv[0]);
811                         break;
812                 }
813         }
814
815         set_termio_mode();
816
817         if (depth <= 8)
818                 bpp = 8;
819         else if (depth <= 16)
820                 bpp = 16;
821         else if (depth <= 32)
822                 bpp = 32;
823
824         if (!test_all_modes && !force_mode && !test_preferred_mode &&
825             specified_mode_num == -1 && !test_stereo_modes)
826                 test_all_modes = 1;
827
828         drm_fd = drm_open_any();
829
830         if (test_stereo_modes &&
831             drmSetClientCap(drm_fd, DRM_CLIENT_CAP_STEREO_3D, 1) < 0) {
832                 igt_warn("DRM_CLIENT_CAP_STEREO_3D failed\n");
833                 goto out_close;
834         }
835
836         if (opt_dump_info) {
837                 dump_info();
838                 goto out_close;
839         }
840
841         igt_set_vt_graphics_mode();
842
843         mainloop = g_main_loop_new(NULL, FALSE);
844         if (!mainloop) {
845                 igt_warn("failed to create glib mainloop\n");
846                 ret = -1;
847                 goto out_close;
848         }
849
850         if (!testdisplay_setup_hotplug()) {
851                 igt_warn("failed to initialize hotplug support\n");
852                 goto out_mainloop;
853         }
854
855         stdinchannel = g_io_channel_unix_new(0);
856         if (!stdinchannel) {
857                 igt_warn("failed to create stdin GIO channel\n");
858                 goto out_hotplug;
859         }
860
861         ret = g_io_add_watch(stdinchannel, G_IO_IN | G_IO_ERR, input_event,
862                              NULL);
863         if (ret < 0) {
864                 igt_warn("failed to add watch on stdin GIO channel\n");
865                 goto out_stdio;
866         }
867
868         ret = 0;
869
870         if (!update_display()) {
871                 ret = 1;
872                 goto out_stdio;
873         }
874
875         if (test_all_modes)
876                 goto out_stdio;
877
878         g_main_loop_run(mainloop);
879
880 out_stdio:
881         g_io_channel_shutdown(stdinchannel, TRUE, NULL);
882 out_hotplug:
883         testdisplay_cleanup_hotplug();
884 out_mainloop:
885         g_main_loop_unref(mainloop);
886 out_close:
887         close(drm_fd);
888
889         return ret;
890 }